Search in sources :

Example 1 with OperandStack

use of org.codehaus.groovy.classgen.asm.OperandStack in project groovy by apache.

the class AsmClassGenerator method storeThisInstanceField.

private void storeThisInstanceField(FieldExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    FieldNode field = expression.getField();
    boolean setReferenceFromReference = field.isHolder() && expression.isUseReferenceDirectly();
    String ownerName = (field.getOwner().equals(controller.getClassNode())) ? controller.getInternalClassName() : BytecodeHelper.getClassInternalName(field.getOwner());
    OperandStack operandStack = controller.getOperandStack();
    if (setReferenceFromReference) {
        // rhs is ready to use reference, just put it in the field
        mv.visitVarInsn(ALOAD, 0);
        operandStack.push(controller.getClassNode());
        operandStack.swap();
        mv.visitFieldInsn(PUTFIELD, ownerName, field.getName(), BytecodeHelper.getTypeDescription(field.getType()));
    } else if (field.isHolder()) {
        // rhs is normal value, set the value in the Reference
        operandStack.doGroovyCast(field.getOriginType());
        operandStack.box();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, ownerName, expression.getFieldName(), BytecodeHelper.getTypeDescription(field.getType()));
        mv.visitInsn(SWAP);
        mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false);
    } else {
        // rhs is normal value, set normal value
        operandStack.doGroovyCast(field.getOriginType());
        mv.visitVarInsn(ALOAD, 0);
        operandStack.push(controller.getClassNode());
        operandStack.swap();
        mv.visitFieldInsn(PUTFIELD, ownerName, field.getName(), BytecodeHelper.getTypeDescription(field.getType()));
    }
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 2 with OperandStack

use of org.codehaus.groovy.classgen.asm.OperandStack in project groovy by apache.

the class AsmClassGenerator method visitRangeExpression.

public void visitRangeExpression(RangeExpression expression) {
    OperandStack operandStack = controller.getOperandStack();
    expression.getFrom().visit(this);
    operandStack.box();
    expression.getTo().visit(this);
    operandStack.box();
    operandStack.pushBool(expression.isInclusive());
    createRangeMethod.call(controller.getMethodVisitor());
    operandStack.replace(ClassHelper.RANGE_TYPE, 3);
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack)

Example 3 with OperandStack

use of org.codehaus.groovy.classgen.asm.OperandStack in project groovy by apache.

the class InvokeDynamicWriter method makeIndyCall.

private void makeIndyCall(MethodCallerMultiAdapter adapter, Expression receiver, boolean implicitThis, boolean safe, String methodName, Expression arguments) {
    OperandStack operandStack = controller.getOperandStack();
    String sig = prepareIndyCall(receiver, implicitThis);
    // load arguments
    int numberOfArguments = 1;
    ArgumentListExpression ae = makeArgumentList(arguments);
    boolean containsSpreadExpression = AsmClassGenerator.containsSpreadExpression(arguments);
    if (containsSpreadExpression) {
        controller.getAcg().despreadList(ae.getExpressions(), true);
        sig += getTypeDescription(Object[].class);
    } else {
        for (Expression arg : ae.getExpressions()) {
            arg.visit(controller.getAcg());
            if (arg instanceof CastExpression) {
                operandStack.box();
                controller.getAcg().loadWrapper(arg);
                sig += getTypeDescription(Wrapper.class);
            } else {
                sig += getTypeDescription(operandStack.getTopOperand());
            }
            numberOfArguments++;
        }
    }
    sig += ")Ljava/lang/Object;";
    String callSiteName = METHOD.getCallSiteName();
    if (adapter == null)
        callSiteName = INIT.getCallSiteName();
    int flags = getMethodCallFlags(adapter, safe, containsSpreadExpression);
    finishIndyCall(BSM, callSiteName, sig, numberOfArguments, methodName, flags);
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) Wrapper(org.codehaus.groovy.runtime.wrappers.Wrapper) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 4 with OperandStack

use of org.codehaus.groovy.classgen.asm.OperandStack in project groovy-core by groovy.

the class InvokeDynamicWriter method makeIndyCall.

private void makeIndyCall(MethodCallerMultiAdapter adapter, Expression receiver, boolean implicitThis, boolean safe, String methodName, Expression arguments) {
    OperandStack operandStack = controller.getOperandStack();
    String sig = prepareIndyCall(receiver, implicitThis);
    // load arguments
    int numberOfArguments = 1;
    ArgumentListExpression ae = makeArgumentList(arguments);
    boolean containsSpreadExpression = AsmClassGenerator.containsSpreadExpression(arguments);
    if (containsSpreadExpression) {
        controller.getAcg().despreadList(ae.getExpressions(), true);
        sig += getTypeDescription(Object[].class);
    } else {
        for (Expression arg : ae.getExpressions()) {
            arg.visit(controller.getAcg());
            if (arg instanceof CastExpression) {
                operandStack.box();
                controller.getAcg().loadWrapper(arg);
                sig += getTypeDescription(Wrapper.class);
            } else {
                sig += getTypeDescription(operandStack.getTopOperand());
            }
            numberOfArguments++;
        }
    }
    sig += ")Ljava/lang/Object;";
    String callSiteName = METHOD.getCallSiteName();
    if (adapter == null)
        callSiteName = INIT.getCallSiteName();
    int flags = getMethodCallFlags(adapter, safe, containsSpreadExpression);
    finishIndyCall(BSM, callSiteName, sig, numberOfArguments, methodName, flags);
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) Wrapper(org.codehaus.groovy.runtime.wrappers.Wrapper) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 5 with OperandStack

use of org.codehaus.groovy.classgen.asm.OperandStack in project groovy-core by groovy.

the class InvokeDynamicWriter method finishIndyCall.

private void finishIndyCall(Handle bsmHandle, String methodName, String sig, int numberOfArguments, Object... bsmArgs) {
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();
    controller.getMethodVisitor().visitInvokeDynamicInsn(methodName, sig, bsmHandle, bsmArgs);
    operandStack.replace(ClassHelper.OBJECT_TYPE, numberOfArguments);
    compileStack.popLHS();
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) CompileStack(org.codehaus.groovy.classgen.asm.CompileStack)

Aggregations

OperandStack (org.codehaus.groovy.classgen.asm.OperandStack)18 CompileStack (org.codehaus.groovy.classgen.asm.CompileStack)5 MethodVisitor (org.objectweb.asm.MethodVisitor)5 MethodCallerMultiAdapter (org.codehaus.groovy.classgen.asm.MethodCallerMultiAdapter)4 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)3 Expression (org.codehaus.groovy.ast.expr.Expression)3 ArrayList (java.util.ArrayList)2 ClassNode (org.codehaus.groovy.ast.ClassNode)2 FieldNode (org.codehaus.groovy.ast.FieldNode)2 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)2 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)2 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)2 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)2 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)2 Wrapper (org.codehaus.groovy.runtime.wrappers.Wrapper)2 Parameter (org.codehaus.groovy.ast.Parameter)1 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)1 TypeChooser (org.codehaus.groovy.classgen.asm.TypeChooser)1