X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FExecutionEngine%2FMCJIT%2FMCJIT.cpp;h=5f1fac7eff16a68c5ff8b8a06677734de74d7d43;hb=875710a2fd6b3c4f814961582594bd5c1cdb493a;hp=4d5723a7a1369022c049f9e638d07b37caf326f6;hpb=6880f0e19f24f7e7f0877e876b74c5cab10abc22;p=oota-llvm.git diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/lib/ExecutionEngine/MCJIT/MCJIT.cpp index 4d5723a7a13..5f1fac7eff1 100644 --- a/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -28,6 +28,7 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MutexGuard.h" #include "llvm/Target/TargetLowering.h" +#include "llvm/Target/TargetSubtargetInfo.h" using namespace llvm; @@ -45,24 +46,21 @@ extern "C" void LLVMLinkInMCJIT() { ExecutionEngine *MCJIT::createJIT(Module *M, std::string *ErrorStr, RTDyldMemoryManager *MemMgr, - bool GVsWithCode, TargetMachine *TM) { // Try to register the program as a source of symbols to resolve against. // // FIXME: Don't do this here. - sys::DynamicLibrary::LoadLibraryPermanently(0, NULL); + sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr); - return new MCJIT(M, TM, MemMgr ? MemMgr : new SectionMemoryManager(), - GVsWithCode); + return new MCJIT(M, TM, MemMgr ? MemMgr : new SectionMemoryManager()); } -MCJIT::MCJIT(Module *m, TargetMachine *tm, RTDyldMemoryManager *MM, - bool AllocateGVsWithCode) - : ExecutionEngine(m), TM(tm), Ctx(0), MemMgr(this, MM), Dyld(&MemMgr), - ObjCache(0) { +MCJIT::MCJIT(Module *m, TargetMachine *tm, RTDyldMemoryManager *MM) + : ExecutionEngine(m), TM(tm), Ctx(nullptr), MemMgr(this, MM), Dyld(&MemMgr), + ObjCache(nullptr) { OwnedModules.addModule(m); - setDataLayout(TM->getDataLayout()); + setDataLayout(TM->getSubtargetImpl()->getDataLayout()); } MCJIT::~MCJIT() { @@ -90,12 +88,6 @@ MCJIT::~MCJIT() { } LoadedObjects.clear(); - - SmallVector::iterator ArIt, ArEnd; - for (ArIt = Archives.begin(), ArEnd = Archives.end(); ArIt != ArEnd; ++ArIt) { - object::Archive *A = *ArIt; - delete A; - } Archives.clear(); delete TM; @@ -113,9 +105,9 @@ bool MCJIT::removeModule(Module *M) { -void MCJIT::addObjectFile(object::ObjectFile *Obj) { - ObjectImage *LoadedObject = Dyld.loadObject(Obj); - if (!LoadedObject) +void MCJIT::addObjectFile(std::unique_ptr Obj) { + ObjectImage *LoadedObject = Dyld.loadObject(std::move(Obj)); + if (!LoadedObject || Dyld.hasError()) report_fatal_error(Dyld.getErrorString()); LoadedObjects.push_back(LoadedObject); @@ -123,8 +115,8 @@ void MCJIT::addObjectFile(object::ObjectFile *Obj) { NotifyObjectEmitted(*LoadedObject); } -void MCJIT::addArchive(object::Archive *A) { - Archives.push_back(A); +void MCJIT::addArchive(std::unique_ptr A) { + Archives.push_back(std::move(A)); } @@ -142,14 +134,16 @@ ObjectBufferStream* MCJIT::emitObject(Module *M) { PassManager PM; - PM.add(new DataLayout(*TM->getDataLayout())); + M->setDataLayout(TM->getSubtargetImpl()->getDataLayout()); + PM.add(new DataLayoutPass(M)); // The RuntimeDyld will take ownership of this shortly - OwningPtr CompiledObject(new ObjectBufferStream()); + std::unique_ptr CompiledObject(new ObjectBufferStream()); // Turn the machine code intermediate representation into bytes in memory // that may be executed. - if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(), false)) { + if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(), + !getVerifyModules())) { report_fatal_error("Target does not support MC emission!"); } @@ -163,11 +157,11 @@ ObjectBufferStream* MCJIT::emitObject(Module *M) { if (ObjCache) { // MemoryBuffer is a thin wrapper around the actual memory, so it's OK // to create a temporary object here and delete it after the call. - OwningPtr MB(CompiledObject->getMemBuffer()); + std::unique_ptr MB(CompiledObject->getMemBuffer()); ObjCache->notifyObjectCompiled(M, MB.get()); } - return CompiledObject.take(); + return CompiledObject.release(); } void MCJIT::generateCodeForModule(Module *M) { @@ -182,12 +176,12 @@ void MCJIT::generateCodeForModule(Module *M) { if (OwnedModules.hasModuleBeenLoaded(M)) return; - OwningPtr ObjectToLoad; + std::unique_ptr ObjectToLoad; // Try to load the pre-compiled object from cache if possible - if (0 != ObjCache) { - OwningPtr PreCompiledObject(ObjCache->getObject(M)); - if (0 != PreCompiledObject.get()) - ObjectToLoad.reset(new ObjectBuffer(PreCompiledObject.take())); + if (ObjCache) { + std::unique_ptr PreCompiledObject(ObjCache->getObject(M)); + if (PreCompiledObject.get()) + ObjectToLoad.reset(new ObjectBuffer(PreCompiledObject.release())); } // If the cache did not contain a suitable object, compile the object @@ -198,7 +192,7 @@ void MCJIT::generateCodeForModule(Module *M) { // Load the object into the dynamic linker. // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list). - ObjectImage *LoadedObject = Dyld.loadObject(ObjectToLoad.take()); + ObjectImage *LoadedObject = Dyld.loadObject(ObjectToLoad.release()); LoadedObjects.push_back(LoadedObject); if (!LoadedObject) report_fatal_error(Dyld.getErrorString()); @@ -253,12 +247,8 @@ void MCJIT::finalizeModule(Module *M) { finalizeLoadedModules(); } -void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) { - report_fatal_error("not yet implemented"); -} - uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) { - Mangler Mang(TM->getDataLayout()); + Mangler Mang(TM->getSubtargetImpl()->getDataLayout()); SmallString<128> FullName; Mang.getNameWithPrefix(FullName, Name); return Dyld.getSymbolLoadAddress(FullName); @@ -284,7 +274,7 @@ Module *MCJIT::findModuleForSymbol(const std::string &Name, } } // We didn't find the symbol in any of our modules. - return NULL; + return nullptr; } uint64_t MCJIT::getSymbolAddress(const std::string &Name, @@ -297,19 +287,21 @@ uint64_t MCJIT::getSymbolAddress(const std::string &Name, if (Addr) return Addr; - SmallVector::iterator I, E; - for (I = Archives.begin(), E = Archives.end(); I != E; ++I) { - object::Archive *A = *I; + for (std::unique_ptr &A : Archives) { // Look for our symbols in each Archive object::Archive::child_iterator ChildIt = A->findSym(Name); if (ChildIt != A->child_end()) { - OwningPtr ChildBin; // FIXME: Support nested archives? - if (!ChildIt->getAsBinary(ChildBin) && ChildBin->isObject()) { - object::ObjectFile *OF = reinterpret_cast( - ChildBin.take()); + ErrorOr> ChildBinOrErr = + ChildIt->getAsBinary(); + if (ChildBinOrErr.getError()) + continue; + std::unique_ptr &ChildBin = ChildBinOrErr.get(); + if (ChildBin->isObject()) { + std::unique_ptr OF( + static_cast(ChildBin.release())); // This causes the object file to be loaded. - addObjectFile(OF); + addObjectFile(std::move(OF)); // The address should be here now. Addr = getExistingSymbolAddress(Name); if (Addr) @@ -364,26 +356,18 @@ void *MCJIT::getPointerToFunction(Function *F) { generateCodeForModule(M); else if (!OwnedModules.hasModuleBeenLoaded(M)) // If this function doesn't belong to one of our modules, we're done. - return NULL; + return nullptr; // FIXME: Should the Dyld be retaining module information? Probably not. // // This is the accessor for the target address, so make sure to check the // load address of the symbol, not the local address. - Mangler Mang(TM->getDataLayout()); + Mangler Mang(TM->getSubtargetImpl()->getDataLayout()); SmallString<128> Name; - TM->getTargetLowering()->getNameWithPrefix(Name, F, Mang); + TM->getNameWithPrefix(Name, F, Mang); return (void*)Dyld.getSymbolLoadAddress(Name); } -void *MCJIT::recompileAndRelinkFunction(Function *F) { - report_fatal_error("not yet implemented"); -} - -void MCJIT::freeMachineCodeForFunction(Function *F) { - report_fatal_error("not yet implemented"); -} - void MCJIT::runStaticConstructorsDestructorsInModulePtrSet( bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) { for (; I != E; ++I) { @@ -408,7 +392,7 @@ Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName, if (Function *F = (*I)->getFunction(FnName)) return F; } - return 0; + return nullptr; } Function *MCJIT::FindFunctionNamed(const char *FnName) { @@ -540,21 +524,20 @@ void *MCJIT::getPointerToNamedFunction(const std::string &Name, report_fatal_error("Program used external function '"+Name+ "' which could not be resolved!"); } - return 0; + return nullptr; } void MCJIT::RegisterJITEventListener(JITEventListener *L) { - if (L == NULL) + if (!L) return; MutexGuard locked(lock); EventListeners.push_back(L); } void MCJIT::UnregisterJITEventListener(JITEventListener *L) { - if (L == NULL) + if (!L) return; MutexGuard locked(lock); - SmallVector::reverse_iterator I= - std::find(EventListeners.rbegin(), EventListeners.rend(), L); + auto I = std::find(EventListeners.rbegin(), EventListeners.rend(), L); if (I != EventListeners.rend()) { std::swap(*I, EventListeners.back()); EventListeners.pop_back(); @@ -570,7 +553,8 @@ void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) { void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) { MutexGuard locked(lock); for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { - EventListeners[I]->NotifyFreeingObject(Obj); + JITEventListener *L = EventListeners[I]; + L->NotifyFreeingObject(Obj); } }