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