Search in sources :

Example 31 with FunctionType

use of org.robovm.compiler.llvm.FunctionType in project robovm by robovm.

the class NativeMethodCompiler method createNative.

private FunctionRef createNative(ModuleBuilder mb, SootMethod method) {
    String targetInternalName = getInternalName(method.getDeclaringClass());
    String methodName = method.getName();
    String methodDesc = getDescriptor(method);
    FunctionType nativeFunctionType = Types.getNativeFunctionType(methodDesc, method.isStatic());
    String shortName = mangleNativeMethod(targetInternalName, methodName);
    String longName = mangleNativeMethod(targetInternalName, methodName, methodDesc);
    /*
         * To support statically linked native method implementation we create
         * weak stub functions with the same names as the expected JNI functions
         * (long and short names). These will be discarded by the linker if
         * proper functions are available at link time.
         * 
         * The weak stub with the short JNI name just calls the weak stub with
         * the long name.
         * 
         * The weak stub with the long name calls _bcResolveNative() which will
         * try to resolve the native method against dynamically loaded JNI libs.
         * If _bcResolveNative() finds a matching symbol in a dynamic lib or an
         * implementation has previously been registered using JNI
         * RegisterNatives() that will be stored in the native method pointer
         * passed to it and returned. The stub will call the implementation
         * returned by _bcResolveNative(). If no implementation can be found
         * _bcResolveNative() throws an UnsatisfiedLinkError and doesn't return
         * to the stub.
         * 
         * The limitation of this approach is that RegisterNatives() only works
         * for dynamically linked native methods and can only be used prior to
         * the first call of such a method. Native methods can never be rewired
         * or unregistered.
         */
    /*
         * The function with the long JNI name. This is the one that calls
         * _bcResolveNative() and then calls the implementation.
         */
    Function fn = new FunctionBuilder(longName, nativeFunctionType).linkage(weak).build();
    Global g = new Global(Symbols.nativeMethodPtrSymbol(targetInternalName, methodName, methodDesc), new NullConstant(I8_PTR));
    mb.addGlobal(g);
    FunctionRef ldcFn = FunctionBuilder.ldcInternal(targetInternalName).ref();
    Value theClass = call(fn, ldcFn, fn.getParameterRef(0));
    Value implI8Ptr = call(fn, BC_RESOLVE_NATIVE, fn.getParameterRef(0), theClass, mb.getString(methodName), mb.getString(methodDesc), mb.getString(shortName), mb.getString(longName), g.ref());
    Variable nullTest = fn.newVariable(I1);
    fn.add(new Icmp(nullTest, Condition.ne, implI8Ptr, new NullConstant(I8_PTR)));
    Label trueLabel = new Label();
    Label falseLabel = new Label();
    fn.add(new Br(nullTest.ref(), fn.newBasicBlockRef(trueLabel), fn.newBasicBlockRef(falseLabel)));
    fn.newBasicBlock(falseLabel);
    if (fn.getType().getReturnType() instanceof IntegerType) {
        fn.add(new Ret(new IntegerConstant(0, (IntegerType) fn.getType().getReturnType())));
    } else if (fn.getType().getReturnType() instanceof FloatingPointType) {
        fn.add(new Ret(new FloatingPointConstant(0.0, (FloatingPointType) fn.getType().getReturnType())));
    } else if (fn.getType().getReturnType() instanceof PointerType) {
        fn.add(new Ret(new NullConstant((PointerType) fn.getType().getReturnType())));
    } else {
        fn.add(new Ret());
    }
    fn.newBasicBlock(trueLabel);
    Variable impl = fn.newVariable(nativeFunctionType);
    fn.add(new Bitcast(impl, implI8Ptr, impl.getType()));
    Value result = call(fn, impl.ref(), fn.getParameterRefs());
    fn.add(new Ret(result));
    mb.addFunction(fn);
    FunctionRef targetFn = fn.ref();
    if (!isLongNativeFunctionNameRequired(method)) {
        /*
             * Generate a function with the short JNI name. This just calls the
             * function with the long name.
             */
        Function fnShort = new FunctionBuilder(shortName, nativeFunctionType).linkage(weak).build();
        Value resultInner = call(fnShort, fn.ref(), fnShort.getParameterRefs());
        fnShort.add(new Ret(resultInner));
        mb.addFunction(fnShort);
        targetFn = fnShort.ref();
    }
    return targetFn;
}
Also used : Ret(org.robovm.compiler.llvm.Ret) Variable(org.robovm.compiler.llvm.Variable) FloatingPointConstant(org.robovm.compiler.llvm.FloatingPointConstant) FunctionType(org.robovm.compiler.llvm.FunctionType) Label(org.robovm.compiler.llvm.Label) NullConstant(org.robovm.compiler.llvm.NullConstant) PointerType(org.robovm.compiler.llvm.PointerType) FloatingPointType(org.robovm.compiler.llvm.FloatingPointType) Global(org.robovm.compiler.llvm.Global) IntegerConstant(org.robovm.compiler.llvm.IntegerConstant) Br(org.robovm.compiler.llvm.Br) IntegerType(org.robovm.compiler.llvm.IntegerType) Function(org.robovm.compiler.llvm.Function) Bitcast(org.robovm.compiler.llvm.Bitcast) Value(org.robovm.compiler.llvm.Value) FunctionRef(org.robovm.compiler.llvm.FunctionRef) Icmp(org.robovm.compiler.llvm.Icmp)

