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 | |
Eric Christopher | 04d4e93 | 2013-04-22 22:47:22 +0000 | [diff] [blame] | 30 | |
Juergen Ributzka | 5fe955c | 2014-01-23 19:23:28 +0000 | [diff] [blame] | 31 | inline LLVMTargetMachineRef wrap(const TargetMachine *P) { |
| 32 | return |
| 33 | reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P)); |
| 34 | } |
| 35 | |
Gordon Henriksen | 2a8cd89 | 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 | 25963c6 | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 40 | LLVMBool IsSigned) { |
Gordon Henriksen | 2a8cd89 | 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 | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 62 | llvm_unreachable("LLVMGenericValueToFloat supports only float and double."); |
Gordon Henriksen | 2a8cd89 | 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 | 25963c6 | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 72 | LLVMBool IsSigned) { |
Gordon Henriksen | 2a8cd89 | 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 | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 91 | llvm_unreachable("LLVMGenericValueToFloat supports only float and double."); |
Gordon Henriksen | 2a8cd89 | 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 | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 101 | LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE, |
| 102 | LLVMModuleRef M, |
| 103 | char **OutError) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 104 | std::string Error; |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 105 | EngineBuilder builder(std::unique_ptr<Module>(unwrap(M))); |
Reid Kleckner | fc8a2d5 | 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 | 2a8cd89 | 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 | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 116 | LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp, |
| 117 | LLVMModuleRef M, |
| 118 | char **OutError) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 119 | std::string Error; |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 120 | EngineBuilder builder(std::unique_ptr<Module>(unwrap(M))); |
Reid Kleckner | fc8a2d5 | 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 | 2a8cd89 | 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 | ad0e0cb | 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 | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 135 | std::string Error; |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 136 | EngineBuilder builder(std::unique_ptr<Module>(unwrap(M))); |
Reid Kleckner | fc8a2d5 | 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 | 2a8cd89 | 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 | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 148 | void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions, |
| 149 | size_t SizeOfPassedOptions) { |
| 150 | LLVMMCJITCompilerOptions options; |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 151 | memset(&options, 0, sizeof(options)); // Most fields are zero by default. |
Filip Pizlo | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 152 | options.CodeModel = LLVMCodeModelJITDefault; |
Filip Pizlo | 85e0d27 | 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 | 31be5ef | 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 | 85e0d27 | 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 | 31be5ef | 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 | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 176 | LLVMInitializeMCJITCompilerOptions(&options, sizeof(options)); |
Andrew Kaylor | 31be5ef | 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 | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 181 | targetOptions.EnableFastISel = options.EnableFastISel; |
Akira Hatanaka | ddf76aa | 2015-05-23 01:14:08 +0000 | [diff] [blame^] | 182 | std::unique_ptr<Module> Mod(unwrap(M)); |
| 183 | |
| 184 | if (Mod) |
| 185 | // Set function attribute "no-frame-pointer-elim" based on |
| 186 | // NoFramePointerElim. |
| 187 | setFunctionAttributes(/* CPU */ "", /* Features */ "", targetOptions, *Mod, |
| 188 | /* AlwaysRecordAttrs */ true); |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 189 | |
| 190 | std::string Error; |
Akira Hatanaka | ddf76aa | 2015-05-23 01:14:08 +0000 | [diff] [blame^] | 191 | EngineBuilder builder(std::move(Mod)); |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 192 | builder.setEngineKind(EngineKind::JIT) |
| 193 | .setErrorStr(&Error) |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 194 | .setOptLevel((CodeGenOpt::Level)options.OptLevel) |
Filip Pizlo | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 195 | .setCodeModel(unwrap(options.CodeModel)) |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 196 | .setTargetOptions(targetOptions); |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 197 | if (options.MCJMM) |
Lang Hames | 4a5697e | 2014-12-03 00:51:19 +0000 | [diff] [blame] | 198 | builder.setMCJITMemoryManager( |
| 199 | std::unique_ptr<RTDyldMemoryManager>(unwrap(options.MCJMM))); |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 200 | if (ExecutionEngine *JIT = builder.create()) { |
| 201 | *OutJIT = wrap(JIT); |
| 202 | return 0; |
| 203 | } |
| 204 | *OutError = strdup(Error.c_str()); |
| 205 | return 1; |
| 206 | } |
| 207 | |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 208 | LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE, |
| 209 | LLVMModuleProviderRef MP, |
| 210 | char **OutError) { |
| 211 | /* The module provider is now actually a module. */ |
| 212 | return LLVMCreateExecutionEngineForModule(OutEE, |
| 213 | reinterpret_cast<LLVMModuleRef>(MP), |
| 214 | OutError); |
| 215 | } |
| 216 | |
| 217 | LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp, |
| 218 | LLVMModuleProviderRef MP, |
| 219 | char **OutError) { |
| 220 | /* The module provider is now actually a module. */ |
| 221 | return LLVMCreateInterpreterForModule(OutInterp, |
| 222 | reinterpret_cast<LLVMModuleRef>(MP), |
| 223 | OutError); |
| 224 | } |
| 225 | |
| 226 | LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT, |
| 227 | LLVMModuleProviderRef MP, |
| 228 | unsigned OptLevel, |
| 229 | char **OutError) { |
| 230 | /* The module provider is now actually a module. */ |
| 231 | return LLVMCreateJITCompilerForModule(OutJIT, |
| 232 | reinterpret_cast<LLVMModuleRef>(MP), |
| 233 | OptLevel, OutError); |
| 234 | } |
| 235 | |
| 236 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 237 | void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) { |
| 238 | delete unwrap(EE); |
| 239 | } |
| 240 | |
| 241 | void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) { |
| 242 | unwrap(EE)->runStaticConstructorsDestructors(false); |
| 243 | } |
| 244 | |
| 245 | void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) { |
| 246 | unwrap(EE)->runStaticConstructorsDestructors(true); |
| 247 | } |
| 248 | |
| 249 | int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F, |
| 250 | unsigned ArgC, const char * const *ArgV, |
| 251 | const char * const *EnvP) { |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 252 | unwrap(EE)->finalizeObject(); |
| 253 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 254 | std::vector<std::string> ArgVec; |
| 255 | for (unsigned I = 0; I != ArgC; ++I) |
| 256 | ArgVec.push_back(ArgV[I]); |
| 257 | |
| 258 | return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP); |
| 259 | } |
| 260 | |
| 261 | LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F, |
| 262 | unsigned NumArgs, |
| 263 | LLVMGenericValueRef *Args) { |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 264 | unwrap(EE)->finalizeObject(); |
| 265 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 266 | std::vector<GenericValue> ArgVec; |
| 267 | ArgVec.reserve(NumArgs); |
| 268 | for (unsigned I = 0; I != NumArgs; ++I) |
| 269 | ArgVec.push_back(*unwrap(Args[I])); |
| 270 | |
| 271 | GenericValue *Result = new GenericValue(); |
| 272 | *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec); |
| 273 | return wrap(Result); |
| 274 | } |
| 275 | |
| 276 | void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 279 | void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){ |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 280 | unwrap(EE)->addModule(std::unique_ptr<Module>(unwrap(M))); |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 283 | void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){ |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 284 | /* The module provider is now actually a module. */ |
| 285 | LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP)); |
| 286 | } |
| 287 | |
| 288 | LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M, |
| 289 | LLVMModuleRef *OutMod, char **OutError) { |
| 290 | Module *Mod = unwrap(M); |
| 291 | unwrap(EE)->removeModule(Mod); |
| 292 | *OutMod = wrap(Mod); |
| 293 | return 0; |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Chris Lattner | 25963c6 | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 296 | LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE, |
| 297 | LLVMModuleProviderRef MP, |
| 298 | LLVMModuleRef *OutMod, char **OutError) { |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 299 | /* The module provider is now actually a module. */ |
| 300 | return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod, |
| 301 | OutError); |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Chris Lattner | 25963c6 | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 304 | LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name, |
| 305 | LLVMValueRef *OutFn) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 306 | if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) { |
| 307 | *OutFn = wrap(F); |
| 308 | return 0; |
| 309 | } |
| 310 | return 1; |
| 311 | } |
Erick Tryzelaar | 8ac07c2 | 2008-03-27 00:27:14 +0000 | [diff] [blame] | 312 | |
Filip Pizlo | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 313 | void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, |
| 314 | LLVMValueRef Fn) { |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 315 | return nullptr; |
Duncan Sands | 330134b | 2010-07-19 09:33:13 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Erick Tryzelaar | 8ac07c2 | 2008-03-27 00:27:14 +0000 | [diff] [blame] | 318 | LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) { |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 319 | return wrap(unwrap(EE)->getDataLayout()); |
Erick Tryzelaar | 8ac07c2 | 2008-03-27 00:27:14 +0000 | [diff] [blame] | 320 | } |
Gordon Henriksen | 9f33754 | 2008-06-20 02:16:11 +0000 | [diff] [blame] | 321 | |
Juergen Ributzka | 5fe955c | 2014-01-23 19:23:28 +0000 | [diff] [blame] | 322 | LLVMTargetMachineRef |
| 323 | LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE) { |
| 324 | return wrap(unwrap(EE)->getTargetMachine()); |
| 325 | } |
| 326 | |
Gordon Henriksen | 9f33754 | 2008-06-20 02:16:11 +0000 | [diff] [blame] | 327 | void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global, |
| 328 | void* Addr) { |
| 329 | unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr); |
| 330 | } |
Chris Lattner | 41b43da | 2009-01-21 18:11:10 +0000 | [diff] [blame] | 331 | |
| 332 | void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) { |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 333 | unwrap(EE)->finalizeObject(); |
| 334 | |
Chris Lattner | 41b43da | 2009-01-21 18:11:10 +0000 | [diff] [blame] | 335 | return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global)); |
| 336 | } |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 337 | |
Peter Zotov | c433cd7 | 2014-12-22 18:53:11 +0000 | [diff] [blame] | 338 | uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const char *Name) { |
| 339 | return unwrap(EE)->getGlobalValueAddress(Name); |
| 340 | } |
| 341 | |
| 342 | uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name) { |
| 343 | return unwrap(EE)->getFunctionAddress(Name); |
| 344 | } |
| 345 | |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 346 | /*===-- Operations on memory managers -------------------------------------===*/ |
| 347 | |
| 348 | namespace { |
| 349 | |
| 350 | struct SimpleBindingMMFunctions { |
Anders Waldenborg | 9515b31 | 2013-09-30 19:11:32 +0000 | [diff] [blame] | 351 | LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection; |
| 352 | LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection; |
| 353 | LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory; |
| 354 | LLVMMemoryManagerDestroyCallback Destroy; |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 355 | }; |
| 356 | |
| 357 | class SimpleBindingMemoryManager : public RTDyldMemoryManager { |
| 358 | public: |
| 359 | SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions, |
| 360 | void *Opaque); |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 361 | ~SimpleBindingMemoryManager() override; |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 362 | |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 363 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
| 364 | unsigned SectionID, |
| 365 | StringRef SectionName) override; |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 366 | |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 367 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
| 368 | unsigned SectionID, StringRef SectionName, |
| 369 | bool isReadOnly) override; |
| 370 | |
| 371 | bool finalizeMemory(std::string *ErrMsg) override; |
| 372 | |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 373 | private: |
| 374 | SimpleBindingMMFunctions Functions; |
| 375 | void *Opaque; |
| 376 | }; |
| 377 | |
| 378 | SimpleBindingMemoryManager::SimpleBindingMemoryManager( |
| 379 | const SimpleBindingMMFunctions& Functions, |
| 380 | void *Opaque) |
| 381 | : Functions(Functions), Opaque(Opaque) { |
| 382 | assert(Functions.AllocateCodeSection && |
| 383 | "No AllocateCodeSection function provided!"); |
| 384 | assert(Functions.AllocateDataSection && |
| 385 | "No AllocateDataSection function provided!"); |
| 386 | assert(Functions.FinalizeMemory && |
| 387 | "No FinalizeMemory function provided!"); |
| 388 | assert(Functions.Destroy && |
| 389 | "No Destroy function provided!"); |
| 390 | } |
| 391 | |
| 392 | SimpleBindingMemoryManager::~SimpleBindingMemoryManager() { |
| 393 | Functions.Destroy(Opaque); |
| 394 | } |
| 395 | |
| 396 | uint8_t *SimpleBindingMemoryManager::allocateCodeSection( |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 397 | uintptr_t Size, unsigned Alignment, unsigned SectionID, |
| 398 | StringRef SectionName) { |
| 399 | return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID, |
| 400 | SectionName.str().c_str()); |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | uint8_t *SimpleBindingMemoryManager::allocateDataSection( |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 404 | uintptr_t Size, unsigned Alignment, unsigned SectionID, |
| 405 | StringRef SectionName, bool isReadOnly) { |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 406 | return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 407 | SectionName.str().c_str(), |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 408 | isReadOnly); |
| 409 | } |
| 410 | |
| 411 | bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 412 | char *errMsgCString = nullptr; |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 413 | bool result = Functions.FinalizeMemory(Opaque, &errMsgCString); |
| 414 | assert((result || !errMsgCString) && |
| 415 | "Did not expect an error message if FinalizeMemory succeeded"); |
| 416 | if (errMsgCString) { |
| 417 | if (ErrMsg) |
| 418 | *ErrMsg = errMsgCString; |
| 419 | free(errMsgCString); |
| 420 | } |
| 421 | return result; |
| 422 | } |
| 423 | |
| 424 | } // anonymous namespace |
| 425 | |
| 426 | LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager( |
| 427 | void *Opaque, |
Anders Waldenborg | 9515b31 | 2013-09-30 19:11:32 +0000 | [diff] [blame] | 428 | LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection, |
| 429 | LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection, |
| 430 | LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory, |
| 431 | LLVMMemoryManagerDestroyCallback Destroy) { |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 432 | |
| 433 | if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory || |
| 434 | !Destroy) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 435 | return nullptr; |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 436 | |
| 437 | SimpleBindingMMFunctions functions; |
| 438 | functions.AllocateCodeSection = AllocateCodeSection; |
| 439 | functions.AllocateDataSection = AllocateDataSection; |
| 440 | functions.FinalizeMemory = FinalizeMemory; |
| 441 | functions.Destroy = Destroy; |
| 442 | return wrap(new SimpleBindingMemoryManager(functions, Opaque)); |
| 443 | } |
| 444 | |
| 445 | void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM) { |
| 446 | delete unwrap(MM); |
| 447 | } |
| 448 | |