CodeOtimizadorProcessor.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package Processors;
  2. import API.CodeProcessorInterface;
  3. import common.Block;
  4. import common.Code;
  5. import common.Instruction;
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8. import java.util.LinkedHashMap;
  9. import java.util.LinkedList;
  10. /**
  11. *
  12. * @author Eugenio
  13. */
  14. public class CodeOtimizadorProcessor implements CodeProcessorInterface {
  15. protected Code code;
  16. // protected ArrayList<Filtros> filtros = new ArrayList<Filtros>();
  17. protected boolean enable = true;
  18. public CodeOtimizadorProcessor(boolean enabled) {
  19. enable = enabled;
  20. }
  21. //
  22. // public void otimizar() throws Exception {
  23. // for (Filtros f : filtros) {
  24. // f.filtrar(code);
  25. // }
  26. // }
  27. @Override
  28. public void Exec(Code c, LinkedHashMap<String, CodeProcessorInterface> cp) throws Exception {
  29. if (!enable) {
  30. return;
  31. }
  32. int index = 0;
  33. Block block = c.Block();
  34. Instruction last = null;
  35. LinkedList<Instruction> instructions = block.Instructions();
  36. ArrayList<Instruction> remove = new ArrayList<>();
  37. Iterator<Instruction> interator = block.Instructions().iterator();
  38. // System.out.println("Exec Oti. in block " + block.getName());
  39. while (interator.hasNext()) {
  40. Instruction inst = interator.next();
  41. // System.out.println("Otimizando 3 enderecos:" + inst);
  42. // System.out.println("CodeOtimizadorProcessor:" + inst.Get("type"));
  43. switch (inst.Get("cat")) {
  44. /*Se for uma expressão aritimetica*/
  45. case "exp":
  46. switch (inst.Get("op")) {
  47. /*Se for uma multiplicacao ou divisao por 1 */
  48. case "*":
  49. case "/":
  50. if (inst.eq("p1value", "true") && inst.eq("p1", "1")) {
  51. inst.Set("format", "copy")
  52. .Set("type", "copy")
  53. .Set("p1", inst.Get("p2"))
  54. .Set("p1value", "false")
  55. .R("p2,p2value,op,cat");
  56. } else if (inst.eq("p2value", "true") && inst.eq("p2", "1")) {
  57. inst.Set("format", "copy")
  58. .Set("type", "copy")
  59. .R("p2,p2value,op,cat");
  60. }
  61. case "+":
  62. }
  63. }
  64. // System.out.println("CodeOtimizadorProcessor2:" + inst.Get("type"));
  65. switch (inst.Get("type")) {
  66. case "copy":
  67. if (last != null && last.eq("dst", inst.Get("p1"))) {
  68. last.copy("dst", inst);
  69. remove.add(inst);
  70. }
  71. // if (!inst.eq("copy", "true")) {
  72. // if (!inst.isNumber("p1") && ReplaceVarAndRemoveInstruction(instructions, inst, index, block)) {
  73. // System.out.println("Remove instruction:" + inst);
  74. // remove.add(inst);
  75. // }
  76. // } else {
  77. // System.out.println("COPIA TRUE:" + inst);
  78. // }
  79. // Se o destino da copia não for indexado então pode executar
  80. // o replace da ocorrencia e remover a instrucao de copia
  81. // if (!inst.eq("dst_indexed", "true") && inst.eq("p1value", "false")) {
  82. // if (!inst.eq("dst_indexed", "true")) {
  83. //// System.out.println("Otimizacao - replace " + inst.Get("p1") + " -> " + inst.Get("dst"));
  84. // if (inst.eq("p1value", "true")) {
  85. // block.ReplaceVar(inst.Get("dst"), inst.Get("p1"));
  86. //
  87. // } else {
  88. // block.ReplaceVar(inst.Get("p1"), inst.Get("dst"));
  89. // }
  90. // block.CurrentAddress--;
  91. // instructions.remove();
  92. // }
  93. case "branch":
  94. // if (Utils.re) {
  95. // inst.Set("type", "jump");
  96. // }
  97. break;
  98. }
  99. last = inst;
  100. index++;
  101. }
  102. if (!remove.isEmpty()) {
  103. for (Instruction instruction : remove) {
  104. instructions.remove(instruction);
  105. }
  106. }
  107. c.PosicaoLabel = block.BaseAddress;
  108. block.Update();
  109. }
  110. private boolean ReplaceVarAndRemoveInstruction(LinkedList<Instruction> instructions, Instruction inst, int index, Block block) {
  111. Instruction instruction;
  112. if (inst.eq("p1value", "true")) {
  113. return false;
  114. }
  115. int limit = instructions.size();
  116. String p1 = inst.Get("p1"), dst = inst.Get("dst"), attrIndex = "";
  117. // System.out.println("ReplaceVar[" + index + "]:" + ":" + p1 + ":" + dst + "::" + inst + "{");
  118. index++;
  119. String[] attrs = {"dst", "p1", "p2"};
  120. while (index < limit) {
  121. instruction = instructions.get(index);
  122. // System.out.println("instruction:" + instruction);
  123. for (String attr : attrs) {
  124. if (instruction.contem(attr, dst)) {
  125. instruction.Replace(attr, dst, p1);
  126. attrIndex = attr + ".index";
  127. if (instruction.Has(attrIndex)) {
  128. instruction.Set(attrIndex, p1);
  129. }
  130. }
  131. }
  132. index++;
  133. }
  134. block.Data().Replace(dst, p1);
  135. // System.out.println("ReplaceVarAndRemoveInstruction:" + dst + ":" +);
  136. //
  137. // System.out.println("\t\t" + inst);
  138. // System.out.println("}");
  139. return true;
  140. }
  141. }