DeadInstructionsMiddleware.java 2.2 KB

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