zpu: wip - add pass to convert registers to stack slots
[llvm/zpu.git] / lib / VMCore / LLVMContext.cpp
blob15ae0ec5d5ac4fe900aa3c1c1d7004a1e048942f
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.
13 //===----------------------------------------------------------------------===//
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 using namespace llvm;
24 static ManagedStatic<LLVMContext> GlobalContext;
26 LLVMContext& llvm::getGlobalContext() {
27 return *GlobalContext;
30 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
31 // Create the fixed metadata kinds. This is done in the same order as the
32 // MD_* enum values so that they correspond.
34 // Create the 'dbg' metadata kind.
35 unsigned DbgID = getMDKindID("dbg");
36 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
38 // Create the 'tbaa' metadata kind.
39 unsigned TBAAID = getMDKindID("tbaa");
40 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
42 LLVMContext::~LLVMContext() { delete pImpl; }
44 void LLVMContext::addModule(Module *M) {
45 pImpl->OwnedModules.insert(M);
48 void LLVMContext::removeModule(Module *M) {
49 pImpl->OwnedModules.erase(M);
52 //===----------------------------------------------------------------------===//
53 // Recoverable Backend Errors
54 //===----------------------------------------------------------------------===//
56 void LLVMContext::setInlineAsmDiagnosticHandler(void *DiagHandler,
57 void *DiagContext) {
58 pImpl->InlineAsmDiagHandler = DiagHandler;
59 pImpl->InlineAsmDiagContext = DiagContext;
62 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
63 /// setInlineAsmDiagnosticHandler.
64 void *LLVMContext::getInlineAsmDiagnosticHandler() const {
65 return pImpl->InlineAsmDiagHandler;
68 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
69 /// setInlineAsmDiagnosticHandler.
70 void *LLVMContext::getInlineAsmDiagnosticContext() const {
71 return pImpl->InlineAsmDiagContext;
74 void LLVMContext::emitError(StringRef ErrorStr) {
75 emitError(0U, ErrorStr);
78 void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
79 unsigned LocCookie = 0;
80 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
81 if (SrcLoc->getNumOperands() != 0)
82 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
83 LocCookie = CI->getZExtValue();
85 return emitError(LocCookie, ErrorStr);
88 void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
89 // If there is no error handler installed, just print the error and exit.
90 if (pImpl->InlineAsmDiagHandler == 0) {
91 errs() << "error: " << ErrorStr << "\n";
92 exit(1);
95 // If we do have an error handler, we can report the error and keep going.
96 SMDiagnostic Diag("", "error: " + ErrorStr.str());
98 ((SourceMgr::DiagHandlerTy)(intptr_t)pImpl->InlineAsmDiagHandler)
99 (Diag, pImpl->InlineAsmDiagContext, LocCookie);
103 //===----------------------------------------------------------------------===//
104 // Metadata Kind Uniquing
105 //===----------------------------------------------------------------------===//
107 #ifndef NDEBUG
108 /// isValidName - Return true if Name is a valid custom metadata handler name.
109 static bool isValidName(StringRef MDName) {
110 if (MDName.empty())
111 return false;
113 if (!isalpha(MDName[0]))
114 return false;
116 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
117 ++I) {
118 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
119 return false;
121 return true;
123 #endif
125 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
126 unsigned LLVMContext::getMDKindID(StringRef Name) const {
127 assert(isValidName(Name) && "Invalid MDNode name");
129 // If this is new, assign it its ID.
130 return
131 pImpl->CustomMDKindNames.GetOrCreateValue(
132 Name, pImpl->CustomMDKindNames.size()).second;
135 /// getHandlerNames - Populate client supplied smallvector using custome
136 /// metadata name and ID.
137 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
138 Names.resize(pImpl->CustomMDKindNames.size());
139 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
140 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
141 Names[I->second] = I->first();