LoadStoreMiddleware.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. Instruction inst;
  37. HashMap<String, Boolean> loaded, stored;
  38. String varL, varS;
  39. Integer fix = 0;
  40. int i, j, fixAllocCount = 0;
  41. group.Init();
  42. System.out.println("Inicianado load store processor...." + blockname);
  43. String[] ignore = "alloc,label".split(",");
  44. for (int ctrl = 1; ctrl < instructions.size(); ctrl++) {
  45. if (!instructions.get(ctrl).eq("type", "alloc")) {
  46. break;
  47. }
  48. fixAllocCount++;
  49. }
  50. while (true) {
  51. g = group.getCurrent();
  52. if (g == null) {
  53. break;
  54. }
  55. // check stores
  56. // Verifica a possibilidade de remover a instrucao atual considerando:
  57. // * A instrucao é um store
  58. // * O endereco carregado é uma variavel da pilha
  59. // Variaveis globais sempre são gravadas
  60. stored = new HashMap<>();
  61. leader = g.getLeader() + fixAllocCount;
  62. i = j = (g.getLastPosition() + fixAllocCount);
  63. // System.out.println("------------------------------>(" + leader + "|" + i + "|F" + fixAllocCount + "):");
  64. for (; i > leader; j--) {
  65. IS = instructions.get(j);
  66. if (IS.in("type", ignore)) {
  67. continue;
  68. }
  69. // System.out.println("type :" + IS.Get("type"));
  70. switch (IS.Get("type")) {
  71. case "store":
  72. varS = IS.Get("dst");
  73. // System.out.println("[" + i + "]Era store de " + varS);
  74. // Se não for uma variavel global
  75. if (!varS.contains("_G")) {
  76. //
  77. // System.out.println("[" + i + "]Verificando ocorrencia de " + varS + " " + stored.containsKey(varS));
  78. if (stored.containsKey(varS)) {
  79. // System.out.println("[" + i + "]Marcada para ser removida");
  80. remove.add(IS);
  81. } else {
  82. // System.out.println("[" + i + "]Adicionando primeira ocorrencia");
  83. stored.put(varS, Boolean.TRUE);
  84. }
  85. }
  86. break;
  87. case "load":
  88. varS = IS.Get("p1");
  89. if (stored.containsKey(varS)) {
  90. // System.out.println("Remove " + varS + " dos stores");
  91. stored.remove(varS);
  92. }
  93. break;
  94. default:
  95. // System.out.println("Instruction " + IS);
  96. // for (String param : "dst,dst.indice".split(",")) {
  97. // if (IS.Has(param)) {
  98. // varS = IS.Get(param);
  99. // if (stored.containsKey(varS)) {
  100. // stored.remove(varS);
  101. // }
  102. // }
  103. // }
  104. // System.out.println("[" + i + "]Nao Era store de " + IS.Get("type"));
  105. }
  106. i--;
  107. }
  108. // System.out.println("------------------------------<:");
  109. if (!group.NextBlock()) {
  110. break;
  111. }
  112. }
  113. boolean changed = !remove.isEmpty();
  114. // Remove as instruções sinalizadas para remorcao
  115. System.out.println("Foram removidas " + remove.size() + " instruções de store.");
  116. while (!remove.isEmpty()) {
  117. instructions.remove(remove.remove(0));
  118. }
  119. // Atualiza os blocos basicos e ocorrencias de variaveis
  120. if (changed) {
  121. cp.get("ir.basic.blocks").Exec(c, cp);
  122. }
  123. }
  124. private void UpdateBasicBlocksReference() {
  125. }
  126. }
  127. // // Corrige o endereco inicial do leader caso alguma instrucao tenha sido removida
  128. // leader = g.getLeader() - fix;
  129. // limit = leader + g.Position;
  130. //
  131. // System.out.println("LoadStoreProcessor:[" + leader + "][" + limit + "]");
  132. //
  133. // loaded = new HashMap<>();
  134. // stored = new HashMap<>();
  135. //
  136. // for (int i = limit - 1; i > leader; i++, j--) {
  137. //
  138. // }
  139. // for (int i = leader, j = limit - 1; i < limit; i++, j--) {
  140. // IL = instructions.get(i);
  141. // IS = instructions.get(j);
  142. //
  143. // varL = IL.Get("p1");
  144. // varS = IS.Get("p1");
  145. // // Verifica a possibilidade de remover a instrucao atual considerando:
  146. // // * A instrucao é um load
  147. // // * O endereco carregado é uma variavel da pilha
  148. // // Variaveis globais sempre são lidas
  149. //
  150. // if (IL.eq("type", "load") && varL.contains("_V")) {
  151. //// System.out.println("Looad:" + instruction);
  152. // if (loaded.containsKey(varL)) {
  153. // remove.add(IL);
  154. // fix++;
  155. // } else {
  156. // loaded.put(varL, Boolean.TRUE);
  157. // }
  158. // }
  159. // }
  160. // Corrige o endereco final do bloco caso alguma instrucao tenha sido removida
  161. // g.setLeader(leader);
  162. // g.Position -= fix;