ExecutionEngineBindings.cpp revision 7467e5ed1c04887c8d7bdb760df346f518003f07
1//===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
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/ExecutionEngine.h"
17#include "llvm/ExecutionEngine/GenericValue.h"
18#include "llvm/Support/ErrorHandling.h"
19#include <cstring>
20
21using namespace llvm;
22
23// Wrapping the C bindings types.
24DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue,       LLVMGenericValueRef  )
25
26inline DataLayout *unwrap(LLVMTargetDataRef P) {
27  return reinterpret_cast<DataLayout*>(P);
28}
29
30inline LLVMTargetDataRef wrap(const DataLayout *P) {
31  return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
32}
33
34inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
35  return reinterpret_cast<TargetLibraryInfo*>(P);
36}
37
38inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
39  TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
40  return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
41}
42
43/*===-- Operations on generic values --------------------------------------===*/
44
45LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
46                                                unsigned long long N,
47                                                LLVMBool IsSigned) {
48  GenericValue *GenVal = new GenericValue();
49  GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
50  return wrap(GenVal);
51}
52
53LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
54  GenericValue *GenVal = new GenericValue();
55  GenVal->PointerVal = P;
56  return wrap(GenVal);
57}
58
59LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
60  GenericValue *GenVal = new GenericValue();
61  switch (unwrap(TyRef)->getTypeID()) {
62  case Type::FloatTyID:
63    GenVal->FloatVal = N;
64    break;
65  case Type::DoubleTyID:
66    GenVal->DoubleVal = N;
67    break;
68  default:
69    llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
70  }
71  return wrap(GenVal);
72}
73
74unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
75  return unwrap(GenValRef)->IntVal.getBitWidth();
76}
77
78unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
79                                         LLVMBool IsSigned) {
80  GenericValue *GenVal = unwrap(GenValRef);
81  if (IsSigned)
82    return GenVal->IntVal.getSExtValue();
83  else
84    return GenVal->IntVal.getZExtValue();
85}
86
87void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
88  return unwrap(GenVal)->PointerVal;
89}
90
91double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
92  switch (unwrap(TyRef)->getTypeID()) {
93  case Type::FloatTyID:
94    return unwrap(GenVal)->FloatVal;
95  case Type::DoubleTyID:
96    return unwrap(GenVal)->DoubleVal;
97  default:
98    llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
99  }
100}
101
102void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
103  delete unwrap(GenVal);
104}
105
106/*===-- Operations on execution engines -----------------------------------===*/
107
108LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
109                                            LLVMModuleRef M,
110                                            char **OutError) {
111  std::string Error;
112  EngineBuilder builder(unwrap(M));
113  builder.setEngineKind(EngineKind::Either)
114         .setErrorStr(&Error);
115  if (ExecutionEngine *EE = builder.create()){
116    *OutEE = wrap(EE);
117    return 0;
118  }
119  *OutError = strdup(Error.c_str());
120  return 1;
121}
122
123LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
124                                        LLVMModuleRef M,
125                                        char **OutError) {
126  std::string Error;
127  EngineBuilder builder(unwrap(M));
128  builder.setEngineKind(EngineKind::Interpreter)
129         .setErrorStr(&Error);
130  if (ExecutionEngine *Interp = builder.create()) {
131    *OutInterp = wrap(Interp);
132    return 0;
133  }
134  *OutError = strdup(Error.c_str());
135  return 1;
136}
137
138LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
139                                        LLVMModuleRef M,
140                                        unsigned OptLevel,
141                                        char **OutError) {
142  std::string Error;
143  EngineBuilder builder(unwrap(M));
144  builder.setEngineKind(EngineKind::JIT)
145         .setErrorStr(&Error)
146         .setOptLevel((CodeGenOpt::Level)OptLevel);
147  if (ExecutionEngine *JIT = builder.create()) {
148    *OutJIT = wrap(JIT);
149    return 0;
150  }
151  *OutError = strdup(Error.c_str());
152  return 1;
153}
154
155LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
156                                   LLVMModuleProviderRef MP,
157                                   char **OutError) {
158  /* The module provider is now actually a module. */
159  return LLVMCreateExecutionEngineForModule(OutEE,
160                                            reinterpret_cast<LLVMModuleRef>(MP),
161                                            OutError);
162}
163
164LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
165                               LLVMModuleProviderRef MP,
166                               char **OutError) {
167  /* The module provider is now actually a module. */
168  return LLVMCreateInterpreterForModule(OutInterp,
169                                        reinterpret_cast<LLVMModuleRef>(MP),
170                                        OutError);
171}
172
173LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
174                               LLVMModuleProviderRef MP,
175                               unsigned OptLevel,
176                               char **OutError) {
177  /* The module provider is now actually a module. */
178  return LLVMCreateJITCompilerForModule(OutJIT,
179                                        reinterpret_cast<LLVMModuleRef>(MP),
180                                        OptLevel, OutError);
181}
182
183
184void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
185  delete unwrap(EE);
186}
187
188void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
189  unwrap(EE)->runStaticConstructorsDestructors(false);
190}
191
192void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
193  unwrap(EE)->runStaticConstructorsDestructors(true);
194}
195
196int 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]);
202
203  return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
204}
205
206LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
207                                    unsigned NumArgs,
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]));
213
214  GenericValue *Result = new GenericValue();
215  *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
216  return wrap(Result);
217}
218
219void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
220  unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
221}
222
223void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
224  unwrap(EE)->addModule(unwrap(M));
225}
226
227void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
228  /* The module provider is now actually a module. */
229  LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
230}
231
232LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
233                          LLVMModuleRef *OutMod, char **OutError) {
234  Module *Mod = unwrap(M);
235  unwrap(EE)->removeModule(Mod);
236  *OutMod = wrap(Mod);
237  return 0;
238}
239
240LLVMBool 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,
245                          OutError);
246}
247
248LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
249                          LLVMValueRef *OutFn) {
250  if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
251    *OutFn = wrap(F);
252    return 0;
253  }
254  return 1;
255}
256
257void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, LLVMValueRef Fn) {
258  return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn));
259}
260
261LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
262  return wrap(unwrap(EE)->getDataLayout());
263}
264
265void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
266                          void* Addr) {
267  unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
268}
269
270void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
271  return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
272}
273