Freeze reserved registers before starting register allocation.
[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 'fpaccuracy' metadata kind.
48   unsigned FPAccuracyID = getMDKindID("fpaccuracy");
49   assert(FPAccuracyID == MD_fpaccuracy && "fpaccuracy kind id drifted");
50   (void)FPAccuracyID;
51 }
52 LLVMContext::~LLVMContext() { delete pImpl; }
53
54 void LLVMContext::addModule(Module *M) {
55   pImpl->OwnedModules.insert(M);
56 }
57
58 void LLVMContext::removeModule(Module *M) {
59   pImpl->OwnedModules.erase(M);
60 }
61
62 //===----------------------------------------------------------------------===//
63 // Recoverable Backend Errors
64 //===----------------------------------------------------------------------===//
65
66 void LLVMContext::
67 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
68                               void *DiagContext) {
69   pImpl->InlineAsmDiagHandler = DiagHandler;
70   pImpl->InlineAsmDiagContext = DiagContext;
71 }
72
73 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
74 /// setInlineAsmDiagnosticHandler.
75 LLVMContext::InlineAsmDiagHandlerTy
76 LLVMContext::getInlineAsmDiagnosticHandler() const {
77   return pImpl->InlineAsmDiagHandler;
78 }
79
80 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
81 /// setInlineAsmDiagnosticHandler.
82 void *LLVMContext::getInlineAsmDiagnosticContext() const {
83   return pImpl->InlineAsmDiagContext;
84 }
85
86 void LLVMContext::emitError(const Twine &ErrorStr) {
87   emitError(0U, ErrorStr);
88 }
89
90 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
91   unsigned LocCookie = 0;
92   if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
93     if (SrcLoc->getNumOperands() != 0)
94       if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
95         LocCookie = CI->getZExtValue();
96   }
97   return emitError(LocCookie, ErrorStr);
98 }
99
100 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
101   // If there is no error handler installed, just print the error and exit.
102   if (pImpl->InlineAsmDiagHandler == 0) {
103     errs() << "error: " << ErrorStr << "\n";
104     exit(1);
105   }
106
107   // If we do have an error handler, we can report the error and keep going.
108   SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
109
110   pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
111 }
112
113 //===----------------------------------------------------------------------===//
114 // Metadata Kind Uniquing
115 //===----------------------------------------------------------------------===//
116
117 #ifndef NDEBUG
118 /// isValidName - Return true if Name is a valid custom metadata handler name.
119 static bool isValidName(StringRef MDName) {
120   if (MDName.empty())
121     return false;
122
123   if (!std::isalpha(MDName[0]))
124     return false;
125
126   for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
127        ++I) {
128     if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
129       return false;
130   }
131   return true;
132 }
133 #endif
134
135 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
136 unsigned LLVMContext::getMDKindID(StringRef Name) const {
137   assert(isValidName(Name) && "Invalid MDNode name");
138
139   // If this is new, assign it its ID.
140   return
141     pImpl->CustomMDKindNames.GetOrCreateValue(
142       Name, pImpl->CustomMDKindNames.size()).second;
143 }
144
145 /// getHandlerNames - Populate client supplied smallvector using custome
146 /// metadata name and ID.
147 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
148   Names.resize(pImpl->CustomMDKindNames.size());
149   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
150        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
151     Names[I->second] = I->first();
152 }