简体   繁体   English

如何从F#调用此本机函数? (LLVMCreateJITCompilerForModule)

[英]How can I call this native function from F#? (LLVMCreateJITCompilerForModule)

I'm using the llvm-fs bindings and one method I would like to call is createJITCompilerForModule which is an extern to the native method LLVMCreateJITCompilerForModule in the LLVM C api. 我正在使用llvm-fs绑定,我想调用的一个方法是createJITCompilerForModule ,它是LLVM C api中本机方法LLVMCreateJITCompilerForModule的外部。 The author of llvm-fs has stated he can't make a 'nice' version of this function call in F#: llvm-fs的作者声称他不能在F#中创建这个函数调用的“漂亮”版本:

createJITCompilerForModule in llvm-fs:Generated.fs : createJITCompilerForModule in llvm-fs:Generated.fs

[<DllImport(
    "LLVM-3.1.dll",
    EntryPoint="LLVMCreateJITCompilerForModule",
    CallingConvention=CallingConvention.Cdecl,
    CharSet=CharSet.Ansi)>]
extern bool createJITCompilerForModuleNative(
    void* (* LLVMExecutionEngineRef* *) OutJIT,
    void* (* LLVMModuleRef *) M,
    uint32 OptLevel,
    void* OutError)
// I don't know how to generate an "F# friendly" version of LLVMCreateJITCompilerForModule

Do you know how I would call this function from F#, or even what the native one does? 你知道我如何从F#中调用这个函数,甚至是本机的函数吗? It looks like it has an 'out parameter' for OutJIT (as the native code reassigns a thing the void* points to). 看起来它有一个OutJIT的'out参数'(因为本机代码重新指定了void*指向的东西)。 Here is the native function: 这是本机功能:

LLVMCreateJITCompilerForModule in llvm-c:ExecutionEngineBindings.cpp : LLVMCreateJITCompilerForModule in llvm-c:ExecutionEngineBindings.cpp

LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
                                        LLVMModuleRef M,
                                        unsigned OptLevel,
                                        char **OutError) {
    std::string Error;
    EngineBuilder builder(unwrap(M));
    builder.setEngineKind(EngineKind::JIT)
           .setErrorStr(&Error)
           .setOptLevel((CodeGenOpt::Level)OptLevel);
    if (ExecutionEngine *JIT = builder.create()) {
        *OutJIT = wrap(JIT);
        return 0;
    }
    *OutError = strdup(Error.c_str());
    return 1;
}

The actual function I wanted to use was a special hand made one as it couldn't be generated. 我想要使​​用的实际功能是一个特殊的手工制作,因为它无法生成。 I've put it here as an example of how to call it: 我把这里作为如何调用它的例子:

llvm-fs:ExecutionEngine.fs

let private createEngineForModuleFromNativeFunc
        (nativeFunc : (nativeint * nativeint * nativeint) -> bool)
        (moduleRef : ModuleRef) =

    use outEnginePtr = new NativePtrs([|0n|])
    use outErrPtr = new NativePtrs([|0n|])
    let createFailed =
            nativeFunc (
                outEnginePtr.Ptrs,
                moduleRef.Ptr,
                outErrPtr.Ptrs)
    if createFailed then
        let errStr = Marshal.PtrToStringAuto (Marshal.ReadIntPtr outErrPtr.Ptrs)
        Marshal.FreeHGlobal (Marshal.ReadIntPtr outErrPtr.Ptrs)
        failwith errStr
    else
        ExecutionEngineRef (Marshal.ReadIntPtr outEnginePtr.Ptrs)

let createJITCompilerForModule (modRef : ModuleRef) (optLvl : uint32) =
    let f (engPtr, modPtr, outErrPtr) =
        createJITCompilerForModuleNative (engPtr, modPtr, optLvl, outErrPtr)

    createEngineForModuleFromNativeFunc f modRef

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM