List of usage examples for org.objectweb.asm Opcodes JSR
int JSR
To view the source code for org.objectweb.asm Opcodes JSR.
Click Source Link
From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java
License:Open Source License
private void generateMethodSkel(MethodVisitor cv, Method m, Variables var) { Class[] sig = m.getParameterTypes(); Label readArgs = new Label(); cv.visitLabel(readArgs);/*from w ww. ja v a 2s . c om*/ boolean needcastcheck = false; // ObjectInput in = call.getInputStream(); cv.visitVarInsn(Opcodes.ALOAD, var.get("remotecall")); cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "getInputStream", Type.getMethodDescriptor(Type.getType(ObjectInput.class), new Type[] {})); cv.visitVarInsn(Opcodes.ASTORE, var.allocate("objectinput")); for (int i = 0; i < sig.length; i++) { // dup input stream cv.visitVarInsn(Opcodes.ALOAD, var.get("objectinput")); Class readCls = sig[i].isPrimitive() ? sig[i] : Object.class; // in.readFoo() cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), readMethod(sig[i]), Type.getMethodDescriptor(Type.getType(readCls), new Type[] {})); if (!sig[i].isPrimitive() && !sig[i].equals(Object.class)) { needcastcheck = true; cv.visitTypeInsn(Opcodes.CHECKCAST, typeArg(sig[i])); } // store arg in variable cv.visitVarInsn(storeOpcode(sig[i]), var.allocate(param(m, i), size(sig[i]))); } var.deallocate("objectinput"); Label doCall = new Label(); Label closeInput = new Label(); cv.visitJumpInsn(Opcodes.JSR, closeInput); cv.visitJumpInsn(Opcodes.GOTO, doCall); // throw new UnmarshalException Label handler = new Label(); cv.visitLabel(handler); cv.visitVarInsn(Opcodes.ASTORE, var.allocate("exception")); cv.visitTypeInsn(Opcodes.NEW, typeArg(UnmarshalException.class)); cv.visitInsn(Opcodes.DUP); cv.visitLdcInsn("error unmarshalling arguments"); cv.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception")); cv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnmarshalException.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class), Type.getType(Exception.class) })); cv.visitVarInsn(Opcodes.ASTORE, var.allocate("toThrow")); cv.visitJumpInsn(Opcodes.JSR, closeInput); cv.visitVarInsn(Opcodes.ALOAD, var.get("toThrow")); cv.visitInsn(Opcodes.ATHROW); cv.visitTryCatchBlock(readArgs, handler, handler, Type.getInternalName(IOException.class)); if (needcastcheck) { cv.visitTryCatchBlock(readArgs, handler, handler, Type.getInternalName(ClassCastException.class)); } // finally block cv.visitLabel(closeInput); cv.visitVarInsn(Opcodes.ASTORE, var.allocate("retAddress")); cv.visitVarInsn(Opcodes.ALOAD, var.get("remotecall")); cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "releaseInputStream", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {})); cv.visitVarInsn(Opcodes.RET, var.deallocate("retAddress")); var.deallocate("toThrow"); // do the call using args stored as variables cv.visitLabel(doCall); cv.visitVarInsn(Opcodes.ALOAD, var.get("remoteobj")); for (int i = 0; i < sig.length; i++) cv.visitVarInsn(loadOpcode(sig[i]), var.deallocate(param(m, i))); cv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(clazz), m.getName(), Type.getMethodDescriptor(m)); Class returntype = m.getReturnType(); if (!returntype.equals(Void.TYPE)) { cv.visitVarInsn(storeOpcode(returntype), var.allocate("result", size(returntype))); } // write result to result stream Label writeResult = new Label(); cv.visitLabel(writeResult); cv.visitVarInsn(Opcodes.ALOAD, var.get("remotecall")); cv.visitInsn(Opcodes.ICONST_1); cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "getResultStream", Type.getMethodDescriptor(Type.getType(ObjectOutput.class), new Type[] { Type.BOOLEAN_TYPE })); if (!returntype.equals(Void.TYPE)) { // out.writeFoo(result) cv.visitVarInsn(loadOpcode(returntype), var.deallocate("result")); Class writeCls = returntype.isPrimitive() ? returntype : Object.class; cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), writeMethod(returntype), Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(writeCls) })); } cv.visitInsn(Opcodes.RETURN); // throw new MarshalException Label marshalHandler = new Label(); cv.visitLabel(marshalHandler); cv.visitVarInsn(Opcodes.ASTORE, var.allocate("exception")); cv.visitTypeInsn(Opcodes.NEW, typeArg(MarshalException.class)); cv.visitInsn(Opcodes.DUP); cv.visitLdcInsn("error marshalling return"); cv.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception")); cv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(MarshalException.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class), Type.getType(Exception.class) })); cv.visitInsn(Opcodes.ATHROW); cv.visitTryCatchBlock(writeResult, marshalHandler, marshalHandler, Type.getInternalName(IOException.class)); }
From source file:jaspex.speculation.newspec.RemoveOverspeculation.java
License:Open Source License
/** Scan simples para tentar eliminar overspeculation. */* w w w .ja v a2 s. c o m*/ * A ideia deste mtodo determinar se entre uma operao de spawnSpeculation e o get do seu Future / * nonTransactionalActionAttempted existem instruces suficientes para valer o trabalho de fazer * especulao. * * Quando observamos um spawnSpeculation, comeamos a fazer tracking dele. Se virmos alguma * operao complexa (alguns saltos, outros mtodos, etc), paramos o tracking. Caso contrrio, se * ainda estivermos a fazer tracking quando encontramos o get / nonTransactionalActionAttempted, ento * colocamos o mtodo na lista de especulaes a rejeitar. **/ private TrackingResult scanFuture(MethodInsnNode tracking, AbstractInsnNode start) { for (AbstractInsnNode node = start; node != null; node = node.getNext()) { if (node instanceof MethodInsnNode) { MethodInsnNode mInsn = (MethodInsnNode) node; if (mInsn.owner.equals(CommonTypes.CONTSPECULATIONCONTROL.asmName()) && mInsn.name.equals("spawnSpeculation")) { // do nothing } else if (mInsn.owner.startsWith(CommonTypes.FUTURE.asmName()) && mInsn.name.equals("get")) { int futureId = Integer.parseInt(mInsn.owner.substring(mInsn.owner.lastIndexOf('$') + 1)); if (getFutureId(tracking) == futureId) { return TrackingResult.NEEDVALUE; } } else if (mInsn.owner.equals(CommonTypes.SPECULATIONCONTROL.asmName()) && (mInsn.name.equals("nonTransactionalActionAttempted") || mInsn.name.equals("blacklistedActionAttempted"))) { return TrackingResult.FORCEDSYNC; } else if (mInsn.owner.equals(CommonTypes.TRANSACTION.asmName()) || mInsn.owner.equals(CommonTypes.MARKER_BEFOREINLINEDSTORE) || mInsn.owner.equals(CommonTypes.DEBUGCLASS.asmName())) { continue; } else if (mInsn.owner.equals(CommonTypes.REPLACEMENTS.asmName())) { // do nothing } else if (mInsn.owner.startsWith("jaspex") && !CodegenHelper.isCodegenClass(Type.fromAsm(mInsn.owner))) { throw new AssertionError("Resetting tracking due to call to " + mInsn.owner + "." + mInsn.name); } return TrackingResult.OK; } else if (node instanceof JumpInsnNode) { JumpInsnNode jInsn = (JumpInsnNode) node; if (_mNode.instructions.indexOf(jInsn) > _mNode.instructions.indexOf(jInsn.label)) { // Salto para trs, no continuar tracking return TrackingResult.OK; } if (jInsn.getOpcode() == Opcodes.GOTO) return scanFuture(tracking, jInsn.label); if (jInsn.getOpcode() == Opcodes.JSR) throw new AssertionError(); // Opcode um de // IFEQ, IFNE, IFLT, IFGE, IFGT, IFLE, IF_ICMPEQ, IF_ICMPNE, IF_ICMPLT, // IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ACMPEQ, IF_ACMPNE, IFNULL, IFNONNULL // Portanto vamos ter que analisar ambos os branches return TrackingResult.combine(scanFuture(tracking, jInsn.getNext()), scanFuture(tracking, jInsn.label)); } else if (node instanceof LookupSwitchInsnNode || node instanceof TableSwitchInsnNode) { List<LabelNode> targets = new UtilArrayList<LabelNode>(); // Infelizmente o ASM no tem uma classe comum entre os dois tipos de switch if (node instanceof LookupSwitchInsnNode) { LookupSwitchInsnNode sInsn = (LookupSwitchInsnNode) node; if (sInsn.dflt != null) targets.add(sInsn.dflt); targets.addAll(sInsn.labels); } else { TableSwitchInsnNode sInsn = (TableSwitchInsnNode) node; if (sInsn.dflt != null) targets.add(sInsn.dflt); targets.addAll(sInsn.labels); } // Vamos analisar todos os targets do switch e fazer merge do resultado TrackingResult mergedResult = null; for (LabelNode l : targets) { TrackingResult res = scanFuture(tracking, l); if (mergedResult == null) { mergedResult = res; } else { mergedResult = TrackingResult.combine(mergedResult, res); } } return mergedResult; } else if (node instanceof InsnNode) { InsnNode insn = (InsnNode) node; // Encontrmos fim do mtodo if (insn.getOpcode() == Opcodes.ATHROW || returnOpcodes.contains(insn.getOpcode())) { if (new InvokedMethod(_mNode).returnType().equals(Type.PRIM_VOID)) { // Caso especial: Todos os mtodos que retornam void so // escolhidos para especulao -- Uma consequncia directa // disso que ter um futuro activo aquando do retorno do // mtodo no serve para nada, j que a nica coisa que vai // acontecer a especulao acabada de criar vai tentar fazer // commit imediatamente return TrackingResult.FORCEDSYNC; } return TrackingResult.OK; } } } // FIXME: Alguma vez aqui chegamos? //return TrackingResult.OK; throw new AssertionError("FIXME"); }
From source file:name.martingeisse.minimal.compiler.Compiler.java
License:Open Source License
/** * Compiles a {@link MethodNode} to MCode. * //from w w w .j a va 2 s. co m * @param methodNode the method node to compile * @return the compiled code */ public ImmutableList<MCodeEntry> compile(final MethodNode methodNode) { for (int i = 0; i < methodNode.instructions.size(); i++) { final AbstractInsnNode instruction = methodNode.instructions.get(i); if (instruction instanceof LineNumberNode) { // ignored } else if (instruction instanceof FrameNode) { // this could be useful in the future } else if (instruction instanceof LabelNode) { label(((LabelNode) instruction).getLabel()); } else if (instruction instanceof InsnNode) { switch (instruction.getOpcode()) { case Opcodes.ICONST_M1: iconst(-1); break; case Opcodes.ICONST_0: iconst(0); break; case Opcodes.ICONST_1: iconst(1); break; case Opcodes.ICONST_2: iconst(2); break; case Opcodes.ICONST_3: iconst(3); break; case Opcodes.ICONST_4: iconst(4); break; case Opcodes.ICONST_5: iconst(5); break; default: unsupported(instruction); break; } } else if (instruction instanceof VarInsnNode) { final VarInsnNode varInsnNode = (VarInsnNode) instruction; switch (varInsnNode.getOpcode()) { case Opcodes.ILOAD: iload(varInsnNode.var); break; case Opcodes.ISTORE: istore(varInsnNode.var); break; default: unsupported(instruction); break; } } else if (instruction instanceof IincInsnNode) { final IincInsnNode iincInsnNode = (IincInsnNode) instruction; iinc(iincInsnNode.var, iincInsnNode.incr); } else if (instruction instanceof JumpInsnNode) { final JumpInsnNode jumpInsnNode = (JumpInsnNode) instruction; switch (jumpInsnNode.getOpcode()) { case Opcodes.IFEQ: case Opcodes.IFNE: case Opcodes.IFLT: case Opcodes.IFGE: case Opcodes.IFGT: case Opcodes.IFLE: branch1(jumpInsnNode.label.getLabel(), jumpInsnNode.getOpcode()); break; case Opcodes.IF_ICMPEQ: case Opcodes.IF_ICMPNE: case Opcodes.IF_ICMPLT: case Opcodes.IF_ICMPGE: case Opcodes.IF_ICMPGT: case Opcodes.IF_ICMPLE: branch2(jumpInsnNode.label.getLabel(), jumpInsnNode.getOpcode()); break; case Opcodes.IFNULL: case Opcodes.IFNONNULL: // unsupported: one-argument reference comparison operator unsupported(instruction); break; case Opcodes.IF_ACMPEQ: case Opcodes.IF_ACMPNE: // unsupported: two-argument reference comparison operator unsupported(instruction); break; case Opcodes.GOTO: jump(jumpInsnNode.label.getLabel()); break; case Opcodes.JSR: jsr(jumpInsnNode.label.getLabel()); break; default: unsupported(instruction); break; } } else if (instruction instanceof IntInsnNode) { final IntInsnNode intInsnNode = (IntInsnNode) instruction; if (instruction.getOpcode() == Opcodes.BIPUSH || instruction.getOpcode() == Opcodes.SIPUSH) { iconst(intInsnNode.operand); } else { // NEWARRAY unsupported(instruction); } } else if (instruction instanceof MethodInsnNode) { final MethodInsnNode methodInsnNode = (MethodInsnNode) instruction; if (methodInsnNode.getOpcode() == Opcodes.INVOKESTATIC) { if (methodInsnNode.owner.replace('/', '.').equals(Native.class.getName())) { nativeCall(methodInsnNode.name, methodInsnNode.desc); } else { unsupported(instruction); } } else { unsupported(instruction); } } else { unsupported(instruction); } } return builder.build(); }
From source file:net.sourceforge.cobertura.instrument.FindTouchPointsMethodAdapter.java
License:GNU General Public License
@Override public void visitJumpInsn(int opcode, Label label) { /* Ignore any jump instructions in the "class init" method. When initializing static variables, the JVM first checks that the variable is null before attempting to set it. This check contains an IFNONNULL jump instruction which would confuse people if it showed up in the reports.*/ if ((opcode != Opcodes.GOTO) && (opcode != Opcodes.JSR) && (currentLine != 0) && (!methodName.equals("<clinit>"))) { int eventId = getEventId(); touchPointListener.beforeJump(eventId, label, currentLine, mv); super.visitJumpInsn(opcode, label); touchPointListener.afterJump(eventId, label, currentLine, mv); } else {/*from www. j a v a 2s.com*/ super.visitJumpInsn(opcode, label); } }
From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java
License:Open Source License
@Override public void visitJumpInsn(final int opcode, final Label label) { T t = null;/*from w w w .j a v a2s. c o m*/ Object oValue = null; final int targetPc = getPc(label); switch (opcode) { /******** * GOTO * ********/ case Opcodes.GOTO: { final GOTO op = new GOTO(this.ops.size(), opcode, this.line); add(op); op.setTargetPc(targetPc); if (targetPc < 0) { getUnresolved(label).add(op); } break; } /******** * JCMP * ********/ case Opcodes.IF_ACMPEQ: t = T.REF; oValue = CmpType.T_EQ; // fall through case Opcodes.IF_ACMPNE: if (t == null) { t = T.REF; oValue = CmpType.T_NE; } // fall through case Opcodes.IF_ICMPEQ: if (t == null) { t = T.AINT; // boolean too oValue = CmpType.T_EQ; } // fall through case Opcodes.IF_ICMPGE: if (t == null) { t = T.INT; oValue = CmpType.T_GE; } // fall through case Opcodes.IF_ICMPGT: if (t == null) { t = T.INT; oValue = CmpType.T_GT; } // fall through case Opcodes.IF_ICMPLE: if (t == null) { t = T.INT; oValue = CmpType.T_LE; } // fall through case Opcodes.IF_ICMPLT: if (t == null) { t = T.INT; oValue = CmpType.T_LT; } // fall through case Opcodes.IF_ICMPNE: if (t == null) { t = T.AINT; // boolean too oValue = CmpType.T_NE; } { assert oValue instanceof CmpType; final JCMP op = new JCMP(this.ops.size(), opcode, this.line, t, (CmpType) oValue); add(op); op.setTargetPc(targetPc); if (targetPc < 0) { getUnresolved(label).add(op); } } break; /******** * JCND * ********/ case Opcodes.IFNULL: t = T.REF; oValue = CmpType.T_EQ; // fall through case Opcodes.IFNONNULL: if (t == null) { t = T.REF; oValue = CmpType.T_NE; } // fall through case Opcodes.IFEQ: if (t == null) { t = T.AINT; // boolean too oValue = CmpType.T_EQ; } // fall through case Opcodes.IFGE: if (t == null) { t = T.INT; oValue = CmpType.T_GE; } // fall through case Opcodes.IFGT: if (t == null) { t = T.INT; oValue = CmpType.T_GT; } // fall through case Opcodes.IFLE: if (t == null) { t = T.INT; oValue = CmpType.T_LE; } // fall through case Opcodes.IFLT: if (t == null) { t = T.INT; oValue = CmpType.T_LT; } // fall through case Opcodes.IFNE: if (t == null) { t = T.AINT; // boolean too oValue = CmpType.T_NE; } { assert oValue instanceof CmpType; final JCND op = new JCND(this.ops.size(), opcode, this.line, t, (CmpType) oValue); add(op); op.setTargetPc(targetPc); if (targetPc < 0) { getUnresolved(label).add(op); } } break; /******* * JSR * *******/ case Opcodes.JSR: { final JSR op = new JSR(this.ops.size(), opcode, this.line); add(op); op.setTargetPc(targetPc); if (targetPc < 0) { getUnresolved(label).add(op); } break; } default: log.warn(getM() + ": Unknown jump insn opcode '" + opcode + "'!"); } }
From source file:org.evosuite.graphs.cfg.BytecodeInstructionPool.java
License:Open Source License
/** * Determine how many bytes the current instruction occupies together with * its operands//from w w w . ja va 2 s . c om * * @return */ private int getBytecodeIncrement(AbstractInsnNode instructionNode) { int opcode = instructionNode.getOpcode(); switch (opcode) { case Opcodes.ALOAD: // index case Opcodes.ASTORE: // index case Opcodes.DLOAD: case Opcodes.DSTORE: case Opcodes.FLOAD: case Opcodes.FSTORE: case Opcodes.ILOAD: case Opcodes.ISTORE: case Opcodes.LLOAD: case Opcodes.LSTORE: VarInsnNode varNode = (VarInsnNode) instructionNode; if (varNode.var > 3) return 1; else return 0; case Opcodes.BIPUSH: // byte case Opcodes.NEWARRAY: case Opcodes.RET: return 1; case Opcodes.LDC: LdcInsnNode ldcNode = (LdcInsnNode) instructionNode; if (ldcNode.cst instanceof Double || ldcNode.cst instanceof Long) return 2; // LDC2_W else return 1; case 19: //LDC_W case 20: //LDC2_W return 2; case Opcodes.ANEWARRAY: // indexbyte1, indexbyte2 case Opcodes.CHECKCAST: // indexbyte1, indexbyte2 case Opcodes.GETFIELD: case Opcodes.GETSTATIC: case Opcodes.GOTO: case Opcodes.IF_ACMPEQ: case Opcodes.IF_ACMPNE: case Opcodes.IF_ICMPEQ: case Opcodes.IF_ICMPNE: case Opcodes.IF_ICMPGE: case Opcodes.IF_ICMPGT: case Opcodes.IF_ICMPLE: case Opcodes.IF_ICMPLT: case Opcodes.IFLE: case Opcodes.IFLT: case Opcodes.IFGE: case Opcodes.IFGT: case Opcodes.IFNE: case Opcodes.IFEQ: case Opcodes.IFNONNULL: case Opcodes.IFNULL: case Opcodes.IINC: case Opcodes.INSTANCEOF: case Opcodes.INVOKESPECIAL: case Opcodes.INVOKESTATIC: case Opcodes.INVOKEVIRTUAL: case Opcodes.JSR: case Opcodes.NEW: case Opcodes.PUTFIELD: case Opcodes.PUTSTATIC: case Opcodes.SIPUSH: // case Opcodes.LDC_W // case Opcodes.LDC2_W return 2; case Opcodes.MULTIANEWARRAY: return 3; case Opcodes.INVOKEDYNAMIC: case Opcodes.INVOKEINTERFACE: return 4; case Opcodes.LOOKUPSWITCH: case Opcodes.TABLESWITCH: // TODO: Could be more return 4; // case Opcodes.GOTO_W // case Opcodes.JSR_W } return 0; }
From source file:org.graalvm.compiler.replacements.test.DeoptimizeOnExceptionTest.java
License:Open Source License
private static byte[] makeClazz() { // Code generated the class below using asm. String clazzName = DeoptimizeOnExceptionTest.class.getName().replace('.', '/'); final ClassWriter w = new ClassWriter(0); w.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "t/TestJSR", null, "java/lang/Object", new String[] { "java/lang/Runnable" }); MethodVisitor mv = w.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[] {}); mv.visitCode();/*from www . ja v a 2s . co m*/ mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(10, 10); mv.visitEnd(); mv = w.visitMethod(Opcodes.ACC_PUBLIC, "run", "()V", null, null); mv.visitCode(); mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "getM", "()Ljava/lang/Object;", false); Label l1 = new Label(); mv.visitJumpInsn(Opcodes.JSR, l1); mv.visitInsn(Opcodes.RETURN); mv.visitLabel(l1); mv.visitVarInsn(Opcodes.ASTORE, 1); Label lElse = new Label(); Label lEnd = new Label(); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false); mv.visitInsn(Opcodes.POP2); mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "getM", "()Ljava/lang/Object;", false); mv.visitInsn(Opcodes.DUP); mv.visitJumpInsn(Opcodes.IFNULL, lElse); mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "methodA", "()V", false); mv.visitJumpInsn(Opcodes.GOTO, lEnd); mv.visitLabel(lElse); mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "methodB", "()V", false); mv.visitLabel(lEnd); mv.visitVarInsn(Opcodes.RET, 1); mv.visitMaxs(10, 10); mv.visitEnd(); return w.toByteArray(); }
From source file:org.jacoco.core.internal.flow.LabelFlowAnalyzer.java
License:Open Source License
@Override public void visitJumpInsn(final int opcode, final Label label) { LabelInfo.setTarget(label);/*ww w . j a va 2 s. c o m*/ if (opcode == Opcodes.JSR) { throw new AssertionError("Subroutines not supported."); } successor = opcode != Opcodes.GOTO; first = false; }
From source file:org.jboss.byteman.agent.adapter.cfg.BBlock.java
License:Open Source License
/** * write a string represenattion of this block to the buffer * @param buf the buffer to be written to * @return//from w w w. j av a2s.c om */ void printTo(StringBuffer buf) { int blockIdx = this.getBlockIdx(); buf.append(this.getLabel().getOffset()); buf.append(": BB "); buf.append(blockIdx); buf.append("\n"); FanOut containsFanOut = cfg.getContains(this); Iterator<Label> containsIter; Label containedLabel; int containedPosition; if (containsFanOut != null) { containsIter = containsFanOut.iterator(); if (containsIter.hasNext()) { containedLabel = containsIter.next(); containedPosition = cfg.getBlockInstructionIdx(containedLabel); } else { containedLabel = null; containedPosition = -1; } } else { containsIter = null; containedLabel = null; containedPosition = -1; } int instructionCount = this.getInstructionCount(); for (int i = 0; i < instructionCount; i++) { // we will never enter this if containedPosition is -1 which safeguards us when containsIter // is null or containedLabel is null while (containedPosition == i) { buf.append(containedLabel.getOffset()); buf.append(": "); buf.append(containedLabel); buf.append(" +"); buf.append(containedPosition); if (cfg.tryCatchStart(containedLabel)) { List<TryCatchDetails> detailsList = cfg.tryCatchStartDetails(containedLabel); int detailsCount = detailsList.size(); for (int j = 0; j < detailsCount; j++) { TryCatchDetails details = detailsList.get(j); Label handlerLabel = details.getHandler(); CodeLocation handlerLocation = cfg.getLocation(handlerLabel); buf.append(" try "); buf.append(details.getType()); buf.append(" "); if (handlerLocation != null) { buf.append(handlerLabel.getOffset()); } else { buf.append("??"); } buf.append(" "); buf.append(handlerLabel); } } if (cfg.tryCatchEnd(containedLabel)) { List<TryCatchDetails> detailsList = cfg.tryCatchEndDetails(containedLabel); int detailsCount = detailsList.size(); for (int j = 0; j < detailsCount; j++) { TryCatchDetails details = detailsList.get(j); Label handlerLabel = details.getHandler(); CodeLocation handlerLocation = cfg.getLocation(handlerLabel); buf.append(" catch "); buf.append(details.getType()); buf.append(" "); if (handlerLocation != null) { buf.append(handlerLabel.getOffset()); } else { buf.append("??"); } buf.append(" "); buf.append(handlerLabel); } } if (cfg.tryCatchHandlerStart(containedLabel)) { List<TryCatchDetails> detailsList = cfg.tryCatchHandlerStartDetails(containedLabel); int detailsCount = detailsList.size(); for (int j = 0; j < detailsCount; j++) { TryCatchDetails details = detailsList.get(j); buf.append(" handle "); buf.append(details.getType()); buf.append(" "); buf.append(details.getStart().getOffset()); buf.append(" "); buf.append(details.getEnd().getOffset()); } } if (cfg.triggerStart(containedLabel)) { buf.append(" trigger start"); TriggerDetails details = cfg.triggerStartDetails(containedLabel); } if (cfg.triggerEnd(containedLabel)) { buf.append(" trigger end"); } buf.append("\n"); List<CodeLocation> openEnters = cfg.getOpenMonitorEnters(containedLabel); if (openEnters != null) { int openCount = openEnters.size(); if (openCount > 0) { buf.append("open monitors: "); for (int j = 0; j < openCount; j++) { CodeLocation l = openEnters.get(j); buf.append(" BB"); buf.append(l.getBlock().getBlockIdx()); buf.append("."); buf.append(l.getInstructionIdx()); } buf.append('\n'); } } containedLabel = (containsIter.hasNext() ? containsIter.next() : null); containedPosition = (containedLabel != null ? cfg.getBlockInstructionIdx(containedLabel) : -1); } // buf.append(" "); buf.append(blockIdx); buf.append("."); buf.append(i); buf.append(": "); int opcode = this.getInstruction(i); switch (OpcodesHelper.insnType(opcode)) { case OpcodesHelper.INSN_NONE: { // print the instruction name buf.append(OpcodesHelper.insnName(opcode)); if (opcode == Opcodes.MONITOREXIT) { CodeLocation exit = new CodeLocation(this, i); CodeLocation enter = cfg.getPairedEnter(exit); // print the corresponding open instruction buf.append(" (enter: "); buf.append(enter); buf.append(")"); } buf.append("\n"); } break; case OpcodesHelper.INSN_INT: { // just print the instruction name and one integer argument int intValue = this.getInstructionArg(i, 0); buf.append(OpcodesHelper.insnName(opcode)); buf.append(" "); buf.append(intValue); buf.append("\n"); } break; case OpcodesHelper.INSN_LDC: { // print the instruction and one constant argument int nameIdx = this.getInstructionArg(i, 0); String name = cfg.getName(nameIdx); buf.append(OpcodesHelper.insnName(opcode)); buf.append(" "); buf.append(name); buf.append("\n"); } break; case OpcodesHelper.INSN_VAR: { // print the instruction and the var idx int varIdx = this.getInstructionArg(i, 0); buf.append(OpcodesHelper.insnName(opcode)); buf.append(" "); buf.append(varIdx); buf.append("\n"); } break; case OpcodesHelper.INSN_IINC: { // print the instruction and the var idx int increment = this.getInstructionArg(i, 0); buf.append(OpcodesHelper.insnName(opcode)); buf.append(" "); buf.append(increment); buf.append("\n"); } break; case OpcodesHelper.INSN_JUMP: { // note that we may not have generated the code for the jump target yet Label targetLabel = this.firstOut(); CodeLocation targetLocation = cfg.getLocation(targetLabel); int targetPos = (targetLocation != null ? targetLabel.getOffset() : -1); switch (opcode) { case Opcodes.IFEQ: buf.append("IFEQ "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IFNE: buf.append("IFNE "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IFLT: buf.append("IFLT "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IFGE: buf.append("IFGE "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IFGT: buf.append("IFGT "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IFLE: buf.append("IFLE "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IF_ICMPEQ: buf.append("IF_ICMPEQ "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IF_ICMPNE: buf.append("IF_ICMPNE "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IF_ICMPLT: buf.append("IF_ICMPLT "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IF_ICMPGE: buf.append("IF_ICMPGE "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IF_ICMPGT: buf.append("IF_ICMPGT "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IF_ICMPLE: buf.append("IF_ICMPLE "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IF_ACMPEQ: buf.append("IF_ACMPEQ "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IF_ACMPNE: buf.append("IF_ACMPNE "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.GOTO: buf.append("GOTO "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.JSR: buf.append("JSR "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IFNULL: buf.append("IFNULL "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; case Opcodes.IFNONNULL: buf.append("IFNONNULL "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } buf.append("\n"); break; } } break; case OpcodesHelper.INSN_TSWITCH: { Label targetLabel; CodeLocation targetLocation; int targetPos; // print the instruction followed by the jump table discriminant min and max and then // the jump labels int min = this.getInstructionArg(i, 0); int max = this.getInstructionArg(i, 1); int count = (max + 1 - min); buf.append(OpcodesHelper.insnName(opcode)); buf.append(" "); buf.append(min); buf.append(" "); buf.append(max); buf.append("\n"); for (int j = 1; j <= count; j++) { // note that we may not have generated the code for the jump target yet targetLabel = this.nthOut(j); targetLocation = cfg.getLocation(targetLabel); targetPos = (targetLocation != null ? targetLabel.getOffset() : -1); buf.append(" "); buf.append(min + j); buf.append(" : "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } } targetLabel = this.firstOut(); targetLocation = cfg.getLocation(targetLabel); targetPos = (targetLocation != null ? targetLabel.getOffset() : -1); buf.append(" dflt : "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } } break; case OpcodesHelper.INSN_LOOKUP: { Label targetLabel; CodeLocation targetLocation; int targetPos; // print the instruction followed by each jump table discriminant and label int count = this.getInstructionArg(i, 0); buf.append(OpcodesHelper.insnName(opcode)); buf.append("\n"); for (int j = 1; j <= count; j++) { // note that we may not have generated the code for the jump target yet targetLabel = this.nthOut(j); targetLocation = cfg.getLocation(targetLabel); targetPos = (targetLocation != null ? targetLabel.getOffset() : -1); buf.append(" "); buf.append(this.getInstructionArg(i, j)); buf.append(" : "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } } targetLabel = this.firstOut(); targetLocation = cfg.getLocation(targetLabel); targetPos = (targetLocation != null ? targetLabel.getOffset() : -1); buf.append(" dflt : "); if (targetPos >= 0) { buf.append(targetPos); buf.append(" BB "); buf.append(targetLocation.getBlock().getBlockIdx()); buf.append(" "); buf.append(targetLabel); } else { buf.append("?? "); buf.append(targetLabel); } } break; case OpcodesHelper.INSN_FIELD: case OpcodesHelper.INSN_METHOD: { // print the instruction with the owner, name and descriptor int idx1 = this.getInstructionArg(i, 0); int idx2 = this.getInstructionArg(i, 1); int idx3 = this.getInstructionArg(i, 2); String owner = cfg.getName(idx1); String name = cfg.getName(idx2); String desc = cfg.getName(idx3); buf.append(OpcodesHelper.insnName(opcode)); buf.append(" "); buf.append(owner); buf.append(" "); buf.append(name); buf.append(" "); buf.append(desc); buf.append("\n"); } break; case OpcodesHelper.INSN_TYPE: { // print the instruction with the type name int idx = this.getInstructionArg(i, 0); String name = cfg.getName(idx); buf.append(OpcodesHelper.insnName(opcode)); buf.append(" "); buf.append(name); buf.append("\n"); } break; case OpcodesHelper.INSN_MULTIANEWARRAY: { // print the instruction with the typename and the dimension count int idx = this.getInstructionArg(i, 0); int dims = this.getInstructionArg(i, 1); String name = cfg.getName(idx); buf.append(OpcodesHelper.insnName(opcode)); buf.append(" "); buf.append(name); buf.append(" "); buf.append(dims); buf.append("\n"); } break; case OpcodesHelper.INSN_UNUSED: { // print the instruction buf.append(OpcodesHelper.insnName(opcode)); buf.append("!!!\n"); } break; } } // print the active starts for this block if (activeTryStarts != null) { Iterator<TryCatchDetails> activeStartsIter = activeTryStarts.iterator(); while (activeStartsIter.hasNext()) { TryCatchDetails details = activeStartsIter.next(); Label label = details.getStart(); BBlock block = cfg.getBlock(label); buf.append("try: "); if (block != null) { buf.append(label.getOffset()); buf.append(" "); buf.append(block.getBlockIdx()); buf.append("."); buf.append(cfg.getBlockInstructionIdx(label)); } else { buf.append(label); } buf.append(" catch: "); label = details.getEnd(); block = cfg.getBlock(label); if (block != null) { buf.append(label.getOffset()); buf.append(" "); buf.append(block.getBlockIdx()); buf.append("."); buf.append(cfg.getBlockInstructionIdx(label)); } else { buf.append(label); } buf.append(" handle: "); label = details.getHandler(); block = cfg.getBlock(label); if (block != null) { buf.append(label.getOffset()); buf.append(" "); buf.append(block.getBlockIdx()); buf.append("."); buf.append(cfg.getBlockInstructionIdx(label)); } else { buf.append(label); } buf.append(" "); buf.append(details.getType()); buf.append("\n"); } } }
From source file:org.jboss.byteman.agent.adapter.RuleTriggerMethodAdapter.java
License:Open Source License
@Override public void visitJumpInsn(int opcode, Label label) { super.visitJumpInsn(opcode, label); switch (opcode) { case Opcodes.IFEQ: case Opcodes.IFNE: case Opcodes.IFLT: case Opcodes.IFGE: case Opcodes.IFGT: case Opcodes.IFLE: case Opcodes.IF_ICMPEQ: case Opcodes.IF_ICMPNE: case Opcodes.IF_ICMPLT: case Opcodes.IF_ICMPGE: case Opcodes.IF_ICMPGT: case Opcodes.IF_ICMPLE: case Opcodes.IF_ACMPEQ: case Opcodes.IF_ACMPNE: { // create a new current block and add the label supplied in the call as the // first out of the old current block and the label of the new current block as // the second out cfg.add(opcode);/*from w w w . j a va 2 s . co m*/ Label newStart = super.newLabel(); // must call split before visiting the label cfg.split(newStart, label, newStart); visitLabel(newStart); } break; case Opcodes.GOTO: { // create a new current block and add the label supplied in the call as the // first out of the old current block cfg.add(opcode); Label newStart = super.newLabel(); // must call split before visiting the label cfg.split(newStart, label); visitLabel(newStart); } break; case Opcodes.JSR: { // create a new current block and add the label supplied in the call as the first out // of the current block -- the new current block is a potential return point from the // JSR but we cannot represent that statically cfg.add(opcode); Label newStart = super.newLabel(); // must call split before visiting the label cfg.split(newStart, label, newStart); visitLabel(newStart); } break; case Opcodes.IFNULL: case Opcodes.IFNONNULL: { // create a new current block and add the label supplied in the call as the // first out of the old current block and the label of the new current block as // the second out cfg.add(opcode); Label newStart = super.newLabel(); // must call split before visiting the label cfg.split(newStart, label, newStart); visitLabel(newStart); } break; } }