1 //===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the C bindings for the ExecutionEngine library.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "jit"
15 #include "llvm-c/ExecutionEngine.h"
16 #include "llvm/ExecutionEngine/ExecutionEngine.h"
17 #include "llvm/ExecutionEngine/GenericValue.h"
18 #include "llvm/Support/ErrorHandling.h"
23 // Wrapping the C bindings types.
24 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef )
26 inline DataLayout *unwrap(LLVMTargetDataRef P) {
27 return reinterpret_cast<DataLayout*>(P);
30 inline LLVMTargetDataRef wrap(const DataLayout *P) {
31 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
34 inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
35 return reinterpret_cast<TargetLibraryInfo*>(P);
38 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
39 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
40 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
43 /*===-- Operations on generic values --------------------------------------===*/
45 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
48 GenericValue *GenVal = new GenericValue();
49 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
53 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
54 GenericValue *GenVal = new GenericValue();
55 GenVal->PointerVal = P;
59 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
60 GenericValue *GenVal = new GenericValue();
61 switch (unwrap(TyRef)->getTypeID()) {
65 case Type::DoubleTyID:
66 GenVal->DoubleVal = N;
69 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
74 unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
75 return unwrap(GenValRef)->IntVal.getBitWidth();
78 unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
80 GenericValue *GenVal = unwrap(GenValRef);
82 return GenVal->IntVal.getSExtValue();
84 return GenVal->IntVal.getZExtValue();
87 void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
88 return unwrap(GenVal)->PointerVal;
91 double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
92 switch (unwrap(TyRef)->getTypeID()) {
94 return unwrap(GenVal)->FloatVal;
95 case Type::DoubleTyID:
96 return unwrap(GenVal)->DoubleVal;
98 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
102 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
103 delete unwrap(GenVal);
106 /*===-- Operations on execution engines -----------------------------------===*/
108 LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
112 EngineBuilder builder(unwrap(M));
113 builder.setEngineKind(EngineKind::Either)
114 .setErrorStr(&Error);
115 if (ExecutionEngine *EE = builder.create()){
119 *OutError = strdup(Error.c_str());
123 LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
127 EngineBuilder builder(unwrap(M));
128 builder.setEngineKind(EngineKind::Interpreter)
129 .setErrorStr(&Error);
130 if (ExecutionEngine *Interp = builder.create()) {
131 *OutInterp = wrap(Interp);
134 *OutError = strdup(Error.c_str());
138 LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
143 EngineBuilder builder(unwrap(M));
144 builder.setEngineKind(EngineKind::JIT)
146 .setOptLevel((CodeGenOpt::Level)OptLevel);
147 if (ExecutionEngine *JIT = builder.create()) {
151 *OutError = strdup(Error.c_str());
155 LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
156 LLVMModuleProviderRef MP,
158 /* The module provider is now actually a module. */
159 return LLVMCreateExecutionEngineForModule(OutEE,
160 reinterpret_cast<LLVMModuleRef>(MP),
164 LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
165 LLVMModuleProviderRef MP,
167 /* The module provider is now actually a module. */
168 return LLVMCreateInterpreterForModule(OutInterp,
169 reinterpret_cast<LLVMModuleRef>(MP),
173 LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
174 LLVMModuleProviderRef MP,
177 /* The module provider is now actually a module. */
178 return LLVMCreateJITCompilerForModule(OutJIT,
179 reinterpret_cast<LLVMModuleRef>(MP),
184 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
188 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
189 unwrap(EE)->runStaticConstructorsDestructors(false);
192 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
193 unwrap(EE)->runStaticConstructorsDestructors(true);
196 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
197 unsigned ArgC, const char * const *ArgV,
198 const char * const *EnvP) {
199 std::vector<std::string> ArgVec;
200 for (unsigned I = 0; I != ArgC; ++I)
201 ArgVec.push_back(ArgV[I]);
203 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
206 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
208 LLVMGenericValueRef *Args) {
209 std::vector<GenericValue> ArgVec;
210 ArgVec.reserve(NumArgs);
211 for (unsigned I = 0; I != NumArgs; ++I)
212 ArgVec.push_back(*unwrap(Args[I]));
214 GenericValue *Result = new GenericValue();
215 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
219 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
220 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
223 void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
224 unwrap(EE)->addModule(unwrap(M));
227 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
228 /* The module provider is now actually a module. */
229 LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
232 LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
233 LLVMModuleRef *OutMod, char **OutError) {
234 Module *Mod = unwrap(M);
235 unwrap(EE)->removeModule(Mod);
240 LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
241 LLVMModuleProviderRef MP,
242 LLVMModuleRef *OutMod, char **OutError) {
243 /* The module provider is now actually a module. */
244 return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
248 LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
249 LLVMValueRef *OutFn) {
250 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
257 void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, LLVMValueRef Fn) {
258 return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn));
261 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
262 return wrap(unwrap(EE)->getDataLayout());
265 void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
267 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
270 void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
271 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));