Example usage for org.objectweb.asm Opcodes ILOAD

List of usage examples for org.objectweb.asm Opcodes ILOAD

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ILOAD.

Prototype

int ILOAD

To view the source code for org.objectweb.asm Opcodes ILOAD.

Click Source Link

Usage

From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java

License:Open Source License

/**
 * Generates the instruction to push a local variable on the stack.
 *
 * @param type the type of the local variable to be loaded.
 * @param index an index in the frame's local variables array.
 *//*w  w w .  j  a  v a 2  s  .c o  m*/
private void loadInsn(final Type type, final int index) {
    visitVarInsn(type.getOpcode(Opcodes.ILOAD), index);
}

From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java

License:Open Source License

/**
 * load a value onto the stack from a local var slot which can obtained from a call to newLocal or
 * from a lcoal variable table entry.//from ww  w.  ja v a  2s .  co m
 * @param local the slot to load from
 */

public void loadLocal(int local) {
    Type type = (Type) localTypes.get(local);
    visitVarInsn(type.getOpcode(Opcodes.ILOAD), local);
}

From source file:org.jtsan.LocalVarsSaver.java

License:Apache License

public void loadStack() {
    for (int i = 0; i < types.size(); i++) {
        mv.visitVarInsn(types.get(i).getOpcode(Opcodes.ILOAD), vars.get(i));
    }
}

From source file:org.jtsan.LocalVarsSaver.java

License:Apache License

public void loadReturnValue() {
    if (!hasReturnValue()) {
        throw new RuntimeException("Return value type is not porperly initialized to be loaded.");
    }/*w w w.  j a va2  s.  c om*/
    mv.visitVarInsn(returnType.getOpcode(Opcodes.ILOAD), returnVar);
}

From source file:org.lambdamatic.analyzer.ast.LambdaExpressionReader.java

License:Open Source License

/**
 * Reads the bytecode from the given {@link InsnCursor}'s <strong>current position</strong>, until
 * there is no further instruction to proceed. It is the responsability of the caller to set the
 * cursor position.//www  .  ja v  a  2  s  . c om
 * 
 * @param insnCursor the instruction cursor used to read the bytecode.
 * @param expressionStack the expression stack to put on or pop from.
 * @param localVariables the local variables
 * @return a {@link List} of {@link Statement} containing the {@link Statement}
 */
