void removeUnusedLocals(Method mOrig, MethodGen m) { InstructionList il = m.getInstructionList(); InstructionHandle[] ins = il.getInstructionHandles(); for (int i = 0; i < ins.length; i++) { Instruction in = ins[i].getInstruction(); if (in instanceof LocalVariableInstruction) { LocalVariableInstruction curr = (LocalVariableInstruction) in; if (mtab.getLocal(m, curr, ins[i].getPosition()) != null && curr.getIndex() < m.getMaxLocals() - 5 && !mtab.isLocalUsedInInlet(mOrig, curr.getIndex())) { if (curr instanceof IINC) { ins[i].setInstruction(new NOP()); } else if (curr instanceof LSTORE || curr instanceof DSTORE) { ins[i].setInstruction(new POP2()); } else if (curr instanceof StoreInstruction) { ins[i].setInstruction(new POP()); } else if (curr instanceof ALOAD) { ins[i].setInstruction(new ACONST_NULL()); } else if (curr instanceof FLOAD) { ins[i].setInstruction(new FCONST((float) 0.0)); } else if (curr instanceof ILOAD) { ins[i].setInstruction(new ICONST(0)); } else if (curr instanceof DLOAD) { ins[i].setInstruction(new DCONST(0.0)); } else if (curr instanceof LLOAD) { ins[i].setInstruction(new LCONST(0L)); } else { System.out.println("unhandled ins in " + "removeUnusedLocals: " + curr); System.exit(1); } } } } }
void insertReturnPop(Method m, InstructionList il) { Type returnType = m.getReturnType(); if (returnType.equals(Type.DOUBLE) || returnType.equals(Type.LONG)) { il.append(new POP2()); } else if (returnType.equals(Type.VOID)) { // do nothing } else { il.append(new POP()); } }
InstructionList getAndRemoveStoreIns(InstructionList il, InstructionHandle i) { int netto_stack_inc = 0; InstructionHandle storeStart = i; do { if (i == null) { // Could not find store sequence. return null; } int inc = i.getInstruction().produceStack(cpg) - i.getInstruction().consumeStack(cpg); netto_stack_inc += inc; i = i.getNext(); } while (netto_stack_inc >= 0); if (i == null) { // may happen if the result is used like, for instance: // return f().clone(); // No store sequence, so this is wrong as well. return null; } Instruction store = i.getPrev().getInstruction(); if (store instanceof ReturnInstruction) { return null; } if (store instanceof POP || store instanceof POP2) { return null; } InstructionList result = new InstructionList(); InstructionHandle ip = storeStart; do { result.append(ip.getInstruction()); ip = ip.getNext(); } while (ip != i); deleteIns(il, storeStart, ip.getPrev(), ip); return result; }