Example usage for org.objectweb.asm Opcodes GOTO

List of usage examples for org.objectweb.asm Opcodes GOTO

Introduction

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

Prototype

int GOTO

To view the source code for org.objectweb.asm Opcodes GOTO.

Click Source Link

Usage

From source file:com.khubla.jvmbasic.jvmbasicc.function.impl.rule.gotostmtRule.java

License:Open Source License

@Override
public boolean execute(GenerationContext generationContext) throws Exception {
    try {/*  w  w w . j  a  v a  2 s.c o m*/
        /*
         * we expect 2 arguments
         */
        if (generationContext.getParseTree().getChildCount() == 2) {
            /*
             * get the line number
             */
            final int lineNumber = Integer.parseInt(generationContext.getChildValue(1));
            /*
             * find the label for the line number
             */
            final Label label = GenerationContext.getProgramStaticAnalysis().getLinesDatabase()
                    .getLine(lineNumber).getLabel();
            /*
             * check
             */
            if (null != label) {
                /*
                 * generate the goto jmp to the label
                 */
                generationContext.getMethodVisitor().visitJumpInsn(Opcodes.GOTO, label);
            } else {
                throw new Exception("Could not find label for BASIC line number '" + lineNumber + "'");
            }
        }
        return true;
    } catch (final Exception e) {
        throw new Exception("Exception in execute", e);
    }
}

From source file:com.khubla.jvmbasic.jvmbasicc.JVMBasicCompiler.java

License:Open Source License

/**
 * generate void main(String[])/*from   w w w .j  av  a 2s .com*/
 * <p>
 * <code>
 * public static void main(String[] args) {
 *   ExampleProgram exampleProgram = new ExampleProgram();
 *  try {
 *       exampleProgram.inputStream = System.in;
 *       exampleProgram.outputStream = System.out;
 *       exampleProgram.program();
 *   } catch (Exception e) {
 *       e.printStackTrace();
 *    }
 *  }
 * </code>
 * </p>
 */
protected void generateMain(String classname, ClassWriter classWriter) throws Exception {
    try {
        /*
         * make method
         */
        final MethodVisitor methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC,
                "main", "([Ljava/lang/String;)V", null, null);
        methodVisitor.visitCode();
        final Label l0 = new Label();
        final Label l1 = new Label();
        final Label l2 = new Label();
        methodVisitor.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
        final Label l3 = new Label();
        methodVisitor.visitLabel(l3);
        /*
         * declare a local instance of the class in the void() main, store as variable 1.
         */
        methodVisitor.visitTypeInsn(Opcodes.NEW, classname);
        methodVisitor.visitInsn(Opcodes.DUP);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, classname, "<init>", "()V");
        methodVisitor.visitVarInsn(Opcodes.ASTORE, 1);
        /*
         * assign the input stream
         */
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "in", "Ljava/io/InputStream;");
        methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, classname, "inputStream", "Ljava/io/InputStream;");
        /*
         * assign the output stream
         */
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, classname, "outputStream", "Ljava/io/PrintStream;");
        /*
         * load the class instance from variable 1 and call "program"
         */
        methodVisitor.visitLabel(l0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classname, "program", "()V");
        methodVisitor.visitLabel(l1);
        final Label l4 = new Label();
        methodVisitor.visitJumpInsn(Opcodes.GOTO, l4);
        methodVisitor.visitLabel(l2);
        methodVisitor.visitFrame(Opcodes.F_FULL, 2, new Object[] { "[Ljava/lang/String;", classname }, 1,
                new Object[] { "java/lang/Exception" });
        methodVisitor.visitVarInsn(Opcodes.ASTORE, 2);
        final Label l5 = new Label();
        methodVisitor.visitLabel(l5);
        methodVisitor.visitLineNumber(21, l5);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 2);
        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "()V");
        /*
         * return
         */
        methodVisitor.visitLabel(l4);
        methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        methodVisitor.visitInsn(Opcodes.RETURN);
        /*
         * declare the parameters
         */
        final Label l6 = new Label();
        methodVisitor.visitLabel(l6);
        methodVisitor.visitLocalVariable("args", "[Ljava/lang/String;", null, l3, l6, 0);
        methodVisitor.visitLocalVariable("exampleProgram", "L" + classname + ";", null, l0, l6, 1);
        methodVisitor.visitLocalVariable("e", "Ljava/lang/Exception;", null, l5, l4, 2);
        /*
         * done
         */
        methodVisitor.visitMaxs(2, 3);
        methodVisitor.visitEnd();
    } catch (final Exception e) {
        throw new Exception("Exception in generateMain", e);
    }
}

From source file:com.mebigfatguy.exagent.StackTraceMethodVisitor.java

License:Apache License

