Nick Lewycky | dbf5081 | 2012-12-26 22:00:49 +0000 | [diff] [blame] | 1 | //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===// |
Owen Anderson | 2bc29dc | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 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 | 5217007 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements LLVMContext, as a wrapper around the opaque |
Benjamin Kramer | 12ddd40 | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 11 | // class LLVMContextImpl. |
Owen Anderson | 5217007 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Owen Anderson | 2bc29dc | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 14 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/Instruction.h" |
| 19 | #include "llvm/IR/Metadata.h" |
Owen Anderson | db47ed0 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 38686bd | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 21 | #include "llvm/Support/SourceMgr.h" |
Nick Lewycky | 476b242 | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 22 | #include <cctype> |
Owen Anderson | 2bc29dc | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Owen Anderson | db47ed0 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 25 | static ManagedStatic<LLVMContext> GlobalContext; |
| 26 | |
Owen Anderson | 4434ed4 | 2009-07-01 23:13:44 +0000 | [diff] [blame] | 27 | LLVMContext& llvm::getGlobalContext() { |
Owen Anderson | 31895e7 | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 28 | return *GlobalContext; |
Owen Anderson | db47ed0 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 31 | LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { |
Dan Gohman | b2143b6 | 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 | 2615645 | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 35 | // Create the 'dbg' metadata kind. |
Chris Lattner | ec39f09 | 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 | b2143b6 | 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 | 9da9934 | 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; |
Peter Collingbourne | 999f90b | 2011-10-27 19:19:14 +0000 | [diff] [blame] | 46 | |
Duncan Sands | 5e5c5f8 | 2012-04-14 12:36:06 +0000 | [diff] [blame] | 47 | // Create the 'fpmath' metadata kind. |
| 48 | unsigned FPAccuracyID = getMDKindID("fpmath"); |
| 49 | assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted"); |
Peter Collingbourne | 999f90b | 2011-10-27 19:19:14 +0000 | [diff] [blame] | 50 | (void)FPAccuracyID; |
Rafael Espindola | 39dd328 | 2012-03-24 00:14:51 +0000 | [diff] [blame] | 51 | |
| 52 | // Create the 'range' metadata kind. |
| 53 | unsigned RangeID = getMDKindID("range"); |
| 54 | assert(RangeID == MD_range && "range kind id drifted"); |
| 55 | (void)RangeID; |
Dan Gohman | b54834b | 2012-09-13 17:56:17 +0000 | [diff] [blame] | 56 | |
| 57 | // Create the 'tbaa.struct' metadata kind. |
| 58 | unsigned TBAAStructID = getMDKindID("tbaa.struct"); |
| 59 | assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted"); |
| 60 | (void)TBAAStructID; |
Shuxin Yang | 985dac6 | 2013-03-06 17:48:48 +0000 | [diff] [blame] | 61 | |
| 62 | // Create the 'invariant.load' metadata kind. |
| 63 | unsigned InvariantLdId = getMDKindID("invariant.load"); |
| 64 | assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted"); |
| 65 | (void)InvariantLdId; |
Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 66 | } |
Owen Anderson | 2bc29dc | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 67 | LLVMContext::~LLVMContext() { delete pImpl; } |
Owen Anderson | 48b2f3e | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 68 | |
Owen Anderson | 30268be | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 69 | void LLVMContext::addModule(Module *M) { |
| 70 | pImpl->OwnedModules.insert(M); |
| 71 | } |
| 72 | |
| 73 | void LLVMContext::removeModule(Module *M) { |
| 74 | pImpl->OwnedModules.erase(M); |
| 75 | } |
| 76 | |
Chris Lattner | 38686bd | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 77 | //===----------------------------------------------------------------------===// |
| 78 | // Recoverable Backend Errors |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | |
Bob Wilson | f64c889 | 2013-02-11 05:37:07 +0000 | [diff] [blame] | 81 | void LLVMContext:: |
| 82 | setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, |
| 83 | void *DiagContext) { |
| 84 | pImpl->InlineAsmDiagHandler = DiagHandler; |
| 85 | pImpl->InlineAsmDiagContext = DiagContext; |
Chris Lattner | 42a4ee0 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Bob Wilson | f64c889 | 2013-02-11 05:37:07 +0000 | [diff] [blame] | 88 | /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by |
| 89 | /// setInlineAsmDiagnosticHandler. |
| 90 | LLVMContext::InlineAsmDiagHandlerTy |
| 91 | LLVMContext::getInlineAsmDiagnosticHandler() const { |
| 92 | return pImpl->InlineAsmDiagHandler; |
Chris Lattner | 42a4ee0 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Bob Wilson | f64c889 | 2013-02-11 05:37:07 +0000 | [diff] [blame] | 95 | /// getInlineAsmDiagnosticContext - Return the diagnostic context set by |
| 96 | /// setInlineAsmDiagnosticHandler. |
| 97 | void *LLVMContext::getInlineAsmDiagnosticContext() const { |
| 98 | return pImpl->InlineAsmDiagContext; |
Chris Lattner | 42a4ee0 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 99 | } |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 3a4c60c | 2012-01-03 23:47:05 +0000 | [diff] [blame] | 101 | void LLVMContext::emitError(const Twine &ErrorStr) { |
Chris Lattner | 38686bd | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 102 | emitError(0U, ErrorStr); |
| 103 | } |
| 104 | |
Bob Wilson | 5844691 | 2013-02-08 21:48:29 +0000 | [diff] [blame] | 105 | void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { |
Chris Lattner | 38686bd | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 106 | unsigned LocCookie = 0; |
| 107 | if (const MDNode *SrcLoc = I->getMetadata("srcloc")) { |
| 108 | if (SrcLoc->getNumOperands() != 0) |
| 109 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0))) |
| 110 | LocCookie = CI->getZExtValue(); |
| 111 | } |
| 112 | return emitError(LocCookie, ErrorStr); |
| 113 | } |
| 114 | |
Chris Lattner | 3a4c60c | 2012-01-03 23:47:05 +0000 | [diff] [blame] | 115 | void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { |
Chris Lattner | 38686bd | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 116 | // If there is no error handler installed, just print the error and exit. |
Bob Wilson | f64c889 | 2013-02-11 05:37:07 +0000 | [diff] [blame] | 117 | if (pImpl->InlineAsmDiagHandler == 0) { |
Chris Lattner | 38686bd | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 118 | errs() << "error: " << ErrorStr << "\n"; |
| 119 | exit(1); |
| 120 | } |
Bob Wilson | 2615645 | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 38686bd | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 122 | // If we do have an error handler, we can report the error and keep going. |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 123 | SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str()); |
Bob Wilson | 2615645 | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 124 | |
Bob Wilson | f64c889 | 2013-02-11 05:37:07 +0000 | [diff] [blame] | 125 | pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie); |
Chris Lattner | 38686bd | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | // Metadata Kind Uniquing |
| 130 | //===----------------------------------------------------------------------===// |
| 131 | |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 132 | #ifndef NDEBUG |
| 133 | /// isValidName - Return true if Name is a valid custom metadata handler name. |
| 134 | static bool isValidName(StringRef MDName) { |
| 135 | if (MDName.empty()) |
| 136 | return false; |
Bob Wilson | 2615645 | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 137 | |
Guy Benyei | 87d0b9e | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 138 | if (!std::isalpha(static_cast<unsigned char>(MDName[0]))) |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 139 | return false; |
Bob Wilson | 2615645 | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 141 | for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E; |
| 142 | ++I) { |
Guy Benyei | 87d0b9e | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 143 | if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' && |
| 144 | *I != '-' && *I != '.') |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 145 | return false; |
| 146 | } |
| 147 | return true; |
Owen Anderson | 48b2f3e | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 148 | } |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 149 | #endif |
| 150 | |
| 151 | /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. |
| 152 | unsigned LLVMContext::getMDKindID(StringRef Name) const { |
| 153 | assert(isValidName(Name) && "Invalid MDNode name"); |
Dan Gohman | 19538d1 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 155 | // If this is new, assign it its ID. |
Dan Gohman | 19538d1 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 156 | return |
| 157 | pImpl->CustomMDKindNames.GetOrCreateValue( |
| 158 | Name, pImpl->CustomMDKindNames.size()).second; |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /// getHandlerNames - Populate client supplied smallvector using custome |
| 162 | /// metadata name and ID. |
| 163 | void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { |
Dan Gohman | 19538d1 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 164 | Names.resize(pImpl->CustomMDKindNames.size()); |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 165 | for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), |
| 166 | E = pImpl->CustomMDKindNames.end(); I != E; ++I) |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 167 | Names[I->second] = I->first(); |
| 168 | } |