Module llvm_executionengine

This header declares the C interface to libLLVMExecutionEngine.o, which implements various analyses of the LLVM IR.

Many exotic languages can interoperate with C code but have a harder time with C++ due to name mangling. So in addition to C, this interface enables tools written in such languages.

Types

GenericValueRef = ptr object 
ExecutionEngineRef = ptr object 
MCJITMemoryManagerRef = ptr object 
MCJITCompilerOptions = object 
  optLevel*: cuint
  codeModel*: CodeModel
  noFramePointerElim*: Bool
  enableFastISel*: Bool
  mcJMM*: MCJITMemoryManagerRef
MemoryManagerAllocateCodeSectionCallback = proc (opaque: pointer; size: uint; 
    alignment: cuint; sectionID: cuint; sectionName: cstring): ptr cuchar {.
    cdecl.}
MemoryManagerAllocateDataSectionCallback = proc (opaque: pointer; size: uint; 
    alignment: cuint; sectionID: cuint; sectionName: cstring; isReadOnly: Bool): ptr cuchar {.
    cdecl.}
MemoryManagerFinalizeMemoryCallback = proc (opaque: pointer; 
    errMsg: cstringArray): Bool {.cdecl.}
MemoryManagerDestroyCallback = proc (opaque: pointer) {.cdecl.}

Procs

proc linkInJIT() {.importc: "LLVMLinkInJIT", cdecl, dynlib: dllname.}
proc linkInMCJIT() {.importc: "LLVMLinkInMCJIT", cdecl, dynlib: dllname.}
proc linkInInterpreter() {.importc: "LLVMLinkInInterpreter", 
                           cdecl, dynlib: dllname.}
proc createGenericValueOfInt(ty: TypeRef; n: culonglong; isSigned: Bool): GenericValueRef {.
    importc: "LLVMCreateGenericValueOfInt", cdecl, dynlib: dllname.}
proc createGenericValueOfPointer(p: pointer): GenericValueRef {.
    importc: "LLVMCreateGenericValueOfPointer", cdecl, dynlib: dllname.}
proc createGenericValueOfFloat(ty: TypeRef; n: cdouble): GenericValueRef {.
    importc: "LLVMCreateGenericValueOfFloat", cdecl, dynlib: dllname.}
proc genericValueIntWidth(genValRef: GenericValueRef): cuint {.
    importc: "LLVMGenericValueIntWidth", cdecl, dynlib: dllname.}
proc genericValueToInt(genVal: GenericValueRef; isSigned: Bool): culonglong {.
    importc: "LLVMGenericValueToInt", cdecl, dynlib: dllname.}
proc genericValueToPointer(genVal: GenericValueRef): pointer {.
    importc: "LLVMGenericValueToPointer", cdecl, dynlib: dllname.}
proc genericValueToFloat(tyRef: TypeRef; genVal: GenericValueRef): cdouble {.
    importc: "LLVMGenericValueToFloat", cdecl, dynlib: dllname.}
proc disposeGenericValue(genVal: GenericValueRef) {.
    importc: "LLVMDisposeGenericValue", cdecl, dynlib: dllname.}
proc createExecutionEngineForModule(outEE: ptr ExecutionEngineRef; m: ModuleRef; 
                                    outError: cstringArray): Bool {.
    importc: "LLVMCreateExecutionEngineForModule", cdecl, dynlib: dllname.}
proc createInterpreterForModule(outInterp: ptr ExecutionEngineRef; m: ModuleRef; 
                                outError: cstringArray): Bool {.
    importc: "LLVMCreateInterpreterForModule", cdecl, dynlib: dllname.}
proc createJITCompilerForModule(outJIT: ptr ExecutionEngineRef; m: ModuleRef; 
                                optLevel: cuint; outError: cstringArray): Bool {.
    importc: "LLVMCreateJITCompilerForModule", cdecl, dynlib: dllname.}
proc initializeMCJITCompilerOptions(options: ptr MCJITCompilerOptions; 
                                    sizeOfOptions: csize) {.
    importc: "LLVMInitializeMCJITCompilerOptions", cdecl, dynlib: dllname.}
proc createMCJITCompilerForModule(outJIT: ptr ExecutionEngineRef; m: ModuleRef; 
                                  options: ptr MCJITCompilerOptions; 
                                  sizeOfOptions: csize; outError: cstringArray): Bool {.
    importc: "LLVMCreateMCJITCompilerForModule", cdecl, dynlib: dllname.}

Create an MCJIT execution engine for a module, with the given options. It is the responsibility of the caller to ensure that all fields in Options up to the given SizeOfOptions are initialized. It is correct to pass a smaller value of SizeOfOptions that omits some fields. The canonical way of using this is:

LLVMMCJITCompilerOptions options; LLVMInitializeMCJITCompilerOptions(&options, sizeof(options)); ... fill in those options you care about

LLVMCreateMCJITCompilerForModule(&jit, mod, &options, sizeof(options),
&error);

Note that this is also correct, though possibly suboptimal:

LLVMCreateMCJITCompilerForModule(&jit, mod, 0, 0, &error);

proc createExecutionEngine(outEE: ptr ExecutionEngineRef; mp: ModuleProviderRef; 
                           outError: cstringArray): Bool {.deprecated, 
    importc: "LLVMCreateExecutionEngine", cdecl, dynlib: dllname.}
