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