1ef860a24SChandler Carruth //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===// 2ef860a24SChandler Carruth // 3ef860a24SChandler Carruth // The LLVM Compiler Infrastructure 4ef860a24SChandler Carruth // 5ef860a24SChandler Carruth // This file is distributed under the University of Illinois Open Source 6ef860a24SChandler Carruth // License. See LICENSE.TXT for details. 7ef860a24SChandler Carruth // 8ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 9ef860a24SChandler Carruth // 10ef860a24SChandler Carruth // This file implements LLVMContext, as a wrapper around the opaque 11ef860a24SChandler Carruth // class LLVMContextImpl. 12ef860a24SChandler Carruth // 13ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 14ef860a24SChandler Carruth 159fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h" 16ef860a24SChandler Carruth #include "LLVMContextImpl.h" 179fb823bbSChandler Carruth #include "llvm/IR/Constants.h" 189fb823bbSChandler Carruth #include "llvm/IR/Instruction.h" 199fb823bbSChandler Carruth #include "llvm/IR/Metadata.h" 20ef860a24SChandler Carruth #include "llvm/Support/ManagedStatic.h" 21ef860a24SChandler Carruth #include "llvm/Support/SourceMgr.h" 22ef860a24SChandler Carruth #include <cctype> 23ef860a24SChandler Carruth using namespace llvm; 24ef860a24SChandler Carruth 25ef860a24SChandler Carruth static ManagedStatic<LLVMContext> GlobalContext; 26ef860a24SChandler Carruth 27ef860a24SChandler Carruth LLVMContext& llvm::getGlobalContext() { 28ef860a24SChandler Carruth return *GlobalContext; 29ef860a24SChandler Carruth } 30ef860a24SChandler Carruth 31ef860a24SChandler Carruth LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { 32ef860a24SChandler Carruth // Create the fixed metadata kinds. This is done in the same order as the 33ef860a24SChandler Carruth // MD_* enum values so that they correspond. 34ef860a24SChandler Carruth 35ef860a24SChandler Carruth // Create the 'dbg' metadata kind. 36ef860a24SChandler Carruth unsigned DbgID = getMDKindID("dbg"); 37ef860a24SChandler Carruth assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID; 38ef860a24SChandler Carruth 39ef860a24SChandler Carruth // Create the 'tbaa' metadata kind. 40ef860a24SChandler Carruth unsigned TBAAID = getMDKindID("tbaa"); 41ef860a24SChandler Carruth assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID; 42ef860a24SChandler Carruth 43ef860a24SChandler Carruth // Create the 'prof' metadata kind. 44ef860a24SChandler Carruth unsigned ProfID = getMDKindID("prof"); 45ef860a24SChandler Carruth assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID; 46ef860a24SChandler Carruth 47ef860a24SChandler Carruth // Create the 'fpmath' metadata kind. 48ef860a24SChandler Carruth unsigned FPAccuracyID = getMDKindID("fpmath"); 49ef860a24SChandler Carruth assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted"); 50ef860a24SChandler Carruth (void)FPAccuracyID; 51ef860a24SChandler Carruth 52ef860a24SChandler Carruth // Create the 'range' metadata kind. 53ef860a24SChandler Carruth unsigned RangeID = getMDKindID("range"); 54ef860a24SChandler Carruth assert(RangeID == MD_range && "range kind id drifted"); 55ef860a24SChandler Carruth (void)RangeID; 56ef860a24SChandler Carruth 57ef860a24SChandler Carruth // Create the 'tbaa.struct' metadata kind. 58ef860a24SChandler Carruth unsigned TBAAStructID = getMDKindID("tbaa.struct"); 59ef860a24SChandler Carruth assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted"); 60ef860a24SChandler Carruth (void)TBAAStructID; 61ef860a24SChandler Carruth } 62ef860a24SChandler Carruth LLVMContext::~LLVMContext() { delete pImpl; } 63ef860a24SChandler Carruth 64ef860a24SChandler Carruth void LLVMContext::addModule(Module *M) { 65ef860a24SChandler Carruth pImpl->OwnedModules.insert(M); 66ef860a24SChandler Carruth } 67ef860a24SChandler Carruth 68ef860a24SChandler Carruth void LLVMContext::removeModule(Module *M) { 69ef860a24SChandler Carruth pImpl->OwnedModules.erase(M); 70ef860a24SChandler Carruth } 71ef860a24SChandler Carruth 72ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 73ef860a24SChandler Carruth // Recoverable Backend Errors 74ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 75ef860a24SChandler Carruth 76ef860a24SChandler Carruth void LLVMContext::setDiagnosticHandler(DiagHandlerTy DiagHandler, 77ef860a24SChandler Carruth void *DiagContext) { 78ef860a24SChandler Carruth pImpl->DiagHandler = DiagHandler; 79ef860a24SChandler Carruth pImpl->DiagContext = DiagContext; 80ef860a24SChandler Carruth } 81ef860a24SChandler Carruth 82ef860a24SChandler Carruth /// getDiagnosticHandler - Return the diagnostic handler set by 83ef860a24SChandler Carruth /// setDiagnosticHandler. 84ef860a24SChandler Carruth LLVMContext::DiagHandlerTy LLVMContext::getDiagnosticHandler() const { 85ef860a24SChandler Carruth return pImpl->DiagHandler; 86ef860a24SChandler Carruth } 87ef860a24SChandler Carruth 88ef860a24SChandler Carruth /// getDiagnosticContext - Return the diagnostic context set by 89ef860a24SChandler Carruth /// setDiagnosticHandler. 90ef860a24SChandler Carruth void *LLVMContext::getDiagnosticContext() const { 91ef860a24SChandler Carruth return pImpl->DiagContext; 92ef860a24SChandler Carruth } 93ef860a24SChandler Carruth 94ef860a24SChandler Carruth void LLVMContext::emitError(const Twine &ErrorStr) { 95ef860a24SChandler Carruth emitError(0U, ErrorStr); 96ef860a24SChandler Carruth } 97ef860a24SChandler Carruth 98*bfb44ef9SBob Wilson void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { 99ef860a24SChandler Carruth unsigned LocCookie = 0; 100ef860a24SChandler Carruth if (const MDNode *SrcLoc = I->getMetadata("srcloc")) { 101ef860a24SChandler Carruth if (SrcLoc->getNumOperands() != 0) 102ef860a24SChandler Carruth if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0))) 103ef860a24SChandler Carruth LocCookie = CI->getZExtValue(); 104ef860a24SChandler Carruth } 105ef860a24SChandler Carruth return emitError(LocCookie, ErrorStr); 106ef860a24SChandler Carruth } 107ef860a24SChandler Carruth 108ef860a24SChandler Carruth void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { 109ef860a24SChandler Carruth // If there is no error handler installed, just print the error and exit. 110ef860a24SChandler Carruth if (pImpl->DiagHandler == 0) { 111ef860a24SChandler Carruth errs() << "error: " << ErrorStr << "\n"; 112ef860a24SChandler Carruth exit(1); 113ef860a24SChandler Carruth } 114ef860a24SChandler Carruth 115ef860a24SChandler Carruth // If we do have an error handler, we can report the error and keep going. 116ef860a24SChandler Carruth SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str()); 117ef860a24SChandler Carruth 118ef860a24SChandler Carruth pImpl->DiagHandler(Diag, pImpl->DiagContext, LocCookie); 119ef860a24SChandler Carruth } 120ef860a24SChandler Carruth 121ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 122ef860a24SChandler Carruth // Metadata Kind Uniquing 123ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 124ef860a24SChandler Carruth 125ef860a24SChandler Carruth #ifndef NDEBUG 126ef860a24SChandler Carruth /// isValidName - Return true if Name is a valid custom metadata handler name. 127ef860a24SChandler Carruth static bool isValidName(StringRef MDName) { 128ef860a24SChandler Carruth if (MDName.empty()) 129ef860a24SChandler Carruth return false; 130ef860a24SChandler Carruth 131ef860a24SChandler Carruth if (!std::isalpha(MDName[0])) 132ef860a24SChandler Carruth return false; 133ef860a24SChandler Carruth 134ef860a24SChandler Carruth for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E; 135ef860a24SChandler Carruth ++I) { 136ef860a24SChandler Carruth if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.') 137ef860a24SChandler Carruth return false; 138ef860a24SChandler Carruth } 139ef860a24SChandler Carruth return true; 140ef860a24SChandler Carruth } 141ef860a24SChandler Carruth #endif 142ef860a24SChandler Carruth 143ef860a24SChandler Carruth /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. 144ef860a24SChandler Carruth unsigned LLVMContext::getMDKindID(StringRef Name) const { 145ef860a24SChandler Carruth assert(isValidName(Name) && "Invalid MDNode name"); 146ef860a24SChandler Carruth 147ef860a24SChandler Carruth // If this is new, assign it its ID. 148ef860a24SChandler Carruth return 149ef860a24SChandler Carruth pImpl->CustomMDKindNames.GetOrCreateValue( 150ef860a24SChandler Carruth Name, pImpl->CustomMDKindNames.size()).second; 151ef860a24SChandler Carruth } 152ef860a24SChandler Carruth 153ef860a24SChandler Carruth /// getHandlerNames - Populate client supplied smallvector using custome 154ef860a24SChandler Carruth /// metadata name and ID. 155ef860a24SChandler Carruth void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { 156ef860a24SChandler Carruth Names.resize(pImpl->CustomMDKindNames.size()); 157ef860a24SChandler Carruth for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), 158ef860a24SChandler Carruth E = pImpl->CustomMDKindNames.end(); I != E; ++I) 159ef860a24SChandler Carruth Names[I->second] = I->first(); 160ef860a24SChandler Carruth } 161