org.codehaus.groovy.classgen.BytecodeExpression Java Examples

The following examples show how to use org.codehaus.groovy.classgen.BytecodeExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StaticTypesUnaryExpressionHelper.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void writeNotExpression(final NotExpression expression) {
    TypeChooser typeChooser = controller.getTypeChooser();
    Expression subExpression = expression.getExpression();
    ClassNode classNode = controller.getClassNode();
    if (typeChooser.resolveType(subExpression, classNode) == boolean_TYPE) {
        subExpression.visit(controller.getAcg());
        controller.getOperandStack().doGroovyCast(boolean_TYPE);
        BytecodeExpression bytecodeExpression = bytecodeX(mv -> {
            Label ne = new Label();
            mv.visitJumpInsn(IFNE, ne);
            mv.visitInsn(ICONST_1);
            Label out = new Label();
            mv.visitJumpInsn(GOTO, out);
            mv.visitLabel(ne);
            mv.visitInsn(ICONST_0);
            mv.visitLabel(out);
        });
        bytecodeExpression.visit(controller.getAcg());
        controller.getOperandStack().remove(1);
        return;
    }
    super.writeNotExpression(expression);
}
 
Example #2
Source File: BinaryExpressionHelper.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void execMethodAndStoreForSubscriptOperator(final int op, String method, final Expression expression, final VariableSlotLoader usesSubscript, final Expression orig) {
    writePostOrPrefixMethod(op, method, expression, orig);

    // we need special code for arrays to store the result (like for a[1]++)
    if (usesSubscript != null) {
        BinaryExpression be = (BinaryExpression) expression;
        CompileStack compileStack = controller.getCompileStack();
        OperandStack operandStack = controller.getOperandStack();
        ClassNode methodResultType = operandStack.getTopOperand();
        int resultIdx = compileStack.defineTemporaryVariable("postfix_" + method, methodResultType, true);
        BytecodeExpression methodResultLoader = new VariableSlotLoader(methodResultType, resultIdx, operandStack);

        // execute the assignment, this will leave the right side (here the method call result) on the stack
        assignToArray(be, be.getLeftExpression(), usesSubscript, methodResultLoader, be.isSafe());

        compileStack.removeVar(resultIdx);

    } else if (expression instanceof VariableExpression || expression instanceof PropertyExpression || expression instanceof FieldExpression) {
        // here we handle a++ and a.b++
        controller.getOperandStack().dup();
        controller.getCompileStack().pushLHS(true);
        expression.visit(controller.getAcg());
        controller.getCompileStack().popLHS();
    }
    // other cases don't need storing, so nothing to be done for them
}
 
Example #3
Source File: ASTNodeVisitor.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public void visitBytecodeExpression(BytecodeExpression node) {
	pushASTNode(node);
	try {
		super.visitBytecodeExpression(node);
	} finally {
		popASTNode();
	}
}
 
