Example usage for org.objectweb.asm Opcodes IFEQ

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

Introduction

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

Prototype

int IFEQ

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

Click Source Link

Usage

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

License:Apache License

/**
 * Returns a Statement that will conditionally detach if the given {@link AdvisingAppendable} has
 * been {@link AdvisingAppendable#softLimitReached() output limited}.
 *///from w  ww.  jav  a  2s  .  co  m
Statement detachLimited(AppendableExpression appendable) {
    if (!appendable.supportsSoftLimiting()) {
        return appendable.toStatement();
    }
    final Label reattachPoint = new Label();
    final SaveRestoreState saveRestoreState = variables.saveRestoreState();

    Statement restore = saveRestoreState.restore();
    int state = addState(reattachPoint, restore);
    final Expression isSoftLimited = appendable.softLimitReached();
    final Statement returnLimited = returnExpression(MethodRef.RENDER_RESULT_LIMITED.invoke());
    final Statement saveState = stateField.putInstanceField(thisExpr, BytecodeUtils.constant(state));
    return new Statement() {
        @Override
        void doGen(CodeBuilder adapter) {
            isSoftLimited.gen(adapter);
            adapter.ifZCmp(Opcodes.IFEQ, reattachPoint); // if !softLimited
            // ok we were limited, save state and return
            saveRestoreState.save().gen(adapter); // save locals
            saveState.gen(adapter); // save the state field
            returnLimited.gen(adapter);
            // Note, the reattach point for 'limited' is _after_ the check.  That means we do not 
            // recheck the limit state.  So if a caller calls us back without freeing any buffer we
            // will print more before checking again.  This is fine, because our caller is breaking the
            // contract.
            adapter.mark(reattachPoint);
        }
    };
}

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

License:Apache License

private static void checkIntComparisonOpcode(Type comparisonType, int opcode) {
    switch (opcode) {
    case Opcodes.IFEQ:
    case Opcodes.IFNE:
        return;/*  w w w  .  jav a2 s  .  c  o  m*/
    case Opcodes.IFGT:
    case Opcodes.IFGE:
    case Opcodes.IFLT:
    case Opcodes.IFLE:
        if (comparisonType.getSort() == Type.ARRAY || comparisonType.getSort() == Type.OBJECT) {
            throw new IllegalArgumentException(
                    "Type: " + comparisonType + " cannot be compared via " + Printer.OPCODES[opcode]);
        }
        return;
    default:
        throw new IllegalArgumentException("Unsupported opcode for comparison operation: " + opcode);
    }
}

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

License:Apache License

/** Compares two {@link SoyExpression}s for equality using soy == semantics. */
public static Expression compareSoyEquals(final SoyExpression left, final SoyExpression right) {
    // We can special case when we know the types.
    // If either is a string, we run special logic so test for that first
    // otherwise we special case primitives and eventually fall back to our runtime.
    SoyRuntimeType leftRuntimeType = left.soyRuntimeType();
    SoyRuntimeType rightRuntimeType = right.soyRuntimeType();
    if (leftRuntimeType.isKnownString()) {
        return doEqualsString(left.unboxAs(String.class), right);
    }/*from w  w w  .  java 2 s. com*/
    if (rightRuntimeType.isKnownString()) {
        // TODO(lukes): we are changing the order of evaluation here.
        return doEqualsString(right.unboxAs(String.class), left);
    }
    if (leftRuntimeType.isKnownInt() && rightRuntimeType.isKnownInt() && left.isNonNullable()
            && right.isNonNullable()) {
        return compare(Opcodes.IFEQ, left.unboxAs(long.class), right.unboxAs(long.class));
    }
    if (leftRuntimeType.isKnownNumber() && rightRuntimeType.isKnownNumber() && left.isNonNullable()
            && right.isNonNullable() && (leftRuntimeType.isKnownFloat() || rightRuntimeType.isKnownFloat())) {
        return compare(Opcodes.IFEQ, left.coerceToDouble(), right.coerceToDouble());
    }
    return MethodRef.RUNTIME_EQUAL.invoke(left.box(), right.box());
}

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  w  w  w  .ja  v  a 2 s . 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.template.soy.jbcsrc.restricted.BytecodeUtils.java

License:Apache License

