LLVM API Documentation
00001 //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements LLVMContext, as a wrapper around the opaque 00011 // class LLVMContextImpl. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/IR/LLVMContext.h" 00016 #include "LLVMContextImpl.h" 00017 #include "llvm/IR/Constants.h" 00018 #include "llvm/IR/Instruction.h" 00019 #include "llvm/IR/Metadata.h" 00020 #include "llvm/Support/ManagedStatic.h" 00021 #include "llvm/Support/SourceMgr.h" 00022 #include <cctype> 00023 using namespace llvm; 00024 00025 static ManagedStatic<LLVMContext> GlobalContext; 00026 00027 LLVMContext& llvm::getGlobalContext() { 00028 return *GlobalContext; 00029 } 00030 00031 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { 00032 // Create the fixed metadata kinds. This is done in the same order as the 00033 // MD_* enum values so that they correspond. 00034 00035 // Create the 'dbg' metadata kind. 00036 unsigned DbgID = getMDKindID("dbg"); 00037 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID; 00038 00039 // Create the 'tbaa' metadata kind. 00040 unsigned TBAAID = getMDKindID("tbaa"); 00041 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID; 00042 00043 // Create the 'prof' metadata kind. 00044 unsigned ProfID = getMDKindID("prof"); 00045 assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID; 00046 00047 // Create the 'fpmath' metadata kind. 00048 unsigned FPAccuracyID = getMDKindID("fpmath"); 00049 assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted"); 00050 (void)FPAccuracyID; 00051 00052 // Create the 'range' metadata kind. 00053 unsigned RangeID = getMDKindID("range"); 00054 assert(RangeID == MD_range && "range kind id drifted"); 00055 (void)RangeID; 00056 00057 // Create the 'tbaa.struct' metadata kind. 00058 unsigned TBAAStructID = getMDKindID("tbaa.struct"); 00059 assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted"); 00060 (void)TBAAStructID; 00061 00062 // Create the 'invariant.load' metadata kind. 00063 unsigned InvariantLdId = getMDKindID("invariant.load"); 00064 assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted"); 00065 (void)InvariantLdId; 00066 } 00067 LLVMContext::~LLVMContext() { delete pImpl; } 00068 00069 void LLVMContext::addModule(Module *M) { 00070 pImpl->OwnedModules.insert(M); 00071 } 00072 00073 void LLVMContext::removeModule(Module *M) { 00074 pImpl->OwnedModules.erase(M); 00075 } 00076 00077 //===----------------------------------------------------------------------===// 00078 // Recoverable Backend Errors 00079 //===----------------------------------------------------------------------===// 00080 00081 void LLVMContext:: 00082 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, 00083 void *DiagContext) { 00084 pImpl->InlineAsmDiagHandler = DiagHandler; 00085 pImpl->InlineAsmDiagContext = DiagContext; 00086 } 00087 00088 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by 00089 /// setInlineAsmDiagnosticHandler. 00090 LLVMContext::InlineAsmDiagHandlerTy 00091 LLVMContext::getInlineAsmDiagnosticHandler() const { 00092 return pImpl->InlineAsmDiagHandler; 00093 } 00094 00095 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by 00096 /// setInlineAsmDiagnosticHandler. 00097 void *LLVMContext::getInlineAsmDiagnosticContext() const { 00098 return pImpl->InlineAsmDiagContext; 00099 } 00100 00101 void LLVMContext::emitError(const Twine &ErrorStr) { 00102 emitError(0U, ErrorStr); 00103 } 00104 00105 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { 00106 unsigned LocCookie = 0; 00107 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) { 00108 if (SrcLoc->getNumOperands() != 0) 00109 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0))) 00110 LocCookie = CI->getZExtValue(); 00111 } 00112 return emitError(LocCookie, ErrorStr); 00113 } 00114 00115 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { 00116 // If there is no error handler installed, just print the error and exit. 00117 if (pImpl->InlineAsmDiagHandler == 0) { 00118 errs() << "error: " << ErrorStr << "\n"; 00119 exit(1); 00120 } 00121 00122 // If we do have an error handler, we can report the error and keep going. 00123 SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str()); 00124 00125 pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie); 00126 } 00127 00128 //===----------------------------------------------------------------------===// 00129 // Metadata Kind Uniquing 00130 //===----------------------------------------------------------------------===// 00131 00132 #ifndef NDEBUG 00133 /// isValidName - Return true if Name is a valid custom metadata handler name. 00134 static bool isValidName(StringRef MDName) { 00135 if (MDName.empty()) 00136 return false; 00137 00138 if (!std::isalpha(static_cast<unsigned char>(MDName[0]))) 00139 return false; 00140 00141 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E; 00142 ++I) { 00143 if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' && 00144 *I != '-' && *I != '.') 00145 return false; 00146 } 00147 return true; 00148 } 00149 #endif 00150 00151 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. 00152 unsigned LLVMContext::getMDKindID(StringRef Name) const { 00153 assert(isValidName(Name) && "Invalid MDNode name"); 00154 00155 // If this is new, assign it its ID. 00156 return 00157 pImpl->CustomMDKindNames.GetOrCreateValue( 00158 Name, pImpl->CustomMDKindNames.size()).second; 00159 } 00160 00161 /// getHandlerNames - Populate client supplied smallvector using custome 00162 /// metadata name and ID. 00163 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { 00164 Names.resize(pImpl->CustomMDKindNames.size()); 00165 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), 00166 E = pImpl->CustomMDKindNames.end(); I != E; ++I) 00167 Names[I->second] = I->first(); 00168 }