Example 32 with FunctionType

use of org.robovm.compiler.llvm.FunctionType in project robovm by robovm.

the class Types method getFunctionType.

private static FunctionType getFunctionType(String methodDesc, boolean ztatic, boolean nativ) {
    List<Type> paramTypes = new ArrayList<Type>();
    paramTypes.add(ENV_PTR);
    if (!ztatic) {
        paramTypes.add(OBJECT_PTR);
    } else if (nativ) {
        paramTypes.add(OBJECT_PTR);
    }
    CharBuffer buffer = CharBuffer.wrap(methodDesc);
    // Skip initial (
    buffer.get();
    while (buffer.get(buffer.position()) != ')') {
        paramTypes.add(getType(buffer));
    }
    // Skip ending )
    buffer.get();
    Type returnType = getType(buffer);
    return new FunctionType(returnType, paramTypes.toArray(new Type[paramTypes.size()]));
}
Also used : PointerType(org.robovm.compiler.llvm.PointerType) RefType(soot.RefType) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) ShortType(soot.ShortType) PackedStructureType(org.robovm.compiler.llvm.PackedStructureType) CharType(soot.CharType) LongType(soot.LongType) IntegerType(org.robovm.compiler.llvm.IntegerType) BooleanType(soot.BooleanType) OpaqueType(org.robovm.compiler.llvm.OpaqueType) StructureType(org.robovm.compiler.llvm.StructureType) RefLikeType(soot.RefLikeType) ByteType(soot.ByteType) Type(org.robovm.compiler.llvm.Type) BottomType(soot.jimple.toolkits.typing.fast.BottomType) AggregateType(org.robovm.compiler.llvm.AggregateType) PrimType(soot.PrimType) VoidType(soot.VoidType) FunctionType(org.robovm.compiler.llvm.FunctionType) FunctionType(org.robovm.compiler.llvm.FunctionType) ArrayList(java.util.ArrayList) CharBuffer(java.nio.CharBuffer)

Example 33 with FunctionType

use of org.robovm.compiler.llvm.FunctionType in project robovm by robovm.

the class Types method getFunctionType.

@SuppressWarnings("unchecked")
public static FunctionType getFunctionType(SootMethodRef methodRef) {
    Type returnType = getType(methodRef.returnType());
    Type[] paramTypes = new Type[(methodRef.isStatic() ? 1 : 2) + methodRef.parameterTypes().size()];
    int i = 0;
    paramTypes[i++] = ENV_PTR;
    if (!methodRef.isStatic()) {
        paramTypes[i++] = OBJECT_PTR;
    }
    for (soot.Type t : (List<soot.Type>) methodRef.parameterTypes()) {
        paramTypes[i++] = getType(t);
    }
    return new FunctionType(returnType, paramTypes);
}
Also used : PointerType(org.robovm.compiler.llvm.PointerType) RefType(soot.RefType) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) ShortType(soot.ShortType) PackedStructureType(org.robovm.compiler.llvm.PackedStructureType) CharType(soot.CharType) LongType(soot.LongType) IntegerType(org.robovm.compiler.llvm.IntegerType) BooleanType(soot.BooleanType) OpaqueType(org.robovm.compiler.llvm.OpaqueType) StructureType(org.robovm.compiler.llvm.StructureType) RefLikeType(soot.RefLikeType) ByteType(soot.ByteType) Type(org.robovm.compiler.llvm.Type) BottomType(soot.jimple.toolkits.typing.fast.BottomType) AggregateType(org.robovm.compiler.llvm.AggregateType) PrimType(soot.PrimType) VoidType(soot.VoidType) FunctionType(org.robovm.compiler.llvm.FunctionType) FunctionType(org.robovm.compiler.llvm.FunctionType) ArrayList(java.util.ArrayList) List(java.util.List) ConstantPtrtoint(org.robovm.compiler.llvm.ConstantPtrtoint)

Aggregations

FunctionType (org.robovm.compiler.llvm.FunctionType)33 PointerType (org.robovm.compiler.llvm.PointerType)16 Test (org.junit.Test)15 StructureType (org.robovm.compiler.llvm.StructureType)15 Type (org.robovm.compiler.llvm.Type)12 FunctionRef (org.robovm.compiler.llvm.FunctionRef)10 Variable (org.robovm.compiler.llvm.Variable)8 ArrayList (java.util.ArrayList)7 Function (org.robovm.compiler.llvm.Function)7 Value (org.robovm.compiler.llvm.Value)7 IntegerType (org.robovm.compiler.llvm.IntegerType)6 Ret (org.robovm.compiler.llvm.Ret)6 ArrayType (org.robovm.compiler.llvm.ArrayType)5 Global (org.robovm.compiler.llvm.Global)5 IntegerConstant (org.robovm.compiler.llvm.IntegerConstant)5 LongType (soot.LongType)5 PrimType (soot.PrimType)5 Call (org.robovm.compiler.llvm.Call)4 Label (org.robovm.compiler.llvm.Label)4 NullConstant (org.robovm.compiler.llvm.NullConstant)4