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();
}
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;
}
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();
}
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);
}
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);
}
Aggregations