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