@Override
public void visitInsn(int opcode) {

    if (RETURN_CODES.get(opcode)) {
        super.visitVarInsn(Opcodes.ILOAD, depthLocalSlot);
        super.visitMethodInsn(Opcodes.INVOKESTATIC, EXASUPPORT_CLASS_NAME, "popMethodInfo", "(I)V", false);
    } else if (opcode == Opcodes.ATHROW) {

        super.visitVarInsn(Opcodes.ASTORE, exLocalSlot);

        Label tryLabel = new Label();
        Label endTryLabel = new Label();
        Label catchLabel = new Label();
        Label continueLabel = new Label();

        super.visitTryCatchBlock(tryLabel, endTryLabel, catchLabel, EXCEPTION_CLASS_NAME);

        super.visitLabel(tryLabel);

        super.visitVarInsn(Opcodes.ALOAD, exLocalSlot);
        super.visitMethodInsn(Opcodes.INVOKESTATIC, EXASUPPORT_CLASS_NAME, "embellishMessage",
                "(Ljava/lang/Throwable;)V", false);
        super.visitVarInsn(Opcodes.ILOAD, depthLocalSlot);
        super.visitMethodInsn(Opcodes.INVOKESTATIC, EXASUPPORT_CLASS_NAME, "popMethodInfo", "(I)V", false);

        super.visitJumpInsn(Opcodes.GOTO, continueLabel);
        super.visitLabel(endTryLabel);

        super.visitLabel(catchLabel);
        super.visitInsn(Opcodes.POP);

        super.visitLabel(continueLabel);
        super.visitVarInsn(Opcodes.ALOAD, exLocalSlot);
    }/*from w  ww. j a  v  a2  s .c o  m*/
    super.visitInsn(opcode);
}

From source file:com.mebigfatguy.junitflood.jvm.OperandStack.java

License:Apache License

public void performJumpInsn(int opcode, Label label) {
    switch (opcode) {
    case Opcodes.IFEQ:
    case Opcodes.IFNE:
    case Opcodes.IFLT:
    case Opcodes.IFGE:
    case Opcodes.IFGT:
    case Opcodes.IFLE:
    case Opcodes.IFNULL:
    case Opcodes.IFNONNULL:
        pop();//from w  w  w .j  a v  a 2s .c  om
        break;

    case Opcodes.IF_ICMPEQ:
    case Opcodes.IF_ICMPNE:
    case Opcodes.IF_ICMPLT:
    case Opcodes.IF_ICMPGE:
    case Opcodes.IF_ICMPGT:
    case Opcodes.IF_ICMPLE:
    case Opcodes.IF_ACMPEQ:
    case Opcodes.IF_ACMPNE:
        pop2();
        break;

    case Opcodes.GOTO:
        //nop
        break;

    case Opcodes.JSR:
        //nop -- a fudge
        break;
    }
}

From source file:com.nginious.http.xsp.expr.AndOperator.java

License:Apache License

/**
 * Creates bytecode for evaluating this and operator. The bytecode is generated using
 * the specified method visitor. The result of the evaluation produces the specified
 * type.//from  www.j  av  a 2  s.com
 * 
 * @param visitor the method visitor
 * @param type the type
 */
void compile(MethodVisitor visitor, Type type) {
    Label labelFalse = new Label();
    Label labelEnd = new Label();

    // Test first value
    value1.compile(visitor, type);
    visitor.visitInsn(Opcodes.ICONST_1);
    visitor.visitJumpInsn(Opcodes.IF_ICMPNE, labelFalse);

    // Test second value
    value2.compile(visitor, type);
    visitor.visitInsn(Opcodes.ICONST_1);
    visitor.visitJumpInsn(Opcodes.IF_ICMPNE, labelFalse);

    // True
    visitor.visitLdcInsn(true);
    visitor.visitJumpInsn(Opcodes.GOTO, labelEnd);

    // False
    visitor.visitLabel(labelFalse);
    visitor.visitLdcInsn(false);

    visitor.visitLabel(labelEnd);
}

From source file:com.nginious.http.xsp.expr.AttributeValue.java

License:Apache License

/**
 * Creates bytecode for evaluating this attribute value. The bytecode is created using the
 * specified method visitor and creates a result of specified type.
 * /*  w  w w  .  j  a  v  a 2s.c o m*/
 * @param visitor the method visitor
 * @param type the type
 */
void compile(MethodVisitor visitor, Type type) {
    visitor.visitLdcInsn(this.name);
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "getVariable",
            "(Ljava/lang/String;)Ljava/lang/Object;");

    if (type != Type.ANY) {
        visitor.visitInsn(Opcodes.DUP);
    }

    if (type == Type.STRING) {
        Label nullLabel = new Label();
        Label notNullLabel = new Label();

        visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

        visitor.visitLabel(nullLabel);
        visitor.visitInsn(Opcodes.POP);
        visitor.visitInsn(Opcodes.ACONST_NULL);

        visitor.visitLabel(notNullLabel);
    } else if (type == Type.INT) {
        Label nullLabel = new Label();
        Label notNullLabel = new Label();

        visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I");
        visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

        visitor.visitLabel(nullLabel);
        visitor.visitInsn(Opcodes.POP);
        visitor.visitLdcInsn(0);

        visitor.visitLabel(notNullLabel);
    } else if (type == Type.DOUBLE) {
        Label nullLabel = new Label();
        Label notNullLabel = new Label();

        visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "parseDouble",
                "(Ljava/lang/String;)D");
        visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

        visitor.visitLabel(nullLabel);
        visitor.visitInsn(Opcodes.POP);
        visitor.visitLdcInsn(0.0d);

        visitor.visitLabel(notNullLabel);
    }
}

