Search in sources :

Example 1 with VirtualInvokeExpr

use of soot.jimple.VirtualInvokeExpr in project robovm by robovm.

the class MethodCompiler method invokeExpr.

private Value invokeExpr(Stmt stmt, InvokeExpr expr) {
    SootMethodRef methodRef = expr.getMethodRef();
    ArrayList<Value> args = new ArrayList<Value>();
    args.add(env);
    if (!(expr instanceof StaticInvokeExpr)) {
        Value base = immediate(stmt, (Immediate) ((InstanceInvokeExpr) expr).getBase());
        checkNull(stmt, base);
        args.add(base);
    }
    int i = 0;
    for (soot.Value sootArg : (List<soot.Value>) expr.getArgs()) {
        Value arg = immediate(stmt, (Immediate) sootArg);
        args.add(narrowFromI32Value(stmt, getType(methodRef.parameterType(i)), arg));
        i++;
    }
    Value result = null;
    FunctionRef functionRef = config.isDebug() ? null : Intrinsics.getIntrinsic(sootMethod, stmt, expr);
    if (functionRef == null) {
        Trampoline trampoline = null;
        String targetClassName = getInternalName(methodRef.declaringClass());
        String methodName = methodRef.name();
        String methodDesc = getDescriptor(methodRef);
        if (expr instanceof SpecialInvokeExpr) {
            soot.Type runtimeType = ((SpecialInvokeExpr) expr).getBase().getType();
            String runtimeClassName = runtimeType == NullType.v() ? targetClassName : getInternalName(runtimeType);
            trampoline = new Invokespecial(this.className, targetClassName, methodName, methodDesc, runtimeClassName);
        } else if (expr instanceof StaticInvokeExpr) {
            trampoline = new Invokestatic(this.className, targetClassName, methodName, methodDesc);
        } else if (expr instanceof VirtualInvokeExpr) {
            soot.Type runtimeType = ((VirtualInvokeExpr) expr).getBase().getType();
            String runtimeClassName = runtimeType == NullType.v() ? targetClassName : getInternalName(runtimeType);
            trampoline = new Invokevirtual(this.className, targetClassName, methodName, methodDesc, runtimeClassName);
        } else if (expr instanceof InterfaceInvokeExpr) {
            trampoline = new Invokeinterface(this.className, targetClassName, methodName, methodDesc);
        }
        trampolines.add(trampoline);
        if (canCallDirectly(expr)) {
            SootMethod method = this.sootMethod.getDeclaringClass().getMethod(methodRef.name(), methodRef.parameterTypes(), methodRef.returnType());
            if (method.isSynchronized()) {
                functionRef = FunctionBuilder.synchronizedWrapper(method).ref();
            } else {
                functionRef = createMethodFunction(method).ref();
            }
        } else {
            functionRef = trampoline.getFunctionRef();
        }
    }
    result = call(stmt, functionRef, args.toArray(new Value[0]));
    if (result != null) {
        return widenToI32Value(stmt, result, methodRef.returnType().equals(CharType.v()));
    } else {
        return null;
    }
}
Also used : Trampoline(org.robovm.compiler.trampoline.Trampoline) SootMethodRef(soot.SootMethodRef) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) ArrayList(java.util.ArrayList) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) InterfaceInvokeExpr(soot.jimple.InterfaceInvokeExpr) Invokeinterface(org.robovm.compiler.trampoline.Invokeinterface) Invokespecial(org.robovm.compiler.trampoline.Invokespecial) StaticInvokeExpr(soot.jimple.StaticInvokeExpr) Invokestatic(org.robovm.compiler.trampoline.Invokestatic) Value(org.robovm.compiler.llvm.Value) SootMethod(soot.SootMethod) ArrayList(java.util.ArrayList) List(java.util.List) VirtualInvokeExpr(soot.jimple.VirtualInvokeExpr) Invokevirtual(org.robovm.compiler.trampoline.Invokevirtual) FunctionRef(org.robovm.compiler.llvm.FunctionRef)

Example 2 with VirtualInvokeExpr

use of soot.jimple.VirtualInvokeExpr in project robovm by robovm.

the class MethodCompiler method canCallDirectly.

private boolean canCallDirectly(InvokeExpr expr) {
    if (expr instanceof InterfaceInvokeExpr) {
        // Never possible
        return false;
    }
    SootClass sootClass = this.sootMethod.getDeclaringClass();
    SootMethodRef methodRef = expr.getMethodRef();
    if (!methodRef.declaringClass().equals(sootClass)) {
        return false;
    }
    try {
        SootMethod method = sootClass.getMethod(methodRef.name(), methodRef.parameterTypes(), methodRef.returnType());
        if (method.isAbstract()) {
            return false;
        }
        /*
             * The method exists and isn't abstract. Non virtual (invokespecial) 
             * as well as static calls and calls to final methods can be done directly.
             */
        if (method.isStatic()) {
            // want an exception to be thrown so we need a trampoline.
            return expr instanceof StaticInvokeExpr;
        }
        if (expr instanceof SpecialInvokeExpr) {
            return true;
        }
        if (expr instanceof VirtualInvokeExpr) {
            // the method must be private
            return Modifier.isFinal(sootClass.getModifiers()) || Modifier.isFinal(method.getModifiers()) || method.isPrivate();
        }
        return false;
    } catch (RuntimeException e) {
        // isn't declared in the class.
        return false;
    }
}
Also used : StaticInvokeExpr(soot.jimple.StaticInvokeExpr) SootMethodRef(soot.SootMethodRef) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) InterfaceInvokeExpr(soot.jimple.InterfaceInvokeExpr) SootMethod(soot.SootMethod) SootClass(soot.SootClass) VirtualInvokeExpr(soot.jimple.VirtualInvokeExpr)

Aggregations

SootMethod (soot.SootMethod)2 SootMethodRef (soot.SootMethodRef)2 InterfaceInvokeExpr (soot.jimple.InterfaceInvokeExpr)2 SpecialInvokeExpr (soot.jimple.SpecialInvokeExpr)2 StaticInvokeExpr (soot.jimple.StaticInvokeExpr)2 VirtualInvokeExpr (soot.jimple.VirtualInvokeExpr)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FunctionRef (org.robovm.compiler.llvm.FunctionRef)1 Value (org.robovm.compiler.llvm.Value)1 Invokeinterface (org.robovm.compiler.trampoline.Invokeinterface)1 Invokespecial (org.robovm.compiler.trampoline.Invokespecial)1 Invokestatic (org.robovm.compiler.trampoline.Invokestatic)1 Invokevirtual (org.robovm.compiler.trampoline.Invokevirtual)1 Trampoline (org.robovm.compiler.trampoline.Trampoline)1 SootClass (soot.SootClass)1 InstanceInvokeExpr (soot.jimple.InstanceInvokeExpr)1