Search in sources :

Example 26 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class OptimizingStatementWriter method writeFastPathPrelude.

private void writeFastPathPrelude(FastPathData meta) {
    MethodVisitor mv = controller.getMethodVisitor();
    mv.visitJumpInsn(GOTO, meta.afterPath);
    mv.visitLabel(meta.pathStart);
    controller.switchToFastPath();
}
Also used : MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 27 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class OptimizingStatementWriter method writeGuards.

private FastPathData writeGuards(StatementMeta meta, Statement statement) {
    if (notEnableFastPath(meta))
        return null;
    controller.getAcg().onLineNumber(statement, null);
    MethodVisitor mv = controller.getMethodVisitor();
    FastPathData fastPathData = new FastPathData();
    Label slowPath = new Label();
    for (int i = 0; i < guards.length; i++) {
        if (meta.involvedTypes[i]) {
            guards[i].call(mv);
            mv.visitJumpInsn(IFEQ, slowPath);
        }
    }
    // meta class check with boolean holder
    String owner = BytecodeHelper.getClassInternalName(controller.getClassNode());
    MethodNode mn = controller.getMethodNode();
    if (mn != null) {
        mv.visitFieldInsn(GETSTATIC, owner, Verifier.STATIC_METACLASS_BOOL, "Z");
        mv.visitJumpInsn(IFNE, slowPath);
    }
    //standard metaclass check
    disabledStandardMetaClass.call(mv);
    mv.visitJumpInsn(IFNE, slowPath);
    // other guards here
    mv.visitJumpInsn(GOTO, fastPathData.pathStart);
    mv.visitLabel(slowPath);
    return fastPathData;
}
Also used : Label(org.objectweb.asm.Label) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 28 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class StatementWriter method writeForInLoop.

protected void writeForInLoop(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());
    // Declare the loop counter.
    BytecodeVariable variable = compileStack.defineVariable(loop.getVariable(), false);
    // Then get the iterator and generate the loop control
    MethodCallExpression iterator = new MethodCallExpression(loop.getCollectionExpression(), "iterator", new ArgumentListExpression());
    iterator.visit(controller.getAcg());
    operandStack.doGroovyCast(ClassHelper.Iterator_TYPE);
    final int iteratorIdx = compileStack.defineTemporaryVariable("iterator", ClassHelper.Iterator_TYPE, true);
    Label continueLabel = compileStack.getContinueLabel();
    Label breakLabel = compileStack.getBreakLabel();
    mv.visitLabel(continueLabel);
    mv.visitVarInsn(ALOAD, iteratorIdx);
    writeIteratorHasNext(mv);
    // note: ifeq tests for ==0, a boolean is 0 if it is false
    mv.visitJumpInsn(IFEQ, breakLabel);
    mv.visitVarInsn(ALOAD, iteratorIdx);
    writeIteratorNext(mv);
    operandStack.push(ClassHelper.OBJECT_TYPE);
    operandStack.storeVar(variable);
    // Generate the loop body
    loop.getLoopBlock().visit(controller.getAcg());
    mv.visitJumpInsn(GOTO, continueLabel);
    mv.visitLabel(breakLabel);
    compileStack.removeVar(iteratorIdx);
    compileStack.pop();
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Label(org.objectweb.asm.Label) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 29 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class StatementWriter method writeSynchronized.

public void writeSynchronized(SynchronizedStatement statement) {
    controller.getAcg().onLineNumber(statement, "visitSynchronizedStatement");
    writeStatementLabel(statement);
    final MethodVisitor mv = controller.getMethodVisitor();
    CompileStack compileStack = controller.getCompileStack();
    statement.getExpression().visit(controller.getAcg());
    controller.getOperandStack().box();
    final int index = compileStack.defineTemporaryVariable("synchronized", ClassHelper.OBJECT_TYPE, true);
    final Label synchronizedStart = new Label();
    final Label synchronizedEnd = new Label();
    final Label catchAll = new Label();
    mv.visitVarInsn(ALOAD, index);
    mv.visitInsn(MONITORENTER);
    mv.visitLabel(synchronizedStart);
    // place holder for "empty" synchronized blocks, for example
    // if there is only a break/continue.
    mv.visitInsn(NOP);
    Runnable finallyPart = new Runnable() {

        public void run() {
            mv.visitVarInsn(ALOAD, index);
            mv.visitInsn(MONITOREXIT);
        }
    };
    BlockRecorder fb = new BlockRecorder(finallyPart);
    fb.startRange(synchronizedStart);
    compileStack.pushBlockRecorder(fb);
    statement.getCode().visit(controller.getAcg());
    fb.closeRange(catchAll);
    compileStack.writeExceptionTable(fb, catchAll, null);
    //pop fb
    compileStack.pop();
    finallyPart.run();
    mv.visitJumpInsn(GOTO, synchronizedEnd);
    mv.visitLabel(catchAll);
    finallyPart.run();
    mv.visitInsn(ATHROW);
    mv.visitLabel(synchronizedEnd);
    compileStack.removeVar(index);
}
Also used : BlockRecorder(org.codehaus.groovy.classgen.asm.CompileStack.BlockRecorder) Label(org.objectweb.asm.Label) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 30 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class StatementWriter method writeThrow.

public void writeThrow(ThrowStatement statement) {
    controller.getAcg().onLineNumber(statement, "visitThrowStatement");
    writeStatementLabel(statement);
    MethodVisitor mv = controller.getMethodVisitor();
    statement.getExpression().visit(controller.getAcg());
    // we should infer the type of the exception from the expression
    mv.visitTypeInsn(CHECKCAST, "java/lang/Throwable");
    mv.visitInsn(ATHROW);
    controller.getOperandStack().remove(1);
}
Also used : MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

MethodVisitor (org.objectweb.asm.MethodVisitor)403 Label (org.objectweb.asm.Label)114 ClassNode (org.codehaus.groovy.ast.ClassNode)57 ClassWriter (org.objectweb.asm.ClassWriter)33 Type (org.objectweb.asm.Type)33 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)30 ArrayList (java.util.ArrayList)27 Test (org.junit.Test)23 ClassVisitor (org.objectweb.asm.ClassVisitor)23 Parameter (org.codehaus.groovy.ast.Parameter)22 LinkedList (java.util.LinkedList)19 InterfaceHelperClassNode (org.codehaus.groovy.ast.InterfaceHelperClassNode)18 AsmClassGenerator (org.codehaus.groovy.classgen.AsmClassGenerator)18 BytecodeExpression (org.codehaus.groovy.classgen.BytecodeExpression)18 FieldVisitor (org.objectweb.asm.FieldVisitor)18 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)17 ClassReader (org.objectweb.asm.ClassReader)17 List (java.util.List)16 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)16 MethodDescription (net.bytebuddy.description.method.MethodDescription)15