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