Search in sources :

Example 1 with AsmClassGenerator

use of org.codehaus.groovy.classgen.AsmClassGenerator in project groovy by apache.

the class CompareIdentityExpression method visit.

@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        AsmClassGenerator acg = (AsmClassGenerator) visitor;
        WriterController controller = acg.getController();
        controller.getTypeChooser().resolveType(leftExpression, controller.getClassNode());
        controller.getTypeChooser().resolveType(rightExpression, controller.getClassNode());
        MethodVisitor mv = controller.getMethodVisitor();
        leftExpression.visit(acg);
        controller.getOperandStack().box();
        rightExpression.visit(acg);
        controller.getOperandStack().box();
        Label l1 = new Label();
        mv.visitJumpInsn(IF_ACMPNE, l1);
        mv.visitInsn(ICONST_1);
        Label l2 = new Label();
        mv.visitJumpInsn(GOTO, l2);
        mv.visitLabel(l1);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(l2);
        controller.getOperandStack().replace(ClassHelper.boolean_TYPE, 2);
    } else {
        super.visit(visitor);
    }
}
Also used : WriterController(org.codehaus.groovy.classgen.asm.WriterController) Label(org.objectweb.asm.Label) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 2 with AsmClassGenerator

use of org.codehaus.groovy.classgen.AsmClassGenerator in project groovy by apache.

the class StaticInvocationWriter method loadArguments.

protected void loadArguments(List<Expression> argumentList, Parameter[] para) {
    if (para.length == 0)
        return;
    ClassNode lastParaType = para[para.length - 1].getOriginType();
    AsmClassGenerator acg = controller.getAcg();
    TypeChooser typeChooser = controller.getTypeChooser();
    OperandStack operandStack = controller.getOperandStack();
    ClassNode lastArgType = !argumentList.isEmpty() ? typeChooser.resolveType(argumentList.get(argumentList.size() - 1), controller.getClassNode()) : null;
    if (lastParaType.isArray() && ((argumentList.size() > para.length) || ((argumentList.size() == (para.length - 1)) && !lastParaType.equals(lastArgType)) || ((argumentList.size() == para.length && lastArgType != null && !lastArgType.isArray()) && (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(lastArgType, lastParaType.getComponentType()))) || ClassHelper.GSTRING_TYPE.equals(lastArgType) && ClassHelper.STRING_TYPE.equals(lastParaType.getComponentType()))) {
        int stackLen = operandStack.getStackLength() + argumentList.size();
        MethodVisitor mv = controller.getMethodVisitor();
        //mv = new org.objectweb.asm.util.TraceMethodVisitor(mv);
        controller.setMethodVisitor(mv);
        // first parameters as usual
        for (int i = 0; i < para.length - 1; i++) {
            Expression expression = argumentList.get(i);
            expression.visit(acg);
            if (!isNullConstant(expression)) {
                operandStack.doGroovyCast(para[i].getType());
            }
        }
        // last parameters wrapped in an array
        List<Expression> lastParams = new LinkedList<Expression>();
        for (int i = para.length - 1; i < argumentList.size(); i++) {
            lastParams.add(argumentList.get(i));
        }
        ArrayExpression array = new ArrayExpression(lastParaType.getComponentType(), lastParams);
        array.visit(acg);
        // adjust stack length
        while (operandStack.getStackLength() < stackLen) {
            operandStack.push(ClassHelper.OBJECT_TYPE);
        }
        if (argumentList.size() == para.length - 1) {
            operandStack.remove(1);
        }
    } else if (argumentList.size() == para.length) {
        for (int i = 0; i < argumentList.size(); i++) {
            Expression expression = argumentList.get(i);
            expression.visit(acg);
            if (!isNullConstant(expression)) {
                operandStack.doGroovyCast(para[i].getType());
            }
        }
    } else {
        // method call with default arguments
        ClassNode classNode = controller.getClassNode();
        Expression[] arguments = new Expression[para.length];
        for (int i = 0, j = 0; i < para.length; i++) {
            Parameter curParam = para[i];
            ClassNode curParamType = curParam.getType();
            Expression curArg = j < argumentList.size() ? argumentList.get(j) : null;
            Expression initialExpression = (Expression) curParam.getNodeMetaData(StaticTypesMarker.INITIAL_EXPRESSION);
            if (initialExpression == null && curParam.hasInitialExpression())
                initialExpression = curParam.getInitialExpression();
            if (initialExpression == null && curParam.getNodeMetaData(Verifier.INITIAL_EXPRESSION) != null) {
                initialExpression = (Expression) curParam.getNodeMetaData(Verifier.INITIAL_EXPRESSION);
            }
            ClassNode curArgType = curArg == null ? null : typeChooser.resolveType(curArg, classNode);
            if (initialExpression != null && !compatibleArgumentType(curArgType, curParamType)) {
                // use default expression
                arguments[i] = initialExpression;
            } else {
                arguments[i] = curArg;
                j++;
            }
        }
        for (int i = 0; i < arguments.length; i++) {
            Expression expression = arguments[i];
            expression.visit(acg);
            if (!isNullConstant(expression)) {
                operandStack.doGroovyCast(para[i].getType());
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) TemporaryVariableExpression(org.codehaus.groovy.transform.sc.TemporaryVariableExpression) Parameter(org.codehaus.groovy.ast.Parameter) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) LinkedList(java.util.LinkedList) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 3 with AsmClassGenerator

use of org.codehaus.groovy.classgen.AsmClassGenerator in project groovy by apache.

the class StaticTypesStatementWriter method writeOptimizedForEachLoop.

private void writeOptimizedForEachLoop(CompileStack compileStack, OperandStack operandStack, MethodVisitor mv, ForStatement loop, Expression collectionExpression, ClassNode collectionType, Parameter loopVariable) {
    BytecodeVariable variable = compileStack.defineVariable(loopVariable, false);
    Label continueLabel = compileStack.getContinueLabel();
    Label breakLabel = compileStack.getBreakLabel();
    AsmClassGenerator acg = controller.getAcg();
    // load array on stack
    collectionExpression.visit(acg);
    mv.visitInsn(DUP);
    int array = compileStack.defineTemporaryVariable("$arr", collectionType, true);
    mv.visitJumpInsn(IFNULL, breakLabel);
    // $len = array.length
    mv.visitVarInsn(ALOAD, array);
    mv.visitInsn(ARRAYLENGTH);
    operandStack.push(ClassHelper.int_TYPE);
    int arrayLen = compileStack.defineTemporaryVariable("$len", ClassHelper.int_TYPE, true);
    // $idx = 0
    mv.visitInsn(ICONST_0);
    operandStack.push(ClassHelper.int_TYPE);
    int loopIdx = compileStack.defineTemporaryVariable("$idx", ClassHelper.int_TYPE, true);
    mv.visitLabel(continueLabel);
    // $idx<$len?
    mv.visitVarInsn(ILOAD, loopIdx);
    mv.visitVarInsn(ILOAD, arrayLen);
    mv.visitJumpInsn(IF_ICMPGE, breakLabel);
    // get array element
    loadFromArray(mv, variable, array, loopIdx);
    // $idx++
    mv.visitIincInsn(loopIdx, 1);
    // loop body
    loop.getLoopBlock().visit(acg);
    mv.visitJumpInsn(GOTO, continueLabel);
    mv.visitLabel(breakLabel);
    compileStack.removeVar(loopIdx);
    compileStack.removeVar(arrayLen);
    compileStack.removeVar(array);
}
Also used : Label(org.objectweb.asm.Label) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) BytecodeVariable(org.codehaus.groovy.classgen.asm.BytecodeVariable)

Example 4 with AsmClassGenerator

use of org.codehaus.groovy.classgen.AsmClassGenerator in project groovy by apache.

the class InvocationWriter method invokeClosure.

private void invokeClosure(Expression arguments, String methodName) {
    AsmClassGenerator acg = controller.getAcg();
    acg.visitVariableExpression(new VariableExpression(methodName));
    controller.getOperandStack().box();
    if (arguments instanceof TupleExpression) {
        arguments.visit(acg);
    } else {
        new TupleExpression(arguments).visit(acg);
    }
    invokeClosureMethod.call(controller.getMethodVisitor());
    controller.getOperandStack().replace(ClassHelper.OBJECT_TYPE);
}
Also used : AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator)

