List of usage examples for org.objectweb.asm Opcodes ACONST_NULL
int ACONST_NULL
To view the source code for org.objectweb.asm Opcodes ACONST_NULL.
Click Source Link
From source file:blue.origami.asm.OGeneratorAdapter.java
License:Apache License
public void pushNull() { this.visitInsn(Opcodes.ACONST_NULL); }
From source file:bytecode.InstructionExporter.java
License:Apache License
/** * Outputs constants into stack operations. This will use different JVM * instructions depending on whether there are short-hands for the given * constant etc.// ww w . j a v a 2s . c o m * * @param instruction Constant. * @return <code>null</code> */ @Override public Void visit(Constant instruction) { // NULL Pointer if (instruction.getConstant() == null) { mv.visitInsn(Opcodes.ACONST_NULL); // Integers } else if (instruction.getConstant() instanceof Integer) { Integer i = (Integer) instruction.getConstant(); switch (i.intValue()) { case -1: mv.visitInsn(Opcodes.ICONST_M1); break; case 0: mv.visitInsn(Opcodes.ICONST_0); break; case 1: mv.visitInsn(Opcodes.ICONST_1); break; case 2: mv.visitInsn(Opcodes.ICONST_2); break; case 3: mv.visitInsn(Opcodes.ICONST_3); break; case 4: mv.visitInsn(Opcodes.ICONST_4); break; case 5: mv.visitInsn(Opcodes.ICONST_5); break; default: mv.visitLdcInsn(i); } // Longs } else if (instruction.getConstant() instanceof Long) { Long l = (Long) instruction.getConstant(); if (l.longValue() == 0) mv.visitInsn(Opcodes.LCONST_0); else if (l.longValue() == 1) mv.visitInsn(Opcodes.ICONST_1); else mv.visitLdcInsn(l); // Floats } else if (instruction.getConstant() instanceof Float) { Float f = (Float) instruction.getConstant(); // Should be safe since 0 and 1 are both exactly representable in FP. if (f.floatValue() == 0.0f) mv.visitInsn(Opcodes.FCONST_0); else if (f.floatValue() == 1.0f) mv.visitInsn(Opcodes.FCONST_1); else if (f.floatValue() == 2.0f) mv.visitInsn(Opcodes.FCONST_2); else mv.visitLdcInsn(f); // Doubles } else if (instruction.getConstant() instanceof Double) { Double d = (Double) instruction.getConstant(); // Should be safe since 0 and 1 are both exactly representable in FP. if (d.doubleValue() == 0.0f) mv.visitInsn(Opcodes.DCONST_0); else if (d.doubleValue() == 1.0f) mv.visitInsn(Opcodes.DCONST_1); else mv.visitLdcInsn(d); // Byte Pushes } else if (instruction.getConstant() instanceof Byte) { Byte b = (Byte) instruction.getConstant(); mv.visitIntInsn(Opcodes.BIPUSH, b.intValue()); // Short Pushes } else if (instruction.getConstant() instanceof Short) { Short s = (Short) instruction.getConstant(); mv.visitIntInsn(Opcodes.SIPUSH, s.intValue()); // Otherwise a constant pool load. } else { mv.visitLdcInsn(instruction.getConstant()); } return null; }
From source file:bytecode.MethodImporter.java
License:Apache License
/** * Imports instructions with no immediate operands. * * @param opcode Opcode./*from ww w.jav a 2 s . c om*/ */ @Override public void visitInsn(final int opcode) { Producer a, b, c, d; Type top; switch (opcode) { // Constants case Opcodes.ACONST_NULL: createConstant(null); break; case Opcodes.ICONST_M1: createConstant(new Integer(-1)); break; case Opcodes.ICONST_0: createConstant(new Integer(0)); break; case Opcodes.ICONST_1: createConstant(new Integer(1)); break; case Opcodes.ICONST_2: createConstant(new Integer(2)); break; case Opcodes.ICONST_3: createConstant(new Integer(3)); break; case Opcodes.ICONST_4: createConstant(new Integer(4)); break; case Opcodes.ICONST_5: createConstant(new Integer(5)); break; case Opcodes.LCONST_0: createConstant(new Long(0)); break; case Opcodes.LCONST_1: createConstant(new Long(1)); break; case Opcodes.FCONST_0: createConstant(new Float(0.0f)); break; case Opcodes.FCONST_1: createConstant(new Float(1.0f)); break; case Opcodes.FCONST_2: createConstant(new Float(2.0f)); break; case Opcodes.DCONST_0: createConstant(new Double(0.0f)); break; case Opcodes.DCONST_1: createConstant(new Double(1.0f)); break; // Binary Operations case Opcodes.IADD: createArithmetic(Arithmetic.Operator.ADD, Type.INT); break; case Opcodes.LADD: createArithmetic(Arithmetic.Operator.ADD, Type.LONG); break; case Opcodes.FADD: createArithmetic(Arithmetic.Operator.ADD, Type.FLOAT); break; case Opcodes.DADD: createArithmetic(Arithmetic.Operator.ADD, Type.DOUBLE); break; case Opcodes.ISUB: createArithmetic(Arithmetic.Operator.SUB, Type.INT); break; case Opcodes.LSUB: createArithmetic(Arithmetic.Operator.SUB, Type.LONG); break; case Opcodes.FSUB: createArithmetic(Arithmetic.Operator.SUB, Type.FLOAT); break; case Opcodes.DSUB: createArithmetic(Arithmetic.Operator.SUB, Type.DOUBLE); break; case Opcodes.IMUL: createArithmetic(Arithmetic.Operator.MUL, Type.INT); break; case Opcodes.LMUL: createArithmetic(Arithmetic.Operator.MUL, Type.LONG); break; case Opcodes.FMUL: createArithmetic(Arithmetic.Operator.MUL, Type.FLOAT); break; case Opcodes.DMUL: createArithmetic(Arithmetic.Operator.MUL, Type.DOUBLE); break; case Opcodes.IDIV: createArithmetic(Arithmetic.Operator.DIV, Type.INT); break; case Opcodes.LDIV: createArithmetic(Arithmetic.Operator.DIV, Type.LONG); break; case Opcodes.FDIV: createArithmetic(Arithmetic.Operator.DIV, Type.FLOAT); break; case Opcodes.DDIV: createArithmetic(Arithmetic.Operator.DIV, Type.DOUBLE); break; case Opcodes.IREM: createArithmetic(Arithmetic.Operator.REM, Type.INT); break; case Opcodes.LREM: createArithmetic(Arithmetic.Operator.REM, Type.LONG); break; case Opcodes.FREM: createArithmetic(Arithmetic.Operator.REM, Type.FLOAT); break; case Opcodes.DREM: createArithmetic(Arithmetic.Operator.REM, Type.DOUBLE); break; case Opcodes.IAND: createArithmetic(Arithmetic.Operator.AND, Type.INT); break; case Opcodes.LAND: createArithmetic(Arithmetic.Operator.AND, Type.LONG); break; case Opcodes.IOR: createArithmetic(Arithmetic.Operator.OR, Type.INT); break; case Opcodes.LOR: createArithmetic(Arithmetic.Operator.OR, Type.LONG); break; case Opcodes.IXOR: createArithmetic(Arithmetic.Operator.XOR, Type.INT); break; case Opcodes.LXOR: createArithmetic(Arithmetic.Operator.XOR, Type.LONG); break; case Opcodes.ISHL: createArithmetic(Arithmetic.Operator.SHL, Type.INT); break; case Opcodes.LSHL: createArithmetic(Arithmetic.Operator.SHL, Type.LONG); break; case Opcodes.ISHR: createArithmetic(Arithmetic.Operator.SHR, Type.INT); break; case Opcodes.LSHR: createArithmetic(Arithmetic.Operator.SHR, Type.LONG); break; case Opcodes.IUSHR: createArithmetic(Arithmetic.Operator.USHR, Type.INT); break; case Opcodes.LUSHR: createArithmetic(Arithmetic.Operator.USHR, Type.LONG); break; case Opcodes.LCMP: createCompare(false, Type.LONG); break; case Opcodes.FCMPL: createCompare(false, Type.FLOAT); break; case Opcodes.FCMPG: createCompare(true, Type.FLOAT); break; case Opcodes.DCMPL: createCompare(false, Type.DOUBLE); break; case Opcodes.DCMPG: createCompare(true, Type.DOUBLE); break; case Opcodes.INEG: createNegate(); break; case Opcodes.LNEG: createNegate(); break; case Opcodes.FNEG: createNegate(); break; case Opcodes.DNEG: createNegate(); break; case Opcodes.I2L: createConvert(Type.LONG); break; case Opcodes.I2F: createConvert(Type.FLOAT); break; case Opcodes.I2D: createConvert(Type.DOUBLE); break; case Opcodes.I2B: createConvert(Type.BYTE); break; case Opcodes.I2C: createConvert(Type.CHAR); break; case Opcodes.I2S: createConvert(Type.SHORT); break; case Opcodes.L2I: createConvert(Type.INT); break; case Opcodes.L2F: createConvert(Type.FLOAT); break; case Opcodes.L2D: createConvert(Type.DOUBLE); break; case Opcodes.F2I: createConvert(Type.INT); break; case Opcodes.F2L: createConvert(Type.LONG); break; case Opcodes.F2D: createConvert(Type.DOUBLE); break; case Opcodes.D2I: createConvert(Type.INT); break; case Opcodes.D2F: createConvert(Type.FLOAT); break; case Opcodes.D2L: createConvert(Type.LONG); break; case Opcodes.IALOAD: createArrayRead(Type.INT); break; case Opcodes.LALOAD: createArrayRead(Type.LONG); break; case Opcodes.FALOAD: createArrayRead(Type.FLOAT); break; case Opcodes.DALOAD: createArrayRead(Type.DOUBLE); break; case Opcodes.AALOAD: createArrayRead(Type.getFreshRef()); break; case Opcodes.BALOAD: createArrayRead(Type.BYTE); break; case Opcodes.CALOAD: createArrayRead(Type.CHAR); break; case Opcodes.SALOAD: createArrayRead(Type.SHORT); break; case Opcodes.IASTORE: createArrayWrite(Type.INT); break; case Opcodes.LASTORE: createArrayWrite(Type.LONG); break; case Opcodes.FASTORE: createArrayWrite(Type.FLOAT); break; case Opcodes.DASTORE: createArrayWrite(Type.DOUBLE); break; case Opcodes.AASTORE: createArrayWrite(Type.getFreshRef()); break; case Opcodes.BASTORE: createArrayWrite(Type.BYTE); break; case Opcodes.CASTORE: createArrayWrite(Type.CHAR); break; case Opcodes.SASTORE: createArrayWrite(Type.SHORT); break; case Opcodes.IRETURN: createReturn(Type.INT); break; case Opcodes.LRETURN: createReturn(Type.LONG); break; case Opcodes.FRETURN: createReturn(Type.FLOAT); break; case Opcodes.DRETURN: createReturn(Type.DOUBLE); break; case Opcodes.ARETURN: createReturn(Type.REF); break; case Opcodes.RETURN: createReturn(null); break; case Opcodes.ATHROW: createThrow(); break; // Array Length case Opcodes.ARRAYLENGTH: ordered.add(stack.push(new ArrayLength(stack.pop()))); break; // Swap case Opcodes.SWAP: a = stack.pop(); b = stack.pop(); stack.push(a); stack.push(b); ordered.add(new StackOperation(StackOperation.Sort.SWAP)); break; // Duplicates case Opcodes.DUP: stack.push(stack.peek()); ordered.add(new StackOperation(StackOperation.Sort.DUP)); break; case Opcodes.DUP2: top = stack.peek().getType(); // Type 2 Values if (top.getSize() == 2) { stack.push(stack.peek()); // Type 1 Values } else { b = stack.pop(); a = stack.pop(); stack.push(a); stack.push(b); stack.push(a); stack.push(b); } ordered.add(new StackOperation(StackOperation.Sort.DUP2)); break; case Opcodes.DUP_X1: b = stack.pop(); a = stack.pop(); stack.push(b); stack.push(a); stack.push(b); ordered.add(new StackOperation(StackOperation.Sort.DUP_X1)); break; case Opcodes.DUP_X2: top = stack.peek().getType(); // Type 2 Values if (top.getSize() == 2) { b = stack.pop(); a = stack.pop(); stack.push(b); stack.push(a); stack.push(b); // Type 1 Values } else { c = stack.pop(); b = stack.pop(); a = stack.pop(); stack.push(c); stack.push(a); stack.push(b); stack.push(c); } ordered.add(new StackOperation(StackOperation.Sort.DUP_X2)); break; // Pops case Opcodes.POP: stack.pop(); ordered.add(new StackOperation(StackOperation.Sort.POP)); break; case Opcodes.POP2: top = stack.peek().getType(); // Type 2 Values if (top.getSize() == 2) { stack.pop(); // Type 1 Values } else { stack.pop(); stack.pop(); } ordered.add(new StackOperation(StackOperation.Sort.POP2)); break; // TODO: DUP2_X1, DUP2_X2, MONITORENTER, MONITOREXIT case Opcodes.MONITORENTER: throw new RuntimeException("visitInsn: MONITORENTER"); case Opcodes.MONITOREXIT: throw new RuntimeException("visitInsn: MONITOREXIT"); case Opcodes.DUP2_X1: throw new RuntimeException("visitInsn: DUP2_X1"); case Opcodes.DUP2_X2: throw new RuntimeException("visitInsn: DUP2_X2"); default: throw new RuntimeException("visitInsn: " + opcode); } }
From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java
License:Open Source License
public void accept(MethodVisitor mv, boolean hasAnnotation) { db.log(LogLevel.INFO, "Instrumenting method %s#%s%s", className, mn.name, mn.desc); mv.visitAnnotation(ALREADY_INSTRUMENTED_DESC, true); final boolean handleProxyInvocations = HANDLE_PROXY_INVOCATIONS & hasSuspendableSuperCalls; mv.visitCode();/*from w w w . ja va 2s . c o m*/ Label lMethodStart = new Label(); Label lMethodStart2 = new Label(); Label lMethodEnd = new Label(); Label lCatchSEE = new Label(); Label lCatchUTE = new Label(); Label lCatchAll = new Label(); Label[] lMethodCalls = new Label[numCodeBlocks - 1]; for (int i = 1; i < numCodeBlocks; i++) lMethodCalls[i - 1] = new Label(); mv.visitInsn(Opcodes.ACONST_NULL); mv.visitVarInsn(Opcodes.ASTORE, lvarInvocationReturnValue); // if (verifyInstrumentation) { // mv.visitInsn(Opcodes.ICONST_0); // mv.visitVarInsn(Opcodes.ISTORE, lvarSuspendableCalled); // } mv.visitTryCatchBlock(lMethodStart, lMethodEnd, lCatchSEE, EXCEPTION_NAME); if (handleProxyInvocations) mv.visitTryCatchBlock(lMethodStart, lMethodEnd, lCatchUTE, UNDECLARED_THROWABLE_NAME); // Prepare visitTryCatchBlocks for InvocationTargetException. // With reflective invocations, the SuspendExecution exception will be wrapped in InvocationTargetException. We need to catch it and unwrap it. // Note that the InvocationTargetException will be regenrated on every park, adding further overhead on top of the reflective call. // This must be done here, before all other visitTryCatchBlock, because the exception's handler // will be matched according to the order of in which visitTryCatchBlock has been called. Earlier calls take precedence. Label[][] refInvokeTryCatch = new Label[numCodeBlocks - 1][]; for (int i = 1; i < numCodeBlocks; i++) { FrameInfo fi = codeBlocks[i]; AbstractInsnNode in = mn.instructions.get(fi.endInstruction); if (mn.instructions.get(fi.endInstruction) instanceof MethodInsnNode) { MethodInsnNode min = (MethodInsnNode) in; if (isReflectInvocation(min.owner, min.name)) { Label[] ls = new Label[3]; for (int k = 0; k < 3; k++) ls[k] = new Label(); refInvokeTryCatch[i - 1] = ls; mv.visitTryCatchBlock(ls[0], ls[1], ls[2], "java/lang/reflect/InvocationTargetException"); } } } for (Object o : mn.tryCatchBlocks) { TryCatchBlockNode tcb = (TryCatchBlockNode) o; if (EXCEPTION_NAME.equals(tcb.type) && !hasAnnotation) // we allow catch of SuspendExecution in method annotated with @Suspendable. throw new UnableToInstrumentException("catch for SuspendExecution", className, mn.name, mn.desc); if (handleProxyInvocations && UNDECLARED_THROWABLE_NAME.equals(tcb.type)) // we allow catch of SuspendExecution in method annotated with @Suspendable. throw new UnableToInstrumentException("catch for UndeclaredThrowableException", className, mn.name, mn.desc); // if (INTERRUPTED_EXCEPTION_NAME.equals(tcb.type)) // throw new UnableToInstrumentException("catch for " + InterruptedException.class.getSimpleName(), className, mn.name, mn.desc); tcb.accept(mv); } if (mn.visibleParameterAnnotations != null) dumpParameterAnnotations(mv, mn.visibleParameterAnnotations, true); if (mn.invisibleParameterAnnotations != null) dumpParameterAnnotations(mv, mn.invisibleParameterAnnotations, false); if (mn.visibleAnnotations != null) { for (Object o : mn.visibleAnnotations) { AnnotationNode an = (AnnotationNode) o; an.accept(mv.visitAnnotation(an.desc, true)); } } mv.visitTryCatchBlock(lMethodStart, lMethodEnd, lCatchAll, null); mv.visitMethodInsn(Opcodes.INVOKESTATIC, STACK_NAME, "getStack", "()L" + STACK_NAME + ";"); mv.visitInsn(Opcodes.DUP); mv.visitVarInsn(Opcodes.ASTORE, lvarStack); // println(mv, "STACK: ", lvarStack); // dumpStack(mv); if (DUAL) { mv.visitJumpInsn(Opcodes.IFNULL, lMethodStart); mv.visitVarInsn(Opcodes.ALOAD, lvarStack); } emitStoreResumed(mv, true); // we'll assume we have been resumed mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "nextMethodEntry", "()I"); mv.visitTableSwitchInsn(1, numCodeBlocks - 1, lMethodStart2, lMethodCalls); mv.visitLabel(lMethodStart2); // the following code handles the case of an instrumented method called not as part of a suspendable code path // isFirstInStack will return false in that case. mv.visitVarInsn(Opcodes.ALOAD, lvarStack); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "isFirstInStackOrPushed", "()Z"); mv.visitJumpInsn(Opcodes.IFNE, lMethodStart); // if true mv.visitInsn(Opcodes.ACONST_NULL); mv.visitVarInsn(Opcodes.ASTORE, lvarStack); mv.visitLabel(lMethodStart); emitStoreResumed(mv, false); // no, we have not been resumed dumpCodeBlock(mv, 0, 0); for (int i = 1; i < numCodeBlocks; i++) { FrameInfo fi = codeBlocks[i]; MethodInsnNode min = (MethodInsnNode) (mn.instructions.get(fi.endInstruction)); if (isYieldMethod(min.owner, min.name)) { // special case - call to yield if (min.getOpcode() != Opcodes.INVOKESTATIC) throw new UnableToInstrumentException("invalid call to suspending method.", className, mn.name, mn.desc); final int numYieldArgs = TypeAnalyzer.getNumArguments(min.desc); final boolean yieldReturnsValue = (Type.getReturnType(min.desc) != Type.VOID_TYPE); emitStoreState(mv, i, fi, numYieldArgs); // we preserve the arguments for the call to yield on the operand stack emitStoreResumed(mv, false); // we have not been resumed // emitSuspendableCalled(mv); min.accept(mv); // we call the yield method if (yieldReturnsValue) mv.visitInsn(Opcodes.POP); // we ignore the returned value... mv.visitLabel(lMethodCalls[i - 1]); // we resume AFTER the call final Label afterPostRestore = new Label(); mv.visitVarInsn(Opcodes.ILOAD, lvarResumed); mv.visitJumpInsn(Opcodes.IFEQ, afterPostRestore); emitPostRestore(mv); mv.visitLabel(afterPostRestore); emitRestoreState(mv, i, fi, numYieldArgs); if (yieldReturnsValue) mv.visitVarInsn(Opcodes.ILOAD, lvarResumed); // ... and replace the returned value with the value of resumed dumpCodeBlock(mv, i, 1); // skip the call } else { final Label lbl = new Label(); if (DUAL) { mv.visitVarInsn(Opcodes.ALOAD, lvarStack); mv.visitJumpInsn(Opcodes.IFNULL, lbl); } // normal case - call to a suspendable method - resume before the call emitStoreState(mv, i, fi, 0); emitStoreResumed(mv, false); // we have not been resumed // emitPreemptionPoint(mv, PREEMPTION_CALL); mv.visitLabel(lMethodCalls[i - 1]); emitRestoreState(mv, i, fi, 0); if (DUAL) mv.visitLabel(lbl); if (isReflectInvocation(min.owner, min.name)) { // We catch the InvocationTargetException and unwrap it if it wraps a SuspendExecution exception. Label[] ls = refInvokeTryCatch[i - 1]; final Label startTry = ls[0]; final Label endTry = ls[1]; final Label startCatch = ls[2]; final Label endCatch = new Label(); final Label notSuspendExecution = new Label(); // mv.visitTryCatchBlock(startTry, endTry, startCatch, "java/lang/reflect/InvocationTargetException"); mv.visitLabel(startTry); // try { min.accept(mv); // method.invoke() mv.visitVarInsn(Opcodes.ASTORE, lvarInvocationReturnValue); // save return value mv.visitLabel(endTry); // } mv.visitJumpInsn(Opcodes.GOTO, endCatch); mv.visitLabel(startCatch); // catch(InvocationTargetException ex) { mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "getCause", "()Ljava/lang/Throwable;"); mv.visitTypeInsn(Opcodes.INSTANCEOF, EXCEPTION_NAME); mv.visitJumpInsn(Opcodes.IFEQ, notSuspendExecution); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "getCause", "()Ljava/lang/Throwable;"); mv.visitLabel(notSuspendExecution); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(endCatch); mv.visitVarInsn(Opcodes.ALOAD, lvarInvocationReturnValue); // restore return value dumpCodeBlock(mv, i, 1); // skip the call } else { // emitSuspendableCalled(mv); dumpCodeBlock(mv, i, 0); } } } mv.visitLabel(lMethodEnd); if (handleProxyInvocations) { mv.visitLabel(lCatchUTE); mv.visitInsn(Opcodes.DUP); // println(mv, "CTCH: "); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "getCause", "()Ljava/lang/Throwable;"); // println(mv, "CAUSE: "); mv.visitTypeInsn(Opcodes.INSTANCEOF, EXCEPTION_NAME); mv.visitJumpInsn(Opcodes.IFEQ, lCatchAll); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "getCause", "()Ljava/lang/Throwable;"); mv.visitJumpInsn(Opcodes.GOTO, lCatchSEE); } mv.visitLabel(lCatchAll); emitPopMethod(mv); mv.visitLabel(lCatchSEE); // println(mv, "THROW: "); mv.visitInsn(Opcodes.ATHROW); // rethrow shared between catchAll and catchSSE if (mn.localVariables != null) { for (Object o : mn.localVariables) ((LocalVariableNode) o).accept(mv); } mv.visitMaxs(mn.maxStack + ADD_OPERANDS, mn.maxLocals + NUM_LOCALS + additionalLocals); mv.visitEnd(); }
From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java
License:Open Source License
private void emitStoreState(MethodVisitor mv, int idx, FrameInfo fi, int numArgsToPreserve) { Frame f = frames[fi.endInstruction]; if (fi.lBefore != null) fi.lBefore.accept(mv);//from w w w .jav a2 s. c o m mv.visitVarInsn(Opcodes.ALOAD, lvarStack); emitConst(mv, idx); emitConst(mv, fi.numSlots); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "pushMethod", "(II)V"); // store operand stack for (int i = f.getStackSize(); i-- > 0;) { BasicValue v = (BasicValue) f.getStack(i); if (!isOmitted(v)) { if (!isNullType(v)) { int slotIdx = fi.stackSlotIndices[i]; assert slotIdx >= 0 && slotIdx < fi.numSlots; emitStoreValue(mv, v, lvarStack, slotIdx, -1); } else { db.log(LogLevel.DEBUG, "NULL stack entry: type=%s size=%d", v.getType(), v.getSize()); mv.visitInsn(Opcodes.POP); } } } // store local vars for (int i = firstLocal; i < f.getLocals(); i++) { BasicValue v = (BasicValue) f.getLocal(i); if (!isNullType(v)) { mv.visitVarInsn(v.getType().getOpcode(Opcodes.ILOAD), i); int slotIdx = fi.localSlotIndices[i]; assert slotIdx >= 0 && slotIdx < fi.numSlots; emitStoreValue(mv, v, lvarStack, slotIdx, i); } } // restore last numArgsToPreserve operands for (int i = f.getStackSize() - numArgsToPreserve; i < f.getStackSize(); i++) { BasicValue v = (BasicValue) f.getStack(i); if (!isOmitted(v)) { if (!isNullType(v)) { int slotIdx = fi.stackSlotIndices[i]; assert slotIdx >= 0 && slotIdx < fi.numSlots; emitRestoreValue(mv, v, lvarStack, slotIdx, -1); } else { mv.visitInsn(Opcodes.ACONST_NULL); } } } }
From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java
License:Open Source License
private void emitRestoreState(MethodVisitor mv, int idx, FrameInfo fi, int numArgsPreserved) { Frame f = frames[fi.endInstruction]; // restore local vars for (int i = firstLocal; i < f.getLocals(); i++) { BasicValue v = (BasicValue) f.getLocal(i); if (!isNullType(v)) { int slotIdx = fi.localSlotIndices[i]; assert slotIdx >= 0 && slotIdx < fi.numSlots; emitRestoreValue(mv, v, lvarStack, slotIdx, i); mv.visitVarInsn(v.getType().getOpcode(Opcodes.ISTORE), i); } else if (v != BasicValue.UNINITIALIZED_VALUE) { mv.visitInsn(Opcodes.ACONST_NULL); mv.visitVarInsn(Opcodes.ASTORE, i); }//from w w w .j a v a 2 s .c om } // restore operand stack for (int i = 0; i < f.getStackSize() - numArgsPreserved; i++) { BasicValue v = (BasicValue) f.getStack(i); if (!isOmitted(v)) { if (!isNullType(v)) { int slotIdx = fi.stackSlotIndices[i]; assert slotIdx >= 0 && slotIdx < fi.numSlots; emitRestoreValue(mv, v, lvarStack, slotIdx, -1); } else { mv.visitInsn(Opcodes.ACONST_NULL); } } } if (fi.lAfter != null) { fi.lAfter.accept(mv); } }
From source file:com.alibaba.hotswap.processor.constructor.modifier.ConstructorInvokeModifier.java
License:Open Source License
@Override public void visitMethodInsn(int opcode, String owner, String name, String desc) { if (opcode == Opcodes.INVOKESPECIAL && name.equals(HotswapConstants.INIT)) { if (HotswapRuntime.hasClassMeta(owner)) { // Try to reload owner. ClassMeta classMeta = HotswapRuntime.getClassMeta(owner); String mk = HotswapMethodUtil.getMethodKey(name, desc); MethodMeta mm = classMeta.initMetas.get(mk); if (mm != null && mm.isAdded()) { Type[] argsType = Type.getArgumentTypes(desc); push(argsType.length);//from www. j a va 2 s . c o m newArray(Type.getType(Object.class)); for (int i = argsType.length - 1; i >= 0; i--) { Type type = argsType[i]; swap(); box(type); push(i); swap(); // arrayStore(Type.getType(Object.class)); mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapMethodUtil.class), "processConstructorArgs", "([Ljava/lang/Object;ILjava/lang/Object;)[Ljava/lang/Object;"); } mv.visitLdcInsn(Opcodes.ACONST_NULL); swap(); mv.visitLdcInsn(mm.getIndex()); swap(); super.visitMethodInsn(opcode, owner, name, HotswapConstants.UNIFORM_CONSTRUCTOR_DESC); return; } } } super.visitMethodInsn(opcode, owner, name, desc); }
From source file:com.android.build.gradle.internal.incremental.ConstructorArgsRedirection.java
License:Apache License
@Override protected void restore(GeneratorAdapter mv, List<Type> args) { // At this point, init$args has been called and the result Object is on the stack. // The value of that Object is Object[] with exactly n + 1 elements. // The first element is a string with the qualified name of the constructor to call. // The remaining elements are the constructtor arguments. // Create a new local that holds the result of init$args call. mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); int constructorArgs = mv.newLocal(Type.getType("[Ljava/lang/Object;")); mv.storeLocal(constructorArgs);//w ww . ja v a 2s.c om // Reinstate local values mv.loadLocal(locals); int stackIndex = 0; for (int arrayIndex = 0; arrayIndex < args.size(); arrayIndex++) { Type arg = args.get(arrayIndex); // Do not restore "this" if (arrayIndex > 0) { // duplicates the array mv.dup(); // index in the array of objects to restore the boxed parameter. mv.push(arrayIndex); // get it from the array mv.arrayLoad(Type.getType(Object.class)); // unbox the argument ByteCodeUtils.unbox(mv, arg); // restore the argument mv.visitVarInsn(arg.getOpcode(Opcodes.ISTORE), stackIndex); } // stack index must progress according to the parameter type we just processed. stackIndex += arg.getSize(); } // pops the array mv.pop(); // Push a null for the marker parameter. mv.loadLocal(constructorArgs); mv.visitInsn(Opcodes.ACONST_NULL); // Invoke the constructor mv.visitMethodInsn(Opcodes.INVOKESPECIAL, thisClassName, "<init>", DISPATCHING_THIS_SIGNATURE, false); mv.goTo(end.getLabel()); }
From source file:com.android.build.gradle.internal.incremental.ConstructorRedirection.java
License:Apache License
@Override protected void doRedirect(GeneratorAdapter mv, int change) { mv.loadLocal(change);// w ww . j a v a2 s .c o m mv.push("init$args." + constructor.args.desc); Type arrayType = Type.getType("[Ljava/lang/Object;"); // init$args args (including this) + locals mv.push(types.size() + 1); mv.newArray(Type.getType(Object.class)); int array = mv.newLocal(arrayType); mv.dup(); mv.storeLocal(array); // "this" is not ready yet, use null instead. mv.dup(); mv.push(0); mv.visitInsn(Opcodes.ACONST_NULL); mv.arrayStore(Type.getType(Object.class)); // Set the arguments in positions 1..(n-1); ByteCodeUtils.loadVariableArray(mv, ByteCodeUtils.toLocalVariables(types), 1); // Skip the this value // Add the locals array at the last position. mv.dup(); // The index of the last position of the array. mv.push(types.size()); // Create the array with all the local variables declared up to this point. ByteCodeUtils.newVariableArray(mv, constructor.variables.subList(0, constructor.localsAtLoadThis)); mv.arrayStore(Type.getType(Object.class)); mv.invokeInterface(IncrementalVisitor.CHANGE_TYPE, Method.getMethod("Object access$dispatch(String, Object[])")); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); //// At this point, init$args has been called and the result Object is on the stack. //// The value of that Object is Object[] with exactly n + 2 elements. //// The first element is the resulting local variables //// The second element is a string with the qualified name of the constructor to call. //// The remaining elements are the constructor arguments. // Keep a reference to the new locals array mv.dup(); mv.push(0); mv.arrayLoad(Type.getType("[Ljava/lang/Object;")); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); mv.storeLocal(array); // Call super constructor // Put this behind the returned array mv.visitVarInsn(Opcodes.ALOAD, 0); mv.swap(); // Push a null for the marker parameter. mv.visitInsn(Opcodes.ACONST_NULL); // Invoke the constructor mv.visitMethodInsn(Opcodes.INVOKESPECIAL, constructor.owner, "<init>", DISPATCHING_THIS_SIGNATURE, false); // Dispatch to init$body mv.loadLocal(change); mv.push("init$body." + constructor.body.desc); mv.loadLocal(array); // Now "this" can be set mv.dup(); mv.push(0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.arrayStore(Type.getType(Object.class)); mv.invokeInterface(IncrementalVisitor.CHANGE_TYPE, Method.getMethod("Object access$dispatch(String, Object[])")); mv.pop(); }
From source file:com.android.build.gradle.internal.incremental.IncrementalChangeVisitor.java
License:Apache License
/** * To each class, add the dispatch method called by the original code that acts as a trampoline to * invoke the changed methods.// www . ja v a 2 s. co m * <p> * Pseudo code: * <code> * Object access$dispatch(String name, object[] args) { * if (name.equals( * "firstMethod.(L$type;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;")) { * return firstMethod(($type)arg[0], (String)arg[1], arg[2]); * } * if (name.equals("secondMethod.(L$type;Ljava/lang/String;I;)V")) { * secondMethod(($type)arg[0], (String)arg[1], (int)arg[2]); * return; * } * ... * StringBuilder $local1 = new StringBuilder(); * $local1.append("Method not found "); * $local1.append(name); * $local1.append(" in " + visitedClassName + * "$dispatch implementation, restart the application"); * throw new $package/InstantReloadException($local1.toString()); * } * </code> */ private void addDispatchMethod() { int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_VARARGS; Method m = new Method("access$dispatch", "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;"); MethodVisitor visitor = super.visitMethod(access, m.getName(), m.getDescriptor(), null, null); final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor); if (TRACING_ENABLED) { mv.push("Redirecting "); mv.loadArg(0); trace(mv, 2); } List<MethodNode> allMethods = new ArrayList<>(); // if we are disabled, do not generate any dispatch, the method will throw an exception // if invoked which should never happen. if (!instantRunDisabled) { //noinspection unchecked allMethods.addAll(classNode.methods); allMethods.addAll(addedMethods); } final Map<String, MethodNode> methods = new HashMap<>(); for (MethodNode methodNode : allMethods) { if (methodNode.name.equals("<clinit>") || methodNode.name.equals("<init>")) { continue; } if (!isAccessCompatibleWithInstantRun(methodNode.access)) { continue; } methods.put(methodNode.name + "." + methodNode.desc, methodNode); } new StringSwitch() { @Override void visitString() { mv.visitVarInsn(Opcodes.ALOAD, 1); } @Override void visitCase(String methodName) { MethodNode methodNode = methods.get(methodName); String name = methodNode.name; boolean isStatic = (methodNode.access & Opcodes.ACC_STATIC) != 0; String newDesc = computeOverrideMethodDesc(methodNode.desc, isStatic); if (TRACING_ENABLED) { trace(mv, "M: " + name + " P:" + newDesc); } Type[] args = Type.getArgumentTypes(newDesc); int argc = 0; for (Type t : args) { mv.visitVarInsn(Opcodes.ALOAD, 2); mv.push(argc); mv.visitInsn(Opcodes.AALOAD); ByteCodeUtils.unbox(mv, t); argc++; } mv.visitMethodInsn(Opcodes.INVOKESTATIC, visitedClassName + "$override", isStatic ? computeOverrideMethodName(name, methodNode.desc) : name, newDesc, false); Type ret = Type.getReturnType(methodNode.desc); if (ret.getSort() == Type.VOID) { mv.visitInsn(Opcodes.ACONST_NULL); } else { mv.box(ret); } mv.visitInsn(Opcodes.ARETURN); } @Override void visitDefault() { writeMissingMessageWithHash(mv, visitedClassName); } }.visit(mv, methods.keySet()); mv.visitMaxs(0, 0); mv.visitEnd(); super.visitEnd(); }