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