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:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

@Override
public void visit(FloatLiteralExpression node) throws ASTVisitorException {
    if (ASTUtils.isBooleanExpression(node)) {
        JumpInsnNode i = new JumpInsnNode(Opcodes.GOTO, null);
        mn.instructions.add(i);/*ww w .jav a2s  .  co  m*/
        if (node.getLiteral() != 0) {
            ASTUtils.getTrueList(node).add(i);
        } else {
            ASTUtils.getFalseList(node).add(i);
        }
    } else {
        Float d = node.getLiteral();
        mn.instructions.add(new LdcInsnNode((d)));
    }
}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

@Override
public void visit(IfElseStatement node) throws ASTVisitorException {
    ASTUtils.setBooleanExpression(node.getExpression(), true);
    node.getExpression().accept(this);

    LabelNode stmt1StartLabelNode = new LabelNode();
    mn.instructions.add(stmt1StartLabelNode);
    node.getStatement1().accept(this);

    JumpInsnNode skipGoto = new JumpInsnNode(Opcodes.GOTO, null);
    mn.instructions.add(skipGoto);/*from w  w  w .j  a  v  a  2 s .  c om*/

    LabelNode stmt2StartLabelNode = new LabelNode();
    mn.instructions.add(stmt2StartLabelNode);
    node.getStatement2().accept(this);

    backpatch(ASTUtils.getTrueList(node.getExpression()), stmt1StartLabelNode);
    backpatch(ASTUtils.getFalseList(node.getExpression()), stmt2StartLabelNode);

    ASTUtils.getNextList(node).addAll(ASTUtils.getNextList(node.getStatement1()));
    ASTUtils.getNextList(node).addAll(ASTUtils.getNextList(node.getStatement2()));
    ASTUtils.getNextList(node).add(skipGoto);

    ASTUtils.getBreakList(node).addAll(ASTUtils.getBreakList(node.getStatement1()));
    ASTUtils.getBreakList(node).addAll(ASTUtils.getBreakList(node.getStatement2()));

    ASTUtils.getContinueList(node).addAll(ASTUtils.getContinueList(node.getStatement1()));
    ASTUtils.getContinueList(node).addAll(ASTUtils.getContinueList(node.getStatement2()));
}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

@Override
public void visit(IntegerLiteralExpression node) throws ASTVisitorException {
    if (ASTUtils.isBooleanExpression(node)) {
        JumpInsnNode i = new JumpInsnNode(Opcodes.GOTO, null);
        mn.instructions.add(i);/*from  w w  w. j av a2  s  .  c o m*/
        if (node.getLiteral() != 0) {
            ASTUtils.getTrueList(node).add(i);
        } else {
            ASTUtils.getFalseList(node).add(i);
        }
    } else {
        Integer d = node.getLiteral();
        mn.instructions.add(new LdcInsnNode(d));
    }
}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

@Override
public void visit(WhileStatement node) throws ASTVisitorException {
    ASTUtils.setBooleanExpression(node.getExpression(), true);

    LabelNode beginLabelNode = new LabelNode();
    mn.instructions.add(beginLabelNode);

    node.getExpression().accept(this);

    LabelNode trueLabelNode = new LabelNode();
    mn.instructions.add(trueLabelNode);/*from www .ja  v a2  s .  c  o m*/
    backpatch(ASTUtils.getTrueList(node.getExpression()), trueLabelNode);

    node.getStatement().accept(this);

    backpatch(ASTUtils.getNextList(node.getStatement()), beginLabelNode);
    backpatch(ASTUtils.getContinueList(node.getStatement()), beginLabelNode);

    mn.instructions.add(new JumpInsnNode(Opcodes.GOTO, beginLabelNode));

    ASTUtils.getNextList(node).addAll(ASTUtils.getFalseList(node.getExpression()));
    ASTUtils.getNextList(node).addAll(ASTUtils.getBreakList(node.getStatement()));
}

From source file:org.jacoco.core.instr.ResizeInstructionsTest.java

License:Open Source License

/**
 * Adds code that requires//from  w w w .  java2 s  .c o m
 * {@link ClassWriter#getCommonSuperClass(String, String)}.
 *
 * <pre>
 * Object o = this;
 * while (true) {
 *    o = (Integer) null;
 * }
 * </pre>
 */
private static void addCauseOfGetCommonSuperClass(final MethodVisitor mv) {
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ASTORE, 1);
    Label label = new Label();
    mv.visitLabel(label);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Integer");
    mv.visitVarInsn(Opcodes.ASTORE, 1);
    mv.visitJumpInsn(Opcodes.GOTO, label);
}

From source file:org.jacoco.core.internal.analysis.AC_MethodAnalyzerTest.java

License:Open Source License

private void createJumpBackwards() {
    final Label l0 = new Label();
    method.visitLabel(l0);//from   w ww.java  2s.c o m
    method.visitLineNumber(1001, l0);
    final Label l1 = new Label();
    method.visitJumpInsn(Opcodes.GOTO, l1);
    final Label l2 = new Label();
    method.visitLabel(l2);
    method.visitLineNumber(1002, l2);
    method.visitInsn(Opcodes.RETURN);
    method.visitLabel(l1);
    method.visitLineNumber(1003, l1);
    method.visitJumpInsn(Opcodes.GOTO, l2);
}

From source file:org.jacoco.core.internal.analysis.AC_MethodAnalyzerTest.java

License:Open Source License

