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

时间:2012-08-31 03:42:15

标签: f# llvm dllimport llvm-c++-api llvm-fs

我正在使用llvm-fs绑定,我想调用的一个方法是createJITCompilerForModule,它是LLVM C api中本机方法LLVMCreateJITCompilerForModule的外部。 llvm-fs的作者声称他不能在F#中创建这个函数调用的“漂亮”版本:

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

你知道我将如何从F#中调用此函数,甚至是本机的函数吗?它看起来像是OutJIT的'out参数'(作为本机代码重新分配void*指向的东西。这是本机函数:

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;
}

1 个答案:

答案 0 :(得分:3)

我想要使用的实际功能是一个特殊的手工制作,因为它无法生成。我把这里作为如何调用它的例子:

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