Example usage for org.objectweb.asm Label Label

List of usage examples for org.objectweb.asm Label Label

Introduction

In this page you can find the example usage for org.objectweb.asm Label Label.

Prototype

public Label() 

Source Link

Document

Constructs a new label.

Usage

From source file:dijkstra.gen.DijkstraCodeGenerator.java

License:Open Source License

/**
 * Get the values of the left and right children on the stack and then perform
 * the operation./*from  www.  jav  a 2  s .  c om*/
 * @see dijkstra.ast.ASTVisitor#visit(dijkstra.ast.ASTNodeFactory.BinaryExpressionNode)
 */
public byte[] visit(BinaryExpressionNode binary) {
    final MethodVisitor mv = mvStack.peek();
    DijkstraType childOneType = binary.getExpr1().getType();
    if (childOneType == DijkstraType.ARRAY) {
        childOneType = ((ArraySymbol) ((ArrayAccessorNode) binary.getExpr1()).getId().symbol).getArrayType();
    }
    DijkstraType childTwoType = binary.getExpr2().getType();
    if (childTwoType == DijkstraType.ARRAY) {
        childTwoType = ((ArraySymbol) ((ArrayAccessorNode) binary.getExpr2()).getId().symbol).getArrayType();
    }
    Label lab1, lab2;
    if (binary.getOp() == DijkstraParser.AMP) {
        lab1 = new Label();
        lab2 = new Label();
        binary.getExpr1().accept(this);
        mv.visitJumpInsn(IFEQ, lab1);
        binary.getExpr2().accept(this);
        mv.visitJumpInsn(IFEQ, lab1);
        mv.visitInsn(ICONST_1);
        mv.visitJumpInsn(GOTO, lab2);
        mv.visitLabel(lab1);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(lab2);
    } else if (binary.getOp() == DijkstraParser.PIPE) {
        lab1 = new Label();
        lab2 = new Label();
        binary.getExpr1().accept(this);
        mv.visitJumpInsn(IFNE, lab1);
        binary.getExpr2().accept(this);
        mv.visitJumpInsn(IFNE, lab1);
        mv.visitInsn(ICONST_0);
        mv.visitJumpInsn(GOTO, lab2);
        mv.visitLabel(lab1);
        mv.visitInsn(ICONST_1);
        mv.visitLabel(lab2);
    } else {
        binary.getExpr1().accept(this);
        if ((childOneType == DijkstraType.INT && childTwoType == DijkstraType.FLOAT)
                || (binary.getOp() == DijkstraParser.SLASH && childOneType == DijkstraType.INT)) {
            mv.visitInsn(L2D);
        }
        binary.getExpr2().accept(this);
        if ((childOneType == DijkstraType.FLOAT && childTwoType == DijkstraType.INT)
                || (binary.getOp() == DijkstraParser.SLASH && childTwoType == DijkstraType.INT)) {
            mv.visitInsn(L2D);
        }
        switch (binary.getOp()) {
        case DijkstraParser.LT:
            lab1 = new Label();
            lab2 = new Label();
            if (childOneType == DijkstraType.INT && childTwoType == DijkstraType.INT) {
                mv.visitInsn(LCMP);
            } else {
                mv.visitInsn(DCMPL);
            }
            mv.visitJumpInsn(IFGE, lab1);
            mv.visitInsn(ICONST_1); // left < right
            mv.visitJumpInsn(GOTO, lab2);
            mv.visitLabel(lab1);
            mv.visitInsn(ICONST_0); // right >= left
            mv.visitLabel(lab2);
            break;
        case DijkstraParser.LTEQ:
            lab1 = new Label();
            lab2 = new Label();
            if (childOneType == DijkstraType.INT && childTwoType == DijkstraType.INT) {
                mv.visitInsn(LCMP);
            } else {
                mv.visitInsn(DCMPL);
            }
            mv.visitJumpInsn(IFGT, lab1);
            mv.visitInsn(ICONST_1); // left <= right
            mv.visitJumpInsn(GOTO, lab2);
            mv.visitLabel(lab1);
            mv.visitInsn(ICONST_0); // right > left
            mv.visitLabel(lab2);
            break;
        case DijkstraParser.GT:
            lab1 = new Label();
            lab2 = new Label();
            if (childOneType == DijkstraType.INT && childTwoType == DijkstraType.INT) {
                mv.visitInsn(LCMP);
            } else {
                mv.visitInsn(DCMPL);
            }
            mv.visitJumpInsn(IFLE, lab1);
            mv.visitInsn(ICONST_1); // left > right
            mv.visitJumpInsn(GOTO, lab2);
            mv.visitLabel(lab1);
            mv.visitInsn(ICONST_0); // right <= left
            mv.visitLabel(lab2);
            break;
        case DijkstraParser.GTEQ:
            lab1 = new Label();
            lab2 = new Label();
            if (childOneType == DijkstraType.INT && childTwoType == DijkstraType.INT) {
                mv.visitInsn(LCMP);
            } else {
                mv.visitInsn(DCMPL);
            }
            mv.visitJumpInsn(IFLT, lab1);
            mv.visitInsn(ICONST_1); // left >= right
            mv.visitJumpInsn(GOTO, lab2);
            mv.visitLabel(lab1);
            mv.visitInsn(ICONST_0); // right < left
            mv.visitLabel(lab2);
            break;
        case DijkstraParser.PLUS:
            if (childOneType == DijkstraType.INT && childTwoType == DijkstraType.INT) {
                mv.visitInsn(LADD);
            } else {
                mv.visitInsn(DADD);
            }
            break;
        case DijkstraParser.MINUS:
            if (childOneType == DijkstraType.INT && childTwoType == DijkstraType.INT) {
                mv.visitInsn(LSUB);
            } else {
                mv.visitInsn(DSUB);
            }
            break;
        case DijkstraParser.STAR:
            if (childOneType == DijkstraType.INT && childTwoType == DijkstraType.INT) {
                mv.visitInsn(LMUL);
            } else {
                mv.visitInsn(DMUL);
            }
            break;
        case DijkstraParser.SLASH:
            mv.visitInsn(DDIV);
            break;
        case DijkstraParser.MOD:
            mv.visitInsn(LREM);
            break;
        case DijkstraParser.DIV:
            mv.visitInsn(LDIV);
            break;
        case DijkstraParser.EQ:
            lab1 = new Label();
            lab2 = new Label();
            if (childOneType == DijkstraType.INT && childTwoType == DijkstraType.INT) {
                mv.visitInsn(LCMP);
                mv.visitJumpInsn(IFNE, lab1);
            } else if (childOneType == DijkstraType.BOOLEAN) {
                mv.visitJumpInsn(IF_ICMPNE, lab1);
            } else {
                mv.visitInsn(DCMPL);
                mv.visitJumpInsn(IFNE, lab1);
            }
            mv.visitInsn(ICONST_1);
            mv.visitJumpInsn(GOTO, lab2);
            mv.visitLabel(lab1);
            mv.visitInsn(ICONST_0);
            mv.visitLabel(lab2);
            break;
        case DijkstraParser.NEQ:
            lab1 = new Label();
            lab2 = new Label();
            if (childOneType == DijkstraType.INT && childTwoType == DijkstraType.INT) {
                mv.visitInsn(LCMP);
                mv.visitJumpInsn(IFEQ, lab1);
            } else if (childOneType == DijkstraType.BOOLEAN) {
                mv.visitJumpInsn(IF_ICMPEQ, lab1);
            } else {
                mv.visitInsn(DCMPL);
                mv.visitJumpInsn(IFEQ, lab1);
            }
            mv.visitInsn(ICONST_1); // left != right
            mv.visitJumpInsn(GOTO, lab2);
            mv.visitLabel(lab1);
            mv.visitInsn(ICONST_0); // left = right
            mv.visitLabel(lab2);
            break;
        }
    }
    return null;
}

