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" |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 22 | #include <cctype> |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Owen Anderson | 1938fb1 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 25 | static ManagedStatic<LLVMContext> GlobalContext; |
| 26 | |
Owen Anderson | 2a15443 | 2009-07-01 23:13:44 +0000 | [diff] [blame] | 27 | LLVMContext& llvm::getGlobalContext() { |
Owen Anderson | 1cf085d | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 28 | return *GlobalContext; |
Owen Anderson | 1938fb1 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 31 | LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 32 | // Create the fixed metadata kinds. This is done in the same order as the |
| 33 | // MD_* enum values so that they correspond. |
| 34 | |
Bob Wilson | ec3ff9c | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 35 | // Create the 'dbg' metadata kind. |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 36 | unsigned DbgID = getMDKindID("dbg"); |
| 37 | assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID; |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 38 | |
| 39 | // Create the 'tbaa' metadata kind. |
| 40 | unsigned TBAAID = getMDKindID("tbaa"); |
| 41 | assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 42 | |
| 43 | // Create the 'prof' metadata kind. |
| 44 | unsigned ProfID = getMDKindID("prof"); |
| 45 | assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 46 | } |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 47 | LLVMContext::~LLVMContext() { delete pImpl; } |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 48 | |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 49 | void LLVMContext::addModule(Module *M) { |
| 50 | pImpl->OwnedModules.insert(M); |
| 51 | } |
| 52 | |
| 53 | void LLVMContext::removeModule(Module *M) { |
| 54 | pImpl->OwnedModules.erase(M); |
| 55 | } |
| 56 | |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 57 | //===----------------------------------------------------------------------===// |
| 58 | // Recoverable Backend Errors |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
Chris Lattner | b0e3608 | 2010-11-17 08:13:01 +0000 | [diff] [blame] | 61 | void LLVMContext:: |
Bob Wilson | ec3ff9c | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 62 | setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, |
Chris Lattner | b0e3608 | 2010-11-17 08:13:01 +0000 | [diff] [blame] | 63 | void *DiagContext) { |
Chris Lattner | 60955d4 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 64 | pImpl->InlineAsmDiagHandler = DiagHandler; |
| 65 | pImpl->InlineAsmDiagContext = DiagContext; |
| 66 | } |
| 67 | |
| 68 | /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by |
| 69 | /// setInlineAsmDiagnosticHandler. |
Chris Lattner | b0e3608 | 2010-11-17 08:13:01 +0000 | [diff] [blame] | 70 | LLVMContext::InlineAsmDiagHandlerTy |
| 71 | LLVMContext::getInlineAsmDiagnosticHandler() const { |
Chris Lattner | 60955d4 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 72 | return pImpl->InlineAsmDiagHandler; |
| 73 | } |
| 74 | |
| 75 | /// getInlineAsmDiagnosticContext - Return the diagnostic context set by |
| 76 | /// setInlineAsmDiagnosticHandler. |
| 77 | void *LLVMContext::getInlineAsmDiagnosticContext() const { |
| 78 | return pImpl->InlineAsmDiagContext; |
| 79 | } |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 81 | void LLVMContext::emitError(StringRef ErrorStr) { |
| 82 | emitError(0U, ErrorStr); |
| 83 | } |
| 84 | |
| 85 | void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) { |
| 86 | unsigned LocCookie = 0; |
| 87 | if (const MDNode *SrcLoc = I->getMetadata("srcloc")) { |
| 88 | if (SrcLoc->getNumOperands() != 0) |
| 89 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0))) |
| 90 | LocCookie = CI->getZExtValue(); |
| 91 | } |
| 92 | return emitError(LocCookie, ErrorStr); |
| 93 | } |
| 94 | |
| 95 | void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) { |
| 96 | // If there is no error handler installed, just print the error and exit. |
| 97 | if (pImpl->InlineAsmDiagHandler == 0) { |
| 98 | errs() << "error: " << ErrorStr << "\n"; |
| 99 | exit(1); |
| 100 | } |
Bob Wilson | ec3ff9c | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 102 | // If we do have an error handler, we can report the error and keep going. |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame^] | 103 | SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str()); |
Bob Wilson | ec3ff9c | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 104 | |
Chris Lattner | b0e3608 | 2010-11-17 08:13:01 +0000 | [diff] [blame] | 105 | pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie); |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | // Metadata Kind Uniquing |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 112 | #ifndef NDEBUG |
| 113 | /// isValidName - Return true if Name is a valid custom metadata handler name. |
| 114 | static bool isValidName(StringRef MDName) { |
| 115 | if (MDName.empty()) |
| 116 | return false; |
Bob Wilson | ec3ff9c | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 117 | |
Nick Lewycky | b71afe8 | 2010-12-19 20:42:43 +0000 | [diff] [blame] | 118 | if (!std::isalpha(MDName[0])) |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 119 | return false; |
Bob Wilson | ec3ff9c | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 120 | |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 121 | for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E; |
| 122 | ++I) { |
Nick Lewycky | b71afe8 | 2010-12-19 20:42:43 +0000 | [diff] [blame] | 123 | if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.') |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | return true; |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 127 | } |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 128 | #endif |
| 129 | |
| 130 | /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. |
| 131 | unsigned LLVMContext::getMDKindID(StringRef Name) const { |
| 132 | assert(isValidName(Name) && "Invalid MDNode name"); |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 133 | |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 134 | // If this is new, assign it its ID. |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 135 | return |
| 136 | pImpl->CustomMDKindNames.GetOrCreateValue( |
| 137 | Name, pImpl->CustomMDKindNames.size()).second; |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /// getHandlerNames - Populate client supplied smallvector using custome |
| 141 | /// metadata name and ID. |
| 142 | void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 143 | Names.resize(pImpl->CustomMDKindNames.size()); |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 144 | for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), |
| 145 | E = pImpl->CustomMDKindNames.end(); I != E; ++I) |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 146 | Names[I->second] = I->first(); |
| 147 | } |