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