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.devtools.build.android.desugar.CoreLibrarySupport.java

License:Open Source License

private void makeDispatchHelperMethod(ClassVisitor helper, EmulatedMethod method,
        ImmutableList<Class<?>> typechecks) {
    checkArgument(method.owner().isInterface());
    String owner = method.owner().getName().replace('.', '/');
    Type methodType = Type.getMethodType(method.descriptor());
    String companionDesc = InterfaceDesugaring.companionDefaultMethodDescriptor(owner, method.descriptor());
    MethodVisitor dispatchMethod = helper.visitMethod(method.access() | Opcodes.ACC_STATIC, method.name(),
            companionDesc, /*signature=*/ null, // signature is invalid due to extra "receiver" argument
            method.exceptions().toArray(EMPTY_LIST));

    dispatchMethod.visitCode();//from w w w  . j  a  v  a  2  s  .  co  m
    {
        // See if the receiver might come with its own implementation of the method, and call it.
        // We do this by testing for the interface type created by EmulatedInterfaceRewriter
        Label fallthrough = new Label();
        String emulationInterface = renameCoreLibrary(owner);
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.INSTANCEOF, emulationInterface);
        dispatchMethod.visitJumpInsn(Opcodes.IFEQ, fallthrough);
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.CHECKCAST, emulationInterface);

        visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */);
        dispatchMethod.visitMethodInsn(Opcodes.INVOKEINTERFACE, emulationInterface, method.name(),
                method.descriptor(), /*itf=*/ true);
        dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN));

        dispatchMethod.visitLabel(fallthrough);
        // Trivial frame for the branch target: same empty stack as before
        dispatchMethod.visitFrame(Opcodes.F_SAME, 0, EMPTY_FRAME, 0, EMPTY_FRAME);
    }

    // Next, check for subtypes with specialized implementations and call them
    for (Class<?> tested : typechecks) {
        Label fallthrough = new Label();
        String testedName = tested.getName().replace('.', '/');
        // In case of a class this must be a member move; for interfaces use the companion.
        String target = tested.isInterface() ? InterfaceDesugaring.getCompanionClassName(testedName)
                : checkNotNull(memberMoves.get(rewriter.unprefix(testedName) + '#' + method.name()));
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.INSTANCEOF, testedName);
        dispatchMethod.visitJumpInsn(Opcodes.IFEQ, fallthrough);
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.CHECKCAST, testedName); // make verifier happy

        visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */);
        dispatchMethod.visitMethodInsn(Opcodes.INVOKESTATIC, target, method.name(),
                InterfaceDesugaring.companionDefaultMethodDescriptor(testedName, method.descriptor()),
                /*itf=*/ false);
        dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN));

        dispatchMethod.visitLabel(fallthrough);
        // Trivial frame for the branch target: same empty stack as before
        dispatchMethod.visitFrame(Opcodes.F_SAME, 0, EMPTY_FRAME, 0, EMPTY_FRAME);
    }

    // Call static type's default implementation in companion class
    dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
    visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */);
    dispatchMethod.visitMethodInsn(Opcodes.INVOKESTATIC, InterfaceDesugaring.getCompanionClassName(owner),
            method.name(), companionDesc, /*itf=*/ false);
    dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN));

    dispatchMethod.visitMaxs(0, 0);
    dispatchMethod.visitEnd();
}

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

License:Open Source License

/**
 * Builds a conditional jump for one int operand from the stack compared to zero.
 *//*from  w ww  . j av  a 2  s. com*/
public static JumpWithoutTarget ifIntOperandToZero(PrimitiveComparison comparison) {
    return new JumpWithoutTarget(Opcodes.IFEQ + comparison.ordinal(), new Size(-1, 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 .  ja  v a  2s .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.template.soy.jbcsrc.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  . j a va 2 s  .  com
    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;
    }
    throw new IllegalArgumentException("Unsupported opcode for comparison operation: " + opcode);
}

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

License:Apache License

/**
 * Compares two {@link SoyExpression}s for equality using soy == semantics.
 *//*from w  ww  .  j av a2  s  .c o  m*/
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.
    if (left.isKnownString()) {
        return doEqualsString(left.unboxAs(String.class), right);
    }
    if (right.isKnownString()) {
        return doEqualsString(right.unboxAs(String.class), left);
    }
    if (left.isKnownInt() && right.isKnownInt()) {
        return compare(Opcodes.IFEQ, left.unboxAs(long.class), right.unboxAs(long.class));
    }
    if (left.isKnownNumber() && right.isKnownNumber() && (left.isKnownFloat() || right.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.BytecodeUtils.java

License:Apache License

/**
 * Returns an expression that evaluates equivalently to a java ternary expression: 
 * {@code condition ? left : right}/*from w w w  .  j a v a2 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.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  ww  . j  a v a2s. 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
        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.BytecodeUtilsTest.java

License:Apache License

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.BytecodeUtilsTest.java

License:Apache License

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.ControlFlow.java

License:Apache License

/**
 * Returns a statement that encodes the given sequence of {@link IfBlock if blocks} as an 
 * if-elseif-else chain./*from   ww w  .ja v a 2  s  .co m*/
 */
static Statement ifElseChain(final List<IfBlock> ifs, final Optional<Statement> elseBlock) {
    checkArgument(!ifs.isEmpty());
    return new Statement() {
        @Override
        void doGen(CodeBuilder adapter) {
            Label end = new Label();
            Label next;
            for (int i = 0; i < ifs.size(); i++) {
                IfBlock curr = ifs.get(i);
                boolean isLastIfBlock = i == ifs.size() - 1;
                if (isLastIfBlock && !elseBlock.isPresent()) {
                    next = end;
                } else {
                    next = new Label();
                }
                curr.condition().gen(adapter);
                adapter.ifZCmp(Opcodes.IFEQ, next);
                curr.block().gen(adapter);
                if (end != next) {
                    adapter.goTo(end);
                }
                adapter.mark(next);
            }
            if (elseBlock.isPresent()) {
                elseBlock.get().gen(adapter);
                adapter.mark(end);
            }
        }
    };
}