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/DebugLoc.h"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/IR/Metadata.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/SourceMgr.h"
28 static ManagedStatic<LLVMContext> GlobalContext;
30 LLVMContext& llvm::getGlobalContext() {
31 return *GlobalContext;
34 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
35 // Create the fixed metadata kinds. This is done in the same order as the
36 // MD_* enum values so that they correspond.
38 // Create the 'dbg' metadata kind.
39 unsigned DbgID = getMDKindID("dbg");
40 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
42 // Create the 'tbaa' metadata kind.
43 unsigned TBAAID = getMDKindID("tbaa");
44 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
46 // Create the 'prof' metadata kind.
47 unsigned ProfID = getMDKindID("prof");
48 assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
50 // Create the 'fpmath' metadata kind.
51 unsigned FPAccuracyID = getMDKindID("fpmath");
52 assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
55 // Create the 'range' metadata kind.
56 unsigned RangeID = getMDKindID("range");
57 assert(RangeID == MD_range && "range kind id drifted");
60 // Create the 'tbaa.struct' metadata kind.
61 unsigned TBAAStructID = getMDKindID("tbaa.struct");
62 assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
65 // Create the 'invariant.load' metadata kind.
66 unsigned InvariantLdId = getMDKindID("invariant.load");
67 assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
70 LLVMContext::~LLVMContext() { delete pImpl; }
72 void LLVMContext::addModule(Module *M) {
73 pImpl->OwnedModules.insert(M);
76 void LLVMContext::removeModule(Module *M) {
77 pImpl->OwnedModules.erase(M);
80 //===----------------------------------------------------------------------===//
81 // Recoverable Backend Errors
82 //===----------------------------------------------------------------------===//
85 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
87 pImpl->InlineAsmDiagHandler = DiagHandler;
88 pImpl->InlineAsmDiagContext = DiagContext;
91 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
92 /// setInlineAsmDiagnosticHandler.
93 LLVMContext::InlineAsmDiagHandlerTy
94 LLVMContext::getInlineAsmDiagnosticHandler() const {
95 return pImpl->InlineAsmDiagHandler;
98 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
99 /// setInlineAsmDiagnosticHandler.
100 void *LLVMContext::getInlineAsmDiagnosticContext() const {
101 return pImpl->InlineAsmDiagContext;
104 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
105 void *DiagnosticContext) {
106 pImpl->DiagnosticHandler = DiagnosticHandler;
107 pImpl->DiagnosticContext = DiagnosticContext;
110 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
111 return pImpl->DiagnosticHandler;
114 void *LLVMContext::getDiagnosticContext() const {
115 return pImpl->DiagnosticContext;
118 void LLVMContext::emitError(const Twine &ErrorStr) {
119 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
122 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
123 assert (I && "Invalid instruction");
124 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
127 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
128 // If there is a report handler, use it.
129 if (pImpl->DiagnosticHandler) {
130 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
133 // Otherwise, print the message with a prefix based on the severity.
134 std::string MsgStorage;
135 raw_string_ostream Stream(MsgStorage);
136 DiagnosticPrinterRawOStream DP(Stream);
139 switch (DI.getSeverity()) {
141 errs() << "error: " << MsgStorage << "\n";
144 errs() << "warning: " << MsgStorage << "\n";
147 errs() << "remark: " << MsgStorage << "\n";
150 errs() << "note: " << MsgStorage << "\n";
155 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
156 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
159 void LLVMContext::emitOptimizationRemark(const char *PassName,
161 const DebugLoc &DLoc,
163 if (pImpl->optimizationRemarksEnabledFor(PassName))
164 diagnose(DiagnosticInfoOptimizationRemark(PassName, Fn, DLoc, Msg));
167 //===----------------------------------------------------------------------===//
168 // Metadata Kind Uniquing
169 //===----------------------------------------------------------------------===//
172 /// isValidName - Return true if Name is a valid custom metadata handler name.
173 static bool isValidName(StringRef MDName) {
177 if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
180 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
182 if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
183 *I != '-' && *I != '.')
190 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
191 unsigned LLVMContext::getMDKindID(StringRef Name) const {
192 assert(isValidName(Name) && "Invalid MDNode name");
194 // If this is new, assign it its ID.
196 pImpl->CustomMDKindNames.GetOrCreateValue(
197 Name, pImpl->CustomMDKindNames.size()).second;
200 /// getHandlerNames - Populate client supplied smallvector using custome
201 /// metadata name and ID.
202 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
203 Names.resize(pImpl->CustomMDKindNames.size());
204 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
205 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
206 Names[I->second] = I->first();