List of usage examples for org.objectweb.asm MethodVisitor visitLabel
public void visitLabel(final Label label)
From source file:org.codehaus.groovy.classgen.asm.sc.StaticTypesCallSiteWriter.java
License:Apache License
private void writeMapDotProperty(final Expression receiver, final String propertyName, final MethodVisitor mv, final boolean safe) { receiver.visit(controller.getAcg()); // load receiver Label exit = new Label(); if (safe) {//from w w w. ja v a2 s .co m Label doGet = new Label(); mv.visitJumpInsn(IFNONNULL, doGet); controller.getOperandStack().remove(1); mv.visitInsn(ACONST_NULL); mv.visitJumpInsn(GOTO, exit); mv.visitLabel(doGet); receiver.visit(controller.getAcg()); } mv.visitLdcInsn(propertyName); // load property name mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true); if (safe) { mv.visitLabel(exit); } controller.getOperandStack().replace(OBJECT_TYPE); }
From source file:org.codehaus.groovy.classgen.asm.sc.StaticTypesCallSiteWriter.java
License:Apache License
private void writeListDotProperty(final Expression receiver, final String propertyName, final MethodVisitor mv, final boolean safe) { ClassNode componentType = receiver.getNodeMetaData(StaticCompilationMetadataKeys.COMPONENT_TYPE); if (componentType == null) { componentType = OBJECT_TYPE;//from w ww . ja v a 2 s.co m } // for lists, replace list.foo with: // def result = new ArrayList(list.size()) // for (e in list) { result.add (e.foo) } // result CompileStack compileStack = controller.getCompileStack(); Label exit = new Label(); if (safe) { receiver.visit(controller.getAcg()); Label doGet = new Label(); mv.visitJumpInsn(IFNONNULL, doGet); controller.getOperandStack().remove(1); mv.visitInsn(ACONST_NULL); mv.visitJumpInsn(GOTO, exit); mv.visitLabel(doGet); } Variable tmpList = new VariableExpression("tmpList", ClassHelper.make(ArrayList.class)); int var = compileStack.defineTemporaryVariable(tmpList, false); Variable iterator = new VariableExpression("iterator", Iterator_TYPE); int it = compileStack.defineTemporaryVariable(iterator, false); Variable nextVar = new VariableExpression("next", componentType); final int next = compileStack.defineTemporaryVariable(nextVar, false); mv.visitTypeInsn(NEW, "java/util/ArrayList"); mv.visitInsn(DUP); receiver.visit(controller.getAcg()); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "size", "()I", true); controller.getOperandStack().remove(1); mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "(I)V", false); mv.visitVarInsn(ASTORE, var); Label l1 = new Label(); mv.visitLabel(l1); receiver.visit(controller.getAcg()); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "iterator", "()Ljava/util/Iterator;", true); controller.getOperandStack().remove(1); mv.visitVarInsn(ASTORE, it); Label l2 = new Label(); mv.visitLabel(l2); mv.visitVarInsn(ALOAD, it); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z", true); Label l3 = new Label(); mv.visitJumpInsn(IFEQ, l3); mv.visitVarInsn(ALOAD, it); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;", true); mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(componentType)); mv.visitVarInsn(ASTORE, next); Label l4 = new Label(); mv.visitLabel(l4); mv.visitVarInsn(ALOAD, var); final ClassNode finalComponentType = componentType; PropertyExpression pexp = new PropertyExpression(new BytecodeExpression() { @Override public void visit(final MethodVisitor mv) { mv.visitVarInsn(ALOAD, next); } @Override public ClassNode getType() { return finalComponentType; } }, propertyName); pexp.visit(controller.getAcg()); controller.getOperandStack().box(); controller.getOperandStack().remove(1); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true); mv.visitInsn(POP); Label l5 = new Label(); mv.visitLabel(l5); mv.visitJumpInsn(GOTO, l2); mv.visitLabel(l3); mv.visitVarInsn(ALOAD, var); if (safe) { mv.visitLabel(exit); } controller.getOperandStack().push(ClassHelper.make(ArrayList.class)); controller.getCompileStack().removeVar(next); controller.getCompileStack().removeVar(it); controller.getCompileStack().removeVar(var); }
From source file:org.codehaus.groovy.classgen.asm.sc.StaticTypesCallSiteWriter.java
License:Apache License
boolean makeGetField(final Expression receiver, final ClassNode receiverType, final String fieldName, final boolean safe, final boolean implicitThis) { FieldNode field = receiverType.getField(fieldName); if (field != null && isDirectAccessAllowed(field, controller.getClassNode())) { CompileStack compileStack = controller.getCompileStack(); MethodVisitor mv = controller.getMethodVisitor(); ClassNode replacementType = field.getOriginType(); OperandStack operandStack = controller.getOperandStack(); if (field.isStatic()) { mv.visitFieldInsn(GETSTATIC, BytecodeHelper.getClassInternalName(field.getOwner()), fieldName, BytecodeHelper.getTypeDescription(replacementType)); operandStack.push(replacementType); } else {/*from ww w .j a v a2s . c om*/ if (implicitThis) { compileStack.pushImplicitThis(implicitThis); receiver.visit(controller.getAcg()); compileStack.popImplicitThis(); } else { receiver.visit(controller.getAcg()); } Label exit = new Label(); if (safe) { mv.visitInsn(DUP); Label doGet = new Label(); mv.visitJumpInsn(IFNONNULL, doGet); mv.visitInsn(POP); mv.visitInsn(ACONST_NULL); mv.visitJumpInsn(GOTO, exit); mv.visitLabel(doGet); } if (!operandStack.getTopOperand().isDerivedFrom(field.getOwner())) { mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(field.getOwner())); } mv.visitFieldInsn(GETFIELD, BytecodeHelper.getClassInternalName(field.getOwner()), fieldName, BytecodeHelper.getTypeDescription(replacementType)); if (safe) { if (ClassHelper.isPrimitiveType(replacementType)) { operandStack.replace(replacementType); operandStack.box(); replacementType = operandStack.getTopOperand(); } mv.visitLabel(exit); } } operandStack.replace(replacementType); return true; } for (ClassNode face : receiverType.getInterfaces()) { // GROOVY-7039 if (face != receiverType && makeGetField(receiver, face, fieldName, safe, implicitThis)) { return true; } } ClassNode superClass = receiverType.getSuperClass(); if (superClass != null && !OBJECT_TYPE.equals(superClass)) { return makeGetField(receiver, superClass, fieldName, safe, implicitThis); } return false; }
From source file:org.codehaus.groovy.classgen.asm.sc.StaticTypesStatementWriter.java
License:Apache License
private void writeOptimizedForEachLoop(CompileStack compileStack, OperandStack operandStack, MethodVisitor mv, ForStatement loop, Expression collectionExpression, ClassNode collectionType, Parameter loopVariable) { BytecodeVariable variable = compileStack.defineVariable(loopVariable, false); Label continueLabel = compileStack.getContinueLabel(); Label breakLabel = compileStack.getBreakLabel(); AsmClassGenerator acg = controller.getAcg(); // load array on stack collectionExpression.visit(acg);/* w w w . ja v a 2 s . c o m*/ mv.visitInsn(DUP); int array = compileStack.defineTemporaryVariable("$arr", collectionType, true); mv.visitJumpInsn(IFNULL, breakLabel); // $len = array.length mv.visitVarInsn(ALOAD, array); mv.visitInsn(ARRAYLENGTH); operandStack.push(ClassHelper.int_TYPE); int arrayLen = compileStack.defineTemporaryVariable("$len", ClassHelper.int_TYPE, true); // $idx = 0 mv.visitInsn(ICONST_0); operandStack.push(ClassHelper.int_TYPE); int loopIdx = compileStack.defineTemporaryVariable("$idx", ClassHelper.int_TYPE, true); mv.visitLabel(continueLabel); // $idx<$len? mv.visitVarInsn(ILOAD, loopIdx); mv.visitVarInsn(ILOAD, arrayLen); mv.visitJumpInsn(IF_ICMPGE, breakLabel); // get array element loadFromArray(mv, variable, array, loopIdx); // $idx++ mv.visitIincInsn(loopIdx, 1); // loop body loop.getLoopBlock().visit(acg); mv.visitJumpInsn(GOTO, continueLabel); mv.visitLabel(breakLabel); compileStack.removeVar(loopIdx); compileStack.removeVar(arrayLen); compileStack.removeVar(array); }
From source file:org.codehaus.groovy.classgen.asm.sc.StaticTypesStatementWriter.java
License:Apache License
private void writeIteratorBasedForEachLoop(CompileStack compileStack, OperandStack operandStack, MethodVisitor mv, ForStatement loop, Expression collectionExpression, ClassNode collectionType, Parameter loopVariable) { // Declare the loop counter. BytecodeVariable variable = compileStack.defineVariable(loopVariable, false); if (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(collectionType, ITERABLE_CLASSNODE)) { MethodCallExpression iterator = new MethodCallExpression(collectionExpression, "iterator", new ArgumentListExpression()); iterator.setMethodTarget(collectionType.getMethod("iterator", Parameter.EMPTY_ARRAY)); iterator.setImplicitThis(false); iterator.visit(controller.getAcg()); } else {// w ww . ja v a2 s . c om collectionExpression.visit(controller.getAcg()); mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/DefaultGroovyMethods", "iterator", "(Ljava/lang/Object;)Ljava/util/Iterator;", false); operandStack.replace(ClassHelper.Iterator_TYPE); } // Then get the iterator and generate the loop control 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); }
From source file:org.codehaus.groovy.classgen.asm.sc.StaticTypesStatementWriter.java
License:Apache License
private void writeEnumerationBasedForEachLoop(CompileStack compileStack, OperandStack operandStack, MethodVisitor mv, ForStatement loop, Expression collectionExpression, ClassNode collectionType, Parameter loopVariable) { // Declare the loop counter. BytecodeVariable variable = compileStack.defineVariable(loopVariable, false); collectionExpression.visit(controller.getAcg()); // Then get the iterator and generate the loop control int enumIdx = compileStack.defineTemporaryVariable("$enum", ENUMERATION_CLASSNODE, true); Label continueLabel = compileStack.getContinueLabel(); Label breakLabel = compileStack.getBreakLabel(); mv.visitLabel(continueLabel); mv.visitVarInsn(ALOAD, enumIdx);//from w ww. jav a 2s. c om ENUMERATION_HASMORE_METHOD.call(mv); // note: ifeq tests for ==0, a boolean is 0 if it is false mv.visitJumpInsn(IFEQ, breakLabel); mv.visitVarInsn(ALOAD, enumIdx); ENUMERATION_NEXT_METHOD.call(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); }
From source file:org.codehaus.groovy.classgen.asm.sc.StaticTypesUnaryExpressionHelper.java
License:Apache License
@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 = new BytecodeExpression() { @Override//from ww w. jav a 2s .c o m public void visit(final MethodVisitor 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); }
From source file:org.codehaus.groovy.classgen.asm.StatementWriter.java
License:Apache License
public void writeBlockStatement(BlockStatement block) { writeStatementLabel(block);/*www .j av a2 s .c o m*/ int mark = controller.getOperandStack().getStackLength(); CompileStack compileStack = controller.getCompileStack(); compileStack.pushVariableScope(block.getVariableScope()); List<Statement> statementList = block.getStatements(); for (Statement statement : statementList) { statement.visit(controller.getAcg()); } compileStack.pop(); // GROOVY-7647 if (block.getLastLineNumber() > 0 && !isMethodOrConstructorNonEmptyBlock(block) // GROOVY-9126 ) { MethodVisitor mv = controller.getMethodVisitor(); Label blockEnd = new Label(); mv.visitLabel(blockEnd); mv.visitLineNumber(block.getLastLineNumber(), blockEnd); } controller.getOperandStack().popDownTo(mark); }
From source file:org.codehaus.groovy.classgen.asm.StatementWriter.java
License:Apache License
protected void writeForInLoop(ForStatement loop) { controller.getAcg().onLineNumber(loop, "visitForLoop"); writeStatementLabel(loop);/* www . j a v a 2 s .co m*/ 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(); }
From source file:org.codehaus.groovy.classgen.asm.StatementWriter.java
License:Apache License
protected void writeForLoopWithClosureList(ForStatement loop) { controller.getAcg().onLineNumber(loop, "visitForLoop"); writeStatementLabel(loop);// w w w. j a v a 2 s. c o m MethodVisitor mv = controller.getMethodVisitor(); controller.getCompileStack().pushLoop(loop.getVariableScope(), loop.getStatementLabels()); ClosureListExpression clExpr = (ClosureListExpression) loop.getCollectionExpression(); controller.getCompileStack().pushVariableScope(clExpr.getVariableScope()); List<Expression> expressions = clExpr.getExpressions(); int size = expressions.size(); // middle element is condition, lower half is init, higher half is increment int condIndex = (size - 1) / 2; // visit init for (int i = 0; i < condIndex; i++) { visitExpressionOfLoopStatement(expressions.get(i)); } Label continueLabel = controller.getCompileStack().getContinueLabel(); Label breakLabel = controller.getCompileStack().getBreakLabel(); Label cond = new Label(); mv.visitLabel(cond); // visit condition leave boolean on stack { Expression condExpr = expressions.get(condIndex); int mark = controller.getOperandStack().getStackLength(); condExpr.visit(controller.getAcg()); controller.getOperandStack().castToBool(mark, true); } // jump if we don't want to continue // note: ifeq tests for ==0, a boolean is 0 if it is false controller.getOperandStack().jump(IFEQ, breakLabel); // Generate the loop body loop.getLoopBlock().visit(controller.getAcg()); // visit increment mv.visitLabel(continueLabel); // fix for being on the wrong line when debugging for loop controller.getAcg().onLineNumber(loop, "increment condition"); for (int i = condIndex + 1; i < size; i += 1) { visitExpressionOfLoopStatement(expressions.get(i)); } // jump to test the condition again mv.visitJumpInsn(GOTO, cond); // loop end mv.visitLabel(breakLabel); controller.getCompileStack().pop(); controller.getCompileStack().pop(); }