From source file:dijkstra.gen.DijkstraCodeGenerator.java

License:Open Source License

public byte[] visit(ProcedureDeclarationNode procDeclNode) {
    if (processingClassFields) {
        return null;
    }//from   www . j a  v  a 2  s  . c  o m
    final String methodName = procDeclNode.getIDNode().getName();
    final StringBuilder sig = new StringBuilder();
    sig.append('(');
    for (IDNode param : procDeclNode.getParamList()) {
        if (param.getType() == DijkstraType.INT) {
            sig.append('J');
        } else if (param.getType() == DijkstraType.FLOAT) {
            sig.append('D');
        } else {
            sig.append('Z');
        }
    }
    sig.append(")V");
    if (inClass) {
        mvStack.push(cw.visitMethod(ACC_PUBLIC, methodName, sig.toString(), null, null));
    } else {
        mvStack.push(cw.visitMethod(ACC_PUBLIC + ACC_STATIC, methodName, sig.toString(), null, null));
    }
    final MethodVisitor mv = mvStack.peek();

    JVMInfo.enterScope(inClass);
    // Load the parameters into the JVMInfo so it knows the proper addresses
    for (IDNode param : procDeclNode.getParamList()) {
        param.getAddress();
    }
    mv.visitCode();
    final Label startLabel = new Label();
    mv.visitLabel(startLabel);

    procDeclNode.getCompoundNode().accept(this);

    mv.visitInsn(RETURN);
    final Label endLabel = new Label();
    mv.visitLabel(endLabel);
    int paramLoc = 0;
    for (IDNode param : procDeclNode.getParamList()) {
        if (param.getType() == DijkstraType.INT) {
            mv.visitLocalVariable(param.getName(), "J", null, startLabel, endLabel, paramLoc);
            paramLoc += 2;
        } else if (param.getType() == DijkstraType.FLOAT) {
            mv.visitLocalVariable(param.getName(), "D", null, startLabel, endLabel, paramLoc);
            paramLoc += 2;
        } else {
            mv.visitLocalVariable(param.getName(), "Z", null, startLabel, endLabel, paramLoc);
            paramLoc += 1;
        }
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    JVMInfo.exitScope();
    mvStack.pop();
    return null;
}

From source file:dijkstra.gen.DijkstraCodeGenerator.java

License:Open Source License

public byte[] visit(FunctionDeclarationNode funDeclNode) {
    if (processingClassFields) {
        return null;
    }//from www .ja va  2s.  co m
    final String methodName = funDeclNode.getIDNode().getName();
    final StringBuilder sig = new StringBuilder();
    sig.append('(');
    for (IDNode param : funDeclNode.getParamList()) {
        if (param.getType() == DijkstraType.INT) {
            sig.append('J');
        } else if (param.getType() == DijkstraType.FLOAT) {
            sig.append('D');
        } else {
            sig.append('Z');
        }
    }
    sig.append(')');
    sig.append("[Ljava/lang/Object;");

    if (inClass) {
        mvStack.push(cw.visitMethod(ACC_PUBLIC, methodName, sig.toString(), null, null));
    } else {
        mvStack.push(cw.visitMethod(ACC_PUBLIC + ACC_STATIC, methodName, sig.toString(), null, null));
    }
    final MethodVisitor mv = mvStack.peek();

    JVMInfo.enterScope(inClass);
    // Load the parameters into the JVMInfo so it knows the proper addresses
    for (IDNode param : funDeclNode.getParamList()) {
        param.getAddress();
    }

    mv.visitIntInsn(SIPUSH, funDeclNode.getReturnTypes().size());
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    returnArrayStack.push(new ArraySymbol(null, UNDEFINED));
    mv.visitVarInsn(ASTORE, JVMInfo.getAddressForSymbol(returnArrayStack.peek()));

    mv.visitInsn(ICONST_0);
    returnIndexStack.push(new Symbol(null, UNDEFINED));
    mv.visitVarInsn(ISTORE, JVMInfo.getAddressForSymbol(returnIndexStack.peek()));

    mv.visitCode();
    final Label startLabel = new Label();
    mv.visitLabel(startLabel);

    funDeclNode.getCompoundNode().accept(this);

    returnIndexStack.pop();
    mv.visitVarInsn(ALOAD, JVMInfo.getAddressForSymbol(returnArrayStack.pop()));
    mv.visitInsn(ARETURN);
    final Label endLabel = new Label();
    mv.visitLabel(endLabel);
    int paramLoc = 0;
    for (IDNode param : funDeclNode.getParamList()) {
        if (param.getType() == DijkstraType.INT) {
            mv.visitLocalVariable(param.getName(), "J", null, startLabel, endLabel, paramLoc);
            paramLoc += 2;
        } else if (param.getType() == DijkstraType.FLOAT) {
            mv.visitLocalVariable(param.getName(), "D", null, startLabel, endLabel, paramLoc);
            paramLoc += 2;
        } else {
            mv.visitLocalVariable(param.getName(), "Z", null, startLabel, endLabel, paramLoc);
            paramLoc += 1;
        }
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    JVMInfo.exitScope();
    mvStack.pop();
    return null;
}

From source file:dodola.anole.lib.IncrementalSupportVisitor.java

License:Apache License

/***
 * Inserts a trampoline to this class so that the updated methods can make calls to
 * constructors./*from   w  ww .  j  av  a2 s  .co m*/
 * <p>
 * <p/>
 * Pseudo code for this trampoline:
 * <code>
 * ClassName(Object[] args, Marker unused) {
 * String name = (String) args[0];
 * if (name.equals(
 * "java/lang/ClassName.(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;")) {
 * this((String)arg[1], arg[2]);
 * return
 * }
 * if (name.equals("SuperClassName.(Ljava/lang/String;I)V")) {
 * super((String)arg[1], (int)arg[2]);
 * return;
 * }
 * ...
 * StringBuilder $local1 = new StringBuilder();
 * $local1.append("Method not found ");
 * $local1.append(name);
 * $local1.append(" in " $classType $super implementation");
 * throw new $package/InstantReloadException($local1.toString());
 * }
 * </code>
 */
private void createDispatchingThis() {
    // Gather all methods from itself and its superclasses to generate a giant constructor
    // implementation.
    // This will work fine as long as we don't support adding constructors to classes.
    final Map<String, MethodNode> uniqueMethods = new HashMap<String, MethodNode>();

    addAllNewConstructors(uniqueMethods, classNode, true /*keepPrivateConstructors*/);
    for (ClassNode parentNode : parentNodes) {
        addAllNewConstructors(uniqueMethods, parentNode, false /*keepPrivateConstructors*/);
    }

    int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC;

    Method m = new Method(AsmUtils.CONSTRUCTOR, ConstructorArgsRedirection.DISPATCHING_THIS_SIGNATURE);
    MethodVisitor visitor = super.visitMethod(0, m.getName(), m.getDescriptor(), null, null);
    final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor);

    mv.visitCode();
    // Mark this code as redirection code
    Label label = new Label();
    mv.visitLineNumber(0, label);

    // Get and store the constructor canonical name.
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.push(0);
    mv.visitInsn(Opcodes.AALOAD);
    mv.unbox(Type.getType("Ljava/lang/String;"));
    final int constructorCanonicalName = mv.newLocal(Type.getType("Ljava/lang/String;"));
    mv.storeLocal(constructorCanonicalName);

    new StringSwitch() {

        @Override
        void visitString() {
            mv.loadLocal(constructorCanonicalName);
        }

        @Override
        void visitCase(String canonicalName) {
            MethodNode methodNode = uniqueMethods.get(canonicalName);
            String owner = canonicalName.split("\\.")[0];

            // Parse method arguments and
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            Type[] args = Type.getArgumentTypes(methodNode.desc);
            int argc = 0;
            for (Type t : args) {
                mv.visitVarInsn(Opcodes.ALOAD, 1);
                mv.push(argc + 1);
                mv.visitInsn(Opcodes.AALOAD);
                ByteCodeUtils.unbox(mv, t);
                argc++;
            }

            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, owner, AsmUtils.CONSTRUCTOR, methodNode.desc, false);

            mv.visitInsn(Opcodes.RETURN);
        }

        @Override
        void visitDefault() {
            writeMissingMessageWithHash(mv, visitedClassName);
        }
    }.visit(mv, uniqueMethods.keySet());

    mv.visitMaxs(1, 3);
    mv.visitEnd();
}

From source file:dodola.anole.lib.Redirection.java

License:Apache License

/**
 * Adds the instructions to do a generic redirection.
 * <p/>/*from w ww  .jav a 2 s  .  co  m*/
 * Note that the generated bytecode does not have a direct translation to code, but as an
 * example, the following code block gets inserted.
 * <code>
 * if ($change != null) {
 * $change.access$dispatch($name, new object[] { arg0, ... argsN })
 * $anyCodeInsertedbyRestore
 * }
 * $originalMethodBody
 * </code>
 *
 * @param mv     the method visitor to add the instructions to.
 * @param change the local variable containing the alternate implementation.
 * @param args   the type of the local variable that need to be forwarded.
 */
void redirect(GeneratorAdapter mv, int change, List<Type> args) {
    // code to check if a new implementation of the current class is available.
    Label l0 = new Label();
    mv.loadLocal(change);
    mv.visitJumpInsn(Opcodes.IFNULL, l0);
    mv.loadLocal(change);
    mv.push(name);

    // create an array of objects capable of containing all the parameters and optionally the "this"
    createLocals(mv, args);

    // we need to maintain the stack index when loading parameters from, as for long and double
    // values, it uses 2 stack elements, all others use only 1 stack element.
    int stackIndex = 0;
    for (int arrayIndex = 0; arrayIndex < args.size(); arrayIndex++) {
        Type arg = args.get(arrayIndex);
        // duplicate the array of objects reference, it will be used to store the value in.
        mv.dup();
        // index in the array of objects to store the boxed parameter.
        mv.push(arrayIndex);
        // Pushes the appropriate local variable on the stack
        redirectLocal(mv, stackIndex, arg);
        // potentially box up intrinsic types.
        mv.box(arg);
        mv.arrayStore(Type.getType(Object.class));
        // stack index must progress according to the parameter type we just processed.
        stackIndex += arg.getSize();
    }

    // now invoke the generic dispatch method.
    mv.invokeInterface(IncrementalVisitor.CHANGE_TYPE,
            Method.getMethod("Object access$dispatch(String, Object[])"));

    // Restore the state after the redirection
    restore(mv, args);
    // jump label for classes without any new implementation, just invoke the original
    // method implementation.
    mv.visitLabel(l0);
}

From source file:dodola.anole.lib.StringSwitch.java

License:Apache License

/**
 * Emit code for a string if-else block.
 *
 *     if (s.equals("collided_method1")) {
 *         visit(s);//from w ww .java2  s. com
 *     } else if (s.equals("collided_method2")) {
 *         visit(s);
 *     }
 *
 * In the most common case of just one string, this degenerates to:
 *
 *      visit(s)
 *
 */
private void visitx(GeneratorAdapter mv, List<String> strings) {
    if (strings.size() == 1) {
        visitCase(strings.get(0));
        return;
    }
    for (int i = 0; i < strings.size(); ++i) {
        String string = strings.get(i);
        Label label = new Label();
        visitString();
        mv.visitLdcInsn(string);
        mv.invokeVirtual(STRING_TYPE, Method.getMethod("boolean equals(Object)"));
        mv.visitJumpInsn(Opcodes.IFEQ, label);
        visitCase(string);
        mv.visitLabel(label);
    }

    visitDefault();
}

From source file:dodola.anole.lib.StringSwitch.java

License:Apache License

/**
 * Emit code for a string switch for the given string classifier.
 *
 * switch(s.hashCode()) {//from  w  w w. j  av  a2s .  c o  m
 *   case 192: visitCase(s);
 *   case 312: visitCase(s);
 *   case 1024:
 *     if (s.equals("collided_method1")) {
 *         visit(s);
 *     } else if (s.equals("collided_method2")) {
 *         visit(s);
 *     }
 *     visitDefault();
 *   default:
 *     visitDefault();
 * }
 *
 **/
private void visitClassifier(GeneratorAdapter mv, Set<String> strings) {
    visitString();
    visitHashMethod(mv);

    // Group strings by hash code.
    Multimap<Integer, String> buckets = Multimaps.index(strings, hashMethod);
    List<Map.Entry<Integer, Collection<String>>> sorted = Ordering.natural()
            .onResultOf(new Function<Map.Entry<Integer, Collection<String>>, Integer>() {
                @Override
                public Integer apply(Map.Entry<Integer, Collection<String>> entry) {
                    return entry.getKey();
                }
            }).immutableSortedCopy(buckets.asMap().entrySet());

    int sortedHashes[] = new int[sorted.size()];
    List<String> sortedCases[] = new List[sorted.size()];
    int index = 0;
    for (Map.Entry<Integer, Collection<String>> entry : sorted) {
        sortedHashes[index] = entry.getKey();
        sortedCases[index] = Lists.newCopyOnWriteArrayList(entry.getValue());
        index++;
    }

    // Label for each hash and for default.
    Label labels[] = new Label[sorted.size()];
    Label defaultLabel = new Label();
    for (int i = 0; i < sorted.size(); ++i) {
        labels[i] = new Label();
    }

    // Create a switch that dispatches to each label from the hash code of
    mv.visitLookupSwitchInsn(defaultLabel, sortedHashes, labels);

    // Create the cases.
    for (int i = 0; i < sorted.size(); ++i) {
        mv.visitLabel(labels[i]);
        visitx(mv, sortedCases[i]);
    }
    mv.visitLabel(defaultLabel);
    visitDefault();
}

From source file:dyco4j.instrumentation.internals.TracingMethodVisitor.java

License:BSD License

@Override
public final void visitMaxs(final int maxStack, final int maxLocals) {
    endOutermostExceptionHandler();/*from ww  w  .  j  a v a 2s. c  o m*/
    for (final Map.Entry<Label, Label> _e : beginLabel2endLabel.entrySet()) {
        final Label _handlerLabel = new Label();
        super.visitLabel(_handlerLabel);
        super.visitTryCatchBlock(_e.getKey(), _e.getValue(), _handlerLabel, "java/lang/Throwable");
        LoggingHelper.emitLogException(this.mv);
        LoggingHelper.emitLogMethodExit(this.mv, this.methodId, LoggingHelper.ExitKind.EXCEPTIONAL);
        super.visitInsn(Opcodes.ATHROW);
    }
    super.visitMaxs(maxStack, maxLocals);
}

From source file:dyco4j.instrumentation.internals.TracingMethodVisitor.java

License:BSD License

void beginOutermostExceptionHandler() {
    this.outermostExceptionHandlerBeginLabel = new Label();
    super.visitLabel(this.outermostExceptionHandlerBeginLabel);
}

From source file:dyco4j.instrumentation.internals.TracingMethodVisitor.java

License:BSD License

void endOutermostExceptionHandler() {
    assert this.outermostExceptionHandlerBeginLabel != null;
    final Label _l = new Label();
    super.visitLabel(_l);
    this.beginLabel2endLabel.put(this.outermostExceptionHandlerBeginLabel, _l);
    this.outermostExceptionHandlerBeginLabel = null;
}