private List<Statement> readStatements(final InsnCursor insnCursor, final Stack<Expression> expressionStack,
        final List<CapturedArgument> capturedArguments, final LocalVariables localVariables) {
    final List<Statement> statements = new ArrayList<>();
    while (insnCursor.hasCurrent()) {
        final AbstractInsnNode currentInstruction = insnCursor.getCurrent();
        switch (currentInstruction.getType()) {
        case AbstractInsnNode.VAR_INSN:
            final VarInsnNode varInstruction = (VarInsnNode) currentInstruction;
            switch (currentInstruction.getOpcode()) {
            // load a reference onto the stack from a local variable
            case Opcodes.ALOAD:
            case Opcodes.ILOAD:
                // load an int value from a local variable
                // Note: The 'var' operand is the index of a local variable
                // all captured arguments come before the local variable in the method signature,
                // which means that the local variables table is empty on the first slots which are
                // "allocated"
                // for the captured arguments.
                if (varInstruction.var < capturedArguments.size()) {
                    // if the variable index matches a captured argument
                    // note: not using actual captured argument but rather, use a _reference_ to it.
                    final Object capturedArgumentValue = capturedArguments.get(varInstruction.var).getValue();
                    final Class<?> capturedArgumentValueType = capturedArgumentValue != null
                            ? capturedArgumentValue.getClass()
                            : Object.class;
                    final CapturedArgumentRef capturedArgumentRef = new CapturedArgumentRef(varInstruction.var,
                            capturedArgumentValueType);
                    expressionStack.add(capturedArgumentRef);
                } else {
                    // the variable index matches a local variable
                    final LocalVariableNode var = localVariables.load(varInstruction.var);
                    expressionStack.add(new LocalVariable(var.index, var.name, readSignature(var.desc)));
                }
                break;
            case Opcodes.ASTORE:
                // store a reference into a local variable
                localVariables.store(varInstruction.var);
                break;
            default:
                throw new AnalyzeException(
                        "Unexpected Variable instruction code: " + varInstruction.getOpcode());
            }
            break;
        case AbstractInsnNode.LDC_INSN:
            // let's move this instruction on top of the stack until it
            // is used as an argument during a method call
            final LdcInsnNode ldcInsnNode = (LdcInsnNode) currentInstruction;
            final Expression constant = ExpressionFactory.getExpression(ldcInsnNode.cst);
            LOGGER.trace("Stacking constant {}", constant);
            expressionStack.add(constant);
            break;
        case AbstractInsnNode.FIELD_INSN:
            final FieldInsnNode fieldInsnNode = (FieldInsnNode) currentInstruction;
            switch (fieldInsnNode.getOpcode()) {
            case Opcodes.GETSTATIC:
                final Type ownerType = Type.getType(fieldInsnNode.desc);
                final FieldAccess staticFieldAccess = new FieldAccess(new ClassLiteral(getType(ownerType)),
                        fieldInsnNode.name);
                expressionStack.add(staticFieldAccess);
                break;

            case Opcodes.GETFIELD:
                final Expression fieldAccessParent = expressionStack.pop();
                final FieldAccess fieldAccess = new FieldAccess(fieldAccessParent, fieldInsnNode.name);
                expressionStack.add(fieldAccess);
                break;
            case Opcodes.PUTFIELD:
                final Expression fieldAssignationValue = expressionStack.pop();
                final Expression parentSource = expressionStack.pop();
                final FieldAccess source = new FieldAccess(parentSource, fieldInsnNode.name);
                final Assignment assignmentExpression = new Assignment(source, fieldAssignationValue);
                statements.add(new ExpressionStatement(assignmentExpression));
                break;
            default:
                throw new AnalyzeException("Unexpected field instruction type: " + fieldInsnNode.getOpcode());

            }
            break;
        case AbstractInsnNode.METHOD_INSN:
            final MethodInsnNode methodInsnNode = (MethodInsnNode) currentInstruction;
            final Type[] argumentTypes = Type.getArgumentTypes(methodInsnNode.desc);
            final List<Expression> args = new ArrayList<>();
            final List<Class<?>> parameterTypes = new ArrayList<>();
            Stream.of(argumentTypes).forEach(argumentType -> {
                final Expression arg = expressionStack.pop();
                final String argumentClassName = argumentType.getClassName();
                args.add(castOperand(arg, argumentClassName));
                try {
                    parameterTypes.add(ClassUtils.getClass(argumentClassName));
                } catch (Exception e) {
                    throw new AnalyzeException("Failed to find class '" + argumentClassName + "'", e);
                }
            });
            // arguments appear in reverse order in the bytecode
            Collections.reverse(args);
            switch (methodInsnNode.getOpcode()) {
            case Opcodes.INVOKEINTERFACE:
            case Opcodes.INVOKEVIRTUAL:
            case Opcodes.INVOKESPECIAL:
                // object instantiation
                if (methodInsnNode.name.equals("<init>")) {
                    final ObjectInstanciation objectVariable = (ObjectInstanciation) expressionStack.pop();
                    objectVariable.setInitArguments(args);
                } else {
                    final Expression sourceExpression = expressionStack.pop();
                    final Method javaMethod = ReflectionUtils.findJavaMethod(sourceExpression.getJavaType(),
                            methodInsnNode.name, parameterTypes);
                    final Class<?> returnType = findReturnType(insnCursor, javaMethod);
                    final MethodInvocation invokedMethod = new MethodInvocation(sourceExpression, javaMethod,
                            returnType, args);
                    expressionStack.add(invokedMethod);
                }
                break;
            case Opcodes.INVOKESTATIC:
                final Type type = Type.getObjectType(methodInsnNode.owner);
                try {
                    final Class<?> sourceClass = Class.forName(type.getClassName());
                    final Method javaMethod = ReflectionUtils.findJavaMethod(sourceClass, methodInsnNode.name,
                            parameterTypes);
                    final Class<?> returnType = findReturnType(insnCursor, javaMethod);
                    final MethodInvocation invokedStaticMethod = new MethodInvocation(
                            new ClassLiteral(sourceClass), javaMethod, returnType, args);
                    expressionStack.add(invokedStaticMethod);
                } catch (ClassNotFoundException e) {
                    throw new AnalyzeException("Failed to retrieve class for " + methodInsnNode.owner, e);
                }
                break;
            default:
                throw new AnalyzeException("Unexpected method invocation type: " + methodInsnNode.getOpcode());
            }
            break;
        case AbstractInsnNode.INVOKE_DYNAMIC_INSN:
            final InvokeDynamicInsnNode invokeDynamicInsnNode = (InvokeDynamicInsnNode) currentInstruction;
            final Handle handle = (Handle) invokeDynamicInsnNode.bsmArgs[1];
            final int argNumber = Type.getArgumentTypes(invokeDynamicInsnNode.desc).length;
            final List<CapturedArgumentRef> lambdaArgs = new ArrayList<>();
            for (int i = 0; i < argNumber; i++) {
                final Expression expr = expressionStack.pop();
                if (expr.getExpressionType() != ExpressionType.CAPTURED_ARGUMENT_REF) {
                    throw new AnalyzeException("Unexpected argument type when following InvokeDynamic call: "
                            + expr.getExpressionType());
                }
                lambdaArgs.add((CapturedArgumentRef) expr); // , expr.getValue()
            }
            Collections.reverse(lambdaArgs);
            final EmbeddedSerializedLambdaInfo lambdaInfo = new EmbeddedSerializedLambdaInfo(handle.getOwner(),
                    handle.getName(), handle.getDesc(), lambdaArgs, capturedArguments);
            final LambdaExpression lambdaExpression = LambdaExpressionAnalyzer.getInstance()
                    .analyzeExpression(lambdaInfo);
            expressionStack.add(lambdaExpression);
            break;
        case AbstractInsnNode.JUMP_INSN:
            statements.addAll(
                    readJumpInstruction(insnCursor, expressionStack, capturedArguments, localVariables));
            return statements;
        case AbstractInsnNode.INT_INSN:
            readIntInstruction((IntInsnNode) currentInstruction, expressionStack, localVariables);
            break;
        case AbstractInsnNode.INSN:
            final List<Statement> instructionStatement = readInstruction(insnCursor, expressionStack,
                    capturedArguments, localVariables);
            statements.addAll(instructionStatement);
            break;
        case AbstractInsnNode.TYPE_INSN:
            readTypeInstruction((TypeInsnNode) currentInstruction, expressionStack, localVariables);
            break;
        default:
            throw new AnalyzeException(
                    "This is embarrassing... We've reached an unexpected instruction operator: "
                            + currentInstruction.getType());
        }
        insnCursor.next();
    }
    return statements;
}

