Search in sources :

Example 16 with OperandStack

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

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)

Example 17 with OperandStack

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

the class StaticTypesStatementWriter method loadFromArray.

private void loadFromArray(MethodVisitor mv, BytecodeVariable variable, int array, int iteratorIdx) {
    OperandStack os = controller.getOperandStack();
    mv.visitVarInsn(ALOAD, array);
    mv.visitVarInsn(ILOAD, iteratorIdx);
    ClassNode varType = variable.getType();
    boolean primitiveType = ClassHelper.isPrimitiveType(varType);
    boolean isByte = ClassHelper.byte_TYPE.equals(varType);
    boolean isShort = ClassHelper.short_TYPE.equals(varType);
    boolean isInt = ClassHelper.int_TYPE.equals(varType);
    boolean isLong = ClassHelper.long_TYPE.equals(varType);
    boolean isFloat = ClassHelper.float_TYPE.equals(varType);
    boolean isDouble = ClassHelper.double_TYPE.equals(varType);
    boolean isChar = ClassHelper.char_TYPE.equals(varType);
    boolean isBoolean = ClassHelper.boolean_TYPE.equals(varType);
    if (primitiveType) {
        if (isByte) {
            mv.visitInsn(BALOAD);
        }
        if (isShort) {
            mv.visitInsn(SALOAD);
        }
        if (isInt || isChar || isBoolean) {
            mv.visitInsn(isChar ? CALOAD : isBoolean ? BALOAD : IALOAD);
        }
        if (isLong) {
            mv.visitInsn(LALOAD);
        }
        if (isFloat) {
            mv.visitInsn(FALOAD);
        }
        if (isDouble) {
            mv.visitInsn(DALOAD);
        }
    } else {
        mv.visitInsn(AALOAD);
    }
    os.push(varType);
    os.storeVar(variable);
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) ClassNode(org.codehaus.groovy.ast.ClassNode)

Example 18 with OperandStack

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

the class StaticTypesStatementWriter method writeForInLoop.

@Override
protected void writeForInLoop(final ForStatement loop) {
    controller.getAcg().onLineNumber(loop, "visitForLoop");
    writeStatementLabel(loop);
    CompileStack compileStack = controller.getCompileStack();
    MethodVisitor mv = controller.getMethodVisitor();
    OperandStack operandStack = controller.getOperandStack();
    compileStack.pushLoop(loop.getVariableScope(), loop.getStatementLabels());
    // Identify type of collection
    TypeChooser typeChooser = controller.getTypeChooser();
    Expression collectionExpression = loop.getCollectionExpression();
    ClassNode collectionType = typeChooser.resolveType(collectionExpression, controller.getClassNode());
    Parameter loopVariable = loop.getVariable();
    int size = operandStack.getStackLength();
    if (collectionType.isArray() && loopVariable.getOriginType().equals(collectionType.getComponentType())) {
        writeOptimizedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
    } else if (ENUMERATION_CLASSNODE.equals(collectionType)) {
        writeEnumerationBasedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
    } else {
        writeIteratorBasedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
    }
    operandStack.popDownTo(size);
    compileStack.pop();
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) ClassNode(org.codehaus.groovy.ast.ClassNode) TypeChooser(org.codehaus.groovy.classgen.asm.TypeChooser) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Parameter(org.codehaus.groovy.ast.Parameter) CompileStack(org.codehaus.groovy.classgen.asm.CompileStack) MethodVisitor(org.objectweb.asm.MethodVisitor)

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