1 //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements LLVMContext, as a wrapper around the opaque
11 // class LLVMContextImpl.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
22 #include <cctype>
23 using namespace llvm;
24
25 static ManagedStatic<LLVMContext> GlobalContext;
26
getGlobalContext()27 LLVMContext& llvm::getGlobalContext() {
28 return *GlobalContext;
29 }
30
LLVMContext()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.
34
35 // Create the 'dbg' metadata kind.
36 unsigned DbgID = getMDKindID("dbg");
37 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
38
39 // Create the 'tbaa' metadata kind.
40 unsigned TBAAID = getMDKindID("tbaa");
41 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
42
43 // Create the 'prof' metadata kind.
44 unsigned ProfID = getMDKindID("prof");
45 assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
46
47 // Create the 'fpmath' metadata kind.
48 unsigned FPAccuracyID = getMDKindID("fpmath");
49 assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
50 (void)FPAccuracyID;
51
52 // Create the 'range' metadata kind.
53 unsigned RangeID = getMDKindID("range");
54 assert(RangeID == MD_range && "range kind id drifted");
55 (void)RangeID;
56
57 // Create the 'tbaa.struct' metadata kind.
58 unsigned TBAAStructID = getMDKindID("tbaa.struct");
59 assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
60 (void)TBAAStructID;
61
62 // Create the 'invariant.load' metadata kind.
63 unsigned InvariantLdId = getMDKindID("invariant.load");
64 assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
65 (void)InvariantLdId;
66 }
~LLVMContext()67 LLVMContext::~LLVMContext() { delete pImpl; }
68
addModule(Module * M)69 void LLVMContext::addModule(Module *M) {
70 pImpl->OwnedModules.insert(M);
71 }
72
removeModule(Module * M)73 void LLVMContext::removeModule(Module *M) {
74 pImpl->OwnedModules.erase(M);
75 }
76
77 //===----------------------------------------------------------------------===//
78 // Recoverable Backend Errors
79 //===----------------------------------------------------------------------===//
80
81 void LLVMContext::
setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,void * DiagContext)82 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
83 void *DiagContext) {
84 pImpl->InlineAsmDiagHandler = DiagHandler;
85 pImpl->InlineAsmDiagContext = DiagContext;
86 }
87
88 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
89 /// setInlineAsmDiagnosticHandler.
90 LLVMContext::InlineAsmDiagHandlerTy
getInlineAsmDiagnosticHandler() const91 LLVMContext::getInlineAsmDiagnosticHandler() const {
92 return pImpl->InlineAsmDiagHandler;
93 }
94
95 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
96 /// setInlineAsmDiagnosticHandler.
getInlineAsmDiagnosticContext() const97 void *LLVMContext::getInlineAsmDiagnosticContext() const {
98 return pImpl->InlineAsmDiagContext;
99 }
100
emitError(const Twine & ErrorStr)101 void LLVMContext::emitError(const Twine &ErrorStr) {
102 emitError(0U, ErrorStr);
103 }
104
emitError(const Instruction * I,const Twine & 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();
111 }
112 return emitError(LocCookie, ErrorStr);
113 }
114
emitError(unsigned LocCookie,const Twine & 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";
119 exit(1);
120 }
121
122 // If we do have an error handler, we can report the error and keep going.
123 SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
124
125 pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
126 }
127
128 //===----------------------------------------------------------------------===//
129 // Metadata Kind Uniquing
130 //===----------------------------------------------------------------------===//
131
132 #ifndef NDEBUG
133 /// isValidName - Return true if Name is a valid custom metadata handler name.
isValidName(StringRef MDName)134 static bool isValidName(StringRef MDName) {
135 if (MDName.empty())
136 return false;
137
138 if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
139 return false;
140
141 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
142 ++I) {
143 if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
144 *I != '-' && *I != '.')
145 return false;
146 }
147 return true;
148 }
149 #endif
150
151 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
getMDKindID(StringRef Name) const152 unsigned LLVMContext::getMDKindID(StringRef Name) const {
153 assert(isValidName(Name) && "Invalid MDNode name");
154
155 // If this is new, assign it its ID.
156 return
157 pImpl->CustomMDKindNames.GetOrCreateValue(
158 Name, pImpl->CustomMDKindNames.size()).second;
159 }
160
161 /// getHandlerNames - Populate client supplied smallvector using custome
162 /// metadata name and ID.
getMDKindNames(SmallVectorImpl<StringRef> & Names) const163 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();
168 }
169