/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package middlewares; import common.Block; import common.Code; import common.Instruction; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.regex.Pattern; import API.MiddlewareInterface; /** * * @author EUGENIO CARVALHO */ public class DeadInstructionsMiddleware implements MiddlewareInterface { public static Pattern varPattern = Pattern.compile("\\_[VT].*", Pattern.CASE_INSENSITIVE); public DeadInstructionsMiddleware() { } @Override public void Exec(Code c, LinkedHashMap cp) throws Exception { String dst; Instruction instruction; Block block = c.Block(); HashMap referenced = new HashMap<>(); LinkedList instructions = block.Instructions(); // System.out.println("instruction:" + instructions.Get(2)); // throw new Exception("xxxxx"); String[] params = new String[]{"p1", "p2"}; Iterator interator = instructions.descendingIterator(); // Remove instrucoes mortas while (interator.hasNext()) { instruction = interator.next(); // System.out.println("ClearCode:" + instruction); dst = instruction.Get("dst"); // Remove a instrucao se: // * A instrucao não esta dentro de um loop // * Tem destino // * É uma variavel local (Variavel ou temporaria) // * E não foi referenciada por instruções seguintes if (!instruction.eq("inloop", "true") && !dst.equals("") && varPattern.matcher(dst).matches() && !referenced.containsKey(dst)) { interator.remove(); continue; } for (String param : params) { if (!instruction.eq(param + "value", "true")) { referenced.put(instruction.Get(param), Boolean.TRUE); } } } } }