Example usage for org.objectweb.asm MethodVisitor visitJumpInsn

List of usage examples for org.objectweb.asm MethodVisitor visitJumpInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitJumpInsn.

Prototype

public void visitJumpInsn(final int opcode, final Label label) 

Source Link

Document

Visits a jump instruction.

Usage

From source file:com.yahoo.yqlplus.engine.internal.bytecode.types.gambit.ExpressionHandler.java

@Override
public BytecodeExpression and(Location loc, final List<BytecodeExpression> inputs) {
    return new BaseTypeExpression(BaseTypeAdapter.BOOLEAN) {
        @Override/*from  w ww.  j  a  va 2 s.  c o m*/
        public void generate(CodeEmitter code) {
            MethodVisitor mv = code.getMethodVisitor();
            Label done = new Label();
            Label isFalse = new Label();
            for (BytecodeExpression input : inputs) {
                Label isTrue = new Label();
                code.exec(input);
                input.getType().getComparisionAdapter().coerceBoolean(code, isTrue, isFalse, isFalse);
                mv.visitLabel(isTrue);
            }
            code.emitBooleanConstant(true);
            mv.visitJumpInsn(Opcodes.GOTO, done);
            mv.visitLabel(isFalse);
            code.emitBooleanConstant(false);
            mv.visitLabel(done);
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.types.gambit.ExpressionHandler.java

@Override
public BytecodeExpression or(Location loc, final List<BytecodeExpression> inputs) {
    return new BaseTypeExpression(BaseTypeAdapter.BOOLEAN) {
        @Override//from  w w w.ja  v  a 2s .co  m
        public void generate(CodeEmitter code) {
            MethodVisitor mv = code.getMethodVisitor();
            Label done = new Label();
            Label isTrue = new Label();
            for (BytecodeExpression input : inputs) {
                Label isFalse = new Label();
                code.exec(input);
                input.getType().getComparisionAdapter().coerceBoolean(code, isTrue, isFalse, isFalse);
                mv.visitLabel(isFalse);
            }
            code.emitBooleanConstant(false);
            mv.visitJumpInsn(Opcodes.GOTO, done);
            mv.visitLabel(isTrue);
            code.emitBooleanConstant(true);
            mv.visitLabel(done);
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.types.gambit.ExpressionHandler.java

@Override
public BytecodeExpression not(Location loc, final BytecodeExpression input) {
    return new BaseTypeExpression(BaseTypeAdapter.BOOLEAN) {
        @Override/*from   w  w  w. j  ava2  s. c om*/
        public void generate(CodeEmitter code) {
            MethodVisitor mv = code.getMethodVisitor();
            Label done = new Label();
            Label isTrue = new Label();
            Label isFalse = new Label();
            code.exec(input);
            input.getType().getComparisionAdapter().coerceBoolean(code, isTrue, isFalse, isFalse);
            mv.visitLabel(isFalse);
            code.emitBooleanConstant(true);
            mv.visitJumpInsn(Opcodes.GOTO, done);
            mv.visitLabel(isTrue);
            code.emitBooleanConstant(false);
            mv.visitLabel(done);
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.types.gambit.ExpressionHandler.java

@Override
public BytecodeExpression bool(Location loc, final BytecodeExpression input) {
    return new BaseTypeExpression(BaseTypeAdapter.BOOLEAN) {
        @Override// w  w  w  .  j a v  a2  s  . c o  m
        public void generate(CodeEmitter code) {
            if (input.getType() == BaseTypeAdapter.BOOLEAN) {
                code.exec(input);
            } else {
                MethodVisitor mv = code.getMethodVisitor();
                Label done = new Label();
                Label isTrue = new Label();
                Label isFalse = new Label();
                code.exec(input);
                input.getType().getComparisionAdapter().coerceBoolean(code, isTrue, isFalse, isFalse);
                mv.visitLabel(isFalse);
                code.emitBooleanConstant(false);
                mv.visitJumpInsn(Opcodes.GOTO, done);
                mv.visitLabel(isTrue);
                code.emitBooleanConstant(true);
                mv.visitLabel(done);
            }
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.types.gambit.ExpressionHandler.java

@Override
public BytecodeExpression isNull(Location location, final BytecodeExpression input) {
    return new BaseTypeExpression(BaseTypeAdapter.BOOLEAN) {
        @Override/*from  w w  w  .  j  ava  2s .c o m*/
        public void generate(CodeEmitter code) {
            MethodVisitor mv = code.getMethodVisitor();
            code.exec(input);
            // it's surprising to not evaluate even if we know input isn't nullable
            if (input.getType().isPrimitive()) {
                code.pop(input.getType());
                code.emitBooleanConstant(false);
            } else {
                Label done = new Label();
                Label isNull = new Label();
                mv.visitJumpInsn(Opcodes.IFNULL, isNull);
                code.emitBooleanConstant(false);
                mv.visitJumpInsn(Opcodes.GOTO, done);
                mv.visitLabel(isNull);
                code.emitBooleanConstant(true);
                mv.visitLabel(done);
            }
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.types.gambit.ExpressionHandler.java

@Override
public BytecodeExpression coalesce(Location loc, final List<BytecodeExpression> inputs) {
    List<TypeWidget> widgets = Lists.newArrayList();
    for (BytecodeExpression expr : inputs) {
        widgets.add(expr.getType());//from  w w w .j a v  a  2  s  .  c  om
    }
    TypeWidget output = unify(widgets);
    return new BaseTypeExpression(output) {
        @Override
        public void generate(CodeEmitter code) {
            Label done = new Label();
            MethodVisitor mv = code.getMethodVisitor();
            boolean lastNullable = true;
            for (BytecodeExpression expr : inputs) {
                Label isNull = new Label();
                code.exec(expr);
                if (code.cast(getType(), expr.getType(), isNull)) {
                    mv.visitJumpInsn(Opcodes.GOTO, done);
                    mv.visitLabel(isNull);
                } else {
                    lastNullable = false;
                    break;
                }
            }
            if (lastNullable) {
                mv.visitInsn(Opcodes.ACONST_NULL);
            }
            mv.visitLabel(done);
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.types.gambit.ExpressionHandler.java

@Override
public BytecodeExpression matches(Location loc, final BytecodeExpression left, final BytecodeExpression right) {
    return new BaseTypeExpression(BaseTypeAdapter.BOOLEAN) {
        @Override//from  ww w  . j ava 2s .  co  m
        public void generate(CodeEmitter code) {
            Label done = new Label();
            Label anyIsNull = new Label();
            CodeEmitter.BinaryCoercion coerce = code.binaryCoercion(right, Pattern.class, left,
                    CharSequence.class, anyIsNull, anyIsNull, anyIsNull);
            MethodVisitor mv = code.getMethodVisitor();
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Pattern.class), "matcher",
                    Type.getMethodDescriptor(Type.getType(Matcher.class), Type.getType(CharSequence.class)),
                    false);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Matcher.class), "matches",
                    Type.getMethodDescriptor(Type.BOOLEAN_TYPE), false);
            if (coerce.leftNullable || coerce.rightNullable) {
                mv.visitJumpInsn(Opcodes.GOTO, done);
                mv.visitLabel(anyIsNull);
                mv.visitInsn(Opcodes.ICONST_0);
                mv.visitLabel(done);
            }
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.types.gambit.ExpressionHandler.java

@Override
public BytecodeExpression in(Location loc, final BytecodeExpression left, final BytecodeExpression right) {
    return new BaseTypeExpression(BaseTypeAdapter.BOOLEAN) {
        @Override//  w w  w  .  j  a v a  2s  .c  o  m
        public void generate(CodeEmitter code) {
            Label done = new Label();
            Label anyIsNull = new Label();
            CodeEmitter.BinaryCoercion coerce = code.binaryCoercion(right, Collection.class, left, Object.class,
                    anyIsNull, anyIsNull, anyIsNull);
            MethodVisitor mv = code.getMethodVisitor();
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Collection.class), "contains",
                    Type.getMethodDescriptor(Type.BOOLEAN_TYPE, Type.getType(Object.class)), true);
            if (coerce.leftNullable || coerce.rightNullable) {
                mv.visitJumpInsn(Opcodes.GOTO, done);
                mv.visitLabel(anyIsNull);
                mv.visitInsn(Opcodes.ICONST_0);
                mv.visitLabel(done);
            }
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.types.gambit.ExpressionHandler.java

@Override
public BytecodeExpression guarded(final BytecodeExpression target, final BytecodeExpression ifTargetIsNotNull,
        final BytecodeExpression ifTargetIsNull) {
    if (!target.getType().isNullable()) {
        return ifTargetIsNotNull;
    }/*from  ww  w .j ava2  s  . c  om*/
    return new BaseTypeExpression(unify(ifTargetIsNotNull.getType(), ifTargetIsNull.getType())) {
        @Override
        public void generate(CodeEmitter code) {
            final MethodVisitor mv = code.getMethodVisitor();
            Label isNull = new Label();
            Label done = new Label();
            code.exec(target);
            code.nullTest(target.getType(), isNull);
            code.pop(target.getType());
            code.exec(ifTargetIsNotNull);
            code.cast(getType(), ifTargetIsNotNull.getType(), isNull);
            mv.visitJumpInsn(Opcodes.GOTO, done);
            mv.visitLabel(isNull);
            code.exec(ifTargetIsNull);
            code.cast(getType(), ifTargetIsNull.getType());
            mv.visitLabel(done);
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.types.gambit.ExpressionHandler.java

@Override
public BytecodeExpression fallback(Location loc, final BytecodeExpression primary,
        final BytecodeExpression caught) {
    TypeWidget unified = unify(primary.getType(), caught.getType());
    return new BaseTypeExpression(unified) {
        @Override/*from   w w w  .j  a v a  2s  .c  om*/
        public void generate(CodeEmitter code) {
            MethodVisitor mv = code.getMethodVisitor();
            final Label start = new Label();
            final Label endCatch = new Label();
            final Label handler = new Label();
            Label done = new Label();
            // this probably should not be catching throwable and instead should be catching Exception
            // or permit certain Errors through only
            mv.visitTryCatchBlock(start, endCatch, handler, "java/lang/Throwable");
            mv.visitLabel(start);
            code.exec(primary);
            Label isNull = new Label();
            boolean maybeNull = code.cast(getType(), primary.getType(), isNull);
            mv.visitJumpInsn(Opcodes.GOTO, done);
            mv.visitLabel(endCatch);
            mv.visitLabel(handler);
            mv.visitInsn(Opcodes.POP);
            if (maybeNull) {
                mv.visitLabel(isNull);
            }
            code.exec(caught);
            code.cast(getType(), caught.getType());
            mv.visitLabel(done);
        }
    };
}