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"
19 #include "llvm/Wrap.h"
24 // Wrapping the C bindings types.
25 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef )
27 inline DataLayout *unwrap(LLVMTargetDataRef P) {
28 return reinterpret_cast<DataLayout*>(P);
31 inline LLVMTargetDataRef wrap(const DataLayout *P) {
32 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
35 inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
36 return reinterpret_cast<TargetLibraryInfo*>(P);
39 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
40 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
41 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
44 /*===-- Operations on generic values --------------------------------------===*/
46 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
49 GenericValue *GenVal = new GenericValue();
50 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
54 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
55 GenericValue *GenVal = new GenericValue();
56 GenVal->PointerVal = P;
60 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
61 GenericValue *GenVal = new GenericValue();
62 switch (unwrap(TyRef)->getTypeID()) {
66 case Type::DoubleTyID:
67 GenVal->DoubleVal = N;
70 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
75 unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
76 return unwrap(GenValRef)->IntVal.getBitWidth();
79 unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
81 GenericValue *GenVal = unwrap(GenValRef);
83 return GenVal->IntVal.getSExtValue();
85 return GenVal->IntVal.getZExtValue();
88 void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
89 return unwrap(GenVal)->PointerVal;
92 double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
93 switch (unwrap(TyRef)->getTypeID()) {
95 return unwrap(GenVal)->FloatVal;
96 case Type::DoubleTyID:
97 return unwrap(GenVal)->DoubleVal;
99 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
103 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
104 delete unwrap(GenVal);
107 /*===-- Operations on execution engines -----------------------------------===*/
109 LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
113 EngineBuilder builder(unwrap(M));
114 builder.setEngineKind(EngineKind::Either)
115 .setErrorStr(&Error);
116 if (ExecutionEngine *EE = builder.create()){
120 *OutError = strdup(Error.c_str());
124 LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
128 EngineBuilder builder(unwrap(M));
129 builder.setEngineKind(EngineKind::Interpreter)
130 .setErrorStr(&Error);
131 if (ExecutionEngine *Interp = builder.create()) {
132 *OutInterp = wrap(Interp);
135 *OutError = strdup(Error.c_str());
139 LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
144 EngineBuilder builder(unwrap(M));
145 builder.setEngineKind(EngineKind::JIT)
147 .setOptLevel((CodeGenOpt::Level)OptLevel);
148 if (ExecutionEngine *JIT = builder.create()) {
152 *OutError = strdup(Error.c_str());
156 LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
157 LLVMModuleProviderRef MP,
159 /* The module provider is now actually a module. */
160 return LLVMCreateExecutionEngineForModule(OutEE,
161 reinterpret_cast<LLVMModuleRef>(MP),
165 LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
166 LLVMModuleProviderRef MP,
168 /* The module provider is now actually a module. */
169 return LLVMCreateInterpreterForModule(OutInterp,
170 reinterpret_cast<LLVMModuleRef>(MP),
174 LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
175 LLVMModuleProviderRef MP,
178 /* The module provider is now actually a module. */
179 return LLVMCreateJITCompilerForModule(OutJIT,
180 reinterpret_cast<LLVMModuleRef>(MP),
185 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
189 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
190 unwrap(EE)->runStaticConstructorsDestructors(false);
193 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
194 unwrap(EE)->runStaticConstructorsDestructors(true);
197 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
198 unsigned ArgC, const char * const *ArgV,
199 const char * const *EnvP) {
200 std::vector<std::string> ArgVec;
201 for (unsigned I = 0; I != ArgC; ++I)
202 ArgVec.push_back(ArgV[I]);
204 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
207 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
209 LLVMGenericValueRef *Args) {
210 std::vector<GenericValue> ArgVec;
211 ArgVec.reserve(NumArgs);
212 for (unsigned I = 0; I != NumArgs; ++I)
213 ArgVec.push_back(*unwrap(Args[I]));
215 GenericValue *Result = new GenericValue();
216 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
220 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
221 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
224 void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
225 unwrap(EE)->addModule(unwrap(M));
228 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
229 /* The module provider is now actually a module. */
230 LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
233 LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
234 LLVMModuleRef *OutMod, char **OutError) {
235 Module *Mod = unwrap(M);
236 unwrap(EE)->removeModule(Mod);
241 LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
242 LLVMModuleProviderRef MP,
243 LLVMModuleRef *OutMod, char **OutError) {
244 /* The module provider is now actually a module. */
245 return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
249 LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
250 LLVMValueRef *OutFn) {
251 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
258 void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, LLVMValueRef Fn) {
259 return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn));
262 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
263 return wrap(unwrap(EE)->getDataLayout());
266 void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
268 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
271 void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
272 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));