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.github.malamut2.low.AllocationMethodAdapter.java

License:Apache License

void calculateArrayLengthAndDispatch(String typeName, int dimCount) {
    // Since the dimensions of the array are not known at instrumentation
    // time, we take the created multi-dimensional array and peel off nesting
    // levels from the left.  For each nesting layer we probe the array length
    // and accumulate a partial product which we can then feed the recording
    // function./* w  w w .ja va  2s .  c  o  m*/

    // below we note the partial product of dimensions 1 to X-1 as productToX
    // (so productTo1 == 1 == no dimensions yet).  We denote by aref0 the
    // array reference at the current nesting level (the containing aref's [0]
    // element).  If we hit a level whose arraylength is 0 there's no point
    // continuing so we shortcut out.
    Label zeroDimension = new Label();
    super.visitInsn(Opcodes.DUP); // -> stack: ... origaref aref0
    super.visitLdcInsn(1); // -> stack: ... origaref aref0 productTo1
    for (int i = 0; i < dimCount; ++i) {
        // pre: stack: ... origaref aref0 productToI
        super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref productToI aref
        super.visitInsn(Opcodes.DUP_X1);
        // -> stack: ... origaref aref0 productToI aref
        super.visitInsn(Opcodes.ARRAYLENGTH);
        // -> stack: ... origaref aref0 productToI dimI

        Label nonZeroDimension = new Label();
        super.visitInsn(Opcodes.DUP);
        // -> stack: ... origaref aref0 productToI dimI dimI
        super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
        // -> stack: ... origaref aref0 productToI dimI
        super.visitInsn(Opcodes.POP);
        // -> stack: ... origaref aref0 productToI
        super.visitJumpInsn(Opcodes.GOTO, zeroDimension);
        super.visitLabel(nonZeroDimension);
        // -> stack: ... origaref aref0 productToI max(dimI,1)

        super.visitInsn(Opcodes.IMUL);
        // -> stack: ... origaref aref0 productTo{I+1}
        if (i < dimCount - 1) {
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... origaref productTo{I+1} aref0
            super.visitInsn(Opcodes.ICONST_0);
            // -> stack: ... origaref productTo{I+1} aref0 0
            super.visitInsn(Opcodes.AALOAD);
            // -> stack: ... origaref productTo{I+1} aref0'
            super.visitInsn(Opcodes.SWAP);
        }
        // post: stack: ... origaref aref0 productTo{I+1}
    }
    super.visitLabel(zeroDimension);

    super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref product aref0
    super.visitInsn(Opcodes.POP); // -> stack: ... origaref product
    super.visitInsn(Opcodes.SWAP); // -> stack: ... product origaref
    invokeRecordAllocation(typeName);
}

From source file:com.google.code.jconts.instrument.gen.AsyncMethodAdapter.java

License:Apache License

private void checkProlog() {
    // Generate only once (at the beginning)
    if (prologGenerated) {
        return;/*from  w w w. j av a2  s.  co  m*/
    }
    prologGenerated = true;

    // try...catch around the whole body. Should be added last (after all
    // other try..catch blocks).
    // Actually, we are making an assumption that reader will emit
    // visitTryCatchBlock BEFORE generating any bytecode.
    // We cannot call visitTryCatchBlock at the end since it should
    // be called before all its labels are visited (which we generate
    // in this block).
    mv.visitTryCatchBlock(tryLabel, dispatchLabel, catchLabel, null);

    // Goto to table switch dispatcher
    mv.visitJumpInsn(Opcodes.GOTO, dispatchLabel);

    // Wrap the whole original body with try...catch that will invoke
    // cont.setException(Throwable t)
    mv.visitLabel(tryLabel);

    // Restore the frame as it was before changing method arguments
    Type[] locs = info.entryLocals;
    Object[] frame = new Object[info.isStatic() ? locs.length : locs.length + 1];

    // Frame
    if (!info.isStatic()) {
        frame[0] = info.owner;
    }

    int off = info.isStatic() ? 0 : 1;
    for (int i = 0; i < locs.length; ++i) {
        frame[off + i] = Frames.toFrameType(locs[i]);
    }

    mv.visitFrame(Opcodes.F_FULL, frame.length, frame, 0, new Object[0]);
    Label label = new Label();
    dispatchTable.add(label);
    mv.visitLabel(label);

    if (!info.isStatic()) {
        locals.add(Type.getObjectType(info.owner));
    }
    Collections.addAll(locals, locs);

    // Restore the original method arguments
    // argX = state.varX
    String[] names = info.entryLocalsVars;
    for (int i = 0; i < info.entryLocals.length; ++i) {
        // We go directly to target, introduced var used
        target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
        mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, names[i], info.entryLocals[i].getDescriptor());
        mv.visitVarInsn(info.entryLocals[i].getOpcode(Opcodes.ISTORE), info.isStatic() ? i : i + 1);

    }
}

