VariablesConstantsProcessor.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 IntermediaryCode;
  7. import API.Utils;
  8. import common.Block;
  9. import common.Code;
  10. import common.Instruction;
  11. import java.util.HashMap;
  12. import java.util.Iterator;
  13. import java.util.LinkedHashMap;
  14. import java.util.LinkedList;
  15. /**
  16. *
  17. * @author EUGENIO CARVALHO
  18. */
  19. class VariablesConstantsProcessor implements CodeProcessing {
  20. private Block block;
  21. private Iterator<Instruction> interator;
  22. private Instruction instruction;
  23. public VariablesConstantsProcessor() {
  24. }
  25. @Override
  26. public void Exec(Code c, LinkedHashMap<String, CodeProcessing> cp) throws Exception {
  27. // String label, dst;
  28. String dst;
  29. block = c.Block();
  30. HashMap<String, Integer> varUpdate = new HashMap<>();
  31. // HashMap<String, Integer> rc = c.labelsReferenceCount;
  32. interator = block.Instructions().iterator();
  33. // Remove todos os labels não referenciados
  34. while (interator.hasNext()) {
  35. instruction = interator.next();
  36. dst = instruction.G("dst");
  37. if (!dst.equals("")) {
  38. if (!varUpdate.containsKey(dst)) {
  39. varUpdate.put(dst, 0);
  40. }
  41. varUpdate.put(dst, varUpdate.get(dst) + 1);
  42. }
  43. }
  44. // System.out.println("varUpdate:" + varUpdate);
  45. interator = block.Instructions().iterator();
  46. int i = 0, value;
  47. while (interator.hasNext()) {
  48. instruction = interator.next();
  49. dst = instruction.G("dst");
  50. // Se a variavel foi escrita apenas uma vez e o valor atribuido não for uma variavel
  51. if (!dst.equals("")
  52. && varUpdate.get(dst) == 1
  53. && instruction.eq("p1value", "true")) {
  54. // && ) {
  55. // if (instruction.eq("type", "assign")) {
  56. //
  57. // }
  58. ReplaceConstant(dst, Resolve(instruction), i);
  59. interator.remove();
  60. }
  61. i++;
  62. }
  63. }
  64. protected String Resolve(Instruction instruction) throws Exception {
  65. System.out.println("instruction:"+ instruction);
  66. int value = instruction.getInt("p1");
  67. switch (instruction.G("cat")) {
  68. case "exp":
  69. if (instruction.eq("p2value", "true")) {
  70. value = Utils.ResolveExpr(
  71. instruction.G("op"),
  72. value,
  73. instruction.getInt("p2"));
  74. }
  75. }
  76. return "" + value;
  77. }
  78. private void ReplaceConstant(String dst, String value, int i) {
  79. LinkedList<Instruction> instructions = block.Instructions();
  80. int size = instructions.size();
  81. String[] fields = new String[]{"p1", "p2"};
  82. for (; i < size; i++) {
  83. instruction = instructions.get(i);
  84. for (String field : fields) {
  85. if (instruction.eq(field, dst)) {
  86. instruction.S(field, value);
  87. instruction.S(field + "value", "true");
  88. }
  89. }
  90. }
  91. }
  92. }