LoadStoreMiddleware.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package middlewares;
  7. import API.MiddlewareInterface;
  8. import common.Block;
  9. import common.Code;
  10. import common.Instruction;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.LinkedHashMap;
  14. import java.util.LinkedList;
  15. /**
  16. *
  17. * @author EUGENIO CARVALHO
  18. */
  19. public class LoadStoreMiddleware implements MiddlewareInterface {
  20. protected BasicBlockMiddleware basicBlocks;
  21. private BlockBaseGroup group;
  22. public LoadStoreMiddleware() {
  23. }
  24. @Override
  25. public void Exec(Code c, LinkedHashMap<String, MiddlewareInterface> cp) throws Exception {
  26. this.basicBlocks = (BasicBlockMiddleware) cp.get("ir.basic.blocks");
  27. BlockBaseOcorrences g;
  28. Integer leader, limit, lposition;
  29. Block block = c.Block();
  30. String blockname = block.getName();
  31. LinkedList<Instruction> instructions = block.Instructions();
  32. ArrayList<Instruction> remove = new ArrayList<>();
  33. Instruction IL, IS;
  34. // Group contem a lista de blocos basicos do bloco atual
  35. group = basicBlocks.getGroups().get(blockname);
  36. HashMap<String, Boolean> loaded, stored;
  37. String varL, varS;
  38. Integer fix = 0;
  39. group.Init();
  40. System.out.println("Inicianado load store processor....");
  41. while (true) {
  42. g = group.getCurrent();
  43. if (g == null) {
  44. break;
  45. }
  46. System.out.println("------------------------------>:");
  47. // Corrige o endereco inicial do leader caso alguma instrucao tenha sido removida
  48. leader = g.getLeader() - fix;
  49. limit = leader + g.Position;
  50. System.out.println("LoadStoreProcessor:[" + leader + "][" + limit + "]");
  51. loaded = new HashMap<>();
  52. stored = new HashMap<>();
  53. for (int i = leader, j = limit - 1; i < limit; i++, j--) {
  54. IL = instructions.get(i);
  55. IS = instructions.get(j);
  56. varL = IL.Get("p1");
  57. varS = IS.Get("p1");
  58. // Verifica a possibilidade de remover a instrucao atual considerando:
  59. // * A instrucao é um load
  60. // * O endereco carregado é uma variavel da pilha
  61. // Variaveis globais sempre são lidas
  62. if (IL.eq("type", "load") && varL.contains("_V")) {
  63. // System.out.println("Looad:" + instruction);
  64. if (loaded.containsKey(varL)) {
  65. remove.add(IL);
  66. fix++;
  67. } else {
  68. loaded.put(varL, Boolean.TRUE);
  69. }
  70. }
  71. // Verifica a possibilidade de remover a instrucao atual considerando:
  72. // * A instrucao é um store
  73. // * O endereco carregado é uma variavel da pilha
  74. // Variaveis globais sempre são gravadas
  75. // if (IS.eq("type", "store") && varS.contains("_V")) {
  76. // if (varS.equals("_V8")) {
  77. // System.out.println("Looad:" + IS + " > " + stored.containsKey(varS));
  78. // }
  79. // if (stored.containsKey(varS)) {
  80. // remove.add(IS);
  81. // fix++;
  82. // } else {
  83. // stored.put(varS, Boolean.TRUE);
  84. // }
  85. // }
  86. }
  87. // Corrige o endereco final do bloco caso alguma instrucao tenha sido removida
  88. g.setLeader(leader);
  89. g.Position -= fix;
  90. System.out.println("------------------------------<:" + g);
  91. if (!group.NextBlock()) {
  92. break;
  93. }
  94. }
  95. for (Instruction i : remove) {
  96. instructions.remove(i);
  97. }
  98. }
  99. }