CodeOtimizadorMiddleware.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package middlewares;
  2. import API.MiddlewareInterface;
  3. import common.Block;
  4. import common.Code;
  5. import common.Instruction;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.LinkedHashMap;
  10. import java.util.LinkedList;
  11. /**
  12. *
  13. * @author Eugenio
  14. */
  15. public class CodeOtimizadorMiddleware implements MiddlewareInterface {
  16. protected Code code;
  17. protected HashMap<String, Ocorrences> ConstantePropagation = new HashMap<>();
  18. // protected ArrayList<Filtros> filtros = new ArrayList<Filtros>();
  19. protected boolean enable = true;
  20. public CodeOtimizadorMiddleware(boolean enabled) {
  21. enable = enabled;
  22. }
  23. //
  24. // public void otimizar() throws Exception {
  25. // for (Filtros f : filtros) {
  26. // f.filtrar(code);
  27. // }
  28. // }
  29. @Override
  30. public void Exec(Code c, LinkedHashMap<String, MiddlewareInterface> cp) throws Exception {
  31. if (!enable) {
  32. return;
  33. }
  34. int index = 0;
  35. String dst;
  36. Block block = c.Block();
  37. Instruction last = null;
  38. LinkedList<Instruction> instructions = block.Instructions();
  39. ArrayList<Instruction> remove = new ArrayList<>();
  40. Iterator<Instruction> interator = block.Instructions().iterator();
  41. // System.out.println("Exec Oti. in block " + block.getName());
  42. while (interator.hasNext()) {
  43. Instruction inst = interator.next();
  44. // System.out.println("Otimizando 3 enderecos:" + inst);
  45. // System.out.println("CodeOtimizadorMiddleware:" + inst.Get("type"));
  46. switch (inst.Get("cat")) {
  47. /*Se for uma expressão aritimetica*/
  48. case "exp":
  49. switch (inst.Get("op")) {
  50. /*Se for uma multiplicacao ou divisao por 1 */
  51. case "*":
  52. case "/":
  53. if (inst.eq("p1value", "true") && inst.eq("p1", "1")) {
  54. inst.Set("format", "copy")
  55. .Set("type", "copy")
  56. .Set("p1", inst.Get("p2"))
  57. .Set("p1value", "false")
  58. .R("p2,p2value,op,cat");
  59. } else if (inst.eq("p2value", "true") && inst.eq("p2", "1")) {
  60. inst.Set("format", "copy")
  61. .Set("type", "copy")
  62. .R("p2,p2value,op,cat");
  63. }
  64. case "+":
  65. }
  66. }
  67. // System.out.println("CodeOtimizadorProcessor2:" + inst.Get("type"));
  68. switch (inst.Get("type")) {
  69. case "copy":
  70. // System.out.println("Copy:" + inst);
  71. if (last != null && last.eq("dst", inst.Get("p1"))) {
  72. last.copy("dst", inst);
  73. remove.add(inst);
  74. } else if (inst.eq("p1value", "true")) {
  75. }
  76. // if (!inst.eq("copy", "true")) {
  77. // if (!inst.isNumber("p1") && ReplaceVarAndRemoveInstruction(instructions, inst, index, block)) {
  78. // System.out.println("Remove instruction:" + inst);
  79. // remove.add(inst);
  80. // }
  81. // } else {
  82. // System.out.println("COPIA TRUE:" + inst);
  83. // }
  84. // Se o destino da copia não for indexado então pode executar
  85. // o replace da ocorrencia e remover a instrucao de copia
  86. // if (!inst.eq("dst.indexed", "true") && inst.eq("p1value", "false")) {
  87. // if (!inst.eq("dst.indexed", "true")) {
  88. //// System.out.println("Otimizacao - replace " + inst.Get("p1") + " -> " + inst.Get("dst"));
  89. // if (inst.eq("p1value", "true")) {
  90. // block.ReplaceVar(inst.Get("dst"), inst.Get("p1"));
  91. //
  92. // } else {
  93. // block.ReplaceVar(inst.Get("p1"), inst.Get("dst"));
  94. // }
  95. // block.CurrentAddress--;
  96. // instructions.remove();
  97. // }
  98. case "branch":
  99. // if (Utils.re) {
  100. // inst.Set("type", "jump");
  101. // }
  102. break;
  103. }
  104. // if (inst.Has("dst")) {
  105. // dst = inst.Get("dst");
  106. // if (!ConstantePropagation.containsKey(dst)) {
  107. // ConstantePropagation.put(dst, new Ocorrences());
  108. // }
  109. // ConstantePropagation.get(dst).Add(inst);
  110. // }
  111. last = inst;
  112. index++;
  113. }
  114. // Instruction inst;
  115. // // Process constant propagation
  116. // for (Map.Entry<String, Ocorrences> entry : ConstantePropagation.entrySet()) {
  117. // LinkedList<Instruction> deps = entry.getValue().getDeps();
  118. //
  119. // if (deps.size() > 2) {
  120. // inst = deps.pollFirst();
  121. // // Marca a instrucao pra remover
  122. // remove.add(inst);
  123. // inst.Get("p1");
  124. // for (Instruction dep : deps) {
  125. // dep.Set("",);
  126. // }
  127. // }
  128. // }
  129. if (!remove.isEmpty()) {
  130. for (Instruction instruction : remove) {
  131. instructions.remove(instruction);
  132. }
  133. }
  134. c.PosicaoLabel = block.BaseAddress;
  135. // block.Update();
  136. }
  137. private boolean ReplaceVarAndRemoveInstruction(LinkedList<Instruction> instructions, Instruction inst, int index, Block block) {
  138. Instruction instruction;
  139. if (inst.eq("p1value", "true")) {
  140. return false;
  141. }
  142. int limit = instructions.size();
  143. String p1 = inst.Get("p1"), dst = inst.Get("dst"), attrIndex = "";
  144. // System.out.println("ReplaceVar[" + index + "]:" + ":" + p1 + ":" + dst + "::" + inst + "{");
  145. index++;
  146. String[] attrs = {"dst", "p1", "p2"};
  147. while (index < limit) {
  148. instruction = instructions.get(index);
  149. // System.out.println("instruction:" + instruction);
  150. for (String attr : attrs) {
  151. if (instruction.contem(attr, dst)) {
  152. instruction.Replace(attr, dst, p1);
  153. attrIndex = attr + ".index";
  154. if (instruction.Has(attrIndex)) {
  155. instruction.Set(attrIndex, p1);
  156. }
  157. }
  158. }
  159. index++;
  160. }
  161. block.Context().Replace(dst, p1);
  162. // System.out.println("ReplaceVarAndRemoveInstruction:" + dst + ":" +);
  163. //
  164. // System.out.println("\t\t" + inst);
  165. // System.out.println("}");
  166. return true;
  167. }
  168. private static class Ocorrences {
  169. protected LinkedList<Instruction> deps = new LinkedList<>();
  170. public Ocorrences() {
  171. }
  172. private void Add(Instruction inst) {
  173. deps.add(inst);
  174. }
  175. public LinkedList<Instruction> getDeps() {
  176. return deps;
  177. }
  178. public void setDeps(LinkedList<Instruction> deps) {
  179. this.deps = deps;
  180. }
  181. }
  182. }