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