List of usage examples for org.objectweb.asm Opcodes RETURN
int RETURN
To view the source code for org.objectweb.asm Opcodes RETURN.
Click Source Link
From source file:org.lambdamatic.analyzer.ast.LambdaExpressionReader.java
License:Open Source License
/** * Reads the current {@link InsnNode} instruction and returns a {@link Statement} or {@code null} * if the instruction is not a full statement (in that case, the instruction is stored in the * given Expression {@link Stack}).//ww w .j a v a2 s . c o m * * @param insnNode the instruction to read * @param expressionStack the expression stack to put on or pop from. * @param localVariables the local variables * @return a {@link List} of {@link Statement} or empty list if no {@link Statement} was created * after reading the current instruction. * @see <a href="https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings">Java bytcode * instruction listings on Wikipedia</a> */ private List<Statement> readInstruction(final InsnCursor insnCursor, final Stack<Expression> expressionStack, final List<CapturedArgument> capturedArguments, final LocalVariables localVariables) { final List<Statement> statements = new ArrayList<>(); final AbstractInsnNode insnNode = insnCursor.getCurrent(); switch (insnNode.getOpcode()) { // return a reference from a method case Opcodes.ARETURN: // return an integer from a method case Opcodes.IRETURN: statements.add(new ReturnStatement(expressionStack.pop())); break; // return void from method case Opcodes.RETURN: // wrap all pending expressions into ExpressionStatements while (!expressionStack.isEmpty()) { final Expression pendingExpression = expressionStack.pop(); statements.add(new ExpressionStatement(pendingExpression)); } break; // push a null reference onto the stack case Opcodes.ACONST_NULL: expressionStack.add(new NullLiteral()); break; // load the int value 0 onto the stack case Opcodes.ICONST_0: // applies for byte, short, int and boolean expressionStack.add(new NumberLiteral(0)); break; // load the int value 1 onto the stack case Opcodes.ICONST_1: // applies for byte, short, int and boolean expressionStack.add(new NumberLiteral(1)); break; // load the int value 2 onto the stack case Opcodes.ICONST_2: expressionStack.add(new NumberLiteral(2)); break; // load the int value 3 onto the stack case Opcodes.ICONST_3: expressionStack.add(new NumberLiteral(3)); break; // load the int value 4 onto the stack case Opcodes.ICONST_4: expressionStack.add(new NumberLiteral(4)); break; // load the int value 5 onto the stack case Opcodes.ICONST_5: expressionStack.add(new NumberLiteral(5)); break; // push the long 0 onto the stack case Opcodes.LCONST_0: expressionStack.add(new NumberLiteral(0L)); break; // push the long 1 onto the stack case Opcodes.LCONST_1: expressionStack.add(new NumberLiteral(1L)); break; // push the 0.0f onto the stack case Opcodes.FCONST_0: expressionStack.add(new NumberLiteral(0f)); break; // push the 1.0f onto the stack case Opcodes.FCONST_1: expressionStack.add(new NumberLiteral(1f)); break; // push the 2.0f onto the stack case Opcodes.FCONST_2: expressionStack.add(new NumberLiteral(2f)); break; // push the constant 0.0 onto the stack case Opcodes.DCONST_0: expressionStack.add(new NumberLiteral(0d)); break; // push the constant 1.0 onto the stack case Opcodes.DCONST_1: expressionStack.add(new NumberLiteral(1d)); break; // compare two longs values case Opcodes.LCMP: // compare two doubles case Opcodes.DCMPL: // compare two doubles case Opcodes.DCMPG: // compare two floats case Opcodes.FCMPL: // compare two floats case Opcodes.FCMPG: statements.addAll( readJumpInstruction(insnCursor.next(), expressionStack, capturedArguments, localVariables)); break; // add 2 ints case Opcodes.IADD: expressionStack.add(readOperation(Operator.ADD, expressionStack)); break; // int subtract case Opcodes.ISUB: expressionStack.add(readOperation(Operator.SUBTRACT, expressionStack)); break; // multiply 2 integers case Opcodes.IMUL: expressionStack.add(readOperation(Operator.MULTIPLY, expressionStack)); break; // divide 2 integers case Opcodes.IDIV: expressionStack.add(readOperation(Operator.DIVIDE, expressionStack)); break; // negate int case Opcodes.INEG: expressionStack.add(inverseInteger(expressionStack)); break; // discard the top value on the stack case Opcodes.POP: statements.add(new ExpressionStatement(expressionStack.pop())); break; // duplicate the value on top of the stack case Opcodes.DUP: expressionStack.push(expressionStack.peek()); break; // insert a copy of the top value into the stack two values from the top. case Opcodes.DUP_X1: expressionStack.add(expressionStack.size() - 2, expressionStack.peek()); break; // store into a reference in an array case Opcodes.AASTORE: readArrayStoreInstruction(insnNode, expressionStack); break; // converts Float to Double -> ignored. case Opcodes.F2D: break; default: throw new AnalyzeException( "Bytecode instruction with OpCode '" + insnNode.getOpcode() + "' is not supported."); } return statements; }
From source file:org.mbte.groovypp.compiler.bytecode.BytecodeExpr.java
License:Apache License
public static void doReturn(MethodVisitor mv, ClassNode returnType) { if (returnType == double_TYPE) { mv.visitInsn(Opcodes.DRETURN);//from ww w . java 2s . c o m } else if (returnType == float_TYPE) { mv.visitInsn(Opcodes.FRETURN); } else if (returnType == long_TYPE) { mv.visitInsn(Opcodes.LRETURN); } else if (returnType == boolean_TYPE || returnType == char_TYPE || returnType == byte_TYPE || returnType == int_TYPE || returnType == short_TYPE) { //byte,short,boolean,int are all IRETURN mv.visitInsn(Opcodes.IRETURN); } else if (returnType == VOID_TYPE) { mv.visitInsn(Opcodes.RETURN); } else { mv.visitInsn(Opcodes.ARETURN); } }
From source file:org.mbte.groovypp.compiler.ClosureUtil.java
License:Apache License
public static void createClosureConstructor(final ClassNode newType, final Parameter[] constrParams, Expression superArgs, CompilerTransformer compiler) { final ClassNode superClass = newType.getSuperClass(); final Parameter[] finalConstrParams; final ArgumentListExpression superCallArgs = new ArgumentListExpression(); if (superArgs != null) { final ArgumentListExpression args = (ArgumentListExpression) superArgs; if (args.getExpressions().size() > 0) { Parameter[] newParams = new Parameter[constrParams.length + args.getExpressions().size()]; System.arraycopy(constrParams, 0, newParams, 0, constrParams.length); for (int i = 0; i != args.getExpressions().size(); ++i) { final Parameter parameter = new Parameter(args.getExpressions().get(i).getType(), "$super$param$" + i); newParams[i + constrParams.length] = parameter; superCallArgs.addExpression(new VariableExpression(parameter)); }//from w w w.j a va2 s . com finalConstrParams = newParams; } else finalConstrParams = constrParams; } else { if (superClass == ClassHelper.CLOSURE_TYPE) { if (constrParams.length > 0) { superCallArgs.addExpression(new VariableExpression(constrParams[0])); superCallArgs.addExpression(new VariableExpression(constrParams[0])); } else if (compiler.methodNode.isStatic() && !compiler.classNode.getName().endsWith("$TraitImpl")) { ClassNode cn = compiler.classNode; superCallArgs.addExpression(new ClassExpression(cn)); superCallArgs.addExpression(new ClassExpression(getOutermostClass(cn))); } else { superCallArgs.addExpression(ConstantExpression.NULL); superCallArgs.addExpression(ConstantExpression.NULL); } } finalConstrParams = constrParams; } ConstructorCallExpression superCall = new ConstructorCallExpression(ClassNode.SUPER, superCallArgs); BytecodeSequence fieldInit = new BytecodeSequence(new BytecodeInstruction() { public void visit(MethodVisitor mv) { for (int i = 0, k = 1; i != constrParams.length; i++) { mv.visitVarInsn(Opcodes.ALOAD, 0); final ClassNode type = constrParams[i].getType(); if (ClassHelper.isPrimitiveType(type)) { if (type == ClassHelper.long_TYPE) { mv.visitVarInsn(Opcodes.LLOAD, k++); k++; } else if (type == ClassHelper.double_TYPE) { mv.visitVarInsn(Opcodes.DLOAD, k++); k++; } else if (type == ClassHelper.float_TYPE) { mv.visitVarInsn(Opcodes.FLOAD, k++); } else { mv.visitVarInsn(Opcodes.ILOAD, k++); } } else { mv.visitVarInsn(Opcodes.ALOAD, k++); } mv.visitFieldInsn(Opcodes.PUTFIELD, BytecodeHelper.getClassInternalName(newType), constrParams[i].getName(), BytecodeHelper.getTypeDescription(type)); } mv.visitInsn(Opcodes.RETURN); } }); BlockStatement code = new BlockStatement(); code.addStatement(new ExpressionStatement(superCall)); ConstructorNode cn = new ConstructorNode(Opcodes.ACC_PUBLIC, finalConstrParams, ClassNode.EMPTY_ARRAY, code); newType.addConstructor(cn); code.addStatement(fieldInit); CleaningVerifier.getCleaningVerifier().visitClass(newType); compiler.replaceMethodCode(newType, cn); if (newType.getOuterClass() != null && newType.getMethods("methodMissing").isEmpty()) { final ClassNode this0Type = (!compiler.methodNode.isStatic() || compiler.classNode.getName().endsWith("$TraitImpl")) ? newType.getOuterClass() : ClassHelper.CLASS_Type; newType.addMethod("methodMissing", Opcodes.ACC_PUBLIC, ClassHelper.OBJECT_TYPE, new Parameter[] { new Parameter(ClassHelper.STRING_TYPE, "name"), new Parameter(ClassHelper.OBJECT_TYPE, "args") }, ClassNode.EMPTY_ARRAY, new BytecodeSequence(new BytecodeInstruction() { public void visit(MethodVisitor mv) { mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, BytecodeHelper.getClassInternalName(newType), "this$0", BytecodeHelper.getTypeDescription(this0Type)); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitVarInsn(Opcodes.ALOAD, 2); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/InvokerHelper", "invokeMethod", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(Opcodes.ARETURN); } })); } }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
/** * Add the <init> method for the given constructor. This also includes initializing the non-static fields that * have initializers./*from w ww. ja va2 s . co m*/ * @param classRep * @param javaConstructor * @param cv * @throws JavaGenerationException */ private static void encodeConstructor(JavaClassRep classRep, JavaConstructor javaConstructor, ClassVisitor cv) throws JavaGenerationException { // gather info on the thrown exceptions final int nThrownExceptions = javaConstructor.getNThrownExceptions(); String[] thrownExceptions = new String[nThrownExceptions]; for (int i = 0; i < nThrownExceptions; ++i) { thrownExceptions[i] = javaConstructor.getThrownException(i).getJVMInternalName(); } MethodVisitor mv = cv.visitMethod(javaConstructor.getModifiers(), "<init>", javaConstructor.getJVMMethodDescriptor(), null, thrownExceptions); final Map<String, JavaTypeName> methodVarToTypeMap = new HashMap<String, JavaTypeName>(); final Map<String, Integer> methodVarToIndexMap = new HashMap<String, Integer>(); methodVarToTypeMap.put("this", classRep.getClassName()); methodVarToIndexMap.put("this", Integer.valueOf(0)); int indexOfNextSlot = 1; for (int i = 0, nParams = javaConstructor.getNParams(); i < nParams; ++i) { JavaTypeName varType = javaConstructor.getParamType(i); final int typeSize = getTypeSize(varType); String varName = javaConstructor.getParamName(i); methodVarToTypeMap.put(varName, varType); methodVarToIndexMap.put(varName, Integer.valueOf(indexOfNextSlot)); indexOfNextSlot += typeSize; } // Start the method's code. mv.visitCode(); // Invoke the superclass initializer. if (javaConstructor.getSuperConstructorParamTypes().length == 0) { //push the "this" reference on the stack mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, classRep.getSuperclassName().getJVMInternalName(), "<init>", "()V"); } else { GenerationContext context = new GenerationContext(methodVarToTypeMap, methodVarToIndexMap, indexOfNextSlot, mv, classRep.getClassName()); MethodInvocation mi = new MethodInvocation.Instance(null, "<init>", classRep.getSuperclassName(), javaConstructor.getSuperConstructorParamValues(), javaConstructor.getSuperConstructorParamTypes(), JavaTypeName.VOID, MethodInvocation.InvocationType.SPECIAL); encodeMethodInvocationExpr(mi, context); } // Add initializers for the non-statically-initialized fields. final int nFields = classRep.getNFieldDeclarations(); for (int i = 0; i < nFields; ++i) { JavaFieldDeclaration fieldDecl = classRep.getFieldDeclaration(i); JavaExpression initializer = fieldDecl.getInitializer(); if (initializer != null && !Modifier.isStatic(fieldDecl.getModifiers())) { GenerationContext context = new GenerationContext(methodVarToTypeMap, methodVarToIndexMap, indexOfNextSlot, mv, classRep.getClassName()); //push the "this" reference mv.visitVarInsn(Opcodes.ALOAD, 0); //evaluate the initializer encodeExpr(initializer, context); //do the assignment mv.visitFieldInsn(Opcodes.PUTFIELD, classRep.getClassName().getJVMInternalName(), fieldDecl.getFieldName(), fieldDecl.getFieldType().getJVMDescriptor()); } } // Add the body code for the constructor GenerationContext context = new GenerationContext(methodVarToTypeMap, methodVarToIndexMap, indexOfNextSlot, mv, classRep.getClassName()); boolean isTerminating = encodeStatement(javaConstructor.getBodyCode(), context); // Add any implicit "return" statement. if (!isTerminating) { mv.visitInsn(Opcodes.RETURN); } //mark the end of encoding the constructor mv.visitMaxs(0, 0); // End the method. mv.visitEnd(); }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
/** * Add the <clinit> method. This initializes the static fields that have initializers. * @param classRep//from www . ja va 2 s .com * @param cv * @param initializeForAsserts * @throws JavaGenerationException */ private static void encodeClassInitializer(JavaClassRep classRep, ClassVisitor cv, boolean initializeForAsserts) throws JavaGenerationException { // Add initializers for the statically-initialized fields. final int nFields = classRep.getNFieldDeclarations(); if (!classRep.hasInitializedStaticField() && !initializeForAsserts) { //we don't need to bother adding a static initializer if there are no static fields to initialize. //note that javac also has this optimization. return; } MethodVisitor mv = cv.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null); // Start the method's code. mv.visitCode(); final JavaTypeName classRepTypeName = classRep.getClassName(); /* * If this class contains assert statements we need to initialize the static final synthetic boolean field * '$assertionsDisabled'. * This is done by loading the Class constant for the top level class. The method 'desiredAssertionStatus()' * is then invoked on this Class object and the returned value is used to initialize $assertionsDisabled. */ if (initializeForAsserts) { // Get the fully-qualified internal class name. final String className = classRepTypeName.getJVMInternalName(); // Get the top level class name. String topLevelClassName = getTopLevelClassJVMInternalName(className); // Load the Class constant for the top level class mv.visitLdcInsn(Type.getType("L" + topLevelClassName + ";")); // Now that we have the Class constant for the top level class we invoke the method // desiredAssertionStatus on it and use the resulting value to initialize the static field $assertionsDisabled in this class. mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "desiredAssertionStatus", "()Z"); Label l0 = new Label(); mv.visitJumpInsn(Opcodes.IFNE, l0); mv.visitInsn(Opcodes.ICONST_1); Label l1 = new Label(); mv.visitJumpInsn(Opcodes.GOTO, l1); mv.visitLabel(l0); mv.visitInsn(Opcodes.ICONST_0); mv.visitLabel(l1); mv.visitFieldInsn(Opcodes.PUTSTATIC, className, "$assertionsDisabled", "Z"); } for (int i = 0; i < nFields; ++i) { JavaFieldDeclaration fieldDecl = classRep.getFieldDeclaration(i); JavaExpression initializer = fieldDecl.getInitializer(); if (initializer != null && Modifier.isStatic(fieldDecl.getModifiers())) { GenerationContext context = new GenerationContext(new HashMap<String, JavaTypeName>(), new HashMap<String, Integer>(), 0, mv, classRepTypeName); //evaluate the initializer encodeExpr(initializer, context); //do the assignment mv.visitFieldInsn(Opcodes.PUTSTATIC, classRep.getClassName().getJVMInternalName(), fieldDecl.getFieldName(), fieldDecl.getFieldType().getJVMDescriptor()); } } // return. mv.visitInsn(Opcodes.RETURN); //mark the end of the method with a call to visitMaxs mv.visitMaxs(0, 0); // End the method. mv.visitEnd(); }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
/** * Encode java byte code for the given method. * @param classRep model for the class in which the method is defined. * @param javaMethod// w w w.j a v a 2s . c om * @param cv * @throws JavaGenerationException */ private static void encodeMethod(JavaClassRep classRep, JavaMethod javaMethod, ClassVisitor cv) throws JavaGenerationException { // gather info on the thrown exceptions final int nThrownExceptions = javaMethod.getNThrownExceptions(); String[] thrownExceptions = new String[nThrownExceptions]; for (int i = 0; i < nThrownExceptions; ++i) { thrownExceptions[i] = javaMethod.getThrownException(i).getJVMInternalName(); } //visit the method itself MethodVisitor mv = cv.visitMethod(javaMethod.getModifiers(), javaMethod.getMethodName(), javaMethod.getJVMMethodDescriptor(), null, thrownExceptions); // Add the code. mv.visitCode(); final Map<String, JavaTypeName> methodVarToTypeMap = new HashMap<String, JavaTypeName>(); final Map<String, Integer> methodVarToIndexMap = new HashMap<String, Integer>(); int indexOfNextSlot = 0; if (!Modifier.isStatic(javaMethod.getModifiers())) { methodVarToTypeMap.put("this", classRep.getClassName()); methodVarToIndexMap.put("this", Integer.valueOf(0)); indexOfNextSlot = 1; } for (int i = 0, nParams = javaMethod.getNParams(); i < nParams; ++i) { JavaTypeName varType = javaMethod.getParamType(i); final int typeSize = getTypeSize(varType); String varName = javaMethod.getParamName(i); methodVarToTypeMap.put(varName, varType); methodVarToIndexMap.put(varName, Integer.valueOf(indexOfNextSlot)); indexOfNextSlot += typeSize; } GenerationContext context = new GenerationContext(methodVarToTypeMap, methodVarToIndexMap, indexOfNextSlot, mv, classRep.getClassName()); boolean isTerminating = encodeStatement(javaMethod.getBodyCode(), context); // Add any implicit "return" statement. if (!isTerminating && javaMethod.getReturnType().equals(JavaTypeName.VOID)) { context.getMethodVisitor().visitInsn(Opcodes.RETURN); } // Apply peep hole optimizations. //todoBI no peephole optimizations with ASM... //PeepHoleOptimizer.peepHoleOptimize(il); //The ClassWriter was constructed with a parameter to automatically compute max stack and max locals. //However, we must still call visitMaxs to mark off the end of the method. The arguments are ignored. mv.visitMaxs(0, 0); // End the method. mv.visitEnd(); }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
private static boolean encodeReturnStatement(JavaStatement.ReturnStatement returnStatement, GenerationContext context) throws JavaGenerationException { MethodVisitor mv = context.getMethodVisitor(); JavaExpression returnExpression = returnStatement.getReturnExpression(); // Label the return instruction and the instruction after the return. // This instruction will be excluded from relevant ranges covered in the exception table. int returnOpCode; if (returnExpression == null) { returnOpCode = Opcodes.RETURN; } else {/*from w w w .j ava 2 s . c o m*/ // encode the result of evaluating the return expression. JavaTypeName returnType = encodeExpr(returnExpression, context); returnOpCode = getReturnOpCode(returnType); } // Add a label for the return instruction. Label returnLabel = new Label(); mv.visitLabel(returnLabel); // Visit the return instruction. mv.visitInsn(returnOpCode); // Add a label following the return instruction. Label afterReturnLabel = new Label(); mv.visitLabel(afterReturnLabel); // Add info about the generated label. context.addJumpReturnLabelInfo(new JumpReturnLabelInfo(returnLabel, afterReturnLabel, null)); // return statements are terminating. return true; }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
/** * @param type/*from ww w . j av a 2 s . c o m*/ * @return the RETURN, IRETURN,... op code, depending on type. */ private static int getReturnOpCode(JavaTypeName type) { switch (type.getTag()) { case JavaTypeName.VOID_TAG: return Opcodes.RETURN; case JavaTypeName.BOOLEAN_TAG: case JavaTypeName.BYTE_TAG: case JavaTypeName.SHORT_TAG: case JavaTypeName.CHAR_TAG: case JavaTypeName.INT_TAG: return Opcodes.IRETURN; case JavaTypeName.LONG_TAG: return Opcodes.LRETURN; case JavaTypeName.DOUBLE_TAG: return Opcodes.DRETURN; case JavaTypeName.FLOAT_TAG: return Opcodes.FRETURN; case JavaTypeName.ARRAY_TAG: case JavaTypeName.OBJECT_TAG: return Opcodes.ARETURN; default: { throw new IllegalArgumentException("invalid type: " + type); } } }
From source file:org.pitest.mutationtest.UnviableClassMutator.java
License:Apache License
@Override protected Map<Integer, ZeroOperandMutation> getMutations() { final Map<Integer, ZeroOperandMutation> map = new HashMap<Integer, ZeroOperandMutation>(); // map.put(Opcodes.ALOAD, new InsnSubstitution(Opcodes.TABLESWITCH, // "Made unviable class")); map.put(Opcodes.IRETURN, new InsnSubstitution(Opcodes.FCMPG, "Made unviable class")); map.put(Opcodes.RETURN, new InsnSubstitution(Opcodes.FCMPG, "Made unviable class")); return map;/* w ww . ja va2s .com*/ }
From source file:org.sonar.java.bytecode.asm.AsmMethodVisitor.java
License:Open Source License
public void visitInsn(int opcode) { if (opcode != Opcodes.RETURN) { emptyMethod = false; } }