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

@Override
public ScopedBuilder when(final BytecodeExpression test) {
    final TypeWidget type = test.getType();
    final Label isFalse = new Label();
    body.add(new BytecodeSequence() {
        @Override/* w w w.  ja  v a 2s.c o  m*/
        public void generate(CodeEmitter code) {
            code.exec(test);
            Label isTrue = new Label();
            type.getComparisionAdapter().coerceBoolean(code, isTrue, isFalse, isFalse);
            MethodVisitor mv = code.getMethodVisitor();
            mv.visitLabel(isTrue);
        }
    });
    Clause block = new Clause(source, body.block());
    body.add(new BytecodeSequence() {
        @Override
        public void generate(CodeEmitter code) {
            MethodVisitor mv = code.getMethodVisitor();
            mv.visitJumpInsn(Opcodes.GOTO, end);
            mv.visitLabel(isFalse);
        }
    });
    return block;
}

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

@Override
public void abort(final BytecodeExpression test) {
    final TypeWidget type = test.getType();
    body.add(new BytecodeSequence() {
        @Override/*from w w  w  . j a  v  a 2  s .  c  o  m*/
        public void generate(CodeEmitter code) {
            code.exec(test);
            Label isTrue = new Label();
            Label isFalse = new Label();
            type.getComparisionAdapter().coerceBoolean(code, isTrue, isFalse, isFalse);
            MethodVisitor mv = code.getMethodVisitor();
            mv.visitLabel(isTrue);
            code.exec(result);
            mv.visitJumpInsn(Opcodes.GOTO, body.getEnd());
            mv.visitLabel(isFalse);
        }
    });
}

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

@Override
public void next(final BytecodeExpression test) {
    final TypeWidget type = test.getType();
    body.add(new BytecodeSequence() {
        @Override//from   www.  j a  v a  2s .  c  o  m
        public void generate(CodeEmitter code) {
            code.exec(test);
            Label isTrue = new Label();
            Label isFalse = new Label();
            type.getComparisionAdapter().coerceBoolean(code, isTrue, isFalse, isFalse);
            MethodVisitor mv = code.getMethodVisitor();
            mv.visitLabel(isTrue);
            code.exec(result);
            mv.visitJumpInsn(Opcodes.GOTO, next);
            mv.visitLabel(isFalse);
        }
    });

}

From source file:com.yahoo.yqlplus.engine.internal.compiler.BooleanCompareExpression.java

@Override
public void generate(CodeEmitter code) {
    code.exec(new CompareExpression(loc, leftExpr, rightExpr));
    MethodVisitor mv = code.getMethodVisitor();
    Label isTrue = new Label();
    Label done = new Label();
    switch (booleanComparison) {
    case LT://from w ww  . j  a  va  2 s. com
        mv.visitJumpInsn(Opcodes.IFLT, isTrue);
        break;
    case LTEQ:
        mv.visitJumpInsn(Opcodes.IFLE, isTrue);
        break;
    case GT:
        mv.visitJumpInsn(Opcodes.IFGT, isTrue);
        break;
    case GTEQ:
        mv.visitJumpInsn(Opcodes.IFGE, isTrue);
        break;
    }
    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.compiler.BytecodeArithmeticExpression.java

@Override
public void generate(CodeEmitter code) {
    MethodVisitor mv = code.getMethodVisitor();
    Label isNull = new Label();
    TypeWidget mathType = getType().unboxed();

    if (!mathType.isPrimitive() || op == ArithmeticOperation.POW) {
        mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(Maths.class), "INSTANCE",
                Type.getDescriptor(Maths.class));
        mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(ArithmeticOperation.class), op.name(),
                Type.getDescriptor(ArithmeticOperation.class));
    }//from  ww w . ja va2s  .  c om

    CodeEmitter.Unification out = code.unifyAs(mathType, leftExpr, rightExpr, isNull, isNull, isNull);
    // now we have both sides unified as (if possible) a primitive type

    // compute the result
    if (mathType.isPrimitive()) {
        switch (op) {
        case ADD:
            mv.visitInsn(mathType.getJVMType().getOpcode(Opcodes.IADD));
            break;
        case SUB:
            mv.visitInsn(mathType.getJVMType().getOpcode(Opcodes.ISUB));
            break;
        case MULT:
            mv.visitInsn(mathType.getJVMType().getOpcode(Opcodes.IMUL));
            break;
        case DIV:
            mv.visitInsn(mathType.getJVMType().getOpcode(Opcodes.IDIV));
            break;
        case MOD:
            mv.visitInsn(mathType.getJVMType().getOpcode(Opcodes.IREM));
            break;
        case POW:
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Maths.class), "binaryMath",
                    Type.getMethodDescriptor(mathType.getJVMType(), Type.getType(ArithmeticOperation.class),
                            mathType.getJVMType(), mathType.getJVMType()),
                    false);
            break;
        default:
            throw new ProgramCompileException(loc, "Unknown BinaryMath operation: " + op);

        }
    } else if (mathType.getValueCoreType() == YQLCoreType.ANY) {
        String desc = Type.getMethodDescriptor(getType().getJVMType(), Type.getType(Maths.class),
                Type.getType(ArithmeticOperation.class), leftExpr.getType().boxed().getJVMType(),
                rightExpr.getType().boxed().getJVMType());
        mv.visitInvokeDynamicInsn("dyn:callMethod:binaryMath", desc, Dynamic.H_DYNALIB_BOOTSTRAP);
    } else {
        throw new ProgramCompileException(loc, "Math operation %s is not defined for type %s", op,
                mathType.getJVMType());
    }

    if (!getType().isPrimitive() && mathType.isPrimitive()) {
        code.box(mathType);
    }

    if (out.nullPossible) {
        Label done = new Label();
        mv.visitJumpInsn(Opcodes.GOTO, done);
        mv.visitLabel(isNull);
        if (!mathType.isPrimitive() || op == ArithmeticOperation.POW) {
            mv.visitInsn(Opcodes.POP2);
        }
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitLabel(done);
    }
}

