Search in sources :

Example 11 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class StaticTypesUnaryExpressionHelper method writeBitwiseNegate.

@Override
public void writeBitwiseNegate(final BitwiseNegationExpression expression) {
    expression.getExpression().visit(controller.getAcg());
    if (isPrimitiveOnTop()) {
        final ClassNode top = getTopOperand();
        if (top == int_TYPE || top == short_TYPE || top == byte_TYPE || top == char_TYPE || top == long_TYPE) {
            BytecodeExpression bytecodeExpression = new BytecodeExpression() {

                @Override
                public void visit(final MethodVisitor mv) {
                    if (long_TYPE == top) {
                        mv.visitLdcInsn(-1);
                        mv.visitInsn(LXOR);
                    } else {
                        mv.visitInsn(ICONST_M1);
                        mv.visitInsn(IXOR);
                        if (byte_TYPE == top) {
                            mv.visitInsn(I2B);
                        } else if (char_TYPE == top) {
                            mv.visitInsn(I2C);
                        } else if (short_TYPE == top) {
                            mv.visitInsn(I2S);
                        }
                    }
                }
            };
            bytecodeExpression.visit(controller.getAcg());
            controller.getOperandStack().remove(1);
            return;
        }
    }
    super.writeBitwiseNegate(EMPTY_BITWISE_NEGATE);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 12 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class StaticTypesUnaryExpressionHelper method writeUnaryMinus.

@Override
public void writeUnaryMinus(final UnaryMinusExpression expression) {
    expression.getExpression().visit(controller.getAcg());
    if (isPrimitiveOnTop()) {
        final ClassNode top = getTopOperand();
        if (top != boolean_TYPE) {
            BytecodeExpression bytecodeExpression = new BytecodeExpression() {

                @Override
                public void visit(final MethodVisitor mv) {
                    if (int_TYPE == top || short_TYPE == top || byte_TYPE == top || char_TYPE == top) {
                        mv.visitInsn(INEG);
                        if (byte_TYPE == top) {
                            mv.visitInsn(I2B);
                        } else if (char_TYPE == top) {
                            mv.visitInsn(I2C);
                        } else if (short_TYPE == top) {
                            mv.visitInsn(I2S);
                        }
                    } else if (long_TYPE == top) {
                        mv.visitInsn(LNEG);
                    } else if (float_TYPE == top) {
                        mv.visitInsn(FNEG);
                    } else if (double_TYPE == top) {
                        mv.visitInsn(DNEG);
                    }
                }
            };
            bytecodeExpression.visit(controller.getAcg());
            controller.getOperandStack().remove(1);
            return;
        }
    }
    // we already visited the sub expression
    super.writeUnaryMinus(EMPTY_UNARY_MINUS);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 13 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class MopWriter method generateMopCalls.

/**
     * generates a Meta Object Protocol method, that is used to call a non public
     * method, or to make a call to super.
     *
     * @param mopCalls list of methods a mop call method should be generated for
     * @param useThis  true if "this" should be used for the naming
     */
protected void generateMopCalls(LinkedList<MethodNode> mopCalls, boolean useThis) {
    for (MethodNode method : mopCalls) {
        String name = getMopMethodName(method, useThis);
        Parameter[] parameters = method.getParameters();
        String methodDescriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(), method.getParameters());
        MethodVisitor mv = controller.getClassVisitor().visitMethod(ACC_PUBLIC | ACC_SYNTHETIC, name, methodDescriptor, null, null);
        controller.setMethodVisitor(mv);
        mv.visitVarInsn(ALOAD, 0);
        int newRegister = 1;
        OperandStack operandStack = controller.getOperandStack();
        for (Parameter parameter : parameters) {
            ClassNode type = parameter.getType();
            operandStack.load(parameter.getType(), newRegister);
            // increment to next register, double/long are using two places
            newRegister++;
            if (type == ClassHelper.double_TYPE || type == ClassHelper.long_TYPE)
                newRegister++;
        }
        operandStack.remove(parameters.length);
        ClassNode declaringClass = method.getDeclaringClass();
        // JDK 8 support for default methods in interfaces
        // this should probably be strenghtened when we support the A.super.foo() syntax
        int opcode = declaringClass.isInterface() ? INVOKEINTERFACE : INVOKESPECIAL;
        mv.visitMethodInsn(opcode, BytecodeHelper.getClassInternalName(declaringClass), method.getName(), methodDescriptor, opcode == INVOKEINTERFACE);
        BytecodeHelper.doReturn(mv, method.getReturnType());
        mv.visitMaxs(0, 0);
        mv.visitEnd();
        controller.getClassNode().addMethod(name, ACC_PUBLIC | ACC_SYNTHETIC, method.getReturnType(), parameters, null, null);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) Parameter(org.codehaus.groovy.ast.Parameter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 14 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class OperandStack method popDownTo.

public void popDownTo(int elements) {
    int last = stack.size();
    MethodVisitor mv = controller.getMethodVisitor();
    while (last > elements) {
        last--;
        ClassNode element = popWithMessage(last);
        if (isTwoSlotType(element)) {
            mv.visitInsn(POP2);
        } else {
            mv.visitInsn(POP);
        }
    }
}
Also used : MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 15 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class OperandStack method storeVar.

public void storeVar(BytecodeVariable variable) {
    MethodVisitor mv = controller.getMethodVisitor();
    int idx = variable.getIndex();
    ClassNode type = variable.getType();
    // value is on stack
    if (variable.isHolder()) {
        doGroovyCast(type);
        box();
        mv.visitVarInsn(ALOAD, idx);
        mv.visitTypeInsn(CHECKCAST, "groovy/lang/Reference");
        mv.visitInsn(SWAP);
        mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false);
    } else {
        doGroovyCast(type);
        if (type == ClassHelper.double_TYPE) {
            mv.visitVarInsn(DSTORE, idx);
        } else if (type == ClassHelper.float_TYPE) {
            mv.visitVarInsn(FSTORE, idx);
        } else if (type == ClassHelper.long_TYPE) {
            mv.visitVarInsn(LSTORE, idx);
        } else if (type == ClassHelper.boolean_TYPE || type == ClassHelper.char_TYPE || type == ClassHelper.byte_TYPE || type == ClassHelper.int_TYPE || type == ClassHelper.short_TYPE) {
            mv.visitVarInsn(ISTORE, idx);
        } else {
            mv.visitVarInsn(ASTORE, idx);
        }
    }
    // remove RHS value from operand stack
    remove(1);
}
Also used : MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

MethodVisitor (org.objectweb.asm.MethodVisitor)403 Label (org.objectweb.asm.Label)114 ClassNode (org.codehaus.groovy.ast.ClassNode)57 ClassWriter (org.objectweb.asm.ClassWriter)33 Type (org.objectweb.asm.Type)33 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)30 ArrayList (java.util.ArrayList)27 Test (org.junit.Test)23 ClassVisitor (org.objectweb.asm.ClassVisitor)23 Parameter (org.codehaus.groovy.ast.Parameter)22 LinkedList (java.util.LinkedList)19 InterfaceHelperClassNode (org.codehaus.groovy.ast.InterfaceHelperClassNode)18 AsmClassGenerator (org.codehaus.groovy.classgen.AsmClassGenerator)18 BytecodeExpression (org.codehaus.groovy.classgen.BytecodeExpression)18 FieldVisitor (org.objectweb.asm.FieldVisitor)18 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)17 ClassReader (org.objectweb.asm.ClassReader)17 List (java.util.List)16 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)16 MethodDescription (net.bytebuddy.description.method.MethodDescription)15