Search in sources :

Example 36 with AsmClassGenerator

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

the class BinaryExpressionHelper method evaluateLogicalOrExpression.

private void evaluateLogicalOrExpression(BinaryExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    AsmClassGenerator acg = controller.getAcg();
    OperandStack operandStack = controller.getOperandStack();
    Label end = new Label();
    expression.getLeftExpression().visit(acg);
    operandStack.doGroovyCast(ClassHelper.boolean_TYPE);
    Label trueCase = operandStack.jump(IFNE);
    expression.getRightExpression().visit(acg);
    operandStack.doGroovyCast(ClassHelper.boolean_TYPE);
    Label falseCase = operandStack.jump(IFEQ);
    mv.visitLabel(trueCase);
    ConstantExpression.PRIM_TRUE.visit(acg);
    operandStack.jump(GOTO, end);
    mv.visitLabel(falseCase);
    ConstantExpression.PRIM_FALSE.visit(acg);
    mv.visitLabel(end);
}
Also used : Label(org.objectweb.asm.Label) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 37 with AsmClassGenerator

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

the class BinaryExpressionHelper method evaluateCompareExpression.

protected void evaluateCompareExpression(MethodCaller compareMethod, BinaryExpression expression) {
    Expression leftExp = expression.getLeftExpression();
    TypeChooser typeChooser = controller.getTypeChooser();
    ClassNode cn = controller.getClassNode();
    ClassNode leftType = typeChooser.resolveType(leftExp, cn);
    Expression rightExp = expression.getRightExpression();
    ClassNode rightType = typeChooser.resolveType(rightExp, cn);
    boolean done = false;
    if (ClassHelper.isPrimitiveType(leftType) && ClassHelper.isPrimitiveType(rightType)) {
        BinaryExpressionMultiTypeDispatcher helper = new BinaryExpressionMultiTypeDispatcher(getController());
        done = helper.doPrimitiveCompare(leftType, rightType, expression);
    }
    if (!done) {
        AsmClassGenerator acg = controller.getAcg();
        OperandStack operandStack = controller.getOperandStack();
        leftExp.visit(acg);
        operandStack.box();
        rightExp.visit(acg);
        operandStack.box();
        compareMethod.call(controller.getMethodVisitor());
        ClassNode resType = ClassHelper.boolean_TYPE;
        if (compareMethod == findRegexMethod) {
            resType = ClassHelper.OBJECT_TYPE;
        }
        operandStack.replace(resType, 2);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) 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)

Example 38 with AsmClassGenerator

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

the class BinaryExpressionHelper method evaluateLogicalAndExpression.

private void evaluateLogicalAndExpression(BinaryExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    AsmClassGenerator acg = controller.getAcg();
    OperandStack operandStack = controller.getOperandStack();
    expression.getLeftExpression().visit(acg);
    operandStack.doGroovyCast(ClassHelper.boolean_TYPE);
    Label falseCase = operandStack.jump(IFEQ);
    expression.getRightExpression().visit(acg);
    operandStack.doGroovyCast(ClassHelper.boolean_TYPE);
    operandStack.jump(IFEQ, falseCase);
    ConstantExpression.PRIM_TRUE.visit(acg);
    Label trueCase = new Label();
    mv.visitJumpInsn(GOTO, trueCase);
    mv.visitLabel(falseCase);
    ConstantExpression.PRIM_FALSE.visit(acg);
    mv.visitLabel(trueCase);
    // have to remove 1 because of the GOTO
    operandStack.remove(1);
}
Also used : Label(org.objectweb.asm.Label) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 39 with AsmClassGenerator

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

the class BinaryExpressionMultiTypeDispatcher method evaluateBinaryExpression.

@Override
protected void evaluateBinaryExpression(final String message, BinaryExpression binExp) {
    int operation = removeAssignment(binExp.getOperation().getType());
    ClassNode current = getController().getClassNode();
    Expression leftExp = binExp.getLeftExpression();
    ClassNode leftTypeOrig = getController().getTypeChooser().resolveType(leftExp, current);
    ClassNode leftType = leftTypeOrig;
    Expression rightExp = binExp.getRightExpression();
    ClassNode rightType = getController().getTypeChooser().resolveType(rightExp, current);
    AsmClassGenerator acg = getController().getAcg();
    OperandStack os = getController().getOperandStack();
    if (operation == LEFT_SQUARE_BRACKET) {
        leftType = leftTypeOrig.getComponentType();
        int operationType = getOperandType(leftType);
        BinaryExpressionWriter bew = binExpWriter[operationType];
        if (leftTypeOrig.isArray() && isIntCastableType(rightExp) && bew.arrayGet(operation, true)) {
            leftExp.visit(acg);
            os.doGroovyCast(leftTypeOrig);
            rightExp.visit(acg);
            os.doGroovyCast(int_TYPE);
            bew.arrayGet(operation, false);
            os.replace(bew.getArrayGetResultType(), 2);
        } else {
            super.evaluateBinaryExpression(message, binExp);
        }
    } else if (operation == DIVIDE) {
        int operationType = getOperandType(getController().getTypeChooser().resolveType(binExp, current));
        BinaryExpressionWriter bew = binExpWriter[operationType];
        if (bew.writeDivision(true)) {
            leftExp.visit(acg);
            os.doGroovyCast(bew.getDevisionOpResultType());
            rightExp.visit(acg);
            os.doGroovyCast(bew.getDevisionOpResultType());
            bew.writeDivision(false);
        } else {
            super.evaluateBinaryExpression(message, binExp);
        }
    } else {
        int operationType = getOperandConversionType(leftType, rightType);
        BinaryExpressionWriter bew = binExpWriter[operationType];
        if (isShiftOperation(operation) && isIntCastableType(rightExp) && bew.write(operation, true)) {
            leftExp.visit(acg);
            os.doGroovyCast(bew.getNormalOpResultType());
            rightExp.visit(acg);
            os.doGroovyCast(int_TYPE);
            bew.write(operation, false);
        } else if (bew.write(operation, true)) {
            leftExp.visit(acg);
            os.doGroovyCast(bew.getNormalOpResultType());
            rightExp.visit(acg);
            os.doGroovyCast(bew.getNormalOpResultType());
            bew.write(operation, false);
        } else {
            super.evaluateBinaryExpression(message, binExp);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) Expression(org.codehaus.groovy.ast.expr.Expression) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator)

Example 40 with AsmClassGenerator

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

the class BinaryExpressionMultiTypeDispatcher method assignToArray.

@Override
protected void assignToArray(Expression orig, Expression receiver, Expression index, Expression rhsValueLoader) {
    ClassNode current = getController().getClassNode();
    ClassNode arrayType = getController().getTypeChooser().resolveType(receiver, current);
    ClassNode arrayComponentType = arrayType.getComponentType();
    int operationType = getOperandType(arrayComponentType);
    BinaryExpressionWriter bew = binExpWriter[operationType];
    AsmClassGenerator acg = getController().getAcg();
    if (bew.arraySet(true) && arrayType.isArray()) {
        OperandStack operandStack = getController().getOperandStack();
        // load the array
        receiver.visit(acg);
        operandStack.doGroovyCast(arrayType);
        // load index
        index.visit(acg);
        operandStack.doGroovyCast(int_TYPE);
        // load rhs
        rhsValueLoader.visit(acg);
        operandStack.doGroovyCast(arrayComponentType);
        // store value in array
        bew.arraySet(false);
        // load return value && correct operand stack stack
        operandStack.remove(3);
        rhsValueLoader.visit(acg);
    } else {
        super.assignToArray(orig, receiver, index, rhsValueLoader);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) 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