DeadInstructionsProcessor.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 common.Block;
  8. import common.Code;
  9. import common.Instruction;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.util.LinkedHashMap;
  13. import java.util.LinkedList;
  14. import java.util.regex.Pattern;
  15. /**
  16. *
  17. * @author EUGENIO CARVALHO
  18. */
  19. class DeadInstructionsProcessor implements CodeProcessing {
  20. public static Pattern varPattern = Pattern.compile("\\_[VT].*", Pattern.CASE_INSENSITIVE);
  21. public DeadInstructionsProcessor() {
  22. }
  23. @Override
  24. public void Exec(Code c, LinkedHashMap<String, CodeProcessing> cp) throws Exception {
  25. String dst;
  26. Instruction instruction;
  27. Block block = c.Block();
  28. HashMap<String, Boolean> referenced = new HashMap<>();
  29. LinkedList<Instruction> instructions = block.Instructions();
  30. // System.out.println("instruction:" + instructions.get(2));
  31. // throw new Exception("xxxxx");
  32. String[] params = new String[]{"p1", "p2"};
  33. Iterator<Instruction> interator = instructions.descendingIterator();
  34. // Remove instrucoes mortas
  35. while (interator.hasNext()) {
  36. instruction = interator.next();
  37. // System.out.println("ClearCode:" + instruction);
  38. dst = instruction.G("dst");
  39. // Remove a instrucao se:
  40. // * A instrucao não esta dentro de um loop
  41. // * Tem destino
  42. // * É uma variavel local (Variavel ou temporaria)
  43. // * E não foi referenciada por instruções seguintes
  44. if (!instruction.eq("inloop", "true")
  45. && !dst.equals("")
  46. && varPattern.matcher(dst).matches()
  47. && !referenced.containsKey(dst)) {
  48. interator.remove();
  49. continue;
  50. }
  51. for (String param : params) {
  52. if (!instruction.eq(param + "value", "true")) {
  53. referenced.put(instruction.G(param), Boolean.TRUE);
  54. }
  55. }
  56. }
  57. }
  58. }