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 | |
| 14 | #define DEBUG_TYPE "jit" |
| 15 | #include "llvm-c/ExecutionEngine.h" |
| 16 | #include "llvm/ExecutionEngine/GenericValue.h" |
| 17 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 19 | #include <cstring> |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | /*===-- Operations on generic values --------------------------------------===*/ |
| 24 | |
| 25 | LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty, |
| 26 | unsigned long long N, |
Chris Lattner | d686c8e | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 27 | LLVMBool IsSigned) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 28 | GenericValue *GenVal = new GenericValue(); |
| 29 | GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned); |
| 30 | return wrap(GenVal); |
| 31 | } |
| 32 | |
| 33 | LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) { |
| 34 | GenericValue *GenVal = new GenericValue(); |
| 35 | GenVal->PointerVal = P; |
| 36 | return wrap(GenVal); |
| 37 | } |
| 38 | |
| 39 | LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) { |
| 40 | GenericValue *GenVal = new GenericValue(); |
| 41 | switch (unwrap(TyRef)->getTypeID()) { |
| 42 | case Type::FloatTyID: |
| 43 | GenVal->FloatVal = N; |
| 44 | break; |
| 45 | case Type::DoubleTyID: |
| 46 | GenVal->DoubleVal = N; |
| 47 | break; |
| 48 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 49 | llvm_unreachable("LLVMGenericValueToFloat supports only float and double."); |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 50 | } |
| 51 | return wrap(GenVal); |
| 52 | } |
| 53 | |
| 54 | unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) { |
| 55 | return unwrap(GenValRef)->IntVal.getBitWidth(); |
| 56 | } |
| 57 | |
| 58 | unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef, |
Chris Lattner | d686c8e | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 59 | LLVMBool IsSigned) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 60 | GenericValue *GenVal = unwrap(GenValRef); |
| 61 | if (IsSigned) |
| 62 | return GenVal->IntVal.getSExtValue(); |
| 63 | else |
| 64 | return GenVal->IntVal.getZExtValue(); |
| 65 | } |
| 66 | |
| 67 | void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) { |
| 68 | return unwrap(GenVal)->PointerVal; |
| 69 | } |
| 70 | |
| 71 | double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) { |
| 72 | switch (unwrap(TyRef)->getTypeID()) { |
| 73 | case Type::FloatTyID: |
| 74 | return unwrap(GenVal)->FloatVal; |
| 75 | case Type::DoubleTyID: |
| 76 | return unwrap(GenVal)->DoubleVal; |
| 77 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 78 | llvm_unreachable("LLVMGenericValueToFloat supports only float and double."); |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 79 | break; |
| 80 | } |
Chris Lattner | d27c991 | 2008-03-30 18:22:13 +0000 | [diff] [blame] | 81 | return 0; // Not reached |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) { |
| 85 | delete unwrap(GenVal); |
| 86 | } |
| 87 | |
| 88 | /*===-- Operations on execution engines -----------------------------------===*/ |
| 89 | |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 90 | LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE, |
| 91 | LLVMModuleRef M, |
| 92 | char **OutError) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 93 | std::string Error; |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 94 | EngineBuilder builder(unwrap(M)); |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 95 | builder.setEngineKind(EngineKind::Either) |
| 96 | .setErrorStr(&Error); |
| 97 | if (ExecutionEngine *EE = builder.create()){ |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 98 | *OutEE = wrap(EE); |
| 99 | return 0; |
| 100 | } |
| 101 | *OutError = strdup(Error.c_str()); |
| 102 | return 1; |
| 103 | } |
| 104 | |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 105 | LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp, |
| 106 | LLVMModuleRef M, |
| 107 | char **OutError) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 108 | std::string Error; |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 109 | EngineBuilder builder(unwrap(M)); |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 110 | builder.setEngineKind(EngineKind::Interpreter) |
| 111 | .setErrorStr(&Error); |
| 112 | if (ExecutionEngine *Interp = builder.create()) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 113 | *OutInterp = wrap(Interp); |
| 114 | return 0; |
| 115 | } |
| 116 | *OutError = strdup(Error.c_str()); |
| 117 | return 1; |
| 118 | } |
| 119 | |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 120 | LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, |
| 121 | LLVMModuleRef M, |
| 122 | unsigned OptLevel, |
| 123 | char **OutError) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 124 | std::string Error; |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 125 | EngineBuilder builder(unwrap(M)); |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 126 | builder.setEngineKind(EngineKind::JIT) |
| 127 | .setErrorStr(&Error) |
| 128 | .setOptLevel((CodeGenOpt::Level)OptLevel); |
| 129 | if (ExecutionEngine *JIT = builder.create()) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 130 | *OutJIT = wrap(JIT); |
| 131 | return 0; |
| 132 | } |
| 133 | *OutError = strdup(Error.c_str()); |
| 134 | return 1; |
| 135 | } |
| 136 | |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 137 | LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE, |
| 138 | LLVMModuleProviderRef MP, |
| 139 | char **OutError) { |
| 140 | /* The module provider is now actually a module. */ |
| 141 | return LLVMCreateExecutionEngineForModule(OutEE, |
| 142 | reinterpret_cast<LLVMModuleRef>(MP), |
| 143 | OutError); |
| 144 | } |
| 145 | |
| 146 | LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp, |
| 147 | LLVMModuleProviderRef MP, |
| 148 | char **OutError) { |
| 149 | /* The module provider is now actually a module. */ |
| 150 | return LLVMCreateInterpreterForModule(OutInterp, |
| 151 | reinterpret_cast<LLVMModuleRef>(MP), |
| 152 | OutError); |
| 153 | } |
| 154 | |
| 155 | LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT, |
| 156 | LLVMModuleProviderRef MP, |
| 157 | unsigned OptLevel, |
| 158 | char **OutError) { |
| 159 | /* The module provider is now actually a module. */ |
| 160 | return LLVMCreateJITCompilerForModule(OutJIT, |
| 161 | reinterpret_cast<LLVMModuleRef>(MP), |
| 162 | OptLevel, OutError); |
| 163 | } |
| 164 | |
| 165 | |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 166 | void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) { |
| 167 | delete unwrap(EE); |
| 168 | } |
| 169 | |
| 170 | void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) { |
| 171 | unwrap(EE)->runStaticConstructorsDestructors(false); |
| 172 | } |
| 173 | |
| 174 | void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) { |
| 175 | unwrap(EE)->runStaticConstructorsDestructors(true); |
| 176 | } |
| 177 | |
| 178 | int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F, |
| 179 | unsigned ArgC, const char * const *ArgV, |
| 180 | const char * const *EnvP) { |
| 181 | std::vector<std::string> ArgVec; |
| 182 | for (unsigned I = 0; I != ArgC; ++I) |
| 183 | ArgVec.push_back(ArgV[I]); |
| 184 | |
| 185 | return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP); |
| 186 | } |
| 187 | |
| 188 | LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F, |
| 189 | unsigned NumArgs, |
| 190 | LLVMGenericValueRef *Args) { |
| 191 | std::vector<GenericValue> ArgVec; |
| 192 | ArgVec.reserve(NumArgs); |
| 193 | for (unsigned I = 0; I != NumArgs; ++I) |
| 194 | ArgVec.push_back(*unwrap(Args[I])); |
| 195 | |
| 196 | GenericValue *Result = new GenericValue(); |
| 197 | *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec); |
| 198 | return wrap(Result); |
| 199 | } |
| 200 | |
| 201 | void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) { |
| 202 | unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F)); |
| 203 | } |
| 204 | |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 205 | void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){ |
| 206 | unwrap(EE)->addModule(unwrap(M)); |
| 207 | } |
| 208 | |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 209 | void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){ |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 210 | /* The module provider is now actually a module. */ |
| 211 | LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP)); |
| 212 | } |
| 213 | |
| 214 | LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M, |
| 215 | LLVMModuleRef *OutMod, char **OutError) { |
| 216 | Module *Mod = unwrap(M); |
| 217 | unwrap(EE)->removeModule(Mod); |
| 218 | *OutMod = wrap(Mod); |
| 219 | return 0; |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Chris Lattner | d686c8e | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 222 | LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE, |
| 223 | LLVMModuleProviderRef MP, |
| 224 | LLVMModuleRef *OutMod, char **OutError) { |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 225 | /* The module provider is now actually a module. */ |
| 226 | return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod, |
| 227 | OutError); |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Chris Lattner | d686c8e | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 230 | LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name, |
| 231 | LLVMValueRef *OutFn) { |
Gordon Henriksen | 2e855e6 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 232 | if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) { |
| 233 | *OutFn = wrap(F); |
| 234 | return 0; |
| 235 | } |
| 236 | return 1; |
| 237 | } |
Erick Tryzelaar | 7c1483b | 2008-03-27 00:27:14 +0000 | [diff] [blame] | 238 | |
Duncan Sands | d90fee9 | 2010-07-19 09:33:13 +0000 | [diff] [blame] | 239 | void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, LLVMValueRef Fn) { |
Duncan Sands | e117b63 | 2010-07-19 09:36:45 +0000 | [diff] [blame] | 240 | return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn)); |
Duncan Sands | d90fee9 | 2010-07-19 09:33:13 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Erick Tryzelaar | 7c1483b | 2008-03-27 00:27:14 +0000 | [diff] [blame] | 243 | LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) { |
| 244 | return wrap(unwrap(EE)->getTargetData()); |
| 245 | } |
Gordon Henriksen | 54227f6 | 2008-06-20 02:16:11 +0000 | [diff] [blame] | 246 | |
| 247 | void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global, |
| 248 | void* Addr) { |
| 249 | unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr); |
| 250 | } |
Chris Lattner | 1e42c5b | 2009-01-21 18:11:10 +0000 | [diff] [blame] | 251 | |
| 252 | void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) { |
| 253 | return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global)); |
| 254 | } |