From source file:org.mbte.groovypp.compiler.bytecode.BytecodeExpr.java

License:Apache License

public static void load(ClassNode type, int idx, MethodVisitor mv) {
    if (type == double_TYPE) {
        mv.visitVarInsn(Opcodes.DLOAD, idx);
    } else if (type == float_TYPE) {
        mv.visitVarInsn(Opcodes.FLOAD, idx);
    } else if (type == long_TYPE) {
        mv.visitVarInsn(Opcodes.LLOAD, idx);
    } else if (type == boolean_TYPE || type == char_TYPE || type == byte_TYPE || type == int_TYPE
            || type == short_TYPE) {
        mv.visitVarInsn(Opcodes.ILOAD, idx);
    } else {//from w ww .j  av  a2s  .  c o m
        mv.visitVarInsn(Opcodes.ALOAD, idx);
    }
}

From source file:org.mbte.groovypp.compiler.ClosureUtil.java

License:Apache License

private static void makeOneMethodClass(List<MethodNode> abstractMethods, final ClassNode closureType,
        ClassNode baseType, CompilerTransformer compiler, final MethodNode doCall) {
    boolean traitMethods = false;
    int k = 0;/*from  w ww .  j  a v a 2  s  .c o  m*/
    for (final MethodNode missed : abstractMethods) {
        final Parameter[] parameters = eraseParameterTypes(missed.getParameters());
        if (k == 0) {
            closureType.addMethod(missed.getName(), Opcodes.ACC_PUBLIC,
                    getSubstitutedReturnType(doCall, missed, closureType, baseType), parameters,
                    ClassNode.EMPTY_ARRAY, new BytecodeSequence(new BytecodeInstruction() {
                        public void visit(MethodVisitor mv) {
                            mv.visitVarInsn(Opcodes.ALOAD, 0);
                            Parameter pp[] = parameters;
                            for (int i = 0, k = 1; i != pp.length; ++i) {
                                final ClassNode type = pp[i].getType();
                                ClassNode expectedType = doCall.getParameters()[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++);
                                    }
                                    BytecodeExpr.box(type, mv);
                                    BytecodeExpr.cast(TypeUtil.wrapSafely(type),
                                            TypeUtil.wrapSafely(expectedType), mv);
                                } else {
                                    mv.visitVarInsn(Opcodes.ALOAD, k++);
                                    BytecodeExpr.checkCast(TypeUtil.wrapSafely(expectedType), mv);
                                }
                                BytecodeExpr.unbox(expectedType, mv);
                            }
                            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                                    BytecodeHelper.getClassInternalName(doCall.getDeclaringClass()),
                                    doCall.getName(), BytecodeHelper.getMethodDescriptor(doCall.getReturnType(),
                                            doCall.getParameters()));

                            if (missed.getReturnType() != ClassHelper.VOID_TYPE
                                    && !missed.getReturnType().equals(doCall.getReturnType())) {
                                BytecodeExpr.box(doCall.getReturnType(), mv);
                                BytecodeExpr.checkCast(TypeUtil.wrapSafely(doCall.getReturnType()), mv);
                                BytecodeExpr.unbox(missed.getReturnType(), mv);
                            }
                            BytecodeExpr.doReturn(mv, missed.getReturnType());
                        }
                    }));
        } else {
            if (traitMethod(missed)) {
                traitMethods = true;
            } else if (likeGetter(missed)) {
                String pname = missed.getName().substring(3);
                pname = Character.toLowerCase(pname.charAt(0)) + pname.substring(1);
                final PropertyNode propertyNode = closureType.addProperty(pname, Opcodes.ACC_PUBLIC,
                        missed.getReturnType(), null, null, null);
                propertyNode.getField().addAnnotation(new AnnotationNode(TypeUtil.NO_EXTERNAL_INITIALIZATION));
            } else {
                if (likeSetter(missed)) {
                    String pname = missed.getName().substring(3);
                    pname = Character.toLowerCase(pname.charAt(0)) + pname.substring(1);
                    final PropertyNode propertyNode = closureType.addProperty(pname, Opcodes.ACC_PUBLIC,
                            parameters[0].getType(), null, null, null);
                    propertyNode.getField()
                            .addAnnotation(new AnnotationNode(TypeUtil.NO_EXTERNAL_INITIALIZATION));
                }
            }
        }
        k++;
    }

    if (traitMethods)
        TraitASTTransformFinal.improveAbstractMethods(closureType);
}

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  v  a2s.  c  o  m
            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.mbte.groovypp.compiler.TraitASTTransformFinal.java

