RemoveUnusedLabelsProcessor.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.ArrayList;
  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. class RemoveUnusedLabelsProcessor implements CodeProcessing {
  21. protected HashMap<String, HashMap<String, ArrayList<Instruction>>> labelMap = new HashMap<>();
  22. protected String currentBlock;
  23. private Block block;
  24. public RemoveUnusedLabelsProcessor() {
  25. }
  26. @Override
  27. public void Exec(Code c, LinkedHashMap<String, CodeProcessing> cp) throws Exception {
  28. String label;
  29. Instruction instruction = null;
  30. block = c.Block();
  31. currentBlock = block.getName();
  32. HashMap<String, Integer> rc = c.labelsReferenceCount;
  33. LinkedList<Instruction> instructions = block.Instructions();
  34. ArrayList<Instruction> remover = new ArrayList();
  35. Iterator<Instruction> interator = instructions.iterator();
  36. String[] ignore = new String[]{"block", "user"};
  37. Instruction branch = null, last = null;
  38. int position = 0;
  39. System.out.println("block:" + block.getName() + "-------------------------------");
  40. // Remove todos os labels não referenciados
  41. while (interator.hasNext()) {
  42. position++;
  43. last = instruction;
  44. instruction = interator.next();
  45. // System.out.println("instruction:" + instruction);
  46. if (instruction.eq("type", "branch")) {
  47. branch = instruction;
  48. position = 0;
  49. }
  50. if (position == 3 && branch != null && last.eq("type", "jump")) {
  51. // System.out.println(":" + branch + last);
  52. branch.S("op", Utils.ComplementOperation(branch.G("op")))
  53. .S("label", last.G("label"));
  54. branch = null;
  55. // instructions.remove(last);
  56. // interator.remove();
  57. // interator.remove(last);
  58. remover.add(last);
  59. remover.add(instruction);
  60. continue;
  61. }
  62. if (!instruction.eq("type", "label") || instruction.in("label_type", ignore)) {
  63. continue;
  64. // Unifica labels sequenciais e atualiza as chamadas dos outros labels
  65. }
  66. label = instruction.G("label");
  67. if (!rc.containsKey(label) || rc.get(label) == 0) {
  68. interator.remove();
  69. }
  70. }
  71. // System.out.println("Remover:" + remover);
  72. Instruction r;
  73. while (!remover.isEmpty()) {
  74. r = remover.remove(0);
  75. instructions.remove(r);
  76. }
  77. // Remove labels sequenciais
  78. for (Instruction i : block.Instructions()) {
  79. if (last != null) {
  80. if (i.eq("type", "label") && last.eq("type", "label")) {
  81. UpdateLabel(last.G("label"), i.G("label"));
  82. last.copy("label", i);
  83. remover.add(last);
  84. }
  85. }
  86. last = i;
  87. }
  88. while (!remover.isEmpty()) {
  89. r = remover.remove(0);
  90. instructions.remove(r);
  91. }
  92. }
  93. protected void UpdateLabel(String old, String newl) {
  94. HashMap<String, ArrayList<Instruction>> lab;
  95. ArrayList<Instruction> labelInstructions;
  96. String label;
  97. // Cria o mapa de labels para o bloco atual caso não exista
  98. if (!labelMap.containsKey(currentBlock)) {
  99. lab = new HashMap<String, ArrayList<Instruction>>();
  100. labelMap.put(currentBlock, lab);
  101. // Popula a lista de cada de instrucao de cada label
  102. for (Instruction instruction : block.Instructions()) {
  103. if (instruction.Has("label") || instruction.eq("type", "label")) {
  104. label = instruction.G("label");
  105. if (!lab.containsKey(label)) {
  106. lab.put(label, new ArrayList<Instruction>());
  107. }
  108. lab.get(label).add(instruction);
  109. }
  110. }
  111. } else {
  112. lab = labelMap.get(currentBlock);
  113. }
  114. // System.out.println("lab:" + lab);
  115. // System.out.println("update label:" + old + "." + newl);
  116. if (lab.containsKey(old)) {
  117. for (Instruction instruction : lab.get(old)) {
  118. instruction.S("label", newl);
  119. }
  120. }
  121. //labelInstructions
  122. }
  123. }