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"
18*b4c44d23SQuentin Colombet #include "llvm/IR/DiagnosticInfo.h"
19*b4c44d23SQuentin Colombet #include "llvm/IR/DiagnosticPrinter.h"
209fb823bbSChandler Carruth #include "llvm/IR/Instruction.h"
219fb823bbSChandler Carruth #include "llvm/IR/Metadata.h"
22ef860a24SChandler Carruth #include "llvm/Support/ManagedStatic.h"
23ef860a24SChandler Carruth #include "llvm/Support/SourceMgr.h"
24ef860a24SChandler Carruth #include <cctype>
25ef860a24SChandler Carruth using namespace llvm;
26ef860a24SChandler Carruth 
27ef860a24SChandler Carruth static ManagedStatic<LLVMContext> GlobalContext;
28ef860a24SChandler Carruth 
29ef860a24SChandler Carruth LLVMContext& llvm::getGlobalContext() {
30ef860a24SChandler Carruth   return *GlobalContext;
31ef860a24SChandler Carruth }
32ef860a24SChandler Carruth 
33ef860a24SChandler Carruth LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
34ef860a24SChandler Carruth   // Create the fixed metadata kinds. This is done in the same order as the
35ef860a24SChandler Carruth   // MD_* enum values so that they correspond.
36ef860a24SChandler Carruth 
37ef860a24SChandler Carruth   // Create the 'dbg' metadata kind.
38ef860a24SChandler Carruth   unsigned DbgID = getMDKindID("dbg");
39ef860a24SChandler Carruth   assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
40ef860a24SChandler Carruth 
41ef860a24SChandler Carruth   // Create the 'tbaa' metadata kind.
42ef860a24SChandler Carruth   unsigned TBAAID = getMDKindID("tbaa");
43ef860a24SChandler Carruth   assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
44ef860a24SChandler Carruth 
45ef860a24SChandler Carruth   // Create the 'prof' metadata kind.
46ef860a24SChandler Carruth   unsigned ProfID = getMDKindID("prof");
47ef860a24SChandler Carruth   assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
48ef860a24SChandler Carruth 
49ef860a24SChandler Carruth   // Create the 'fpmath' metadata kind.
50ef860a24SChandler Carruth   unsigned FPAccuracyID = getMDKindID("fpmath");
51ef860a24SChandler Carruth   assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
52ef860a24SChandler Carruth   (void)FPAccuracyID;
53ef860a24SChandler Carruth 
54ef860a24SChandler Carruth   // Create the 'range' metadata kind.
55ef860a24SChandler Carruth   unsigned RangeID = getMDKindID("range");
56ef860a24SChandler Carruth   assert(RangeID == MD_range && "range kind id drifted");
57ef860a24SChandler Carruth   (void)RangeID;
58ef860a24SChandler Carruth 
59ef860a24SChandler Carruth   // Create the 'tbaa.struct' metadata kind.
60ef860a24SChandler Carruth   unsigned TBAAStructID = getMDKindID("tbaa.struct");
61ef860a24SChandler Carruth   assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
62ef860a24SChandler Carruth   (void)TBAAStructID;
63408bdad5SShuxin Yang 
64408bdad5SShuxin Yang   // Create the 'invariant.load' metadata kind.
65408bdad5SShuxin Yang   unsigned InvariantLdId = getMDKindID("invariant.load");
66408bdad5SShuxin Yang   assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
67408bdad5SShuxin Yang   (void)InvariantLdId;
68ef860a24SChandler Carruth }
69ef860a24SChandler Carruth LLVMContext::~LLVMContext() { delete pImpl; }
70ef860a24SChandler Carruth 
71ef860a24SChandler Carruth void LLVMContext::addModule(Module *M) {
72ef860a24SChandler Carruth   pImpl->OwnedModules.insert(M);
73ef860a24SChandler Carruth }
74ef860a24SChandler Carruth 
75ef860a24SChandler Carruth void LLVMContext::removeModule(Module *M) {
76ef860a24SChandler Carruth   pImpl->OwnedModules.erase(M);
77ef860a24SChandler Carruth }
78ef860a24SChandler Carruth 
79ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
80ef860a24SChandler Carruth // Recoverable Backend Errors
81ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
82ef860a24SChandler Carruth 
83a594fab4SBob Wilson void LLVMContext::
84a594fab4SBob Wilson setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
85ef860a24SChandler Carruth                               void *DiagContext) {
86a594fab4SBob Wilson   pImpl->InlineAsmDiagHandler = DiagHandler;
87a594fab4SBob Wilson   pImpl->InlineAsmDiagContext = DiagContext;
88ef860a24SChandler Carruth }
89ef860a24SChandler Carruth 
90a594fab4SBob Wilson /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
91a594fab4SBob Wilson /// setInlineAsmDiagnosticHandler.
92a594fab4SBob Wilson LLVMContext::InlineAsmDiagHandlerTy
93a594fab4SBob Wilson LLVMContext::getInlineAsmDiagnosticHandler() const {
94a594fab4SBob Wilson   return pImpl->InlineAsmDiagHandler;
95ef860a24SChandler Carruth }
96ef860a24SChandler Carruth 
97a594fab4SBob Wilson /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
98a594fab4SBob Wilson /// setInlineAsmDiagnosticHandler.
99a594fab4SBob Wilson void *LLVMContext::getInlineAsmDiagnosticContext() const {
100a594fab4SBob Wilson   return pImpl->InlineAsmDiagContext;
101ef860a24SChandler Carruth }
102ef860a24SChandler Carruth 
103*b4c44d23SQuentin Colombet void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
104*b4c44d23SQuentin Colombet                                        void *DiagnosticContext) {
105*b4c44d23SQuentin Colombet   pImpl->DiagnosticHandler = DiagnosticHandler;
106*b4c44d23SQuentin Colombet   pImpl->DiagnosticContext = DiagnosticContext;
107*b4c44d23SQuentin Colombet }
108*b4c44d23SQuentin Colombet 
109*b4c44d23SQuentin Colombet LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
110*b4c44d23SQuentin Colombet   return pImpl->DiagnosticHandler;
111*b4c44d23SQuentin Colombet }
112*b4c44d23SQuentin Colombet 
113*b4c44d23SQuentin Colombet void *LLVMContext::getDiagnosticContext() const {
114*b4c44d23SQuentin Colombet   return pImpl->DiagnosticContext;
115*b4c44d23SQuentin Colombet }
116*b4c44d23SQuentin Colombet 
117ef860a24SChandler Carruth void LLVMContext::emitError(const Twine &ErrorStr) {
118ef860a24SChandler Carruth   emitError(0U, ErrorStr);
119ef860a24SChandler Carruth }
120ef860a24SChandler Carruth 
121bfb44ef9SBob Wilson void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
122ef860a24SChandler Carruth   unsigned LocCookie = 0;
123ef860a24SChandler Carruth   if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
124ef860a24SChandler Carruth     if (SrcLoc->getNumOperands() != 0)
125ef860a24SChandler Carruth       if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
126ef860a24SChandler Carruth         LocCookie = CI->getZExtValue();
127ef860a24SChandler Carruth   }
128ef860a24SChandler Carruth   return emitError(LocCookie, ErrorStr);
129ef860a24SChandler Carruth }
130ef860a24SChandler Carruth 
131*b4c44d23SQuentin Colombet void LLVMContext::diagnose(const DiagnosticInfo &DI) {
132*b4c44d23SQuentin Colombet   // If there is a report handler, use it.
133*b4c44d23SQuentin Colombet   if (pImpl->DiagnosticHandler != 0) {
134*b4c44d23SQuentin Colombet     pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
135*b4c44d23SQuentin Colombet     return;
136*b4c44d23SQuentin Colombet   }
137*b4c44d23SQuentin Colombet   // Otherwise, print the message with a prefix based on the severity.
138*b4c44d23SQuentin Colombet   std::string MsgStorage;
139*b4c44d23SQuentin Colombet   raw_string_ostream Stream(MsgStorage);
140*b4c44d23SQuentin Colombet   DiagnosticPrinterRawOStream DP(Stream);
141*b4c44d23SQuentin Colombet   DI.print(DP);
142*b4c44d23SQuentin Colombet   Stream.flush();
143*b4c44d23SQuentin Colombet   switch (DI.getSeverity()) {
144*b4c44d23SQuentin Colombet   case DS_Error:
145*b4c44d23SQuentin Colombet     errs() << "error: " << MsgStorage << "\n";
146*b4c44d23SQuentin Colombet     exit(1);
147*b4c44d23SQuentin Colombet   case DS_Warning:
148*b4c44d23SQuentin Colombet     errs() << "warning: " << MsgStorage << "\n";
149*b4c44d23SQuentin Colombet     break;
150*b4c44d23SQuentin Colombet   case DS_Note:
151*b4c44d23SQuentin Colombet     errs() << "note: " << MsgStorage << "\n";
152*b4c44d23SQuentin Colombet     break;
153*b4c44d23SQuentin Colombet   }
154*b4c44d23SQuentin Colombet }
155*b4c44d23SQuentin Colombet 
156ef860a24SChandler Carruth void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
157ef860a24SChandler Carruth   // If there is no error handler installed, just print the error and exit.
158a594fab4SBob Wilson   if (pImpl->InlineAsmDiagHandler == 0) {
159ef860a24SChandler Carruth     errs() << "error: " << ErrorStr << "\n";
160ef860a24SChandler Carruth     exit(1);
161ef860a24SChandler Carruth   }
162ef860a24SChandler Carruth 
163ef860a24SChandler Carruth   // If we do have an error handler, we can report the error and keep going.
164ef860a24SChandler Carruth   SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
165ef860a24SChandler Carruth 
166a594fab4SBob Wilson   pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
167ef860a24SChandler Carruth }
168ef860a24SChandler Carruth 
169ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
170ef860a24SChandler Carruth // Metadata Kind Uniquing
171ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
172ef860a24SChandler Carruth 
173ef860a24SChandler Carruth #ifndef NDEBUG
174ef860a24SChandler Carruth /// isValidName - Return true if Name is a valid custom metadata handler name.
175ef860a24SChandler Carruth static bool isValidName(StringRef MDName) {
176ef860a24SChandler Carruth   if (MDName.empty())
177ef860a24SChandler Carruth     return false;
178ef860a24SChandler Carruth 
17983c74e9fSGuy Benyei   if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
180ef860a24SChandler Carruth     return false;
181ef860a24SChandler Carruth 
182ef860a24SChandler Carruth   for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
183ef860a24SChandler Carruth        ++I) {
18483c74e9fSGuy Benyei     if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
18583c74e9fSGuy Benyei         *I != '-' && *I != '.')
186ef860a24SChandler Carruth       return false;
187ef860a24SChandler Carruth   }
188ef860a24SChandler Carruth   return true;
189ef860a24SChandler Carruth }
190ef860a24SChandler Carruth #endif
191ef860a24SChandler Carruth 
192ef860a24SChandler Carruth /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
193ef860a24SChandler Carruth unsigned LLVMContext::getMDKindID(StringRef Name) const {
194ef860a24SChandler Carruth   assert(isValidName(Name) && "Invalid MDNode name");
195ef860a24SChandler Carruth 
196ef860a24SChandler Carruth   // If this is new, assign it its ID.
197ef860a24SChandler Carruth   return
198ef860a24SChandler Carruth     pImpl->CustomMDKindNames.GetOrCreateValue(
199ef860a24SChandler Carruth       Name, pImpl->CustomMDKindNames.size()).second;
200ef860a24SChandler Carruth }
201ef860a24SChandler Carruth 
202ef860a24SChandler Carruth /// getHandlerNames - Populate client supplied smallvector using custome
203ef860a24SChandler Carruth /// metadata name and ID.
204ef860a24SChandler Carruth void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
205ef860a24SChandler Carruth   Names.resize(pImpl->CustomMDKindNames.size());
206ef860a24SChandler Carruth   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
207ef860a24SChandler Carruth        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
208ef860a24SChandler Carruth     Names[I->second] = I->first();
209ef860a24SChandler Carruth }
210