From source file:com.yahoo.yqlplus.engine.internal.compiler.BytecodeNegateExpression.java

@Override
public void generate(CodeEmitter code) {
    MethodVisitor mv = code.getMethodVisitor();
    Label isNull = new Label();
    TypeWidget mathType = getType().unboxed();

    if (!mathType.isPrimitive()) {
        mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(Maths.class), "INSTANCE",
                Type.getDescriptor(Maths.class));
    }//  w  ww.  j  a va2 s .c o m

    boolean maybeNull = code.cast(mathType, leftExpr.getType(), isNull);
    // compute the result
    if (mathType.isPrimitive()) {
        mv.visitInsn(mathType.getJVMType().getOpcode(Opcodes.INEG));
    } else if (mathType.getValueCoreType() == YQLCoreType.ANY) {
        String desc = Type.getMethodDescriptor(getType().getJVMType(), Type.getType(Maths.class),
                leftExpr.getType().getJVMType());
        mv.visitInvokeDynamicInsn("dyn:callMethod:negate", desc, Dynamic.H_DYNALIB_BOOTSTRAP);
    } else {
        throw new ProgramCompileException(loc, "Math operation NEGATE is not defined for type %s",
                mathType.getJVMType());
    }

    if (!getType().isPrimitive() && mathType.isPrimitive()) {
        code.box(mathType);
    }

    if (maybeNull) {
        Label done = new Label();
        mv.visitJumpInsn(Opcodes.GOTO, done);
        mv.visitLabel(isNull);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitLabel(done);
    }
}

From source file:com.yahoo.yqlplus.engine.internal.compiler.CodeEmitter.java

public void emitIntegerSwitch(Map<Integer, Label> labels, Label defaultCase) {
    // TODO: we should see if the labels are dense, and if so use TABLESWITCH
    MethodVisitor mv = getMethodVisitor();
    List<Integer> codes = Lists.newArrayList(labels.keySet());
    Collections.sort(codes);//from  w  ww .j a  v  a  2  s.co m
    int[] k = new int[codes.size()];
    Label[] kl = new Label[codes.size()];
    for (int i = 0; i < k.length; ++i) {
        k[i] = codes.get(i);
        kl[i] = labels.get(i);
    }
    mv.visitLookupSwitchInsn(defaultCase, k, kl);
    mv.visitJumpInsn(GOTO, defaultCase);
}

From source file:com.yahoo.yqlplus.engine.internal.compiler.CodeEmitter.java