private void createLoop() {
    final Label l0 = new Label();
    method.visitLabel(l0);/*w  ww .j  a v  a 2s .c  o  m*/
    method.visitLineNumber(1001, l0);
    method.visitInsn(Opcodes.ICONST_0);
    method.visitVarInsn(Opcodes.ISTORE, 1);
    final Label l1 = new Label();
    method.visitLabel(l1);
    method.visitLineNumber(1002, l1);
    method.visitVarInsn(Opcodes.ILOAD, 1);
    method.visitInsn(Opcodes.ICONST_5);
    final Label l2 = new Label();
    method.visitJumpInsn(Opcodes.IF_ICMPGE, l2);
    method.visitIincInsn(1, 1);
    method.visitJumpInsn(Opcodes.GOTO, l1);
    method.visitLabel(l2);
    method.visitLineNumber(1003, l2);
    method.visitInsn(Opcodes.RETURN);
}

From source file:org.jacoco.core.internal.analysis.filter.FinallyFilter.java

License:Open Source License

private static void filter(final IFilterOutput output, final List<TryCatchBlockNode> tryCatchBlocks,
        final TryCatchBlockNode catchAnyBlock) {
    final AbstractInsnNode e = next(catchAnyBlock.handler);
    final int size = size(e);
    if (size <= 0) {
        return;//from  w  w  w. ja  v a 2 s  .  c  o m
    }

    // Determine instructions inside regions
    final Set<AbstractInsnNode> inside = new HashSet<AbstractInsnNode>();
    for (final TryCatchBlockNode t : tryCatchBlocks) {
        if (t.handler == catchAnyBlock.handler) {
            AbstractInsnNode i = t.start;
            while (i != t.end) {
                inside.add(i);
                i = i.getNext();
            }
        }
    }

    // Find and merge duplicates at exits of regions
    for (final TryCatchBlockNode t : tryCatchBlocks) {
        if (t.handler == catchAnyBlock.handler) {
            boolean continues = false;
            AbstractInsnNode i = t.start;

            while (i != t.end) {
                switch (i.getType()) {
                case AbstractInsnNode.FRAME:
                case AbstractInsnNode.LINE:
                case AbstractInsnNode.LABEL:
                    break;
                case AbstractInsnNode.JUMP_INSN:
                    final AbstractInsnNode jumpTarget = next(((JumpInsnNode) i).label);
                    if (!inside.contains(jumpTarget)) {
                        merge(output, size, e, jumpTarget);
                    }
                    continues = i.getOpcode() != Opcodes.GOTO;
                    break;
                default:
                    switch (i.getOpcode()) {
                    case Opcodes.IRETURN:
                    case Opcodes.LRETURN:
                    case Opcodes.FRETURN:
                    case Opcodes.DRETURN:
                    case Opcodes.ARETURN:
                    case Opcodes.RETURN:
                    case Opcodes.ATHROW:
                        continues = false;
                        break;
                    default:
                        continues = true;
                        break;
                    }
                    break;
                }
                i = i.getNext();
            }

            i = next(i);
            if (continues && !inside.contains(i)) {
                merge(output, size, e, i);
            }
        }

        if (t != catchAnyBlock && t.start == catchAnyBlock.start && t.end == catchAnyBlock.end) {
            final AbstractInsnNode i = next(next(t.handler));
            if (!inside.contains(i)) {
                // javac's empty catch - merge after ASTORE
                merge(output, size, e, i);
            }
        }
    }
}

From source file:org.jacoco.core.internal.analysis.filter.FinallyFilter.java

License:Open Source License

private static void merge(final IFilterOutput output, final int size, AbstractInsnNode e, AbstractInsnNode n) {
    if (!isSame(size, e, n)) {
        return;/*from  ww  w .j a v a2s  .  c  o m*/
    }
    output.ignore(e, e);
    e = next(e);
    for (int i = 0; i < size; i++) {
        output.merge(e, n);
        e = next(e);
        n = next(n);
    }
    output.ignore(e, next(e));

    if (n != null && n.getOpcode() == Opcodes.GOTO) {
        // goto instructions at the end of non-executed duplicates
        // cause partial coverage of last line of finally block,
        // so should be ignored
        output.ignore(n, n);
    }
}

From source file:org.jacoco.core.internal.analysis.filter.FinallyFilterTest.java

License:Open Source License

@Test
public void javac_try_catch_finally() {
    final Label tryStart = new Label();
    final Label tryEnd = new Label();
    final Label catchStart = new Label();
    final Label catchEnd = new Label();
    final Label finallyStart = new Label();
    final Label finallyEnd = new Label();

    m.visitTryCatchBlock(tryStart, tryEnd, catchStart, "java/lang/Exception");
    m.visitTryCatchBlock(catchStart, catchEnd, finallyStart, null);
    m.visitTryCatchBlock(tryStart, tryEnd, finallyStart, null);

    m.visitLabel(tryStart);/*from   w  w  w  .j  a  va2 s. c  om*/
    m.visitInsn(Opcodes.NOP); // try body
    m.visitLabel(tryEnd);
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitJumpInsn(Opcodes.GOTO, finallyEnd);
    shouldIgnoreLast();

    m.visitLabel(catchStart);
    m.visitInsn(Opcodes.NOP); // catch body
    m.visitLabel(catchEnd);
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitInsn(Opcodes.ATHROW);

    m.visitLabel(finallyStart);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitVarInsn(Opcodes.ALOAD, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.ATHROW);
    shouldIgnoreLast();
    m.visitLabel(finallyEnd);

    m.visitInsn(Opcodes.NOP);

    execute();
}