[C++11] Replace OwningPtr::take() with OwningPtr::release().
[oota-llvm.git] / lib / ExecutionEngine / MCJIT / MCJIT.cpp
1 //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
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 #include "MCJIT.h"
11 #include "llvm/ExecutionEngine/GenericValue.h"
12 #include "llvm/ExecutionEngine/JITEventListener.h"
13 #include "llvm/ExecutionEngine/JITMemoryManager.h"
14 #include "llvm/ExecutionEngine/MCJIT.h"
15 #include "llvm/ExecutionEngine/ObjectBuffer.h"
16 #include "llvm/ExecutionEngine/ObjectImage.h"
17 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/Object/Archive.h"
25 #include "llvm/PassManager.h"
26 #include "llvm/Support/DynamicLibrary.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/MutexGuard.h"
30 #include "llvm/Target/TargetLowering.h"
31
32 using namespace llvm;
33
34 namespace {
35
36 static struct RegisterJIT {
37   RegisterJIT() { MCJIT::Register(); }
38 } JITRegistrator;
39
40 }
41
42 extern "C" void LLVMLinkInMCJIT() {
43 }
44
45 ExecutionEngine *MCJIT::createJIT(Module *M,
46                                   std::string *ErrorStr,
47                                   RTDyldMemoryManager *MemMgr,
48                                   bool GVsWithCode,
49                                   TargetMachine *TM) {
50   // Try to register the program as a source of symbols to resolve against.
51   //
52   // FIXME: Don't do this here.
53   sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
54
55   return new MCJIT(M, TM, MemMgr ? MemMgr : new SectionMemoryManager(),
56                    GVsWithCode);
57 }
58
59 MCJIT::MCJIT(Module *m, TargetMachine *tm, RTDyldMemoryManager *MM,
60              bool AllocateGVsWithCode)
61   : ExecutionEngine(m), TM(tm), Ctx(0), MemMgr(this, MM), Dyld(&MemMgr),
62     ObjCache(0) {
63
64   OwnedModules.addModule(m);
65   setDataLayout(TM->getDataLayout());
66 }
67
68 MCJIT::~MCJIT() {
69   MutexGuard locked(lock);
70   // FIXME: We are managing our modules, so we do not want the base class
71   // ExecutionEngine to manage them as well. To avoid double destruction
72   // of the first (and only) module added in ExecutionEngine constructor
73   // we remove it from EE and will destruct it ourselves.
74   //
75   // It may make sense to move our module manager (based on SmallStPtr) back
76   // into EE if the JIT and Interpreter can live with it.
77   // If so, additional functions: addModule, removeModule, FindFunctionNamed,
78   // runStaticConstructorsDestructors could be moved back to EE as well.
79   //
80   Modules.clear();
81   Dyld.deregisterEHFrames();
82
83   LoadedObjectList::iterator it, end;
84   for (it = LoadedObjects.begin(), end = LoadedObjects.end(); it != end; ++it) {
85     ObjectImage *Obj = *it;
86     if (Obj) {
87       NotifyFreeingObject(*Obj);
88       delete Obj;
89     }
90   }
91   LoadedObjects.clear();
92
93
94   SmallVector<object::Archive *, 2>::iterator ArIt, ArEnd;
95   for (ArIt = Archives.begin(), ArEnd = Archives.end(); ArIt != ArEnd; ++ArIt) {
96     object::Archive *A = *ArIt;
97     delete A;
98   }
99   Archives.clear();
100
101   delete TM;
102 }
103
104 void MCJIT::addModule(Module *M) {
105   MutexGuard locked(lock);
106   OwnedModules.addModule(M);
107 }
108
109 bool MCJIT::removeModule(Module *M) {
110   MutexGuard locked(lock);
111   return OwnedModules.removeModule(M);
112 }
113
114
115
116 void MCJIT::addObjectFile(object::ObjectFile *Obj) {
117   ObjectImage *LoadedObject = Dyld.loadObject(Obj);
118   if (!LoadedObject)
119     report_fatal_error(Dyld.getErrorString());
120
121   LoadedObjects.push_back(LoadedObject);
122
123   NotifyObjectEmitted(*LoadedObject);
124 }
125
126 void MCJIT::addArchive(object::Archive *A) {
127   Archives.push_back(A);
128 }
129
130
131 void MCJIT::setObjectCache(ObjectCache* NewCache) {
132   MutexGuard locked(lock);
133   ObjCache = NewCache;
134 }
135
136 ObjectBufferStream* MCJIT::emitObject(Module *M) {
137   MutexGuard locked(lock);
138
139   // This must be a module which has already been added but not loaded to this
140   // MCJIT instance, since these conditions are tested by our caller,
141   // generateCodeForModule.
142
143   PassManager PM;
144
145   M->setDataLayout(TM->getDataLayout());
146   PM.add(new DataLayoutPass(M));
147
148   // The RuntimeDyld will take ownership of this shortly
149   OwningPtr<ObjectBufferStream> CompiledObject(new ObjectBufferStream());
150
151   // Turn the machine code intermediate representation into bytes in memory
152   // that may be executed.
153   if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(), false)) {
154     report_fatal_error("Target does not support MC emission!");
155   }
156
157   // Initialize passes.
158   PM.run(*M);
159   // Flush the output buffer to get the generated code into memory
160   CompiledObject->flush();
161
162   // If we have an object cache, tell it about the new object.
163   // Note that we're using the compiled image, not the loaded image (as below).
164   if (ObjCache) {
165     // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
166     // to create a temporary object here and delete it after the call.
167     OwningPtr<MemoryBuffer> MB(CompiledObject->getMemBuffer());
168     ObjCache->notifyObjectCompiled(M, MB.get());
169   }
170
171   return CompiledObject.release();
172 }
173
174 void MCJIT::generateCodeForModule(Module *M) {
175   // Get a thread lock to make sure we aren't trying to load multiple times
176   MutexGuard locked(lock);
177
178   // This must be a module which has already been added to this MCJIT instance.
179   assert(OwnedModules.ownsModule(M) &&
180          "MCJIT::generateCodeForModule: Unknown module.");
181
182   // Re-compilation is not supported
183   if (OwnedModules.hasModuleBeenLoaded(M))
184     return;
185
186   OwningPtr<ObjectBuffer> ObjectToLoad;
187   // Try to load the pre-compiled object from cache if possible
188   if (0 != ObjCache) {
189     OwningPtr<MemoryBuffer> PreCompiledObject(ObjCache->getObject(M));
190     if (0 != PreCompiledObject.get())
191       ObjectToLoad.reset(new ObjectBuffer(PreCompiledObject.release()));
192   }
193
194   // If the cache did not contain a suitable object, compile the object
195   if (!ObjectToLoad) {
196     ObjectToLoad.reset(emitObject(M));
197     assert(ObjectToLoad.get() && "Compilation did not produce an object.");
198   }
199
200   // Load the object into the dynamic linker.
201   // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
202   ObjectImage *LoadedObject = Dyld.loadObject(ObjectToLoad.release());
203   LoadedObjects.push_back(LoadedObject);
204   if (!LoadedObject)
205     report_fatal_error(Dyld.getErrorString());
206
207   // FIXME: Make this optional, maybe even move it to a JIT event listener
208   LoadedObject->registerWithDebugger();
209
210   NotifyObjectEmitted(*LoadedObject);
211
212   OwnedModules.markModuleAsLoaded(M);
213 }
214
215 void MCJIT::finalizeLoadedModules() {
216   MutexGuard locked(lock);
217
218   // Resolve any outstanding relocations.
219   Dyld.resolveRelocations();
220
221   OwnedModules.markAllLoadedModulesAsFinalized();
222
223   // Register EH frame data for any module we own which has been loaded
224   Dyld.registerEHFrames();
225
226   // Set page permissions.
227   MemMgr.finalizeMemory();
228 }
229
230 // FIXME: Rename this.
231 void MCJIT::finalizeObject() {
232   MutexGuard locked(lock);
233
234   for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
235                               E = OwnedModules.end_added();
236        I != E; ++I) {
237     Module *M = *I;
238     generateCodeForModule(M);
239   }
240
241   finalizeLoadedModules();
242 }
243
244 void MCJIT::finalizeModule(Module *M) {
245   MutexGuard locked(lock);
246
247   // This must be a module which has already been added to this MCJIT instance.
248   assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
249
250   // If the module hasn't been compiled, just do that.
251   if (!OwnedModules.hasModuleBeenLoaded(M))
252     generateCodeForModule(M);
253
254   finalizeLoadedModules();
255 }
256
257 void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
258   report_fatal_error("not yet implemented");
259 }
260
261 uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) {
262   Mangler Mang(TM->getDataLayout());
263   SmallString<128> FullName;
264   Mang.getNameWithPrefix(FullName, Name);
265   return Dyld.getSymbolLoadAddress(FullName);
266 }
267
268 Module *MCJIT::findModuleForSymbol(const std::string &Name,
269                                    bool CheckFunctionsOnly) {
270   MutexGuard locked(lock);
271
272   // If it hasn't already been generated, see if it's in one of our modules.
273   for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
274                               E = OwnedModules.end_added();
275        I != E; ++I) {
276     Module *M = *I;
277     Function *F = M->getFunction(Name);
278     if (F && !F->isDeclaration())
279       return M;
280     if (!CheckFunctionsOnly) {
281       GlobalVariable *G = M->getGlobalVariable(Name);
282       if (G && !G->isDeclaration())
283         return M;
284       // FIXME: Do we need to worry about global aliases?
285     }
286   }
287   // We didn't find the symbol in any of our modules.
288   return NULL;
289 }
290
291 uint64_t MCJIT::getSymbolAddress(const std::string &Name,
292                                  bool CheckFunctionsOnly)
293 {
294   MutexGuard locked(lock);
295
296   // First, check to see if we already have this symbol.
297   uint64_t Addr = getExistingSymbolAddress(Name);
298   if (Addr)
299     return Addr;
300
301   SmallVector<object::Archive*, 2>::iterator I, E;
302   for (I = Archives.begin(), E = Archives.end(); I != E; ++I) {
303     object::Archive *A = *I;
304     // Look for our symbols in each Archive
305     object::Archive::child_iterator ChildIt = A->findSym(Name);
306     if (ChildIt != A->child_end()) {
307       OwningPtr<object::Binary> ChildBin;
308       // FIXME: Support nested archives?
309       if (!ChildIt->getAsBinary(ChildBin) && ChildBin->isObject()) {
310         object::ObjectFile *OF = reinterpret_cast<object::ObjectFile *>(
311                                                             ChildBin.release());
312         // This causes the object file to be loaded.
313         addObjectFile(OF);
314         // The address should be here now.
315         Addr = getExistingSymbolAddress(Name);
316         if (Addr)
317           return Addr;
318       }
319     }
320   }
321
322   // If it hasn't already been generated, see if it's in one of our modules.
323   Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
324   if (!M)
325     return 0;
326
327   generateCodeForModule(M);
328
329   // Check the RuntimeDyld table again, it should be there now.
330   return getExistingSymbolAddress(Name);
331 }
332
333 uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
334   MutexGuard locked(lock);
335   uint64_t Result = getSymbolAddress(Name, false);
336   if (Result != 0)
337     finalizeLoadedModules();
338   return Result;
339 }
340
341 uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
342   MutexGuard locked(lock);
343   uint64_t Result = getSymbolAddress(Name, true);
344   if (Result != 0)
345     finalizeLoadedModules();
346   return Result;
347 }
348
349 // Deprecated.  Use getFunctionAddress instead.
350 void *MCJIT::getPointerToFunction(Function *F) {
351   MutexGuard locked(lock);
352
353   if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
354     bool AbortOnFailure = !F->hasExternalWeakLinkage();
355     void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
356     addGlobalMapping(F, Addr);
357     return Addr;
358   }
359
360   Module *M = F->getParent();
361   bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
362
363   // Make sure the relevant module has been compiled and loaded.
364   if (HasBeenAddedButNotLoaded)
365     generateCodeForModule(M);
366   else if (!OwnedModules.hasModuleBeenLoaded(M))
367     // If this function doesn't belong to one of our modules, we're done.
368     return NULL;
369
370   // FIXME: Should the Dyld be retaining module information? Probably not.
371   //
372   // This is the accessor for the target address, so make sure to check the
373   // load address of the symbol, not the local address.
374   Mangler Mang(TM->getDataLayout());
375   SmallString<128> Name;
376   TM->getNameWithPrefix(Name, F, Mang);
377   return (void*)Dyld.getSymbolLoadAddress(Name);
378 }
379
380 void *MCJIT::recompileAndRelinkFunction(Function *F) {
381   report_fatal_error("not yet implemented");
382 }
383
384 void MCJIT::freeMachineCodeForFunction(Function *F) {
385   report_fatal_error("not yet implemented");
386 }
387
388 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
389     bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
390   for (; I != E; ++I) {
391     ExecutionEngine::runStaticConstructorsDestructors(*I, isDtors);
392   }
393 }
394
395 void MCJIT::runStaticConstructorsDestructors(bool isDtors) {
396   // Execute global ctors/dtors for each module in the program.
397   runStaticConstructorsDestructorsInModulePtrSet(
398       isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
399   runStaticConstructorsDestructorsInModulePtrSet(
400       isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
401   runStaticConstructorsDestructorsInModulePtrSet(
402       isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
403 }
404
405 Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName,
406                                                  ModulePtrSet::iterator I,
407                                                  ModulePtrSet::iterator E) {
408   for (; I != E; ++I) {
409     if (Function *F = (*I)->getFunction(FnName))
410       return F;
411   }
412   return 0;
413 }
414
415 Function *MCJIT::FindFunctionNamed(const char *FnName) {
416   Function *F = FindFunctionNamedInModulePtrSet(
417       FnName, OwnedModules.begin_added(), OwnedModules.end_added());
418   if (!F)
419     F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
420                                         OwnedModules.end_loaded());
421   if (!F)
422     F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
423                                         OwnedModules.end_finalized());
424   return F;
425 }
426
427 GenericValue MCJIT::runFunction(Function *F,
428                                 const std::vector<GenericValue> &ArgValues) {
429   assert(F && "Function *F was null at entry to run()");
430
431   void *FPtr = getPointerToFunction(F);
432   assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
433   FunctionType *FTy = F->getFunctionType();
434   Type *RetTy = FTy->getReturnType();
435
436   assert((FTy->getNumParams() == ArgValues.size() ||
437           (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
438          "Wrong number of arguments passed into function!");
439   assert(FTy->getNumParams() == ArgValues.size() &&
440          "This doesn't support passing arguments through varargs (yet)!");
441
442   // Handle some common cases first.  These cases correspond to common `main'
443   // prototypes.
444   if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
445     switch (ArgValues.size()) {
446     case 3:
447       if (FTy->getParamType(0)->isIntegerTy(32) &&
448           FTy->getParamType(1)->isPointerTy() &&
449           FTy->getParamType(2)->isPointerTy()) {
450         int (*PF)(int, char **, const char **) =
451           (int(*)(int, char **, const char **))(intptr_t)FPtr;
452
453         // Call the function.
454         GenericValue rv;
455         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
456                                  (char **)GVTOP(ArgValues[1]),
457                                  (const char **)GVTOP(ArgValues[2])));
458         return rv;
459       }
460       break;
461     case 2:
462       if (FTy->getParamType(0)->isIntegerTy(32) &&
463           FTy->getParamType(1)->isPointerTy()) {
464         int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
465
466         // Call the function.
467         GenericValue rv;
468         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
469                                  (char **)GVTOP(ArgValues[1])));
470         return rv;
471       }
472       break;
473     case 1:
474       if (FTy->getNumParams() == 1 &&
475           FTy->getParamType(0)->isIntegerTy(32)) {
476         GenericValue rv;
477         int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
478         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
479         return rv;
480       }
481       break;
482     }
483   }
484
485   // Handle cases where no arguments are passed first.
486   if (ArgValues.empty()) {
487     GenericValue rv;
488     switch (RetTy->getTypeID()) {
489     default: llvm_unreachable("Unknown return type for function call!");
490     case Type::IntegerTyID: {
491       unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
492       if (BitWidth == 1)
493         rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
494       else if (BitWidth <= 8)
495         rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
496       else if (BitWidth <= 16)
497         rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
498       else if (BitWidth <= 32)
499         rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
500       else if (BitWidth <= 64)
501         rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
502       else
503         llvm_unreachable("Integer types > 64 bits not supported");
504       return rv;
505     }
506     case Type::VoidTyID:
507       rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
508       return rv;
509     case Type::FloatTyID:
510       rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
511       return rv;
512     case Type::DoubleTyID:
513       rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
514       return rv;
515     case Type::X86_FP80TyID:
516     case Type::FP128TyID:
517     case Type::PPC_FP128TyID:
518       llvm_unreachable("long double not supported yet");
519     case Type::PointerTyID:
520       return PTOGV(((void*(*)())(intptr_t)FPtr)());
521     }
522   }
523
524   llvm_unreachable("Full-featured argument passing not supported yet!");
525 }
526
527 void *MCJIT::getPointerToNamedFunction(const std::string &Name,
528                                        bool AbortOnFailure) {
529   if (!isSymbolSearchingDisabled()) {
530     void *ptr = MemMgr.getPointerToNamedFunction(Name, false);
531     if (ptr)
532       return ptr;
533   }
534
535   /// If a LazyFunctionCreator is installed, use it to get/create the function.
536   if (LazyFunctionCreator)
537     if (void *RP = LazyFunctionCreator(Name))
538       return RP;
539
540   if (AbortOnFailure) {
541     report_fatal_error("Program used external function '"+Name+
542                        "' which could not be resolved!");
543   }
544   return 0;
545 }
546
547 void MCJIT::RegisterJITEventListener(JITEventListener *L) {
548   if (L == NULL)
549     return;
550   MutexGuard locked(lock);
551   EventListeners.push_back(L);
552 }
553 void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
554   if (L == NULL)
555     return;
556   MutexGuard locked(lock);
557   SmallVector<JITEventListener*, 2>::reverse_iterator I=
558       std::find(EventListeners.rbegin(), EventListeners.rend(), L);
559   if (I != EventListeners.rend()) {
560     std::swap(*I, EventListeners.back());
561     EventListeners.pop_back();
562   }
563 }
564 void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) {
565   MutexGuard locked(lock);
566   MemMgr.notifyObjectLoaded(this, &Obj);
567   for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
568     EventListeners[I]->NotifyObjectEmitted(Obj);
569   }
570 }
571 void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) {
572   MutexGuard locked(lock);
573   for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
574     EventListeners[I]->NotifyFreeingObject(Obj);
575   }
576 }
577
578 uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) {
579   uint64_t Result = ParentEngine->getSymbolAddress(Name, false);
580   // If the symbols wasn't found and it begins with an underscore, try again
581   // without the underscore.
582   if (!Result && Name[0] == '_')
583     Result = ParentEngine->getSymbolAddress(Name.substr(1), false);
584   if (Result)
585     return Result;
586   return ClientMM->getSymbolAddress(Name);
587 }