VariablesConstantsMiddleware.java 3.3 KB

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