CodeOtimizadorProcessor.java 5.7 KB

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