From source file:com.nginious.http.xsp.expr.BeanValue.java

License:Apache License

/**
 * Creates bytecode for evaluating this bean value. The bytecode is generated using the
 * specified method visitor and produces a value of the specified type.
 * //from w w w  .j a v a2  s  .co  m
 * @param visitor the method visitor
 * @param type the type
 */
void compile(MethodVisitor visitor, Type type) {
    visitor.visitLdcInsn(this.beanName);
    visitor.visitLdcInsn(this.propertyName);
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/BeanValue", "getValue",
            "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");

    if (type != Type.ANY) {
        visitor.visitInsn(Opcodes.DUP);
    }

    // visitor.visitJumpInsn(Opcodes.IFNONNULL, null);

    if (type == Type.DOUBLE) {
        Label nullLabel = new Label();
        Label notNullLabel = new Label();

        visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "parseDouble",
                "(Ljava/lang/String;)D");
        visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

        visitor.visitLabel(nullLabel);
        visitor.visitInsn(Opcodes.POP);
        visitor.visitLdcInsn(0.0d);

        visitor.visitLabel(notNullLabel);
    } else if (type == Type.INT) {
        Label nullLabel = new Label();
        Label notNullLabel = new Label();

        visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I");
        visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

        visitor.visitLabel(nullLabel);
        visitor.visitInsn(Opcodes.POP);
        visitor.visitLdcInsn(0);

        visitor.visitLabel(notNullLabel);
    } else if (type == Type.STRING) {
        Label nullLabel = new Label();
        Label notNullLabel = new Label();

        visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

        visitor.visitLabel(nullLabel);
        visitor.visitInsn(Opcodes.POP);
        visitor.visitInsn(Opcodes.ACONST_NULL);

        visitor.visitLabel(notNullLabel);
    }
}

From source file:com.nginious.http.xsp.expr.EqualsOperator.java

License:Apache License

/**
 * Compiles bytecode for evaluating this equals operator producing
 * a string as result. The specified method visitor is used for generating
 * bytecode.//from  w  w  w .  j  a  va  2  s. com
 * 
 * @param visitor the method visitor
 */
private void compileString(MethodVisitor visitor) {
    value1.compile(visitor, Type.STRING);
    visitor.visitVarInsn(Opcodes.ASTORE, 1);
    value2.compile(visitor, Type.STRING);
    visitor.visitVarInsn(Opcodes.ASTORE, 2);
    Label nullLabel = new Label();
    Label notNullLabel = new Label();

    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z");
    visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

    visitor.visitLabel(nullLabel);
    visitor.visitLdcInsn(false);

    visitor.visitLabel(notNullLabel);
}

From source file:com.nginious.http.xsp.expr.EqualsOperator.java

License:Apache License

/**
 * Compiles bytecode for evaluating this equals operator producing
 * an integer as result. The specified method visitor is used for generating
 * bytecode./*from   w w  w  .  j av a2s  .co m*/
 * 
 * @param visitor the method visitor
 */
private void compileInt(MethodVisitor visitor) {
    Label trueLabel = new Label();
    Label falseLabel = new Label();

    value1.compile(visitor, Type.INT);
    value2.compile(visitor, Type.INT);
    visitor.visitJumpInsn(Opcodes.IF_ICMPEQ, trueLabel);

    visitor.visitLdcInsn(false);
    visitor.visitJumpInsn(Opcodes.GOTO, falseLabel);

    visitor.visitLabel(trueLabel);
    visitor.visitLdcInsn(true);

    visitor.visitLabel(falseLabel);
}

From source file:com.nginious.http.xsp.expr.EqualsOperator.java

License:Apache License

/**
 * Compiles bytecode for evaluating this equals operator producing
 * a double as result. The specified method visitor is used for generating
 * bytecode.//from  www .j a v  a2 s . c om
 * 
 * @param visitor the method visitor
 */
private void compileDouble(MethodVisitor visitor) {
    Label trueLabel = new Label();
    Label falseLabel = new Label();

    value1.compile(visitor, Type.DOUBLE);
    value2.compile(visitor, Type.DOUBLE);
    visitor.visitInsn(Opcodes.DCMPL);
    visitor.visitJumpInsn(Opcodes.IFEQ, trueLabel);

    visitor.visitLdcInsn(false);
    visitor.visitJumpInsn(Opcodes.GOTO, falseLabel);

    visitor.visitLabel(trueLabel);
    visitor.visitLdcInsn(true);

    visitor.visitLabel(falseLabel);
}