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" |
Sanjay Patel | d9fb62e | 2015-06-01 21:56:56 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 22 | #include <cstring> |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
Chandler Carruth | f58e376 | 2014-04-22 03:04:17 +0000 | [diff] [blame] | 26 | #define DEBUG_TYPE "jit" |
| 27 | |
Eric Christopher | 04d4e93 | 2013-04-22 22:47:22 +0000 | [diff] [blame] | 28 | // Wrapping the C bindings types. |
Filip Pizlo | dec20e4 | 2013-05-01 20:59:00 +0000 | [diff] [blame] | 29 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef) |
Eric Christopher | 04d4e93 | 2013-04-22 22:47:22 +0000 | [diff] [blame] | 30 | |
Eric Christopher | 04d4e93 | 2013-04-22 22:47:22 +0000 | [diff] [blame] | 31 | |
Diego Novillo | cd973c4 | 2015-07-27 18:27:23 +0000 | [diff] [blame] | 32 | static LLVMTargetMachineRef wrap(const TargetMachine *P) { |
Juergen Ributzka | 5fe955c | 2014-01-23 19:23:28 +0000 | [diff] [blame] | 33 | return |
| 34 | reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P)); |
| 35 | } |
| 36 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 37 | /*===-- Operations on generic values --------------------------------------===*/ |
| 38 | |
| 39 | LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty, |
| 40 | unsigned long long N, |
Chris Lattner | 25963c6 | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 41 | LLVMBool IsSigned) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 42 | GenericValue *GenVal = new GenericValue(); |
| 43 | GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned); |
| 44 | return wrap(GenVal); |
| 45 | } |
| 46 | |
| 47 | LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) { |
| 48 | GenericValue *GenVal = new GenericValue(); |
| 49 | GenVal->PointerVal = P; |
| 50 | return wrap(GenVal); |
| 51 | } |
| 52 | |
| 53 | LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) { |
| 54 | GenericValue *GenVal = new GenericValue(); |
| 55 | switch (unwrap(TyRef)->getTypeID()) { |
| 56 | case Type::FloatTyID: |
| 57 | GenVal->FloatVal = N; |
| 58 | break; |
| 59 | case Type::DoubleTyID: |
| 60 | GenVal->DoubleVal = N; |
| 61 | break; |
| 62 | default: |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 63 | llvm_unreachable("LLVMGenericValueToFloat supports only float and double."); |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 64 | } |
| 65 | return wrap(GenVal); |
| 66 | } |
| 67 | |
| 68 | unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) { |
| 69 | return unwrap(GenValRef)->IntVal.getBitWidth(); |
| 70 | } |
| 71 | |
| 72 | unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef, |
Chris Lattner | 25963c6 | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 73 | LLVMBool IsSigned) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 74 | GenericValue *GenVal = unwrap(GenValRef); |
| 75 | if (IsSigned) |
| 76 | return GenVal->IntVal.getSExtValue(); |
| 77 | else |
| 78 | return GenVal->IntVal.getZExtValue(); |
| 79 | } |
| 80 | |
| 81 | void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) { |
| 82 | return unwrap(GenVal)->PointerVal; |
| 83 | } |
| 84 | |
| 85 | double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) { |
| 86 | switch (unwrap(TyRef)->getTypeID()) { |
| 87 | case Type::FloatTyID: |
| 88 | return unwrap(GenVal)->FloatVal; |
| 89 | case Type::DoubleTyID: |
| 90 | return unwrap(GenVal)->DoubleVal; |
| 91 | default: |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 92 | llvm_unreachable("LLVMGenericValueToFloat supports only float and double."); |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) { |
| 97 | delete unwrap(GenVal); |
| 98 | } |
| 99 | |
| 100 | /*===-- Operations on execution engines -----------------------------------===*/ |
| 101 | |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 102 | LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE, |
| 103 | LLVMModuleRef M, |
| 104 | char **OutError) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 105 | std::string Error; |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 106 | EngineBuilder builder(std::unique_ptr<Module>(unwrap(M))); |
Reid Kleckner | fc8a2d5 | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 107 | builder.setEngineKind(EngineKind::Either) |
| 108 | .setErrorStr(&Error); |
| 109 | if (ExecutionEngine *EE = builder.create()){ |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 110 | *OutEE = wrap(EE); |
| 111 | return 0; |
| 112 | } |
| 113 | *OutError = strdup(Error.c_str()); |
| 114 | return 1; |
| 115 | } |
| 116 | |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 117 | LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp, |
| 118 | LLVMModuleRef M, |
| 119 | char **OutError) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 120 | std::string Error; |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 121 | EngineBuilder builder(std::unique_ptr<Module>(unwrap(M))); |
Reid Kleckner | fc8a2d5 | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 122 | builder.setEngineKind(EngineKind::Interpreter) |
| 123 | .setErrorStr(&Error); |
| 124 | if (ExecutionEngine *Interp = builder.create()) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 125 | *OutInterp = wrap(Interp); |
| 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 LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, |
| 133 | LLVMModuleRef M, |
| 134 | unsigned OptLevel, |
| 135 | char **OutError) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 136 | std::string Error; |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 137 | EngineBuilder builder(std::unique_ptr<Module>(unwrap(M))); |
Reid Kleckner | fc8a2d5 | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 138 | builder.setEngineKind(EngineKind::JIT) |
| 139 | .setErrorStr(&Error) |
| 140 | .setOptLevel((CodeGenOpt::Level)OptLevel); |
| 141 | if (ExecutionEngine *JIT = builder.create()) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 142 | *OutJIT = wrap(JIT); |
| 143 | return 0; |
| 144 | } |
| 145 | *OutError = strdup(Error.c_str()); |
| 146 | return 1; |
| 147 | } |
| 148 | |
Filip Pizlo | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 149 | void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions, |
| 150 | size_t SizeOfPassedOptions) { |
| 151 | LLVMMCJITCompilerOptions options; |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 152 | memset(&options, 0, sizeof(options)); // Most fields are zero by default. |
Filip Pizlo | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 153 | options.CodeModel = LLVMCodeModelJITDefault; |
Filip Pizlo | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 154 | |
| 155 | memcpy(PassedOptions, &options, |
| 156 | std::min(sizeof(options), SizeOfPassedOptions)); |
| 157 | } |
| 158 | |
| 159 | LLVMBool LLVMCreateMCJITCompilerForModule( |
| 160 | LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, |
| 161 | LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions, |
| 162 | char **OutError) { |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 163 | LLVMMCJITCompilerOptions options; |
| 164 | // If the user passed a larger sized options struct, then they were compiled |
| 165 | // against a newer LLVM. Tell them that something is wrong. |
| 166 | if (SizeOfPassedOptions > sizeof(options)) { |
| 167 | *OutError = strdup( |
Filip Pizlo | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 168 | "Refusing to use options struct that is larger than my own; assuming " |
| 169 | "LLVM library mismatch."); |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | // Defend against the user having an old version of the API by ensuring that |
| 174 | // any fields they didn't see are cleared. We must defend against fields being |
| 175 | // set to the bitwise equivalent of zero, and assume that this means "do the |
| 176 | // default" as if that option hadn't been available. |
Filip Pizlo | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 177 | LLVMInitializeMCJITCompilerOptions(&options, sizeof(options)); |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 178 | memcpy(&options, PassedOptions, SizeOfPassedOptions); |
| 179 | |
| 180 | TargetOptions targetOptions; |
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. |
Akira Hatanaka | e36505c | 2015-05-26 20:17:20 +0000 | [diff] [blame] | 187 | for (auto &F : *Mod) { |
| 188 | auto Attrs = F.getAttributes(); |
| 189 | auto Value = options.NoFramePointerElim ? "true" : "false"; |
| 190 | Attrs = Attrs.addAttribute(F.getContext(), AttributeSet::FunctionIndex, |
| 191 | "no-frame-pointer-elim", Value); |
| 192 | F.setAttributes(Attrs); |
| 193 | } |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 194 | |
| 195 | std::string Error; |
Akira Hatanaka | ddf76aa | 2015-05-23 01:14:08 +0000 | [diff] [blame] | 196 | EngineBuilder builder(std::move(Mod)); |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 197 | builder.setEngineKind(EngineKind::JIT) |
| 198 | .setErrorStr(&Error) |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 199 | .setOptLevel((CodeGenOpt::Level)options.OptLevel) |
Filip Pizlo | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 200 | .setCodeModel(unwrap(options.CodeModel)) |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 201 | .setTargetOptions(targetOptions); |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 202 | if (options.MCJMM) |
Lang Hames | 4a5697e | 2014-12-03 00:51:19 +0000 | [diff] [blame] | 203 | builder.setMCJITMemoryManager( |
| 204 | std::unique_ptr<RTDyldMemoryManager>(unwrap(options.MCJMM))); |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 205 | if (ExecutionEngine *JIT = builder.create()) { |
| 206 | *OutJIT = wrap(JIT); |
| 207 | return 0; |
| 208 | } |
| 209 | *OutError = strdup(Error.c_str()); |
| 210 | return 1; |
| 211 | } |
| 212 | |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 213 | LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE, |
| 214 | LLVMModuleProviderRef MP, |
| 215 | char **OutError) { |
| 216 | /* The module provider is now actually a module. */ |
| 217 | return LLVMCreateExecutionEngineForModule(OutEE, |
| 218 | reinterpret_cast<LLVMModuleRef>(MP), |
| 219 | OutError); |
| 220 | } |
| 221 | |
| 222 | LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp, |
| 223 | LLVMModuleProviderRef MP, |
| 224 | char **OutError) { |
| 225 | /* The module provider is now actually a module. */ |
| 226 | return LLVMCreateInterpreterForModule(OutInterp, |
| 227 | reinterpret_cast<LLVMModuleRef>(MP), |
| 228 | OutError); |
| 229 | } |
| 230 | |
| 231 | LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT, |
| 232 | LLVMModuleProviderRef MP, |
| 233 | unsigned OptLevel, |
| 234 | char **OutError) { |
| 235 | /* The module provider is now actually a module. */ |
| 236 | return LLVMCreateJITCompilerForModule(OutJIT, |
| 237 | reinterpret_cast<LLVMModuleRef>(MP), |
| 238 | OptLevel, OutError); |
| 239 | } |
| 240 | |
| 241 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 242 | void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) { |
| 243 | delete unwrap(EE); |
| 244 | } |
| 245 | |
| 246 | void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) { |
| 247 | unwrap(EE)->runStaticConstructorsDestructors(false); |
| 248 | } |
| 249 | |
| 250 | void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) { |
| 251 | unwrap(EE)->runStaticConstructorsDestructors(true); |
| 252 | } |
| 253 | |
| 254 | int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F, |
| 255 | unsigned ArgC, const char * const *ArgV, |
| 256 | const char * const *EnvP) { |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 257 | unwrap(EE)->finalizeObject(); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 258 | |
| 259 | std::vector<std::string> ArgVec(ArgV, ArgV + ArgC); |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 260 | return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP); |
| 261 | } |
| 262 | |
| 263 | LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F, |
| 264 | unsigned NumArgs, |
| 265 | LLVMGenericValueRef *Args) { |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 266 | unwrap(EE)->finalizeObject(); |
| 267 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 268 | std::vector<GenericValue> ArgVec; |
| 269 | ArgVec.reserve(NumArgs); |
| 270 | for (unsigned I = 0; I != NumArgs; ++I) |
| 271 | ArgVec.push_back(*unwrap(Args[I])); |
| 272 | |
| 273 | GenericValue *Result = new GenericValue(); |
| 274 | *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec); |
| 275 | return wrap(Result); |
| 276 | } |
| 277 | |
| 278 | void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 281 | void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){ |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 282 | unwrap(EE)->addModule(std::unique_ptr<Module>(unwrap(M))); |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 285 | void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){ |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 286 | /* The module provider is now actually a module. */ |
| 287 | LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP)); |
| 288 | } |
| 289 | |
| 290 | LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M, |
| 291 | LLVMModuleRef *OutMod, char **OutError) { |
| 292 | Module *Mod = unwrap(M); |
| 293 | unwrap(EE)->removeModule(Mod); |
| 294 | *OutMod = wrap(Mod); |
| 295 | return 0; |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Chris Lattner | 25963c6 | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 298 | LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE, |
| 299 | LLVMModuleProviderRef MP, |
| 300 | LLVMModuleRef *OutMod, char **OutError) { |
Erick Tryzelaar | ad0e0cb | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 301 | /* The module provider is now actually a module. */ |
| 302 | return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod, |
| 303 | OutError); |
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 LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name, |
| 307 | LLVMValueRef *OutFn) { |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 308 | if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) { |
| 309 | *OutFn = wrap(F); |
| 310 | return 0; |
| 311 | } |
| 312 | return 1; |
| 313 | } |
Erick Tryzelaar | 8ac07c2 | 2008-03-27 00:27:14 +0000 | [diff] [blame] | 314 | |
Filip Pizlo | 85e0d27 | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 315 | void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, |
| 316 | LLVMValueRef Fn) { |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 317 | return nullptr; |
Duncan Sands | 330134b | 2010-07-19 09:33:13 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Erick Tryzelaar | 8ac07c2 | 2008-03-27 00:27:14 +0000 | [diff] [blame] | 320 | LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) { |
Mehdi Amini | a3fcefb | 2015-07-16 16:34:23 +0000 | [diff] [blame] | 321 | return wrap(&unwrap(EE)->getDataLayout()); |
Erick Tryzelaar | 8ac07c2 | 2008-03-27 00:27:14 +0000 | [diff] [blame] | 322 | } |
Gordon Henriksen | 9f33754 | 2008-06-20 02:16:11 +0000 | [diff] [blame] | 323 | |
Juergen Ributzka | 5fe955c | 2014-01-23 19:23:28 +0000 | [diff] [blame] | 324 | LLVMTargetMachineRef |
| 325 | LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE) { |
| 326 | return wrap(unwrap(EE)->getTargetMachine()); |
| 327 | } |
| 328 | |
Gordon Henriksen | 9f33754 | 2008-06-20 02:16:11 +0000 | [diff] [blame] | 329 | void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global, |
| 330 | void* Addr) { |
| 331 | unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr); |
| 332 | } |
Chris Lattner | 41b43da | 2009-01-21 18:11:10 +0000 | [diff] [blame] | 333 | |
| 334 | void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) { |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 335 | unwrap(EE)->finalizeObject(); |
| 336 | |
Chris Lattner | 41b43da | 2009-01-21 18:11:10 +0000 | [diff] [blame] | 337 | return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global)); |
| 338 | } |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 339 | |
Peter Zotov | c433cd7 | 2014-12-22 18:53:11 +0000 | [diff] [blame] | 340 | uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const char *Name) { |
| 341 | return unwrap(EE)->getGlobalValueAddress(Name); |
| 342 | } |
| 343 | |
| 344 | uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name) { |
| 345 | return unwrap(EE)->getFunctionAddress(Name); |
| 346 | } |
| 347 | |
Filip Pizlo | 3fdbaff | 2013-05-22 02:46:43 +0000 | [diff] [blame] | 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); |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 363 | ~SimpleBindingMemoryManager() override; |
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 | |