CodeOtimizadorProcessor.java 5.8 KB

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