Example #4
Source File: StaticTypesUnaryExpressionHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void writeUnaryMinus(final UnaryMinusExpression expression) {
    expression.getExpression().visit(controller.getAcg());
    if (isPrimitiveOnTop()) {
        ClassNode top = getTopOperand();
        if (top != boolean_TYPE) {
            BytecodeExpression bytecodeExpression = bytecodeX(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);
}
 
Example #5
Source File: StaticTypesUnaryExpressionHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void writeBitwiseNegate(final BitwiseNegationExpression expression) {
    expression.getExpression().visit(controller.getAcg());
    if (isPrimitiveOnTop()) {
        ClassNode top = getTopOperand();
        if (top == int_TYPE || top == short_TYPE || top == byte_TYPE || top == char_TYPE || top == long_TYPE) {
            BytecodeExpression bytecodeExpression = bytecodeX(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);
}
 
Example #6
Source File: StaticTypesCallSiteWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void makeGroovyObjectGetPropertySite(final Expression receiver, final String propertyName, final boolean safe, final boolean implicitThis) {
    ClassNode receiverType = controller.getClassNode();
    if (!AsmClassGenerator.isThisExpression(receiver) || controller.isInGeneratedFunction()) {
        receiverType = controller.getTypeChooser().resolveType(receiver, receiverType);
    }

    String property = propertyName;
    if (implicitThis && controller.getInvocationWriter() instanceof StaticInvocationWriter) {
        Expression currentCall = ((StaticInvocationWriter) controller.getInvocationWriter()).getCurrentCall();
        if (currentCall != null && currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER) != null) {
            property = currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
            String[] props = property.split("\\.");
            BytecodeExpression thisLoader = bytecodeX(CLOSURE_TYPE, mv -> mv.visitVarInsn(ALOAD, 0));
            PropertyExpression pexp = propX(thisLoader, constX(props[0]), safe);
            for (int i = 1, n = props.length; i < n; i += 1) {
                pexp.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, CLOSURE_TYPE);
                pexp = propX(pexp, props[i]);
            }
            pexp.visit(controller.getAcg());
            return;
        }
    }

    if (makeGetPropertyWithGetter(receiver, receiverType, property, safe, implicitThis)) return;
    if (makeGetPrivateFieldWithBridgeMethod(receiver, receiverType, property, safe, implicitThis)) return;
    if (makeGetField(receiver, receiverType, property, safe, implicitThis)) return;

    MethodCallExpression call = callX(receiver, "getProperty", args(constX(property)));
    call.setImplicitThis(implicitThis);
    call.setMethodTarget(GROOVYOBJECT_GETPROPERTY_METHOD);
    call.setSafe(safe);
    call.visit(controller.getAcg());
}
 
Example #7
Source File: BinaryExpressionHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void writePostOrPrefixMethod(final int op, final String method, final Expression expression, final Expression orig) {
    // at this point the receiver will be already on the stack
    // in a[1]++ the method will be "++" aka "next" and the receiver a[1]
    ClassNode exprType = controller.getTypeChooser().resolveType(expression, controller.getClassNode());
    Expression callSiteReceiverSwap = new BytecodeExpression(exprType) {
        @Override
        public void visit(MethodVisitor mv) {
            OperandStack operandStack = controller.getOperandStack();
            // CallSite is normally not showing up on the
            // operandStack, so we place a dummy here with same
            // slot length.
            operandStack.push(ClassHelper.OBJECT_TYPE);
            // change (receiver,callsite) to (callsite,receiver)
            operandStack.swap();

            setType(operandStack.getTopOperand());

            // no need to keep any of those on the operand stack
            // after this expression is processed, the operand stack
            // will contain callSiteReceiverSwap.getType()
            operandStack.remove(2);
        }
    };
    // execute method
    // this will load the callsite and the receiver normally in the wrong
    // order since the receiver is already present, but before the callsite
    // Therefore we use callSiteReceiverSwap to correct the order.
    // After this call the JVM operand stack will contain the result of
    // the method call... usually simply Object in operandStack
    controller.getCallSiteWriter().makeCallSite(
            callSiteReceiverSwap,
            method,
            MethodCallExpression.NO_ARGUMENTS,
            false, false, false, false);
    // now rhs is completely done and we need only to store. In a[1]++ this
    // would be a.getAt(1).next() for the rhs, "lhs" code is a.putAt(1, rhs)
}
 
Example #8
Source File: GeneralUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static BytecodeExpression bytecodeX(final Consumer<MethodVisitor> writer) {
    return new BytecodeExpression() {
        @Override
        public void visit(final MethodVisitor visitor) {
            writer.accept(visitor);
        }
    };
}
 
Example #9
Source File: CpsTransformer.java    From groovy-cps with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBytecodeExpression(BytecodeExpression expression) {
    // This can't be encountered in a source file.
    sourceUnit.addError(new SyntaxException("Unsupported expression for CPS transformation",
            expression.getLineNumber(), expression.getColumnNumber()));
}
 
Example #10
Source File: RestrictiveCodeVisitor.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void visitBytecodeExpression(BytecodeExpression expression) {
    restrict(expression);
}
 
Example #11
Source File: GeneralUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static BytecodeExpression bytecodeX(final ClassNode type, final Consumer<MethodVisitor> writer) {
    BytecodeExpression expression = bytecodeX(writer);
    expression.setType(type);
    return expression;
}
 
Example #12
Source File: CodeVisitorSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBytecodeExpression(BytecodeExpression expression) {
}
 
Example #13
Source File: TransformingCodeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBytecodeExpression(final BytecodeExpression cle) {
    super.visitBytecodeExpression(cle);
    trn.visitBytecodeExpression(cle);
}
 
Example #14
Source File: SecureASTCustomizer.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBytecodeExpression(final BytecodeExpression expression) {
    assertExpressionAuthorized(expression);
}
 
Example #15
Source File: ContextualClassCodeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBytecodeExpression(final BytecodeExpression cle) {
    pushContext(cle);
    super.visitBytecodeExpression(cle);
    popContext();
}
 
Example #16
Source File: ASTFinder.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBytecodeExpression(final BytecodeExpression cle) {
    super.visitBytecodeExpression(cle);
    tryFind(BytecodeExpression.class, cle);
}
 
Example #17
Source File: ASTChildrenVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBytecodeExpression(BytecodeExpression bce) {
}
 
Example #18
Source File: RestrictiveCodeVisitor.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void visitBytecodeExpression(BytecodeExpression expression) {
    restrict(expression);
}
 
Example #19
Source File: GroovyCodeVisitor.java    From groovy with Apache License 2.0 votes vote down vote up
void visitBytecodeExpression(BytecodeExpression expression);