CodeClearProcessor.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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.Instruction;
  8. import java.util.HashMap;
  9. import java.util.Iterator;
  10. import java.util.LinkedHashMap;
  11. /**
  12. *
  13. * @author EUGENIO CARVALHO
  14. */
  15. class CodeClearProcessor implements CodeProcessing {
  16. public CodeClearProcessor() {
  17. }
  18. @Override
  19. public void Exec(Code c, LinkedHashMap<String, CodeProcessing> cp) throws Exception {
  20. String label;
  21. Instruction instruction;
  22. Block block = c.Block();
  23. HashMap<String, Integer> rc = c.labelsReferenceCount;
  24. Iterator<Instruction> interator = block.Instructions().iterator();
  25. String[] ignore = new String[]{"block", "user"};
  26. // Remove todos os labels não referenciados
  27. while (interator.hasNext()) {
  28. instruction = interator.next();
  29. // System.out.println("instruction:" + instruction);
  30. if (!instruction.eq("type", "label") || instruction.in("label_type", ignore)) {
  31. continue;
  32. }
  33. label = instruction.G("label");
  34. if (!rc.containsKey(label) || rc.get(label) == 0) {
  35. interator.remove();
  36. }
  37. }
  38. }
  39. }