License:Apache License

private static void createGetterSetter(final ClassNode classNode, MethodNode method, final ClassNode fieldType,
        Parameter[] parameters, final boolean getter, final String fieldName) {
    classNode.addMethod(method.getName(), ACC_PUBLIC, getter ? fieldType : ClassHelper.VOID_TYPE, parameters,
            ClassNode.EMPTY_ARRAY, new BytecodeSequence(new BytecodeInstruction() {
                public void visit(MethodVisitor mv) {
                    if (getter) {
                        mv.visitVarInsn(ALOAD, 0);
                        mv.visitFieldInsn(GETFIELD, BytecodeHelper.getClassInternalName(classNode), fieldName,
                                BytecodeHelper.getTypeDescription(fieldType));
                        BytecodeExpr.doReturn(mv, fieldType);
                    } else {
                        mv.visitVarInsn(ALOAD, 0);
                        if (fieldType == double_TYPE) {
                            mv.visitVarInsn(Opcodes.DLOAD, 1);
                        } else if (fieldType == float_TYPE) {
                            mv.visitVarInsn(Opcodes.FLOAD, 1);
                        } else if (fieldType == long_TYPE) {
                            mv.visitVarInsn(Opcodes.LLOAD, 1);
                        } else if (fieldType == boolean_TYPE || fieldType == char_TYPE || fieldType == byte_TYPE
                                || fieldType == int_TYPE || fieldType == short_TYPE) {
                            mv.visitVarInsn(Opcodes.ILOAD, 1);
                        } else {
                            mv.visitVarInsn(Opcodes.ALOAD, 1);
                        }/*from ww w.  j  av  a  2 s .  c  o m*/
                        mv.visitFieldInsn(PUTFIELD, BytecodeHelper.getClassInternalName(classNode), fieldName,
                                BytecodeHelper.getTypeDescription(fieldType));
                        mv.visitInsn(RETURN);
                    }
                }
            }));
}

From source file:org.mutabilitydetector.checkers.settermethod.EffectiveAssignmentInsnVerifier.java

License:Apache License

private static boolean isAliasLoadInstruction(final AbstractInsnNode insn, final int aliasLocalVariable) {
    switch (insn.getOpcode()) {
    case Opcodes.ILOAD:
    case Opcodes.LLOAD:
    case Opcodes.FLOAD:
    case Opcodes.DLOAD:
    case Opcodes.ALOAD:
        final VarInsnNode varInsnNode = (VarInsnNode) insn;
        return aliasLocalVariable == varInsnNode.var;
    default:/*w w  w .j av a 2 s  .  c  o  m*/
        return false;
    }
}