Example 5 with AsmClassGenerator

use of org.codehaus.groovy.classgen.AsmClassGenerator in project groovy by apache.

the class BinaryExpressionHelper method evaluateBinaryExpressionWithAssignment.

protected void evaluateBinaryExpressionWithAssignment(String method, BinaryExpression expression) {
    Expression leftExpression = expression.getLeftExpression();
    AsmClassGenerator acg = controller.getAcg();
    OperandStack operandStack = controller.getOperandStack();
    if (leftExpression instanceof BinaryExpression) {
        BinaryExpression leftBinExpr = (BinaryExpression) leftExpression;
        if (leftBinExpr.getOperation().getType() == Types.LEFT_SQUARE_BRACKET) {
            evaluateArrayAssignmentWithOperator(method, expression, leftBinExpr);
            return;
        }
    }
    evaluateBinaryExpression(method, expression);
    // br to leave a copy of rvalue on the stack. see also isPopRequired()
    operandStack.dup();
    controller.getCompileStack().pushLHS(true);
    leftExpression.visit(acg);
    controller.getCompileStack().popLHS();
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator)

Aggregations

AsmClassGenerator (org.codehaus.groovy.classgen.AsmClassGenerator)45 ClassNode (org.codehaus.groovy.ast.ClassNode)21 MethodVisitor (org.objectweb.asm.MethodVisitor)18 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)14 Expression (org.codehaus.groovy.ast.expr.Expression)14 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)14 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)14 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)10 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)10 FieldExpression (org.codehaus.groovy.ast.expr.FieldExpression)10 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)10 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)10 Label (org.objectweb.asm.Label)10 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)8 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)8 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)8 ElvisOperatorExpression (org.codehaus.groovy.ast.expr.ElvisOperatorExpression)8 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)8 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)8 PostfixExpression (org.codehaus.groovy.ast.expr.PostfixExpression)8