Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 1 | //===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the C bindings for the ExecutionEngine library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 14 | #include "llvm-c/ExecutionEngine.h" |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 15 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/GenericValue.h" |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 17 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
Filip Pizlo | 40be1e8 | 2013-05-01 20:59:00 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DerivedTypes.h" |
| 19 | #include "llvm/IR/Module.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 21 | #include <cstring> |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 25 | #define DEBUG_TYPE "jit" |
| 26 | |
Eric Christopher | 3e39731 | 2013-04-22 22:47:22 +0000 | [diff] [blame] | 27 | // Wrapping the C bindings types. |
Filip Pizlo | 40be1e8 | 2013-05-01 20:59:00 +0000 | [diff] [blame] | 28 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef) |
Eric Christopher | 3e39731 | 2013-04-22 22:47:22 +0000 | [diff] [blame] | 29 | |
Eric Christopher | 3e39731 | 2013-04-22 22:47:22 +0000 | [diff] [blame] | 30 | inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) { |
| 31 | return reinterpret_cast<TargetLibraryInfo*>(P); |
| 32 | } |
| 33 | |
| 34 | inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) { |
| 35 | TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P); |
| 36 | return reinterpret_cast<LLVMTargetLibraryInfoRef>(X); |
| 37 | } |
| 38 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 39 | inline LLVMTargetMachineRef wrap(const TargetMachine *P) { |
| 40 | return |
| 41 | reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P)); |
| 42 | } |
| 43 | |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 44 | /*===-- Operations on generic values --------------------------------------===*/ |
| 45 | |
| 46 | LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty, |
| 47 | unsigned long long N, |
Chris Lattner | d686c8e | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 48 | LLVMBool IsSigned) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 49 | GenericValue *GenVal = new GenericValue(); |
| 50 | GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned); |
| 51 | return wrap(GenVal); |
| 52 | } |
| 53 | |
| 54 | LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) { |
| 55 | GenericValue *GenVal = new GenericValue(); |
| 56 | GenVal->PointerVal = P; |
| 57 | return wrap(GenVal); |
| 58 | } |
| 59 | |
| 60 | LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) { |
| 61 | GenericValue *GenVal = new GenericValue(); |
| 62 | switch (unwrap(TyRef)->getTypeID()) { |
| 63 | case Type::FloatTyID: |
| 64 | GenVal->FloatVal = N; |
| 65 | break; |
| 66 | case Type::DoubleTyID: |
| 67 | GenVal->DoubleVal = N; |
| 68 | break; |
| 69 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 70 | llvm_unreachable("LLVMGenericValueToFloat supports only float and double."); |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 71 | } |
| 72 | return wrap(GenVal); |
| 73 | } |
| 74 | |
| 75 | unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) { |
| 76 | return unwrap(GenValRef)->IntVal.getBitWidth(); |
| 77 | } |
| 78 | |
| 79 | unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef, |
Chris Lattner | d686c8e | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 80 | LLVMBool IsSigned) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 81 | GenericValue *GenVal = unwrap(GenValRef); |
| 82 | if (IsSigned) |
| 83 | return GenVal->IntVal.getSExtValue(); |
| 84 | else |
| 85 | return GenVal->IntVal.getZExtValue(); |
| 86 | } |
| 87 | |
| 88 | void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) { |
| 89 | return unwrap(GenVal)->PointerVal; |
| 90 | } |
| 91 | |
| 92 | double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) { |
| 93 | switch (unwrap(TyRef)->getTypeID()) { |
| 94 | case Type::FloatTyID: |
| 95 | return unwrap(GenVal)->FloatVal; |
| 96 | case Type::DoubleTyID: |
| 97 | return unwrap(GenVal)->DoubleVal; |
| 98 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 99 | llvm_unreachable("LLVMGenericValueToFloat supports only float and double."); |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) { |
| 104 | delete unwrap(GenVal); |
| 105 | } |
| 106 | |
| 107 | /*===-- Operations on execution engines -----------------------------------===*/ |
| 108 | |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 109 | LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE, |
| 110 | LLVMModuleRef M, |
| 111 | char **OutError) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 112 | std::string Error; |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 113 | EngineBuilder builder(unwrap(M)); |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 114 | builder.setEngineKind(EngineKind::Either) |
| 115 | .setErrorStr(&Error); |
| 116 | if (ExecutionEngine *EE = builder.create()){ |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 117 | *OutEE = wrap(EE); |
| 118 | return 0; |
| 119 | } |
| 120 | *OutError = strdup(Error.c_str()); |
| 121 | return 1; |
| 122 | } |
| 123 | |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 124 | LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp, |
| 125 | LLVMModuleRef M, |
| 126 | char **OutError) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 127 | std::string Error; |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 128 | EngineBuilder builder(unwrap(M)); |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 129 | builder.setEngineKind(EngineKind::Interpreter) |
| 130 | .setErrorStr(&Error); |
| 131 | if (ExecutionEngine *Interp = builder.create()) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 132 | *OutInterp = wrap(Interp); |
| 133 | return 0; |
| 134 | } |
| 135 | *OutError = strdup(Error.c_str()); |
| 136 | return 1; |
| 137 | } |
| 138 | |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 139 | LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, |
| 140 | LLVMModuleRef M, |
| 141 | unsigned OptLevel, |
| 142 | char **OutError) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 143 | std::string Error; |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 144 | EngineBuilder builder(unwrap(M)); |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 145 | builder.setEngineKind(EngineKind::JIT) |
| 146 | .setErrorStr(&Error) |
| 147 | .setOptLevel((CodeGenOpt::Level)OptLevel); |
| 148 | if (ExecutionEngine *JIT = builder.create()) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 149 | *OutJIT = wrap(JIT); |
| 150 | return 0; |
| 151 | } |
| 152 | *OutError = strdup(Error.c_str()); |
| 153 | return 1; |
| 154 | } |
| 155 | |
Filip Pizlo | 0e1327e | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 156 | void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions, |
| 157 | size_t SizeOfPassedOptions) { |
| 158 | LLVMMCJITCompilerOptions options; |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 159 | memset(&options, 0, sizeof(options)); // Most fields are zero by default. |
Filip Pizlo | 0e1327e | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 160 | options.CodeModel = LLVMCodeModelJITDefault; |
Filip Pizlo | 0e1327e | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 161 | |
| 162 | memcpy(PassedOptions, &options, |
| 163 | std::min(sizeof(options), SizeOfPassedOptions)); |
| 164 | } |
| 165 | |
| 166 | LLVMBool LLVMCreateMCJITCompilerForModule( |
| 167 | LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, |
| 168 | LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions, |
| 169 | char **OutError) { |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 170 | LLVMMCJITCompilerOptions options; |
| 171 | // If the user passed a larger sized options struct, then they were compiled |
| 172 | // against a newer LLVM. Tell them that something is wrong. |
| 173 | if (SizeOfPassedOptions > sizeof(options)) { |
| 174 | *OutError = strdup( |
Filip Pizlo | 0e1327e | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 175 | "Refusing to use options struct that is larger than my own; assuming " |
| 176 | "LLVM library mismatch."); |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 177 | return 1; |
| 178 | } |
| 179 | |
| 180 | // Defend against the user having an old version of the API by ensuring that |
| 181 | // any fields they didn't see are cleared. We must defend against fields being |
| 182 | // set to the bitwise equivalent of zero, and assume that this means "do the |
| 183 | // default" as if that option hadn't been available. |
Filip Pizlo | 0e1327e | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 184 | LLVMInitializeMCJITCompilerOptions(&options, sizeof(options)); |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 185 | memcpy(&options, PassedOptions, SizeOfPassedOptions); |
| 186 | |
| 187 | TargetOptions targetOptions; |
| 188 | targetOptions.NoFramePointerElim = options.NoFramePointerElim; |
Filip Pizlo | 0e1327e | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 189 | targetOptions.EnableFastISel = options.EnableFastISel; |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 190 | |
| 191 | std::string Error; |
| 192 | EngineBuilder builder(unwrap(M)); |
| 193 | builder.setEngineKind(EngineKind::JIT) |
| 194 | .setErrorStr(&Error) |
| 195 | .setUseMCJIT(true) |
| 196 | .setOptLevel((CodeGenOpt::Level)options.OptLevel) |
Filip Pizlo | 0e1327e | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 197 | .setCodeModel(unwrap(options.CodeModel)) |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 198 | .setTargetOptions(targetOptions); |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 199 | if (options.MCJMM) |
| 200 | builder.setMCJITMemoryManager(unwrap(options.MCJMM)); |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 201 | if (ExecutionEngine *JIT = builder.create()) { |
| 202 | *OutJIT = wrap(JIT); |
| 203 | return 0; |
| 204 | } |
| 205 | *OutError = strdup(Error.c_str()); |
| 206 | return 1; |
| 207 | } |
| 208 | |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 209 | LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE, |
| 210 | LLVMModuleProviderRef MP, |
| 211 | char **OutError) { |
| 212 | /* The module provider is now actually a module. */ |
| 213 | return LLVMCreateExecutionEngineForModule(OutEE, |
| 214 | reinterpret_cast<LLVMModuleRef>(MP), |
| 215 | OutError); |
| 216 | } |
| 217 | |
| 218 | LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp, |
| 219 | LLVMModuleProviderRef MP, |
| 220 | char **OutError) { |
| 221 | /* The module provider is now actually a module. */ |
| 222 | return LLVMCreateInterpreterForModule(OutInterp, |
| 223 | reinterpret_cast<LLVMModuleRef>(MP), |
| 224 | OutError); |
| 225 | } |
| 226 | |
| 227 | LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT, |
| 228 | LLVMModuleProviderRef MP, |
| 229 | unsigned OptLevel, |
| 230 | char **OutError) { |
| 231 | /* The module provider is now actually a module. */ |
| 232 | return LLVMCreateJITCompilerForModule(OutJIT, |
| 233 | reinterpret_cast<LLVMModuleRef>(MP), |
| 234 | OptLevel, OutError); |
| 235 | } |
| 236 | |
| 237 | |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 238 | void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) { |
| 239 | delete unwrap(EE); |
| 240 | } |
| 241 | |
| 242 | void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) { |
| 243 | unwrap(EE)->runStaticConstructorsDestructors(false); |
| 244 | } |
| 245 | |
| 246 | void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) { |
| 247 | unwrap(EE)->runStaticConstructorsDestructors(true); |
| 248 | } |
| 249 | |
| 250 | int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F, |
| 251 | unsigned ArgC, const char * const *ArgV, |
| 252 | const char * const *EnvP) { |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 253 | unwrap(EE)->finalizeObject(); |
| 254 | |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 255 | std::vector<std::string> ArgVec; |
| 256 | for (unsigned I = 0; I != ArgC; ++I) |
| 257 | ArgVec.push_back(ArgV[I]); |
| 258 | |
| 259 | return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP); |
| 260 | } |
| 261 | |
| 262 | LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F, |
| 263 | unsigned NumArgs, |
| 264 | LLVMGenericValueRef *Args) { |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 265 | unwrap(EE)->finalizeObject(); |
| 266 | |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 267 | std::vector<GenericValue> ArgVec; |
| 268 | ArgVec.reserve(NumArgs); |
| 269 | for (unsigned I = 0; I != NumArgs; ++I) |
| 270 | ArgVec.push_back(*unwrap(Args[I])); |
| 271 | |
| 272 | GenericValue *Result = new GenericValue(); |
| 273 | *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec); |
| 274 | return wrap(Result); |
| 275 | } |
| 276 | |
| 277 | void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) { |
| 278 | unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F)); |
| 279 | } |
| 280 | |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 281 | void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){ |
| 282 | unwrap(EE)->addModule(unwrap(M)); |
| 283 | } |
| 284 | |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 285 | void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){ |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 286 | /* The module provider is now actually a module. */ |
| 287 | LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP)); |
| 288 | } |
| 289 | |
| 290 | LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M, |
| 291 | LLVMModuleRef *OutMod, char **OutError) { |
| 292 | Module *Mod = unwrap(M); |
| 293 | unwrap(EE)->removeModule(Mod); |
| 294 | *OutMod = wrap(Mod); |
| 295 | return 0; |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Chris Lattner | d686c8e | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 298 | LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE, |
| 299 | LLVMModuleProviderRef MP, |
| 300 | LLVMModuleRef *OutMod, char **OutError) { |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 301 | /* The module provider is now actually a module. */ |
| 302 | return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod, |
| 303 | OutError); |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Chris Lattner | d686c8e | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 306 | LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name, |
| 307 | LLVMValueRef *OutFn) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 308 | if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) { |
| 309 | *OutFn = wrap(F); |
| 310 | return 0; |
| 311 | } |
| 312 | return 1; |
| 313 | } |
Erick Tryzelaar | 7c1483b | 2008-03-27 00:27:14 +0000 | [diff] [blame] | 314 | |
Filip Pizlo | 0e1327e | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 315 | void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, |
| 316 | LLVMValueRef Fn) { |
Duncan Sands | e117b63 | 2010-07-19 09:36:45 +0000 | [diff] [blame] | 317 | return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn)); |
Duncan Sands | d90fee9 | 2010-07-19 09:33:13 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Erick Tryzelaar | 7c1483b | 2008-03-27 00:27:14 +0000 | [diff] [blame] | 320 | LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 321 | return wrap(unwrap(EE)->getDataLayout()); |
Erick Tryzelaar | 7c1483b | 2008-03-27 00:27:14 +0000 | [diff] [blame] | 322 | } |
Gordon Henriksen | 54227f6 | 2008-06-20 02:16:11 +0000 | [diff] [blame] | 323 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 324 | LLVMTargetMachineRef |
| 325 | LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE) { |
| 326 | return wrap(unwrap(EE)->getTargetMachine()); |
| 327 | } |
| 328 | |
Gordon Henriksen | 54227f6 | 2008-06-20 02:16:11 +0000 | [diff] [blame] | 329 | void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global, |
| 330 | void* Addr) { |
| 331 | unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr); |
| 332 | } |
Chris Lattner | 1e42c5b | 2009-01-21 18:11:10 +0000 | [diff] [blame] | 333 | |
| 334 | void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) { |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 335 | unwrap(EE)->finalizeObject(); |
| 336 | |
Chris Lattner | 1e42c5b | 2009-01-21 18:11:10 +0000 | [diff] [blame] | 337 | return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global)); |
| 338 | } |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 339 | |
| 340 | /*===-- Operations on memory managers -------------------------------------===*/ |
| 341 | |
| 342 | namespace { |
| 343 | |
| 344 | struct SimpleBindingMMFunctions { |
Anders Waldenborg | 5be8123 | 2013-09-30 19:11:32 +0000 | [diff] [blame] | 345 | LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection; |
| 346 | LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection; |
| 347 | LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory; |
| 348 | LLVMMemoryManagerDestroyCallback Destroy; |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | class SimpleBindingMemoryManager : public RTDyldMemoryManager { |
| 352 | public: |
| 353 | SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions, |
| 354 | void *Opaque); |
| 355 | virtual ~SimpleBindingMemoryManager(); |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 356 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 357 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
| 358 | unsigned SectionID, |
| 359 | StringRef SectionName) override; |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 360 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 361 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
| 362 | unsigned SectionID, StringRef SectionName, |
| 363 | bool isReadOnly) override; |
| 364 | |
| 365 | bool finalizeMemory(std::string *ErrMsg) override; |
| 366 | |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 367 | private: |
| 368 | SimpleBindingMMFunctions Functions; |
| 369 | void *Opaque; |
| 370 | }; |
| 371 | |
| 372 | SimpleBindingMemoryManager::SimpleBindingMemoryManager( |
| 373 | const SimpleBindingMMFunctions& Functions, |
| 374 | void *Opaque) |
| 375 | : Functions(Functions), Opaque(Opaque) { |
| 376 | assert(Functions.AllocateCodeSection && |
| 377 | "No AllocateCodeSection function provided!"); |
| 378 | assert(Functions.AllocateDataSection && |
| 379 | "No AllocateDataSection function provided!"); |
| 380 | assert(Functions.FinalizeMemory && |
| 381 | "No FinalizeMemory function provided!"); |
| 382 | assert(Functions.Destroy && |
| 383 | "No Destroy function provided!"); |
| 384 | } |
| 385 | |
| 386 | SimpleBindingMemoryManager::~SimpleBindingMemoryManager() { |
| 387 | Functions.Destroy(Opaque); |
| 388 | } |
| 389 | |
| 390 | uint8_t *SimpleBindingMemoryManager::allocateCodeSection( |
Filip Pizlo | 6eb43d2 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 391 | uintptr_t Size, unsigned Alignment, unsigned SectionID, |
| 392 | StringRef SectionName) { |
| 393 | return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID, |
| 394 | SectionName.str().c_str()); |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | uint8_t *SimpleBindingMemoryManager::allocateDataSection( |
Filip Pizlo | 6eb43d2 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 398 | uintptr_t Size, unsigned Alignment, unsigned SectionID, |
| 399 | StringRef SectionName, bool isReadOnly) { |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 400 | return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID, |
Filip Pizlo | 6eb43d2 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 401 | SectionName.str().c_str(), |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 402 | isReadOnly); |
| 403 | } |
| 404 | |
| 405 | bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 406 | char *errMsgCString = nullptr; |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 407 | bool result = Functions.FinalizeMemory(Opaque, &errMsgCString); |
| 408 | assert((result || !errMsgCString) && |
| 409 | "Did not expect an error message if FinalizeMemory succeeded"); |
| 410 | if (errMsgCString) { |
| 411 | if (ErrMsg) |
| 412 | *ErrMsg = errMsgCString; |
| 413 | free(errMsgCString); |
| 414 | } |
| 415 | return result; |
| 416 | } |
| 417 | |
| 418 | } // anonymous namespace |
| 419 | |
| 420 | LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager( |
| 421 | void *Opaque, |
Anders Waldenborg | 5be8123 | 2013-09-30 19:11:32 +0000 | [diff] [blame] | 422 | LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection, |
| 423 | LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection, |
| 424 | LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory, |
| 425 | LLVMMemoryManagerDestroyCallback Destroy) { |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 426 | |
| 427 | if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory || |
| 428 | !Destroy) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 429 | return nullptr; |
Filip Pizlo | 6cfed36 | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 430 | |
| 431 | SimpleBindingMMFunctions functions; |
| 432 | functions.AllocateCodeSection = AllocateCodeSection; |
| 433 | functions.AllocateDataSection = AllocateDataSection; |
| 434 | functions.FinalizeMemory = FinalizeMemory; |
| 435 | functions.Destroy = Destroy; |
| 436 | return wrap(new SimpleBindingMemoryManager(functions, Opaque)); |
| 437 | } |
| 438 | |
| 439 | void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM) { |
| 440 | delete unwrap(MM); |
| 441 | } |
| 442 | |