private static Expression doShortCircuitingLogicalOperator(
        final ImmutableList<? extends Expression> expressions, final boolean isOrOperator) {
    checkArgument(!expressions.isEmpty());
    for (Expression expr : expressions) {
        expr.checkAssignableTo(Type.BOOLEAN_TYPE);
    }/*from w  w w. ja  v  a  2s. c o m*/
    if (expressions.size() == 1) {
        return expressions.get(0);
    }

    return new Expression(Type.BOOLEAN_TYPE,
            Expression.areAllCheap(expressions) ? Features.of(Feature.CHEAP) : Features.of()) {
        @Override
        protected void doGen(CodeBuilder adapter) {
            Label end = new Label();
            Label shortCircuit = new Label();
            for (int i = 0; i < expressions.size(); i++) {
                Expression expr = expressions.get(i);
                expr.gen(adapter);
                if (i == expressions.size() - 1) {
                    // if we are the last one, just goto end. Whatever the result of the last expression is
                    // determines the result of the whole expression (when all prior tests fail).
                    adapter.goTo(end);
                } else {
                    adapter.ifZCmp(isOrOperator ? Opcodes.IFNE : Opcodes.IFEQ, shortCircuit);
                }
            }
            adapter.mark(shortCircuit);
            adapter.pushBoolean(isOrOperator); // default for || is true && is false
            adapter.mark(end);
        }
    };
}

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

License:Apache License

@Test
public void testCompareLongs() {
    Expression one = constant(1L);
    Expression two = constant(2L);
    assertThatExpression(compare(Opcodes.IFNE, one, two)).evaluatesTo(true);
    assertThatExpression(compare(Opcodes.IFLT, one, two)).evaluatesTo(true);
    assertThatExpression(compare(Opcodes.IFLE, one, two)).evaluatesTo(true);
    assertThatExpression(compare(Opcodes.IFGT, one, two)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFGE, one, two)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFEQ, one, two)).evaluatesTo(false);

    assertThatExpression(compare(Opcodes.IFLE, two, two)).evaluatesTo(true);
    assertThatExpression(compare(Opcodes.IFGE, two, two)).evaluatesTo(true);
}

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

License:Apache License

@Test
public void testCompareDoubles() {
    Expression one = constant(1D);
    Expression two = constant(2D);
    Expression nan = constant(Double.NaN);

    assertThatExpression(compare(Opcodes.IFNE, one, two)).evaluatesTo(true);
    assertThatExpression(compare(Opcodes.IFLT, one, two)).evaluatesTo(true);
    assertThatExpression(compare(Opcodes.IFLE, one, two)).evaluatesTo(true);
    assertThatExpression(compare(Opcodes.IFGT, one, two)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFGE, one, two)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFEQ, one, two)).evaluatesTo(false);

    assertThatExpression(compare(Opcodes.IFLE, two, two)).evaluatesTo(true);
    assertThatExpression(compare(Opcodes.IFGE, two, two)).evaluatesTo(true);

    // There are special cases for NaN that we need to test, basically every expression involving
    // NaN should evaluate to false with the exception of NaN != * which always == true
    assertThatExpression(compare(Opcodes.IFNE, nan, two)).evaluatesTo(true);
    assertThatExpression(compare(Opcodes.IFNE, two, nan)).evaluatesTo(true);
    assertThatExpression(compare(Opcodes.IFNE, nan, nan)).evaluatesTo(true);

    assertThatExpression(compare(Opcodes.IFEQ, nan, two)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFEQ, two, nan)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFEQ, nan, nan)).evaluatesTo(false);

    assertThatExpression(compare(Opcodes.IFLE, nan, two)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFLE, two, nan)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFLT, nan, two)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFLT, two, nan)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFGE, nan, two)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFGE, two, nan)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFGT, nan, two)).evaluatesTo(false);
    assertThatExpression(compare(Opcodes.IFGT, two, nan)).evaluatesTo(false);
}

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

License:Apache License

