Search in sources :

Example 11 with OperandStack

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

the class AsmClassGenerator method visitPropertyExpression.

public void visitPropertyExpression(PropertyExpression expression) {
    Expression objectExpression = expression.getObjectExpression();
    OperandStack operandStack = controller.getOperandStack();
    int mark = operandStack.getStackLength() - 1;
    MethodCallerMultiAdapter adapter;
    if (controller.getCompileStack().isLHS()) {
        //operandStack.box();
        adapter = setProperty;
        if (isGroovyObject(objectExpression))
            adapter = setGroovyObjectProperty;
        if (isThisOrSuperInStaticContext(objectExpression))
            adapter = setProperty;
    } else {
        adapter = getProperty;
        if (isGroovyObject(objectExpression))
            adapter = getGroovyObjectProperty;
        if (isThisOrSuperInStaticContext(objectExpression))
            adapter = getProperty;
    }
    visitAttributeOrProperty(expression, adapter);
    if (controller.getCompileStack().isLHS()) {
        // remove surplus values
        operandStack.remove(operandStack.getStackLength() - mark);
    } else {
        controller.getAssertionWriter().record(expression.getProperty());
    }
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) MethodCallerMultiAdapter(org.codehaus.groovy.classgen.asm.MethodCallerMultiAdapter)

Example 12 with OperandStack

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

the class AsmClassGenerator method visitAttributeExpression.

public void visitAttributeExpression(AttributeExpression expression) {
    Expression objectExpression = expression.getObjectExpression();
    MethodCallerMultiAdapter adapter;
    OperandStack operandStack = controller.getOperandStack();
    int mark = operandStack.getStackLength() - 1;
    if (controller.getCompileStack().isLHS()) {
        adapter = setField;
        if (isGroovyObject(objectExpression))
            adapter = setGroovyObjectField;
        if (usesSuper(expression))
            adapter = setFieldOnSuper;
    } else {
        adapter = getField;
        if (isGroovyObject(objectExpression))
            adapter = getGroovyObjectField;
        if (usesSuper(expression))
            adapter = getFieldOnSuper;
    }
    visitAttributeOrProperty(expression, adapter);
    if (!controller.getCompileStack().isLHS()) {
        controller.getAssertionWriter().record(expression.getProperty());
    } else {
        operandStack.remove(operandStack.getStackLength() - mark);
    }
}
Also used : MethodCallerMultiAdapter(org.codehaus.groovy.classgen.asm.MethodCallerMultiAdapter) OperandStack(org.codehaus.groovy.classgen.asm.OperandStack)

Example 13 with OperandStack

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

the class AsmClassGenerator method visitListExpression.

public void visitListExpression(ListExpression expression) {
    onLineNumber(expression, "ListExpression");
    int size = expression.getExpressions().size();
    boolean containsSpreadExpression = containsSpreadExpression(expression);
    boolean containsOnlyConstants = !containsSpreadExpression && containsOnlyConstants(expression);
    OperandStack operandStack = controller.getOperandStack();
    if (!containsSpreadExpression) {
        MethodVisitor mv = controller.getMethodVisitor();
        BytecodeHelper.pushConstant(mv, size);
        mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
        int maxInit = 1000;
        if (size < maxInit || !containsOnlyConstants) {
            for (int i = 0; i < size; i++) {
                mv.visitInsn(DUP);
                BytecodeHelper.pushConstant(mv, i);
                expression.getExpression(i).visit(this);
                operandStack.box();
                mv.visitInsn(AASTORE);
            }
            controller.getOperandStack().remove(size);
        } else {
            List<Expression> expressions = expression.getExpressions();
            List<String> methods = new ArrayList();
            MethodVisitor oldMv = mv;
            int index = 0;
            while (index < size) {
                String methodName = "$createListEntry_" + controller.getNextHelperMethodIndex();
                methods.add(methodName);
                mv = controller.getClassVisitor().visitMethod(ACC_PRIVATE + ACC_STATIC + ACC_SYNTHETIC, methodName, "([Ljava/lang/Object;)V", null, null);
                controller.setMethodVisitor(mv);
                mv.visitCode();
                int methodBlockSize = Math.min(size - index, maxInit);
                int methodBlockEnd = index + methodBlockSize;
                for (; index < methodBlockEnd; index++) {
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitLdcInsn(index);
                    expressions.get(index).visit(this);
                    operandStack.box();
                    mv.visitInsn(AASTORE);
                }
                operandStack.remove(methodBlockSize);
                mv.visitInsn(RETURN);
                mv.visitMaxs(0, 0);
                mv.visitEnd();
            }
            mv = oldMv;
            controller.setMethodVisitor(mv);
            for (String methodName : methods) {
                mv.visitInsn(DUP);
                mv.visitMethodInsn(INVOKESTATIC, controller.getInternalClassName(), methodName, "([Ljava/lang/Object;)V", false);
            }
        }
    } else {
        despreadList(expression.getExpressions(), false);
    }
    createListMethod.call(controller.getMethodVisitor());
    operandStack.push(ClassHelper.LIST_TYPE);
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) ArrayList(java.util.ArrayList) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 14 with OperandStack

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

the class AsmClassGenerator method visitPropertyExpression.

public void visitPropertyExpression(PropertyExpression expression) {
    Expression objectExpression = expression.getObjectExpression();
    OperandStack operandStack = controller.getOperandStack();
    int mark = operandStack.getStackLength() - 1;
    MethodCallerMultiAdapter adapter;
    if (controller.getCompileStack().isLHS()) {
        //operandStack.box();
        adapter = setProperty;
        if (isGroovyObject(objectExpression))
            adapter = setGroovyObjectProperty;
        if (isThisOrSuperInStaticContext(objectExpression))
            adapter = setProperty;
    } else {
        adapter = getProperty;
        if (isGroovyObject(objectExpression))
            adapter = getGroovyObjectProperty;
        if (isThisOrSuperInStaticContext(objectExpression))
            adapter = getProperty;
    }
    visitAttributeOrProperty(expression, adapter);
    if (controller.getCompileStack().isLHS()) {
        // remove surplus values
        operandStack.remove(operandStack.getStackLength() - mark);
    } else {
        controller.getAssertionWriter().record(expression.getProperty());
    }
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) MethodCallerMultiAdapter(org.codehaus.groovy.classgen.asm.MethodCallerMultiAdapter)

Example 15 with OperandStack

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

the class InvokeDynamicWriter method prepareIndyCall.

private String prepareIndyCall(Expression receiver, boolean implicitThis) {
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();
    compileStack.pushLHS(false);
    // load normal receiver as first argument
    compileStack.pushImplicitThis(implicitThis);
    receiver.visit(controller.getAcg());
    compileStack.popImplicitThis();
    return "(" + getTypeDescription(operandStack.getTopOperand());
}
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