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/ExecutionEngine/RTDyldMemoryManager.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/Module.h"
21#include "llvm/Support/ErrorHandling.h"
22#include <cstring>
23
24using namespace llvm;
25
26// Wrapping the C bindings types.
27DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef)
28
29inline DataLayout *unwrap(LLVMTargetDataRef P) {
30  return reinterpret_cast<DataLayout*>(P);
31}
32
33inline LLVMTargetDataRef wrap(const DataLayout *P) {
34  return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
35}
36
37inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
38  return reinterpret_cast<TargetLibraryInfo*>(P);
39}
40
41inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
42  TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
43  return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
44}
45
46/*===-- Operations on generic values --------------------------------------===*/
47
48LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
49                                                unsigned long long N,
50                                                LLVMBool IsSigned) {
51  GenericValue *GenVal = new GenericValue();
52  GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
53  return wrap(GenVal);
54}
55
56LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
57  GenericValue *GenVal = new GenericValue();
58  GenVal->PointerVal = P;
59  return wrap(GenVal);
60}
61
62LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
63  GenericValue *GenVal = new GenericValue();
64  switch (unwrap(TyRef)->getTypeID()) {
65  case Type::FloatTyID:
66    GenVal->FloatVal = N;
67    break;
68  case Type::DoubleTyID:
69    GenVal->DoubleVal = N;
70    break;
71  default:
72    llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
73  }
74  return wrap(GenVal);
75}
76
77unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
78  return unwrap(GenValRef)->IntVal.getBitWidth();
79}
80
81unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
82                                         LLVMBool IsSigned) {
83  GenericValue *GenVal = unwrap(GenValRef);
84  if (IsSigned)
85    return GenVal->IntVal.getSExtValue();
86  else
87    return GenVal->IntVal.getZExtValue();
88}
89
90void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
91  return unwrap(GenVal)->PointerVal;
92}
93
94double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
95  switch (unwrap(TyRef)->getTypeID()) {
96  case Type::FloatTyID:
97    return unwrap(GenVal)->FloatVal;
98  case Type::DoubleTyID:
99    return unwrap(GenVal)->DoubleVal;
100  default:
101    llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
102  }
103}
104
105void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
106  delete unwrap(GenVal);
107}
108
109/*===-- Operations on execution engines -----------------------------------===*/
110
111LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
112                                            LLVMModuleRef M,
113                                            char **OutError) {
114  std::string Error;
115  EngineBuilder builder(unwrap(M));
116  builder.setEngineKind(EngineKind::Either)
117         .setErrorStr(&Error);
118  if (ExecutionEngine *EE = builder.create()){
119    *OutEE = wrap(EE);
120    return 0;
121  }
122  *OutError = strdup(Error.c_str());
123  return 1;
124}
125
126LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
127                                        LLVMModuleRef M,
128                                        char **OutError) {
129  std::string Error;
130  EngineBuilder builder(unwrap(M));
131  builder.setEngineKind(EngineKind::Interpreter)
132         .setErrorStr(&Error);
133  if (ExecutionEngine *Interp = builder.create()) {
134    *OutInterp = wrap(Interp);
135    return 0;
136  }
137  *OutError = strdup(Error.c_str());
138  return 1;
139}
140
141LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
142                                        LLVMModuleRef M,
143                                        unsigned OptLevel,
144                                        char **OutError) {
145  std::string Error;
146  EngineBuilder builder(unwrap(M));
147  builder.setEngineKind(EngineKind::JIT)
148         .setErrorStr(&Error)
149         .setOptLevel((CodeGenOpt::Level)OptLevel);
150  if (ExecutionEngine *JIT = builder.create()) {
151    *OutJIT = wrap(JIT);
152    return 0;
153  }
154  *OutError = strdup(Error.c_str());
155  return 1;
156}
157
158void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions,
159                                        size_t SizeOfPassedOptions) {
160  LLVMMCJITCompilerOptions options;
161  memset(&options, 0, sizeof(options)); // Most fields are zero by default.
162  options.CodeModel = LLVMCodeModelJITDefault;
163
164  memcpy(PassedOptions, &options,
165         std::min(sizeof(options), SizeOfPassedOptions));
166}
167
168LLVMBool LLVMCreateMCJITCompilerForModule(
169    LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M,
170    LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions,
171    char **OutError) {
172  LLVMMCJITCompilerOptions options;
173  // If the user passed a larger sized options struct, then they were compiled
174  // against a newer LLVM. Tell them that something is wrong.
175  if (SizeOfPassedOptions > sizeof(options)) {
176    *OutError = strdup(
177      "Refusing to use options struct that is larger than my own; assuming "
178      "LLVM library mismatch.");
179    return 1;
180  }
181
182  // Defend against the user having an old version of the API by ensuring that
183  // any fields they didn't see are cleared. We must defend against fields being
184  // set to the bitwise equivalent of zero, and assume that this means "do the
185  // default" as if that option hadn't been available.
186  LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
187  memcpy(&options, PassedOptions, SizeOfPassedOptions);
188
189  TargetOptions targetOptions;
190  targetOptions.NoFramePointerElim = options.NoFramePointerElim;
191  targetOptions.EnableFastISel = options.EnableFastISel;
192
193  std::string Error;
194  EngineBuilder builder(unwrap(M));
195  builder.setEngineKind(EngineKind::JIT)
196         .setErrorStr(&Error)
197         .setUseMCJIT(true)
198         .setOptLevel((CodeGenOpt::Level)options.OptLevel)
199         .setCodeModel(unwrap(options.CodeModel))
200         .setTargetOptions(targetOptions);
201  if (options.MCJMM)
202    builder.setMCJITMemoryManager(unwrap(options.MCJMM));
203  if (ExecutionEngine *JIT = builder.create()) {
204    *OutJIT = wrap(JIT);
205    return 0;
206  }
207  *OutError = strdup(Error.c_str());
208  return 1;
209}
210
211LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
212                                   LLVMModuleProviderRef MP,
213                                   char **OutError) {
214  /* The module provider is now actually a module. */
215  return LLVMCreateExecutionEngineForModule(OutEE,
216                                            reinterpret_cast<LLVMModuleRef>(MP),
217                                            OutError);
218}
219
220LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
221                               LLVMModuleProviderRef MP,
222                               char **OutError) {
223  /* The module provider is now actually a module. */
224  return LLVMCreateInterpreterForModule(OutInterp,
225                                        reinterpret_cast<LLVMModuleRef>(MP),
226                                        OutError);
227}
228
229LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
230                               LLVMModuleProviderRef MP,
231                               unsigned OptLevel,
232                               char **OutError) {
233  /* The module provider is now actually a module. */
234  return LLVMCreateJITCompilerForModule(OutJIT,
235                                        reinterpret_cast<LLVMModuleRef>(MP),
236                                        OptLevel, OutError);
237}
238
239
240void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
241  delete unwrap(EE);
242}
243
244void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
245  unwrap(EE)->runStaticConstructorsDestructors(false);
246}
247
248void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
249  unwrap(EE)->runStaticConstructorsDestructors(true);
250}
251
252int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
253                          unsigned ArgC, const char * const *ArgV,
254                          const char * const *EnvP) {
255  unwrap(EE)->finalizeObject();
256
257  std::vector<std::string> ArgVec;
258  for (unsigned I = 0; I != ArgC; ++I)
259    ArgVec.push_back(ArgV[I]);
260
261  return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
262}
263
264LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
265                                    unsigned NumArgs,
266                                    LLVMGenericValueRef *Args) {
267  unwrap(EE)->finalizeObject();
268
269  std::vector<GenericValue> ArgVec;
270  ArgVec.reserve(NumArgs);
271  for (unsigned I = 0; I != NumArgs; ++I)
272    ArgVec.push_back(*unwrap(Args[I]));
273
274  GenericValue *Result = new GenericValue();
275  *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
276  return wrap(Result);
277}
278
279void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
280  unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
281}
282
283void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
284  unwrap(EE)->addModule(unwrap(M));
285}
286
287void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
288  /* The module provider is now actually a module. */
289  LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
290}
291
292LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
293                          LLVMModuleRef *OutMod, char **OutError) {
294  Module *Mod = unwrap(M);
295  unwrap(EE)->removeModule(Mod);
296  *OutMod = wrap(Mod);
297  return 0;
298}
299
300LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
301                                  LLVMModuleProviderRef MP,
302                                  LLVMModuleRef *OutMod, char **OutError) {
303  /* The module provider is now actually a module. */
304  return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
305                          OutError);
306}
307
308LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
309                          LLVMValueRef *OutFn) {
310  if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
311    *OutFn = wrap(F);
312    return 0;
313  }
314  return 1;
315}
316
317void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
318                                     LLVMValueRef Fn) {
319  return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn));
320}
321
322LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
323  return wrap(unwrap(EE)->getDataLayout());
324}
325
326void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
327                          void* Addr) {
328  unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
329}
330
331void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
332  unwrap(EE)->finalizeObject();
333
334  return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
335}
336
337/*===-- Operations on memory managers -------------------------------------===*/
338
339namespace {
340
341struct SimpleBindingMMFunctions {
342  uint8_t *(*AllocateCodeSection)(void *Opaque,
343                                  uintptr_t Size, unsigned Alignment,
344                                  unsigned SectionID);
345  uint8_t *(*AllocateDataSection)(void *Opaque,
346                                  uintptr_t Size, unsigned Alignment,
347                                  unsigned SectionID, LLVMBool IsReadOnly);
348  LLVMBool (*FinalizeMemory)(void *Opaque, char **ErrMsg);
349  void (*Destroy)(void *Opaque);
350};
351
352class SimpleBindingMemoryManager : public RTDyldMemoryManager {
353public:
354  SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions,
355                             void *Opaque);
356  virtual ~SimpleBindingMemoryManager();
357
358  virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
359                                       unsigned SectionID);
360
361  virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
362                                       unsigned SectionID,
363                                       bool isReadOnly);
364
365  virtual bool finalizeMemory(std::string *ErrMsg);
366
367private:
368  SimpleBindingMMFunctions Functions;
369  void *Opaque;
370};
371
372SimpleBindingMemoryManager::SimpleBindingMemoryManager(
373  const SimpleBindingMMFunctions& Functions,
374  void *Opaque)
375  : Functions(Functions), Opaque(Opaque) {
376  assert(Functions.AllocateCodeSection &&
377         "No AllocateCodeSection function provided!");
378  assert(Functions.AllocateDataSection &&
379         "No AllocateDataSection function provided!");
380  assert(Functions.FinalizeMemory &&
381         "No FinalizeMemory function provided!");
382  assert(Functions.Destroy &&
383         "No Destroy function provided!");
384}
385
386SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
387  Functions.Destroy(Opaque);
388}
389
390uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
391  uintptr_t Size, unsigned Alignment, unsigned SectionID) {
392  return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID);
393}
394
395uint8_t *SimpleBindingMemoryManager::allocateDataSection(
396  uintptr_t Size, unsigned Alignment, unsigned SectionID, bool isReadOnly) {
397  return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
398                                       isReadOnly);
399}
400
401bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
402  char *errMsgCString = 0;
403  bool result = Functions.FinalizeMemory(Opaque, &errMsgCString);
404  assert((result || !errMsgCString) &&
405         "Did not expect an error message if FinalizeMemory succeeded");
406  if (errMsgCString) {
407    if (ErrMsg)
408      *ErrMsg = errMsgCString;
409    free(errMsgCString);
410  }
411  return result;
412}
413
414} // anonymous namespace
415
416LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(
417  void *Opaque,
418  uint8_t *(*AllocateCodeSection)(void *Opaque,
419                                  uintptr_t Size, unsigned Alignment,
420                                  unsigned SectionID),
421  uint8_t *(*AllocateDataSection)(void *Opaque,
422                                  uintptr_t Size, unsigned Alignment,
423                                  unsigned SectionID, LLVMBool IsReadOnly),
424  LLVMBool (*FinalizeMemory)(void *Opaque, char **ErrMsg),
425  void (*Destroy)(void *Opaque)) {
426
427  if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
428      !Destroy)
429    return NULL;
430
431  SimpleBindingMMFunctions functions;
432  functions.AllocateCodeSection = AllocateCodeSection;
433  functions.AllocateDataSection = AllocateDataSection;
434  functions.FinalizeMemory = FinalizeMemory;
435  functions.Destroy = Destroy;
436  return wrap(new SimpleBindingMemoryManager(functions, Opaque));
437}
438
439void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM) {
440  delete unwrap(MM);
441}
442
443