[ms-inline asm] Enumerate the InlineAsm dialects and rename the nsdialect to
[oota-llvm.git] / lib / VMCore / LLVMContext.cpp
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/LLVMContext.h"
16 #include "llvm/Metadata.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Instruction.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "LLVMContextImpl.h"
22 #include <cctype>
23 using namespace llvm;
24
25 static ManagedStatic<LLVMContext> GlobalContext;
26
27 LLVMContext& llvm::getGlobalContext() {
28   return *GlobalContext;
29 }
30
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 LLVMContext::~LLVMContext() { delete pImpl; }
58
59 void LLVMContext::addModule(Module *M) {
60   pImpl->OwnedModules.insert(M);
61 }
62
63 void LLVMContext::removeModule(Module *M) {
64   pImpl->OwnedModules.erase(M);
65 }
66
67 //===----------------------------------------------------------------------===//
68 // Recoverable Backend Errors
69 //===----------------------------------------------------------------------===//
70
71 void LLVMContext::
72 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
73                               void *DiagContext) {
74   pImpl->InlineAsmDiagHandler = DiagHandler;
75   pImpl->InlineAsmDiagContext = DiagContext;
76 }
77
78 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
79 /// setInlineAsmDiagnosticHandler.
80 LLVMContext::InlineAsmDiagHandlerTy
81 LLVMContext::getInlineAsmDiagnosticHandler() const {
82   return pImpl->InlineAsmDiagHandler;
83 }
84
85 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
86 /// setInlineAsmDiagnosticHandler.
87 void *LLVMContext::getInlineAsmDiagnosticContext() const {
88   return pImpl->InlineAsmDiagContext;
89 }
90
91 void LLVMContext::emitError(const Twine &ErrorStr) {
92   emitError(0U, ErrorStr);
93 }
94
95 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
96   unsigned LocCookie = 0;
97   if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
98     if (SrcLoc->getNumOperands() != 0)
99       if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
100         LocCookie = CI->getZExtValue();
101   }
102   return emitError(LocCookie, ErrorStr);
103 }
104
105 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
106   // If there is no error handler installed, just print the error and exit.
107   if (pImpl->InlineAsmDiagHandler == 0) {
108     errs() << "error: " << ErrorStr << "\n";
109     exit(1);
110   }
111
112   // If we do have an error handler, we can report the error and keep going.
113   SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
114
115   pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
116 }
117
118 //===----------------------------------------------------------------------===//
119 // Metadata Kind Uniquing
120 //===----------------------------------------------------------------------===//
121
122 #ifndef NDEBUG
123 /// isValidName - Return true if Name is a valid custom metadata handler name.
124 static bool isValidName(StringRef MDName) {
125   if (MDName.empty())
126     return false;
127
128   if (!std::isalpha(MDName[0]))
129     return false;
130
131   for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
132        ++I) {
133     if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
134       return false;
135   }
136   return true;
137 }
138 #endif
139
140 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
141 unsigned LLVMContext::getMDKindID(StringRef Name) const {
142   assert(isValidName(Name) && "Invalid MDNode name");
143
144   // If this is new, assign it its ID.
145   return
146     pImpl->CustomMDKindNames.GetOrCreateValue(
147       Name, pImpl->CustomMDKindNames.size()).second;
148 }
149
150 /// getHandlerNames - Populate client supplied smallvector using custome
151 /// metadata name and ID.
152 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
153   Names.resize(pImpl->CustomMDKindNames.size());
154   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
155        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
156     Names[I->second] = I->first();
157 }