From source file:com.google.devtools.build.android.desugar.BytecodeTypeInference.java

License:Open Source License

@Override
public void visitJumpInsn(int opcode, Label label) {
    switch (opcode) {
    case Opcodes.IFEQ:
    case Opcodes.IFNE:
    case Opcodes.IFLT:
    case Opcodes.IFGE:
    case Opcodes.IFGT:
    case Opcodes.IFLE:
        pop();//  w  ww.j  av a  2 s.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:
        pop(2);
        break;
    case Opcodes.GOTO:
        break;
    case Opcodes.JSR:
        throw new RuntimeException("The JSR instruction is not supported.");
    case Opcodes.IFNULL:
    case Opcodes.IFNONNULL:
        pop(1);
        break;
    default:
        throw new RuntimeException("Unhandled opcode " + opcode);
    }
    super.visitJumpInsn(opcode, label);
}

From source file:com.google.devtools.build.lib.syntax.compiler.Jump.java

License:Open Source License

/**
 * Builds an unconditional jump to the target label.
 *///  w w w .  ja v a  2  s .  c  o m
public static Jump to(Label target) {
    return new Jump(Opcodes.GOTO, target, new Size(0, 0));
}

From source file:com.google.gwtorm.schema.sql.SqlBooleanTypeInfo.java

License:Apache License

