1 //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements LLVMContext, as a wrapper around the opaque
11 // class LLVMContextImpl.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/LLVMContext.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Instruction.h"
19 #include "llvm/IR/Metadata.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/SourceMgr.h"
25 static ManagedStatic<LLVMContext> GlobalContext;
27 LLVMContext& llvm::getGlobalContext() {
28 return *GlobalContext;
31 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
32 // Create the fixed metadata kinds. This is done in the same order as the
33 // MD_* enum values so that they correspond.
35 // Create the 'dbg' metadata kind.
36 unsigned DbgID = getMDKindID("dbg");
37 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
39 // Create the 'tbaa' metadata kind.
40 unsigned TBAAID = getMDKindID("tbaa");
41 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
43 // Create the 'prof' metadata kind.
44 unsigned ProfID = getMDKindID("prof");
45 assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
47 // Create the 'fpmath' metadata kind.
48 unsigned FPAccuracyID = getMDKindID("fpmath");
49 assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
52 // Create the 'range' metadata kind.
53 unsigned RangeID = getMDKindID("range");
54 assert(RangeID == MD_range && "range kind id drifted");
57 // Create the 'tbaa.struct' metadata kind.
58 unsigned TBAAStructID = getMDKindID("tbaa.struct");
59 assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
62 // Create the 'invariant.load' metadata kind.
63 unsigned InvariantLdId = getMDKindID("invariant.load");
64 assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
67 LLVMContext::~LLVMContext() { delete pImpl; }
69 void LLVMContext::addModule(Module *M) {
70 pImpl->OwnedModules.insert(M);
73 void LLVMContext::removeModule(Module *M) {
74 pImpl->OwnedModules.erase(M);
77 //===----------------------------------------------------------------------===//
78 // Recoverable Backend Errors
79 //===----------------------------------------------------------------------===//
82 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
84 pImpl->InlineAsmDiagHandler = DiagHandler;
85 pImpl->InlineAsmDiagContext = DiagContext;
88 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
89 /// setInlineAsmDiagnosticHandler.
90 LLVMContext::InlineAsmDiagHandlerTy
91 LLVMContext::getInlineAsmDiagnosticHandler() const {
92 return pImpl->InlineAsmDiagHandler;
95 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
96 /// setInlineAsmDiagnosticHandler.
97 void *LLVMContext::getInlineAsmDiagnosticContext() const {
98 return pImpl->InlineAsmDiagContext;
101 void LLVMContext::emitError(const Twine &ErrorStr) {
102 emitError(0U, ErrorStr);
105 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
106 unsigned LocCookie = 0;
107 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
108 if (SrcLoc->getNumOperands() != 0)
109 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
110 LocCookie = CI->getZExtValue();
112 return emitError(LocCookie, ErrorStr);
115 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
116 // If there is no error handler installed, just print the error and exit.
117 if (pImpl->InlineAsmDiagHandler == 0) {
118 errs() << "error: " << ErrorStr << "\n";
122 // If we do have an error handler, we can report the error and keep going.
123 SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
125 pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
128 //===----------------------------------------------------------------------===//
129 // Metadata Kind Uniquing
130 //===----------------------------------------------------------------------===//
133 /// isValidName - Return true if Name is a valid custom metadata handler name.
134 static bool isValidName(StringRef MDName) {
138 if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
141 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
143 if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
144 *I != '-' && *I != '.')
151 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
152 unsigned LLVMContext::getMDKindID(StringRef Name) const {
153 assert(isValidName(Name) && "Invalid MDNode name");
155 // If this is new, assign it its ID.
157 pImpl->CustomMDKindNames.GetOrCreateValue(
158 Name, pImpl->CustomMDKindNames.size()).second;
161 /// getHandlerNames - Populate client supplied smallvector using custome
162 /// metadata name and ID.
163 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
164 Names.resize(pImpl->CustomMDKindNames.size());
165 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
166 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
167 Names[I->second] = I->first();