RemoveUnusedLabelsProcessor.java 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. public RemoveUnusedLabelsProcessor() {
  22. }
  23. @Override
  24. public void Exec(Code c, LinkedHashMap<String, CodeProcessing> cp) throws Exception {
  25. String label;
  26. Instruction instruction = null;
  27. Block block = c.Block();
  28. HashMap<String, Integer> rc = c.labelsReferenceCount;
  29. LinkedList<Instruction> instructions = block.Instructions();
  30. ArrayList<Instruction> remover = new ArrayList();
  31. Iterator<Instruction> interator = instructions.iterator();
  32. String[] ignore = new String[]{"block", "user"};
  33. Instruction branch = null, last;
  34. int position = 0;
  35. System.out.println("block:" + block.getName() + "-------------------------------");
  36. // Remove todos os labels não referenciados
  37. while (interator.hasNext()) {
  38. position++;
  39. last = instruction;
  40. instruction = interator.next();
  41. // System.out.println("instruction:" + instruction);
  42. if (instruction.eq("type", "branch")) {
  43. branch = instruction;
  44. position = 0;
  45. }
  46. if (position == 3 && branch != null && last.eq("type", "jump")) {
  47. System.out.println(":" + branch + last);
  48. branch.S("op", Utils.ComplementOperation(branch.G("op")))
  49. .S("label", last.G("label"));
  50. branch = null;
  51. // instructions.remove(last);
  52. // interator.remove();
  53. // interator.remove(last);
  54. remover.add(last);
  55. remover.add(instruction);
  56. // System.out.println("'ERA UMA LABEL:" + instruction);
  57. continue;
  58. }
  59. // if (position == 1 // && instruction.eq("type", "label") // && (last != null && last.eq("type", "jump"))
  60. // ) {
  61. //
  62. ////
  63. //// instructions.remove(last);
  64. //// instructions.remove(instruction);
  65. ////
  66. //// continue;
  67. // }
  68. if (!instruction.eq("type", "label") || instruction.in("label_type", ignore)) {
  69. continue;
  70. }
  71. label = instruction.G("label");
  72. if (!rc.containsKey(label) || rc.get(label) == 0) {
  73. interator.remove();
  74. }
  75. }
  76. // System.out.println("Remover:" + remover);
  77. for (Instruction r : remover) {
  78. instructions.remove(r);
  79. }
  80. }
  81. }