@Override
public void generatePreparedStatementSet(final CodeGenSupport cgs) {
    cgs.pushSqlHandle();/*  w w w .  j a v  a2  s. com*/
    cgs.pushColumnIndex();
    cgs.pushFieldValue();

    final Label useNo = new Label();
    final Label end = new Label();
    cgs.mv.visitJumpInsn(Opcodes.IFEQ, useNo);
    cgs.mv.visitLdcInsn(getTrueValue());
    cgs.mv.visitJumpInsn(Opcodes.GOTO, end);
    cgs.mv.visitLabel(useNo);
    cgs.mv.visitLdcInsn(getFalseValue());
    cgs.mv.visitLabel(end);
    cgs.invokePreparedStatementSet(getJavaSqlTypeAlias());
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

private void pushProductOfIntArrayOnStack() {
    final Label beginScopeLabel = new Label();
    final Label endScopeLabel = new Label();

    final int dimsArrayIndex = newLocal("[I", beginScopeLabel, endScopeLabel);
    final int counterIndex = newLocal("I", beginScopeLabel, endScopeLabel);
    final int productIndex = newLocal("I", beginScopeLabel, endScopeLabel);
    final Label loopLabel = new Label();
    final Label endLabel = new Label();

    super.visitLabel(beginScopeLabel);

    // stack: ... intArray
    super.visitVarInsn(Opcodes.ASTORE, dimsArrayIndex);
    // -> stack: ...

    // counter = 0
    super.visitInsn(Opcodes.ICONST_0);
    super.visitVarInsn(Opcodes.ISTORE, counterIndex);
    // product = 1
    super.visitInsn(Opcodes.ICONST_1);
    super.visitVarInsn(Opcodes.ISTORE, productIndex);
    // loop://from w w w.j  a  v a 2  s. c o m
    super.visitLabel(loopLabel);
    // if index >= arraylength goto end:
    super.visitVarInsn(Opcodes.ILOAD, counterIndex);
    super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
    super.visitInsn(Opcodes.ARRAYLENGTH);
    super.visitJumpInsn(Opcodes.IF_ICMPGE, endLabel);
    // product = product * max(array[counter],1)
    super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
    super.visitVarInsn(Opcodes.ILOAD, counterIndex);
    super.visitInsn(Opcodes.IALOAD);
    super.visitInsn(Opcodes.DUP);
    final Label nonZeroDimension = new Label();
    super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
    super.visitInsn(Opcodes.POP);
    super.visitInsn(Opcodes.ICONST_1);
    super.visitLabel(nonZeroDimension);
    super.visitVarInsn(Opcodes.ILOAD, productIndex);
    super.visitInsn(Opcodes.IMUL); // if overflow happens it happens.
    super.visitVarInsn(Opcodes.ISTORE, productIndex);
    // iinc counter 1
    super.visitIincInsn(counterIndex, 1);
    // goto loop
    super.visitJumpInsn(Opcodes.GOTO, loopLabel);
    // end:
    super.visitLabel(endLabel);
    // re-push dimensions array
    super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
    // push product
    super.visitVarInsn(Opcodes.ILOAD, productIndex);

    super.visitLabel(endScopeLabel);
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

void calculateArrayLengthAndDispatch(final String typeName, final int dimCount) {
    // Since the dimensions of the array are not known at instrumentation
    // time, we take the created multi-dimensional array and peel off nesting
    // levels from the left.  For each nesting layer we probe the array length
    // and accumulate a partial product which we can then feed the recording
    // function.//  w  w w  . j  ava2 s  .  c  o  m

    // below we note the partial product of dimensions 1 to X-1 as productToX
    // (so productTo1 == 1 == no dimensions yet).  We denote by aref0 the
    // array reference at the current nesting level (the containing aref's [0]
    // element).  If we hit a level whose arraylength is 0 there's no point
    // continuing so we shortcut out.
    final Label zeroDimension = new Label();
    super.visitInsn(Opcodes.DUP); // -> stack: ... origaref aref0
    super.visitLdcInsn(1); // -> stack: ... origaref aref0 productTo1
    for (int i = 0; i < dimCount; ++i) {
        // pre: stack: ... origaref aref0 productToI
        super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref productToI aref
        super.visitInsn(Opcodes.DUP_X1);
        // -> stack: ... origaref aref0 productToI aref
        super.visitInsn(Opcodes.ARRAYLENGTH);
        // -> stack: ... origaref aref0 productToI dimI

        final Label nonZeroDimension = new Label();
        super.visitInsn(Opcodes.DUP);
        // -> stack: ... origaref aref0 productToI dimI dimI
        super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
        // -> stack: ... origaref aref0 productToI dimI
        super.visitInsn(Opcodes.POP);
        // -> stack: ... origaref aref0 productToI
        super.visitJumpInsn(Opcodes.GOTO, zeroDimension);
        super.visitLabel(nonZeroDimension);
        // -> stack: ... origaref aref0 productToI max(dimI,1)

        super.visitInsn(Opcodes.IMUL);
        // -> stack: ... origaref aref0 productTo{I+1}
        if (i < dimCount - 1) {
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... origaref productTo{I+1} aref0
            super.visitInsn(Opcodes.ICONST_0);
            // -> stack: ... origaref productTo{I+1} aref0 0
            super.visitInsn(Opcodes.AALOAD);
            // -> stack: ... origaref productTo{I+1} aref0'
            super.visitInsn(Opcodes.SWAP);
        }
        // post: stack: ... origaref aref0 productTo{I+1}
    }
    super.visitLabel(zeroDimension);

    super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref product aref0
    super.visitInsn(Opcodes.POP); // -> stack: ... origaref product
    super.visitInsn(Opcodes.SWAP); // -> stack: ... product origaref
    invokeRecordAllocation(typeName);
}

From source file:com.google.template.soy.jbcsrc.BytecodeUtils.java

License:Apache License

/**
 * Returns an expression that evaluates equivalently to a java ternary expression: 
 * {@code condition ? left : right}// w w w .j  ava  2 s.  co m
 */
static Expression ternary(final Expression condition, final Expression trueBranch,
        final Expression falseBranch) {
    checkArgument(condition.resultType().equals(Type.BOOLEAN_TYPE));
    checkArgument(trueBranch.resultType().getSort() == falseBranch.resultType().getSort());
    Features features = Features.of();
    if (Expression.areAllCheap(condition, trueBranch, falseBranch)) {
        features = features.plus(Feature.CHEAP);
    }
    if (trueBranch.isNonNullable() && falseBranch.isNonNullable()) {
        features = features.plus(Feature.NON_NULLABLE);
    }
    return new Expression(trueBranch.resultType(), features) {
        @Override
        void doGen(CodeBuilder mv) {
            condition.gen(mv);
            Label ifFalse = new Label();
            Label end = new Label();
            mv.visitJumpInsn(Opcodes.IFEQ, ifFalse); // if 0 goto ifFalse
            trueBranch.gen(mv); // eval true branch
            mv.visitJumpInsn(Opcodes.GOTO, end); // jump to the end
            mv.visitLabel(ifFalse);
            falseBranch.gen(mv); // eval false branch
            mv.visitLabel(end);
        }
    };
}

From source file:com.google.template.soy.jbcsrc.restricted.BytecodeUtils.java

License:Apache License

/**
 * Returns an expression that evaluates equivalently to a java ternary expression: {@code
 * condition ? left : right}//from www  .j  a  v  a 2s  .  c  o  m
 */
public static Expression ternary(final Expression condition, final Expression trueBranch,
        final Expression falseBranch) {
    checkArgument(condition.resultType().equals(Type.BOOLEAN_TYPE));
    checkArgument(trueBranch.resultType().getSort() == falseBranch.resultType().getSort());
    Features features = Features.of();
    if (Expression.areAllCheap(condition, trueBranch, falseBranch)) {
        features = features.plus(Feature.CHEAP);
    }
    if (trueBranch.isNonNullable() && falseBranch.isNonNullable()) {
        features = features.plus(Feature.NON_NULLABLE);
    }
    return new Expression(trueBranch.resultType(), features) {
        @Override
        protected void doGen(CodeBuilder mv) {
            condition.gen(mv);
            Label ifFalse = new Label();
            Label end = new Label();
            mv.visitJumpInsn(Opcodes.IFEQ, ifFalse); // if 0 goto ifFalse
            trueBranch.gen(mv); // eval true branch
            mv.visitJumpInsn(Opcodes.GOTO, end); // jump to the end
            mv.visitLabel(ifFalse);
            falseBranch.gen(mv); // eval false branch
            mv.visitLabel(end);
        }
    };
}

From source file:com.google.test.metric.asm.MethodVisitorBuilder.java

License:Apache License

public void visitJumpInsn(final int opcode, final Label label) {
    if (opcode == Opcodes.GOTO) {
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Transform(lineNumber, "GOTO", null, null, null));
                block.unconditionalGoto(label);
            }//  w ww .  j  av  a2s  . co  m
        });
    } else if (opcode == Opcodes.JSR) {
        recorder.add(new Runnable() {
            public void run() {
                block.jumpSubroutine(label, lineNumber);
            }
        });
    } else {
        recorder.add(new Runnable() {
            public void run() {
                cyclomaticComplexity.add(lineNumber);
                switch (opcode) {
                case Opcodes.IFEQ:
                    if1("IFEQ");
                    break;
                case Opcodes.IFNE:
                    if1("IFNE");
                    break;
                case Opcodes.IFLT:
                    if1("IFLT");
                    break;
                case Opcodes.IFGE:
                    if1("IFGE");
                    break;
                case Opcodes.IFGT:
                    if1("IFGT");
                    break;
                case Opcodes.IFLE:
                    if1("IFLE");
                    break;
                case Opcodes.IFNONNULL:
                    if1("IFNONNULL");
                    break;
                case Opcodes.IFNULL:
                    if1("IFNULL");
                    break;
                case Opcodes.IF_ACMPEQ:
                    if2("IF_ACMPEQ");
                    break;
                case Opcodes.IF_ACMPNE:
                    if2("IF_ACMPNE");
                    break;
                case Opcodes.IF_ICMPEQ:
                    if2("IF_ICMPEQ");
                    break;
                case Opcodes.IF_ICMPGE:
                    if2("IF_ICMPGE");
                    break;
                case Opcodes.IF_ICMPGT:
                    if2("IF_ICMPGT");
                    break;
                case Opcodes.IF_ICMPLE:
                    if2("IF_ICMPLE");
                    break;
                case Opcodes.IF_ICMPLT:
                    if2("IF_ICMPLT");
                    break;
                case Opcodes.IF_ICMPNE:
                    if2("IF_ICMPNE");
                    break;
                default:
                    throw new UnsupportedOperationException("" + opcode);
                }
                block.conditionalGoto(label);
            }

            private void if1(String name) {
                block.addOp(new Transform(lineNumber, name, JavaType.INT, null, null));
            }

            private void if2(String name) {
                block.addOp(new Transform(lineNumber, name, JavaType.INT, JavaType.INT, null));
            }
        });
    }
}