1 /*===-- llvm-c/ExecutionEngine.h - ExecutionEngine Lib C Iface --*- C++ -*-===*\ 2 |* *| 3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *| 4 |* Exceptions. *| 5 |* See https://llvm.org/LICENSE.txt for license information. *| 6 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *| 7 |* *| 8 |*===----------------------------------------------------------------------===*| 9 |* *| 10 |* This header declares the C interface to libLLVMExecutionEngine.o, which *| 11 |* implements various analyses of the LLVM IR. *| 12 |* *| 13 |* Many exotic languages can interoperate with C code but have a harder time *| 14 |* with C++ due to name mangling. So in addition to C, this interface enables *| 15 |* tools written in such languages. *| 16 |* *| 17 \*===----------------------------------------------------------------------===*/ 18 19 #ifndef LLVM_C_EXECUTIONENGINE_H 20 #define LLVM_C_EXECUTIONENGINE_H 21 22 #include "llvm-c/ExternC.h" 23 #include "llvm-c/Target.h" 24 #include "llvm-c/TargetMachine.h" 25 #include "llvm-c/Types.h" 26 #include "llvm-c/Visibility.h" 27 28 LLVM_C_EXTERN_C_BEGIN 29 30 /** 31 * @defgroup LLVMCExecutionEngine Execution Engine 32 * @ingroup LLVMC 33 * 34 * @{ 35 */ 36 37 /** 38 * Empty function used to force the linker to link MCJIT. 39 * Has no effect when called on a pre-built library (dylib interface). 40 */ 41 LLVM_C_ABI void LLVMLinkInMCJIT(void); 42 /** 43 * Empty function used to force the linker to link the LLVM interpreter. 44 * Has no effect when called on a pre-built library (dylib interface). 45 */ 46 LLVM_C_ABI void LLVMLinkInInterpreter(void); 47 48 typedef struct LLVMOpaqueGenericValue *LLVMGenericValueRef; 49 typedef struct LLVMOpaqueExecutionEngine *LLVMExecutionEngineRef; 50 typedef struct LLVMOpaqueMCJITMemoryManager *LLVMMCJITMemoryManagerRef; 51 52 struct LLVMMCJITCompilerOptions { 53 unsigned OptLevel; 54 LLVMCodeModel CodeModel; 55 LLVMBool NoFramePointerElim; 56 LLVMBool EnableFastISel; 57 LLVMMCJITMemoryManagerRef MCJMM; 58 }; 59 60 /*===-- Operations on generic values --------------------------------------===*/ 61 62 LLVM_C_ABI LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty, 63 unsigned long long N, 64 LLVMBool IsSigned); 65 66 LLVM_C_ABI LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P); 67 68 LLVM_C_ABI LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef Ty, 69 double N); 70 71 LLVM_C_ABI unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef); 72 73 LLVM_C_ABI unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenVal, 74 LLVMBool IsSigned); 75 76 LLVM_C_ABI void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal); 77 78 LLVM_C_ABI double LLVMGenericValueToFloat(LLVMTypeRef TyRef, 79 LLVMGenericValueRef GenVal); 80 81 LLVM_C_ABI void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal); 82 83 /*===-- Operations on execution engines -----------------------------------===*/ 84 85 LLVM_C_ABI LLVMBool LLVMCreateExecutionEngineForModule( 86 LLVMExecutionEngineRef *OutEE, LLVMModuleRef M, char **OutError); 87 88 LLVM_C_ABI LLVMBool LLVMCreateInterpreterForModule( 89 LLVMExecutionEngineRef *OutInterp, LLVMModuleRef M, char **OutError); 90 91 LLVM_C_ABI LLVMBool 92 LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, 93 unsigned OptLevel, char **OutError); 94 95 LLVM_C_ABI void 96 LLVMInitializeMCJITCompilerOptions(struct LLVMMCJITCompilerOptions *Options, 97 size_t SizeOfOptions); 98 99 /** 100 * Create an MCJIT execution engine for a module, with the given options. It is 101 * the responsibility of the caller to ensure that all fields in Options up to 102 * the given SizeOfOptions are initialized. It is correct to pass a smaller 103 * value of SizeOfOptions that omits some fields. The canonical way of using 104 * this is: 105 * 106 * LLVMMCJITCompilerOptions options; 107 * LLVMInitializeMCJITCompilerOptions(&options, sizeof(options)); 108 * ... fill in those options you care about 109 * LLVMCreateMCJITCompilerForModule(&jit, mod, &options, sizeof(options), 110 * &error); 111 * 112 * Note that this is also correct, though possibly suboptimal: 113 * 114 * LLVMCreateMCJITCompilerForModule(&jit, mod, 0, 0, &error); 115 */ 116 LLVM_C_ABI LLVMBool LLVMCreateMCJITCompilerForModule( 117 LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, 118 struct LLVMMCJITCompilerOptions *Options, size_t SizeOfOptions, 119 char **OutError); 120 121 LLVM_C_ABI void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE); 122 123 LLVM_C_ABI void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE); 124 125 LLVM_C_ABI void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE); 126 127 LLVM_C_ABI int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F, 128 unsigned ArgC, const char *const *ArgV, 129 const char *const *EnvP); 130 131 LLVM_C_ABI LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, 132 LLVMValueRef F, unsigned NumArgs, 133 LLVMGenericValueRef *Args); 134 135 LLVM_C_ABI void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, 136 LLVMValueRef F); 137 138 LLVM_C_ABI void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M); 139 140 LLVM_C_ABI LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M, 141 LLVMModuleRef *OutMod, char **OutError); 142 143 LLVM_C_ABI LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, 144 const char *Name, LLVMValueRef *OutFn); 145 146 LLVM_C_ABI void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, 147 LLVMValueRef Fn); 148 149 LLVM_C_ABI LLVMTargetDataRef 150 LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE); 151 LLVM_C_ABI LLVMTargetMachineRef 152 LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE); 153 154 LLVM_C_ABI void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, 155 LLVMValueRef Global, void *Addr); 156 157 LLVM_C_ABI void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, 158 LLVMValueRef Global); 159 160 LLVM_C_ABI uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, 161 const char *Name); 162 163 LLVM_C_ABI uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, 164 const char *Name); 165 166 /// Returns true on error, false on success. If true is returned then the error 167 /// message is copied to OutStr and cleared in the ExecutionEngine instance. 168 LLVM_C_ABI LLVMBool LLVMExecutionEngineGetErrMsg(LLVMExecutionEngineRef EE, 169 char **OutError); 170 171 /*===-- Operations on memory managers -------------------------------------===*/ 172 173 typedef uint8_t *(*LLVMMemoryManagerAllocateCodeSectionCallback)( 174 void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID, 175 const char *SectionName); 176 typedef uint8_t *(*LLVMMemoryManagerAllocateDataSectionCallback)( 177 void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID, 178 const char *SectionName, LLVMBool IsReadOnly); 179 typedef LLVMBool (*LLVMMemoryManagerFinalizeMemoryCallback)( 180 void *Opaque, char **ErrMsg); 181 typedef void (*LLVMMemoryManagerDestroyCallback)(void *Opaque); 182 183 /** 184 * Create a simple custom MCJIT memory manager. This memory manager can 185 * intercept allocations in a module-oblivious way. This will return NULL 186 * if any of the passed functions are NULL. 187 * 188 * @param Opaque An opaque client object to pass back to the callbacks. 189 * @param AllocateCodeSection Allocate a block of memory for executable code. 190 * @param AllocateDataSection Allocate a block of memory for data. 191 * @param FinalizeMemory Set page permissions and flush cache. Return 0 on 192 * success, 1 on error. 193 */ 194 LLVM_C_ABI LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager( 195 void *Opaque, 196 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection, 197 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection, 198 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory, 199 LLVMMemoryManagerDestroyCallback Destroy); 200 201 LLVM_C_ABI void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM); 202 203 /*===-- JIT Event Listener functions -------------------------------------===*/ 204 205 LLVM_C_ABI LLVMJITEventListenerRef LLVMCreateGDBRegistrationListener(void); 206 LLVM_C_ABI LLVMJITEventListenerRef LLVMCreateIntelJITEventListener(void); 207 LLVM_C_ABI LLVMJITEventListenerRef LLVMCreateOProfileJITEventListener(void); 208 LLVM_C_ABI LLVMJITEventListenerRef LLVMCreatePerfJITEventListener(void); 209 210 /** 211 * @} 212 */ 213 214 LLVM_C_EXTERN_C_END 215 216 #endif 217