Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Owen Anderson | 36f62e5 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements LLVMContext, as a wrapper around the opaque |
Benjamin Kramer | 78c3bcb | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 11 | // class LLVMContextImpl. |
Owen Anderson | 36f62e5 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 14 | |
| 15 | #include "llvm/LLVMContext.h" |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 16 | #include "llvm/Metadata.h" |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 18 | #include "llvm/Instruction.h" |
Owen Anderson | 1938fb1 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 20 | #include "llvm/Support/SourceMgr.h" |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 21 | #include "LLVMContextImpl.h" |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Owen Anderson | 1938fb1 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 24 | static ManagedStatic<LLVMContext> GlobalContext; |
| 25 | |
Owen Anderson | 2a15443 | 2009-07-01 23:13:44 +0000 | [diff] [blame] | 26 | LLVMContext& llvm::getGlobalContext() { |
Owen Anderson | 1cf085d | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 27 | return *GlobalContext; |
Owen Anderson | 1938fb1 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 30 | LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 31 | // Create the fixed metadata kinds. This is done in the same order as the |
| 32 | // MD_* enum values so that they correspond. |
| 33 | |
| 34 | // Create the 'dbg' metadata kind. |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 35 | unsigned DbgID = getMDKindID("dbg"); |
| 36 | assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID; |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 37 | |
| 38 | // Create the 'tbaa' metadata kind. |
| 39 | unsigned TBAAID = getMDKindID("tbaa"); |
| 40 | assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 41 | } |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 42 | LLVMContext::~LLVMContext() { delete pImpl; } |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 43 | |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 44 | void LLVMContext::addModule(Module *M) { |
| 45 | pImpl->OwnedModules.insert(M); |
| 46 | } |
| 47 | |
| 48 | void LLVMContext::removeModule(Module *M) { |
| 49 | pImpl->OwnedModules.erase(M); |
| 50 | } |
| 51 | |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
| 53 | // Recoverable Backend Errors |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
Chris Lattner | 60955d4 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 56 | void LLVMContext::setInlineAsmDiagnosticHandler(void *DiagHandler, |
| 57 | void *DiagContext) { |
| 58 | pImpl->InlineAsmDiagHandler = DiagHandler; |
| 59 | pImpl->InlineAsmDiagContext = DiagContext; |
| 60 | } |
| 61 | |
| 62 | /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by |
| 63 | /// setInlineAsmDiagnosticHandler. |
| 64 | void *LLVMContext::getInlineAsmDiagnosticHandler() const { |
| 65 | return pImpl->InlineAsmDiagHandler; |
| 66 | } |
| 67 | |
| 68 | /// getInlineAsmDiagnosticContext - Return the diagnostic context set by |
| 69 | /// setInlineAsmDiagnosticHandler. |
| 70 | void *LLVMContext::getInlineAsmDiagnosticContext() const { |
| 71 | return pImpl->InlineAsmDiagContext; |
| 72 | } |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 74 | void LLVMContext::emitError(StringRef ErrorStr) { |
| 75 | emitError(0U, ErrorStr); |
| 76 | } |
| 77 | |
| 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(); |
| 84 | } |
| 85 | return emitError(LocCookie, ErrorStr); |
| 86 | } |
| 87 | |
| 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); |
| 93 | } |
| 94 | |
| 95 | // If we do have an error handler, we can report the error and keep going. |
| 96 | SMDiagnostic Diag("", "error: " + ErrorStr.str()); |
| 97 | |
| 98 | ((SourceMgr::DiagHandlerTy)(intptr_t)pImpl->InlineAsmDiagHandler) |
| 99 | (Diag, pImpl->InlineAsmDiagContext, LocCookie); |
| 100 | |
| 101 | } |
| 102 | |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | // Metadata Kind Uniquing |
| 105 | //===----------------------------------------------------------------------===// |
| 106 | |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 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; |
| 112 | |
| 113 | if (!isalpha(MDName[0])) |
| 114 | return false; |
| 115 | |
| 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; |
| 120 | } |
| 121 | return true; |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 123 | #endif |
| 124 | |
| 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"); |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 128 | |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 129 | // If this is new, assign it its ID. |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 130 | return |
| 131 | pImpl->CustomMDKindNames.GetOrCreateValue( |
| 132 | Name, pImpl->CustomMDKindNames.size()).second; |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /// getHandlerNames - Populate client supplied smallvector using custome |
| 136 | /// metadata name and ID. |
| 137 | void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 138 | Names.resize(pImpl->CustomMDKindNames.size()); |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 139 | for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), |
| 140 | E = pImpl->CustomMDKindNames.end(); I != E; ++I) |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 141 | Names[I->second] = I->first(); |
| 142 | } |