public void visitBranchInstruction( Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction) { byte opcode = branchInstruction.opcode; if (opcode == InstructionConstants.OP_JSR || opcode == InstructionConstants.OP_JSR_W) { int branchOffset = branchInstruction.branchOffset; int branchTarget = offset + branchOffset; // Is the subroutine ever returning? if (branchTargetFinder.isSubroutineReturning(branchTarget)) { // Append a label at this offset instead of the subroutine invocation. codeAttributeComposer.appendLabel(offset); // Inline the invoked subroutine. inlineSubroutine(clazz, method, codeAttribute, offset, branchTarget); } else { if (DEBUG) { System.out.println( "Replacing subroutine invocation at [" + offset + "] by a simple branch"); } // Replace the subroutine invocation by a simple branch. Instruction replacementInstruction = new BranchInstruction(InstructionConstants.OP_GOTO, branchOffset).shrink(); codeAttributeComposer.appendInstruction(offset, replacementInstruction); } } else { // Append the instruction. codeAttributeComposer.appendInstruction(offset, branchInstruction); } }
public void visitVariableInstruction( Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction) { byte opcode = variableInstruction.opcode; if (opcode == InstructionConstants.OP_RET) { // Is the return instruction the last instruction of the subroutine? if (branchTargetFinder.subroutineEnd(offset) == offset + variableInstruction.length(offset)) { if (DEBUG) { System.out.println(" Replacing subroutine return at [" + offset + "] by a label"); } // Append a label at this offset instead of the subroutine return. codeAttributeComposer.appendLabel(offset); } else { if (DEBUG) { System.out.println( " Replacing subroutine return at [" + offset + "] by a simple branch"); } // Replace the instruction by a branch. Instruction replacementInstruction = new BranchInstruction( InstructionConstants.OP_GOTO, branchTargetFinder.subroutineEnd(offset) - offset) .shrink(); codeAttributeComposer.appendInstruction(offset, replacementInstruction); } } else if (branchTargetFinder.isSubroutineStart(offset)) { if (DEBUG) { System.out.println( " Replacing first subroutine instruction at [" + offset + "] by a label"); } // Append a label at this offset instead of saving the subroutine // return address. codeAttributeComposer.appendLabel(offset); } else { // Append the instruction. codeAttributeComposer.appendInstruction(offset, variableInstruction); } }
/** Appends the specified subroutine. */ private void inlineSubroutine( Clazz clazz, Method method, CodeAttribute codeAttribute, int subroutineInvocationOffset, int subroutineStart) { int subroutineEnd = branchTargetFinder.subroutineEnd(subroutineStart); if (DEBUG) { System.out.println( " Inlining subroutine [" + subroutineStart + " -> " + subroutineEnd + "] at [" + subroutineInvocationOffset + "]"); } // Don't go inlining exceptions that are already applicable to this // subroutine invocation. ExceptionInfoVisitor oldSubroutineExceptionInliner = subroutineExceptionInliner; int oldClipStart = clipStart; int oldClipEnd = clipEnd; subroutineExceptionInliner = new ExceptionExcludedOffsetFilter(subroutineInvocationOffset, subroutineExceptionInliner); clipStart = subroutineStart; clipEnd = subroutineEnd; codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength); // Copy the subroutine instructions, inlining any subroutine calls // recursively. codeAttribute.instructionsAccept(clazz, method, subroutineStart, subroutineEnd, this); if (DEBUG) { System.out.println(" Appending label after inlined subroutine at [" + subroutineEnd + "]"); } // Append a label just after the code. codeAttributeComposer.appendLabel(subroutineEnd); // Inline the subroutine exceptions. codeAttribute.exceptionsAccept( clazz, method, subroutineStart, subroutineEnd, subroutineExceptionInliner); // We can again inline exceptions that are applicable to this // subroutine invocation. subroutineExceptionInliner = oldSubroutineExceptionInliner; clipStart = oldClipStart; clipEnd = oldClipEnd; codeAttributeComposer.endCodeFragment(); }
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) { // DEBUG = // clazz.getName().equals("abc/Def") && // method.getName(clazz).equals("abc"); if (DEBUG) { method.accept(clazz, new ClassPrinter()); } branchTargetFinder.visitCodeAttribute(clazz, method, codeAttribute); // Don't bother if there aren't any subroutines anyway. if (!containsSubroutines(codeAttribute)) { return; } if (DEBUG) { System.out.println( "SubroutineInliner: processing [" + clazz.getName() + "." + method.getName(clazz) + method.getDescriptor(clazz) + "]"); } // Append the body of the code. codeAttributeComposer.reset(); codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength); // Copy the non-subroutine instructions. int offset = 0; while (offset < codeAttribute.u4codeLength) { Instruction instruction = InstructionFactory.create(codeAttribute.code, offset); int instructionLength = instruction.length(offset); // Is this returning subroutine? if (branchTargetFinder.isSubroutine(offset) && branchTargetFinder.isSubroutineReturning(offset)) { // Skip the subroutine. if (DEBUG) { System.out.println( " Skipping original subroutine instruction " + instruction.toString(offset)); } // Append a label at this offset instead. codeAttributeComposer.appendLabel(offset); } else { // Copy the instruction, inlining any subroutine call recursively. instruction.accept(clazz, method, codeAttribute, offset, this); } offset += instructionLength; } // Copy the exceptions. Note that exceptions with empty try blocks // are automatically removed. codeAttribute.exceptionsAccept(clazz, method, subroutineExceptionInliner); if (DEBUG) { System.out.println(" Appending label after code at [" + offset + "]"); } // Append a label just after the code. codeAttributeComposer.appendLabel(codeAttribute.u4codeLength); // End and update the code attribute. codeAttributeComposer.endCodeFragment(); codeAttributeComposer.visitCodeAttribute(clazz, method, codeAttribute); if (DEBUG) { method.accept(clazz, new ClassPrinter()); } }