@Override
protected Statement visitForeachNode(ForeachNode node) {
    ForeachNonemptyNode nonEmptyNode = (ForeachNonemptyNode) node.getChild(0);
    SoyExpression expr = exprCompiler.compile(node.getExpr()).unboxAs(List.class);
    Scope scope = variables.enterScope();
    final Variable listVar = scope.createSynthetic(SyntheticVarName.foreachLoopList(nonEmptyNode), expr, STORE);
    final Variable indexVar = scope.createSynthetic(SyntheticVarName.foreachLoopIndex(nonEmptyNode),
            constant(0), STORE);//from  www .j ava 2 s . c o m
    final Variable listSizeVar = scope.createSynthetic(SyntheticVarName.foreachLoopLength(nonEmptyNode),
            MethodRef.LIST_SIZE.invoke(listVar.local()), DERIVED);
    final Variable itemVar = scope.create(nonEmptyNode.getVarName(),
            MethodRef.LIST_GET.invoke(listVar.local(), indexVar.local()).cast(SOY_VALUE_PROVIDER_TYPE),
            SaveStrategy.DERIVED);
    final Statement loopBody = visitChildrenInNewScope(nonEmptyNode);
    final Statement exitScope = scope.exitScope();

    // it important for this to be generated after exitScope is called (or before enterScope)
    final Statement emptyBlock = node.numChildren() == 2
            ? visitChildrenInNewScope((ForeachIfemptyNode) node.getChild(1))
            : null;
    return new Statement() {
        @Override
        void doGen(CodeBuilder adapter) {
            listVar.initializer().gen(adapter);
            listSizeVar.initializer().gen(adapter);
            listSizeVar.local().gen(adapter);
            Label emptyListLabel = new Label();
            adapter.ifZCmp(Opcodes.IFEQ, emptyListLabel);
            indexVar.initializer().gen(adapter);
            Label loopStart = adapter.mark();
            itemVar.initializer().gen(adapter);

            loopBody.gen(adapter);

            adapter.iinc(indexVar.local().index(), 1); // index++
            indexVar.local().gen(adapter);
            listSizeVar.local().gen(adapter);
            adapter.ifICmp(Opcodes.IFLT, loopStart); // if index < list.size(), goto loopstart
            // exit the loop
            exitScope.gen(adapter);

            if (emptyBlock != null) {
                Label skipIfEmptyBlock = new Label();
                adapter.goTo(skipIfEmptyBlock);
                adapter.mark(emptyListLabel);
                emptyBlock.gen(adapter);
                adapter.mark(skipIfEmptyBlock);
            } else {
                adapter.mark(emptyListLabel);
            }
        }
    };
}

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);
            }/*from  w w w . ja  va 2 s .c  o  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));
            }
        });
    }
}

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

License:Open Source License

@Override
public boolean execute(GenerationContext generationContext) throws Exception {
    try {// w w  w. ja va 2  s . c  om
        if (generationContext.getParseTree().getChildCount() == 4) {
            /*
             * dispatch into the test
             */
            final GenerationContext testGenerationContext = new GenerationContext(generationContext,
                    generationContext.getParseTree().getChild(1));
            Dispatcher.dispatchChildren(testGenerationContext);
            /*
             * check the result
             */
            generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
            generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                    generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop",
                    "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    "com/khubla/jvmbasic/jvmbasicrt/Value", "getBoolean", "()Ljava/lang/Boolean;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean",
                    "booleanValue", "()Z");
            /*
             * check result
             */
            final Label l1 = new Label();
            generationContext.getMethodVisitor().visitJumpInsn(Opcodes.IFEQ, l1);
            /*
             * process the contained tree
             */
            final GenerationContext statementGenerationContext = new GenerationContext(generationContext,
                    generationContext.getParseTree().getChild(3));
            Dispatcher.dispatch(statementGenerationContext);
            /*
             * label to skip to
             */
            generationContext.getMethodVisitor().visitLabel(l1);
            generationContext.getMethodVisitor().visitFrame(Opcodes.F_SAME, 0, null, 0, null);
            return true;
        } else if (generationContext.getParseTree().getChildCount() == 3) {
            /*
             * dispatch into the test
             */
            final GenerationContext testGenerationContext = new GenerationContext(generationContext,
                    generationContext.getParseTree().getChild(1));
            Dispatcher.dispatchChildren(testGenerationContext);
            /*
             * check the result
             */
            generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
            generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                    generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop",
                    "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    "com/khubla/jvmbasic/jvmbasicrt/Value", "getBoolean", "()Ljava/lang/Boolean;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean",
                    "booleanValue", "()Z");
            /*
             * check result
             */
            final Label l1 = new Label();
            generationContext.getMethodVisitor().visitJumpInsn(Opcodes.IFEQ, l1);
            /*
             * process the contained tree
             */
            final GenerationContext statementGenerationContext = new GenerationContext(generationContext,
                    generationContext.getParseTree().getChild(2));
            Dispatcher.dispatch(statementGenerationContext);
            /*
             * label to skip to
             */
            generationContext.getMethodVisitor().visitLabel(l1);
            generationContext.getMethodVisitor().visitFrame(Opcodes.F_SAME, 0, null, 0, null);
            return true;
        } else {
            throw new Exception(
                    "Invalid number of tokens in subtree '" + generationContext.getParseTree().getChildCount()
                            + "' expected 3 on line number: " + generationContext.getLineNumber());
        }
    } catch (final Exception e) {
        throw new Exception("Exception in execute", e);
    }
}