Deprecated: Use LLVMCreateExecutionEngineForModule instead.
proc createInterpreter(outInterp: ptr ExecutionEngineRef; mp: ModuleProviderRef; 
                       outError: cstringArray): Bool {.deprecated, 
    importc: "LLVMCreateInterpreter", cdecl, dynlib: dllname.}
Deprecated: Use LLVMCreateInterpreterForModule instead.
proc createJITCompiler(outJIT: ptr ExecutionEngineRef; mp: ModuleProviderRef; 
                       optLevel: cuint; outError: cstringArray): Bool {.
    deprecated, importc: "LLVMCreateJITCompiler", cdecl, dynlib: dllname.}
Deprecated: Use LLVMCreateJITCompilerForModule instead.
proc disposeExecutionEngine(ee: ExecutionEngineRef) {.
    importc: "LLVMDisposeExecutionEngine", cdecl, dynlib: dllname.}
proc runStaticConstructors(ee: ExecutionEngineRef) {.
    importc: "LLVMRunStaticConstructors", cdecl, dynlib: dllname.}
proc runStaticDestructors(ee: ExecutionEngineRef) {.
    importc: "LLVMRunStaticDestructors", cdecl, dynlib: dllname.}
proc runFunctionAsMain(ee: ExecutionEngineRef; f: ValueRef; argC: cuint; 
                       argV: cstringArray; envP: cstringArray): cint {.
    importc: "LLVMRunFunctionAsMain", cdecl, dynlib: dllname.}
proc runFunction(ee: ExecutionEngineRef; f: ValueRef; numArgs: cuint; 
                 args: ptr GenericValueRef): GenericValueRef {.
    importc: "LLVMRunFunction", cdecl, dynlib: dllname.}
proc freeMachineCodeForFunction(ee: ExecutionEngineRef; f: ValueRef) {.
    importc: "LLVMFreeMachineCodeForFunction", cdecl, dynlib: dllname.}
proc addModule(ee: ExecutionEngineRef; m: ModuleRef) {.importc: "LLVMAddModule", 
    cdecl, dynlib: dllname.}
proc addModuleProvider(ee: ExecutionEngineRef; mp: ModuleProviderRef) {.
    importc: "LLVMAddModuleProvider", cdecl, dynlib: dllname.}
Deprecated: Use LLVMAddModule instead.
proc removeModule(ee: ExecutionEngineRef; m: ModuleRef; outMod: ptr ModuleRef; 
                  outError: cstringArray): Bool {.importc: "LLVMRemoveModule", 
    cdecl, dynlib: dllname.}
proc removeModuleProvider(ee: ExecutionEngineRef; mp: ModuleProviderRef; 
                          outMod: ptr ModuleRef; outError: cstringArray): Bool {.
    deprecated, importc: "LLVMRemoveModuleProvider", cdecl, dynlib: dllname.}
Deprecated: Use LLVMRemoveModule instead.
proc findFunction(ee: ExecutionEngineRef; name: cstring; outFn: ptr ValueRef): Bool {.
    importc: "LLVMFindFunction", cdecl, dynlib: dllname.}
proc recompileAndRelinkFunction(ee: ExecutionEngineRef; fn: ValueRef): pointer {.
    importc: "LLVMRecompileAndRelinkFunction", cdecl, dynlib: dllname.}
proc getExecutionEngineTargetData(ee: ExecutionEngineRef): TargetDataRef {.
    importc: "LLVMGetExecutionEngineTargetData", cdecl, dynlib: dllname.}
proc getExecutionEngineTargetMachine(ee: ExecutionEngineRef): TargetMachineRef {.
    importc: "LLVMGetExecutionEngineTargetMachine", cdecl, dynlib: dllname.}
proc addGlobalMapping(ee: ExecutionEngineRef; global: ValueRef; address: pointer) {.
    importc: "LLVMAddGlobalMapping", cdecl, dynlib: dllname.}
proc getPointerToGlobal(ee: ExecutionEngineRef; global: ValueRef): pointer {.
    importc: "LLVMGetPointerToGlobal", cdecl, dynlib: dllname.}
proc createSimpleMCJITMemoryManager(opaque: pointer; allocateCodeSection: MemoryManagerAllocateCodeSectionCallback; 
    allocateDataSection: MemoryManagerAllocateDataSectionCallback; 
    finalizeMemory: MemoryManagerFinalizeMemoryCallback; 
                                    destroy: MemoryManagerDestroyCallback): MCJITMemoryManagerRef {.
    importc: "LLVMCreateSimpleMCJITMemoryManager", cdecl, dynlib: dllname.}

Create a simple custom MCJIT memory manager. This memory manager can intercept allocations in a module-oblivious way. This will return NULL if any of the passed functions are NULL.

@param Opaque An opaque client object to pass back to the callbacks. @param AllocateCodeSection Allocate a block of memory for executable code. @param AllocateDataSection Allocate a block of memory for data.

@param FinalizeMemory Set page permissions and flush cache. Return 0 on
success, 1 on error.
proc disposeMCJITMemoryManager(mm: MCJITMemoryManagerRef) {.
    importc: "LLVMDisposeMCJITMemoryManager", cdecl, dynlib: dllname.}