public void emitStringSwitch(Map<String, Label> labels, Label defaultCase, boolean caseInsensitive) {
    MethodVisitor mv = getMethodVisitor();
    if (caseInsensitive) {
        mv.visitFieldInsn(GETSTATIC, "java/util/Locale", "ENGLISH", "Ljava/util/Locale;");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "toUpperCase",
                "(Ljava/util/Locale;)Ljava/lang/String;", false);
    }//from  w  ww. j a v  a 2  s.co m
    mv.visitInsn(DUP);
    AssignableValue tmp = allocate(String.class);
    tmp.write(BaseTypeAdapter.STRING).generate(this);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    Multimap<Integer, SwitchLabel> keys = ArrayListMultimap.create();
    for (Map.Entry<String, Label> e : labels.entrySet()) {
        String name = e.getKey();
        if (caseInsensitive) {
            name = name.toUpperCase(Locale.ENGLISH);
        }
        keys.put(name.hashCode(), new SwitchLabel(name, e.getValue()));
    }
    List<Integer> codes = Lists.newArrayList(keys.keySet());
    Collections.sort(codes);
    int[] k = new int[codes.size()];
    Label[] kl = new Label[codes.size()];
    for (int i = 0; i < k.length; ++i) {
        k[i] = codes.get(i);
        kl[i] = new Label();
    }
    mv.visitLookupSwitchInsn(defaultCase, k, kl);
    for (int i = 0; i < k.length; ++i) {
        mv.visitLabel(kl[i]);
        for (SwitchLabel switchLabel : keys.get(k[i])) {
            mv.visitLdcInsn(switchLabel.name);
            tmp.read().generate(this);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
            mv.visitJumpInsn(IFNE, switchLabel.label);
        }
        mv.visitJumpInsn(GOTO, defaultCase);
    }
}

From source file:com.yahoo.yqlplus.engine.internal.compiler.CompareExpression.java

@Override
public void generate(CodeEmitter code) {
    // a bit of a hack; should not need to go to dynamic invocation for this unless one arg is ANY
    Label done = new Label();
    MethodVisitor mv = code.getMethodVisitor();
    Label leftNull = new Label();
    Label rightNull = new Label();
    Label bothNull = new Label();
    CodeEmitter.Unification unified = code.unifiedEmit(leftExpr, rightExpr, leftNull, rightNull, bothNull);
    if (unified.type.isPrimitive()) {
        emitPrimitiveCompare(code, unified.type);
    } else {/*  w w w.  j a  v  a  2  s .c o  m*/
        // TODO: statically determine if the unified type is Comparable -- for now treat them all like "any"
        CodeEmitter scope = code.createScope();
        MethodVisitor mv2 = scope.getMethodVisitor();
        AssignableValue right = scope.allocate(unified.type);
        AssignableValue left = scope.allocate(unified.type);
        scope.exec(right.write(unified.type));
        scope.exec(left.write(unified.type));
        scope.exec(left.read());
        Label leftIsNotComparable = new Label();
        scope.emitInstanceCheck(unified.type, Comparable.class, leftIsNotComparable);
        scope.exec(right.read());
        mv2.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Comparable.class), "compareTo",
                Type.getMethodDescriptor(Type.INT_TYPE, Type.getType(Object.class)), true);
        scope.gotoExitScope();
        mv2.visitLabel(leftIsNotComparable);
        scope.exec(scope.getLocal("$program").read());
        scope.exec(left.read());
        scope.emitIntConstant((loc != null) ? loc.getLineNumber() : -1);
        scope.emitIntConstant((loc != null) ? loc.getCharacterOffset() : 0);
        mv2.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(ProgramInvocation.class),
                "notComparable", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class),
                        Type.INT_TYPE, Type.INT_TYPE),
                false);
        // this bit is not reachable, notComparable throws
        mv2.visitInsn(Opcodes.ICONST_0);
        mv2.visitJumpInsn(Opcodes.GOTO, done);
        scope.endScope();
    }
    if (unified.nullPossible) {
        mv.visitJumpInsn(Opcodes.GOTO, done);
        mv.visitLabel(leftNull);
        mv.visitInsn(Opcodes.ICONST_M1);
        mv.visitJumpInsn(Opcodes.GOTO, done);
        mv.visitLabel(rightNull);
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitJumpInsn(Opcodes.GOTO, done);
        mv.visitLabel(bothNull);
        mv.visitInsn(Opcodes.ICONST_0);
    }
    mv.visitLabel(done);
}

From source file:com.yahoo.yqlplus.engine.internal.compiler.EqualsExpression.java

@Override
public void generate(CodeEmitter code) {
    // a bit of a hack; should not need to go to dynamic invocation for this unless one arg is ANY
    MethodVisitor mv = code.getMethodVisitor();
    Label hasNull = new Label();
    CodeEmitter.Unification unified = code.unifiedEmit(leftExpr, rightExpr, hasNull);
    if (unified.type.isPrimitive()) {
        emitPrimitiveEquals(code, unified.type);
    } else {/*from   w  ww . j  a  v  a 2 s .  c om*/
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Object.class), "equals",
                Type.getMethodDescriptor(Type.BOOLEAN_TYPE, Type.getType(Object.class)), false);
        if (negate) {
            emitNegate(mv);
        }
    }
    if (unified.nullPossible) {
        Label done = new Label();
        mv.visitJumpInsn(Opcodes.GOTO, done);
        mv.visitLabel(hasNull);
        emitFalse(code);
        mv.visitLabel(done);
    }
}