Search in sources :

Example 1 with WriterController

use of org.codehaus.groovy.classgen.asm.WriterController 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 WriterController

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

the class CompareIdentityExpression method visit.

@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        AsmClassGenerator acg = (AsmClassGenerator) visitor;
        WriterController controller = acg.getController();
        ClassNode leftType = controller.getTypeChooser().resolveType(leftExpression, controller.getClassNode());
        ClassNode rightType = 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 : ClassNode(org.codehaus.groovy.ast.ClassNode) WriterController(org.codehaus.groovy.classgen.asm.WriterController) Label(org.objectweb.asm.Label) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 3 with WriterController

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

the class AsmClassGenerator method visitClass.

// GroovyClassVisitor interface
//-------------------------------------------------------------------------
public void visitClass(ClassNode classNode) {
    referencedClasses.clear();
    WriterControllerFactory factory = (WriterControllerFactory) classNode.getNodeMetaData(WriterControllerFactory.class);
    WriterController normalController = new WriterController();
    if (factory != null) {
        this.controller = factory.makeController(normalController);
    } else {
        this.controller = normalController;
    }
    this.controller.init(this, context, cv, classNode);
    if (controller.shouldOptimizeForInt() || factory != null) {
        OptimizingStatementWriter.setNodeMeta(controller.getTypeChooser(), classNode);
    }
    try {
        cv.visit(controller.getBytecodeVersion(), adjustedClassModifiersForClassWriting(classNode), controller.getInternalClassName(), BytecodeHelper.getGenericsSignature(classNode), controller.getInternalBaseClassName(), BytecodeHelper.getClassInternalNames(classNode.getInterfaces()));
        cv.visitSource(sourceFile, null);
        if (classNode instanceof InnerClassNode) {
            InnerClassNode innerClass = (InnerClassNode) classNode;
            MethodNode enclosingMethod = innerClass.getEnclosingMethod();
            if (enclosingMethod != null) {
                String outerClassName = BytecodeHelper.getClassInternalName(innerClass.getOuterClass().getName());
                cv.visitOuterClass(outerClassName, enclosingMethod.getName(), BytecodeHelper.getMethodDescriptor(enclosingMethod));
            }
        }
        if (classNode.getName().endsWith("package-info")) {
            PackageNode packageNode = classNode.getPackage();
            if (packageNode != null) {
                // pull them out of package node but treat them like they were on class node
                for (AnnotationNode an : packageNode.getAnnotations()) {
                    // skip built-in properties
                    if (an.isBuiltIn())
                        continue;
                    if (an.hasSourceRetention())
                        continue;
                    AnnotationVisitor av = getAnnotationVisitor(classNode, an, cv);
                    visitAnnotationAttributes(an, av);
                    av.visitEnd();
                }
            }
            cv.visitEnd();
            return;
        } else {
            visitAnnotations(classNode, cv);
        }
        if (classNode.isInterface()) {
            ClassNode owner = classNode;
            if (owner instanceof InnerClassNode) {
                owner = owner.getOuterClass();
            }
            String outerClassName = classNode.getName();
            String name = outerClassName + "$" + context.getNextInnerClassIdx();
            controller.setInterfaceClassLoadingClass(new InterfaceHelperClassNode(owner, name, 4128, ClassHelper.OBJECT_TYPE, controller.getCallSiteWriter().getCallSites()));
            super.visitClass(classNode);
            createInterfaceSyntheticStaticFields();
        } else {
            super.visitClass(classNode);
            MopWriter.Factory mopWriterFactory = classNode.getNodeMetaData(MopWriter.Factory.class);
            if (mopWriterFactory == null) {
                mopWriterFactory = MopWriter.FACTORY;
            }
            MopWriter mopWriter = mopWriterFactory.create(controller);
            mopWriter.createMopMethods();
            controller.getCallSiteWriter().generateCallSiteArray();
            createSyntheticStaticFields();
        }
        // GROOVY-6750 and GROOVY-6808
        for (Iterator<InnerClassNode> iter = classNode.getInnerClasses(); iter.hasNext(); ) {
            InnerClassNode innerClass = iter.next();
            makeInnerClassEntry(innerClass);
        }
        makeInnerClassEntry(classNode);
        cv.visitEnd();
    } catch (GroovyRuntimeException e) {
        e.setModule(classNode.getModule());
        throw e;
    } catch (NegativeArraySizeException nase) {
        throw new GroovyRuntimeException("NegativeArraySizeException while processing " + sourceFile, nase);
    } catch (NullPointerException npe) {
        throw new GroovyRuntimeException("NPE while processing " + sourceFile, npe);
    }
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) WriterController(org.codehaus.groovy.classgen.asm.WriterController) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) PackageNode(org.codehaus.groovy.ast.PackageNode) MopWriter(org.codehaus.groovy.classgen.asm.MopWriter) WriterControllerFactory(org.codehaus.groovy.classgen.asm.WriterControllerFactory)

Example 4 with WriterController

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

the class TemporaryVariableExpression method visit.

@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        if (variable == null) {
            AsmClassGenerator acg = (AsmClassGenerator) visitor;
            WriterController controller = acg.getController();
            variable = new ExpressionAsVariableSlot(controller, expression);
        }
        variable.visit(visitor);
    } else {
        expression.visit(visitor);
    }
}
Also used : WriterController(org.codehaus.groovy.classgen.asm.WriterController) ExpressionAsVariableSlot(org.codehaus.groovy.classgen.asm.ExpressionAsVariableSlot) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator)

Example 5 with WriterController

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

the class CompareToNullExpression method visit.

@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        AsmClassGenerator acg = (AsmClassGenerator) visitor;
        WriterController controller = acg.getController();
        MethodVisitor mv = controller.getMethodVisitor();
        objectExpression.visit(acg);
        ClassNode top = controller.getOperandStack().getTopOperand();
        if (ClassHelper.isPrimitiveType(top)) {
            controller.getOperandStack().pop();
            mv.visitInsn(ICONST_0);
            controller.getOperandStack().push(ClassHelper.boolean_TYPE);
            return;
        }
        Label zero = new Label();
        mv.visitJumpInsn(equalsNull ? IFNONNULL : IFNULL, zero);
        mv.visitInsn(ICONST_1);
        Label end = new Label();
        mv.visitJumpInsn(GOTO, end);
        mv.visitLabel(zero);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(end);
        controller.getOperandStack().replace(ClassHelper.boolean_TYPE);
    } else {
        super.visit(visitor);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) WriterController(org.codehaus.groovy.classgen.asm.WriterController) Label(org.objectweb.asm.Label) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

WriterController (org.codehaus.groovy.classgen.asm.WriterController)8 AsmClassGenerator (org.codehaus.groovy.classgen.AsmClassGenerator)6 ClassNode (org.codehaus.groovy.ast.ClassNode)5 Label (org.objectweb.asm.Label)4 MethodVisitor (org.objectweb.asm.MethodVisitor)4 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)2 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)2 InterfaceHelperClassNode (org.codehaus.groovy.ast.InterfaceHelperClassNode)2 MethodNode (org.codehaus.groovy.ast.MethodNode)2 PackageNode (org.codehaus.groovy.ast.PackageNode)2 ExpressionAsVariableSlot (org.codehaus.groovy.classgen.asm.ExpressionAsVariableSlot)2 MopWriter (org.codehaus.groovy.classgen.asm.MopWriter)2 WriterControllerFactory (org.codehaus.groovy.classgen.asm.WriterControllerFactory)2 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)2