Search in sources :

Example 1 with NativeLookupResult

use of com.oracle.truffle.llvm.runtime.NativeContextExtension.NativeLookupResult in project graal by oracle.

the class AllocExternalSymbolNode method execute.

public LLVMPointer execute(LLVMScopeChain localScope, LLVMScopeChain globalScope, LLVMIntrinsicProvider intrinsicProvider, NativeContextExtension nativeContextExtension, LLVMContext context, LLVMDLOpen.RTLDFlags rtldFlags, LLVMSymbol symbol) {
    LLVMPointer pointerFromLocal = lookupFromScope(localScope, symbol, context);
    // The default case for active default flag is to search the local scope first.
    if (pointerFromLocal != null && isDefaultFlagActive(rtldFlags)) {
        return pointerFromLocal;
    }
    LLVMPointer pointerFromGlobal = lookupFromScope(globalScope, symbol, context);
    // the default flag, global scope takes priority if the default flag is not active.)
    if (pointerFromGlobal != null) {
        return pointerFromGlobal;
    }
    // flag is not active).
    if (pointerFromLocal != null) {
        return pointerFromLocal;
    }
    // Allocating a native global symbol to the symbol table as provided by the nfi context.
    if (symbol.isGlobalVariable()) {
        if (symbol.isExternalWeak()) {
            return LLVMNativePointer.createNull();
        } else if (nativeContextExtension != null) {
            NativeContextExtension.NativePointerIntoLibrary pointer = getNativePointer(nativeContextExtension, symbol);
            if (pointer != null) {
                return LLVMNativePointer.create(pointer.getAddress());
            }
            return null;
        }
    }
    // native function and intrinsic function.
    if (symbol.isFunction()) {
        if (symbol.isExternalWeak()) {
            return LLVMNativePointer.createNull();
        } else if (intrinsicProvider != null && intrinsicProvider.isIntrinsified(symbol.getName())) {
            LLVMFunctionCode functionCode = new LLVMFunctionCode(symbol.asFunction());
            LLVMFunctionDescriptor functionDescriptor = context.createFunctionDescriptor(symbol.asFunction(), functionCode);
            functionDescriptor.getFunctionCode().define(intrinsicProvider, nodeFactory);
            return LLVMManagedPointer.create(functionDescriptor);
        } else if (intrinsicProvider != null && !intrinsicProvider.isIntrinsified(symbol.getName()) && nativeContextExtension != null) {
            /*
                 * Currently native functions/globals that are not in the nfi context are not
                 * written into the symbol table. For function, another lookup will happen when
                 * something tries to call the function. (see {@link
                 * LLVMDispatchNode#doCachedNative}) The function will be taken from the filescope
                 * directly. Ideally the filescope and symbol table is in sync, and any lazy look up
                 * will resolve from the function code in the symbol table.
                 */
            NativeLookupResult nativeFunction = getNativeFunction(nativeContextExtension, symbol);
            if (nativeFunction != null) {
                LLVMFunctionDescriptor functionDescriptor = context.createFunctionDescriptor(symbol.asFunction(), new LLVMFunctionCode(symbol.asFunction()));
                functionDescriptor.getFunctionCode().define(new LLVMFunctionCode.NativeFunction(nativeFunction.getObject()));
                symbol.asFunction().setNFISymbol(nativeFunction.getObject());
                return LLVMManagedPointer.create(functionDescriptor);
            }
            return null;
        }
    }
    /*
         * Fallback for when the same symbol is being overwritten. There exists code where the
         * symbol is not there.
         */
    return null;
}
Also used : LLVMPointer(com.oracle.truffle.llvm.runtime.pointer.LLVMPointer) LLVMFunctionCode(com.oracle.truffle.llvm.runtime.LLVMFunctionCode) LLVMFunctionDescriptor(com.oracle.truffle.llvm.runtime.LLVMFunctionDescriptor) NativePointerIntoLibrary(com.oracle.truffle.llvm.runtime.NativeContextExtension.NativePointerIntoLibrary) NativeLookupResult(com.oracle.truffle.llvm.runtime.NativeContextExtension.NativeLookupResult)

Aggregations

LLVMFunctionCode (com.oracle.truffle.llvm.runtime.LLVMFunctionCode)1 LLVMFunctionDescriptor (com.oracle.truffle.llvm.runtime.LLVMFunctionDescriptor)1 NativeLookupResult (com.oracle.truffle.llvm.runtime.NativeContextExtension.NativeLookupResult)1 NativePointerIntoLibrary (com.oracle.truffle.llvm.runtime.NativeContextExtension.NativePointerIntoLibrary)1 LLVMPointer (com.oracle.truffle.llvm.runtime.pointer.LLVMPointer)1