Example usage for org.objectweb.asm Opcodes INVOKEVIRTUAL

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

Introduction

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

Prototype

int INVOKEVIRTUAL

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

Click Source Link

Usage

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

private BytecodeExpression getInjector(BytecodeExpression program, BytecodeExpression context) {
    return ExactInvocation.boundInvoke(Opcodes.INVOKEVIRTUAL, "getInjector",
            scope.adapt(ProgramInvocation.class, false), scope.adapt(Injector.class, false), program)
            .invoke(Location.NONE);
}

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

@Override
public GambitCreator.Invocable findExactInvoker(Class<?> owner, String methodName, TypeWidget returnType,
        List<TypeWidget> argumentTypes) {
    return ExactInvocation.exactInvoke(owner.isInterface() ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL,
            methodName, adapt(owner, false), returnType, argumentTypes);
}

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 w  w  w  .  j ava2  s  .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.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 {//from  w w  w.  jav  a  2  s. c  om
        // 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 {/*www.j a  v  a2  s  .  c  o  m*/
        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);
    }
}

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

public void invoke(CodeEmitter code) {
    code.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, unit.getInternalName(), name,
            createMethodDescriptor());
}

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

@Override
public GambitCreator.Invocable createInvocable() {
    int op = Opcodes.INVOKEVIRTUAL;
    if (Modifier.isStatic(modifiers)) {
        op = Opcodes.INVOKESTATIC;/*from  w ww  .ja va  2 s. co m*/
    }
    return ExactInvocation.exactInvoke(op, name, unit.getType(), getReturnType(), getArgumentTypes());
}

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

public TaskGenerator(ProgramGenerator program, GambitScope scope) {
    builder = scope.createObject();/*ww w . j  a va  2s .  c  o m*/
    builder.implement(Runnable.class);
    builder.addParameter("$program", program.getType());
    programGenerator = program;
    this.run = builder.method("run");
    GambitCreator.CatchBuilder catcher = run.tryCatchFinally();
    ScopedBuilder body = catcher.body();
    runBody = body.block();
    body.exec(new ReturnCode());
    ScopedBuilder handler = catcher.on("$e", Throwable.class);
    handler.exec(new BytecodeSequence() {
        @Override
        public void generate(CodeEmitter code) {
            code.exec(code.getLocal("$program"));
            code.exec(code.getLocal("$e"));
            code.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    Type.getInternalName(ProgramInvocation.class), "fail",
                    Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Throwable.class)), false);
            code.getMethodVisitor().visitInsn(Opcodes.RETURN);
        }
    });
    run.exec(catcher.build());
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.AnyTypeWidget.java

@Override
public ComparisonAdapter getComparisionAdapter() {
    Preconditions.checkState(!isPrimitive(), "BaseTypeWidget should not be handling a primitive type");
    return new ComparisonAdapter() {
        @Override//from   www. ja  va2  s . c o m
        public void coerceBoolean(CodeEmitter scope, Label isTrue, Label isFalse, Label isNull) {
            // null or true
            scope.nullTest(AnyTypeWidget.getInstance(), isNull);
            Label popTrue = new Label();
            scope.emitInstanceOf(AnyTypeWidget.getInstance(), Boolean.class, popTrue);
            final MethodVisitor mv = scope.getMethodVisitor();
            mv.visitMethodInsn(Opcodes.GETSTATIC, Type.getInternalName(Boolean.class), "TRUE",
                    Type.getMethodDescriptor(Type.getType(Boolean.class)), false);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Boolean.class), "equals",
                    Type.getMethodDescriptor(Type.BOOLEAN_TYPE, Type.getType(Object.class)), false);
            mv.visitJumpInsn(Opcodes.IFEQ, isFalse);
            mv.visitJumpInsn(Opcodes.GOTO, isTrue);
            mv.visitLabel(popTrue);
            scope.pop(AnyTypeWidget.getInstance());
            mv.visitJumpInsn(Opcodes.GOTO, isTrue);
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.EmptyObjectSerializer.java

@Override
public BytecodeSequence serializeTo(final BytecodeExpression source, final BytecodeExpression generator) {
    switch (encoding) {
    case JSON:/*from  w  w  w  .  j  ava  2s  . c  o m*/
        return new BytecodeSequence() {
            @Override
            public void generate(CodeEmitter code) {
                MethodVisitor mv = code.getMethodVisitor();
                final BytecodeExpression gen = code.evaluateOnce(generator);
                code.exec(gen);
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(JsonGenerator.class),
                        "writeStartObject", Type.getMethodDescriptor(Type.VOID_TYPE), false);
                code.exec(gen);
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(JsonGenerator.class),
                        "writeEndObject", Type.getMethodDescriptor(Type.VOID_TYPE), false);
            }
        };
    case TBIN:
        throw new TodoException();
    default:
        throw new UnsupportedOperationException();
    }
}