blob: bae83dd301552b680ff1e05103652d4c0012a1dc [file] [log] [blame]
Nick Lewyckyfca2acb2012-12-26 22:00:49 +00001//===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +00002//
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 Anderson36f62e52009-06-30 17:06:46 +00009//
10// This file implements LLVMContext, as a wrapper around the opaque
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000011// class LLVMContextImpl.
Owen Anderson36f62e52009-06-30 17:06:46 +000012//
13//===----------------------------------------------------------------------===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +000014
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/LLVMContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "LLVMContextImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Constants.h"
Quentin Colombetb4c44d22013-12-17 17:47:22 +000018#include "llvm/IR/DiagnosticInfo.h"
19#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Instruction.h"
21#include "llvm/IR/Metadata.h"
Owen Anderson1938fb12009-06-30 23:39:59 +000022#include "llvm/Support/ManagedStatic.h"
Chris Lattner1e457892010-04-07 23:40:44 +000023#include "llvm/Support/SourceMgr.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000024#include <cctype>
Owen Anderson8e66e0b2009-06-30 00:48:55 +000025using namespace llvm;
26
Owen Anderson1938fb12009-06-30 23:39:59 +000027static ManagedStatic<LLVMContext> GlobalContext;
28
Owen Anderson2a154432009-07-01 23:13:44 +000029LLVMContext& llvm::getGlobalContext() {
Owen Anderson1cf085d2009-07-01 21:22:36 +000030 return *GlobalContext;
Owen Anderson1938fb12009-06-30 23:39:59 +000031}
32
Chris Lattnerc263b422010-03-30 23:03:27 +000033LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
Dan Gohman41f14cf2010-09-14 21:25:10 +000034 // Create the fixed metadata kinds. This is done in the same order as the
35 // MD_* enum values so that they correspond.
36
Bob Wilsonec3ff9c2010-12-17 23:06:32 +000037 // Create the 'dbg' metadata kind.
Chris Lattnerc263b422010-03-30 23:03:27 +000038 unsigned DbgID = getMDKindID("dbg");
39 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
Dan Gohman41f14cf2010-09-14 21:25:10 +000040
41 // Create the 'tbaa' metadata kind.
42 unsigned TBAAID = getMDKindID("tbaa");
43 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
Jakub Staszak3f158fd2011-07-06 18:22:43 +000044
45 // Create the 'prof' metadata kind.
46 unsigned ProfID = getMDKindID("prof");
47 assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
Peter Collingbournef7d1e7b2011-10-27 19:19:14 +000048
Duncan Sands34bd91a2012-04-14 12:36:06 +000049 // Create the 'fpmath' metadata kind.
50 unsigned FPAccuracyID = getMDKindID("fpmath");
51 assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
Peter Collingbournef7d1e7b2011-10-27 19:19:14 +000052 (void)FPAccuracyID;
Rafael Espindolaef9f5502012-03-24 00:14:51 +000053
54 // Create the 'range' metadata kind.
55 unsigned RangeID = getMDKindID("range");
56 assert(RangeID == MD_range && "range kind id drifted");
57 (void)RangeID;
Dan Gohman3effe812012-09-13 17:56:17 +000058
59 // Create the 'tbaa.struct' metadata kind.
60 unsigned TBAAStructID = getMDKindID("tbaa.struct");
61 assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
62 (void)TBAAStructID;
Shuxin Yang408bdad2013-03-06 17:48:48 +000063
64 // Create the 'invariant.load' metadata kind.
65 unsigned InvariantLdId = getMDKindID("invariant.load");
66 assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
67 (void)InvariantLdId;
Chris Lattnerc263b422010-03-30 23:03:27 +000068}
Owen Anderson8e66e0b2009-06-30 00:48:55 +000069LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +000070
Owen Anderson8e89e412010-09-08 18:03:32 +000071void LLVMContext::addModule(Module *M) {
72 pImpl->OwnedModules.insert(M);
73}
74
75void LLVMContext::removeModule(Module *M) {
76 pImpl->OwnedModules.erase(M);
77}
78
Chris Lattner1e457892010-04-07 23:40:44 +000079//===----------------------------------------------------------------------===//
80// Recoverable Backend Errors
81//===----------------------------------------------------------------------===//
82
Bob Wilsona594fab2013-02-11 05:37:07 +000083void LLVMContext::
84setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
85 void *DiagContext) {
86 pImpl->InlineAsmDiagHandler = DiagHandler;
87 pImpl->InlineAsmDiagContext = DiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +000088}
89
Bob Wilsona594fab2013-02-11 05:37:07 +000090/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
91/// setInlineAsmDiagnosticHandler.
92LLVMContext::InlineAsmDiagHandlerTy
93LLVMContext::getInlineAsmDiagnosticHandler() const {
94 return pImpl->InlineAsmDiagHandler;
Chris Lattner60955d42010-04-06 00:44:45 +000095}
96
Bob Wilsona594fab2013-02-11 05:37:07 +000097/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
98/// setInlineAsmDiagnosticHandler.
99void *LLVMContext::getInlineAsmDiagnosticContext() const {
100 return pImpl->InlineAsmDiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000101}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000102
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000103void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
104 void *DiagnosticContext) {
105 pImpl->DiagnosticHandler = DiagnosticHandler;
106 pImpl->DiagnosticContext = DiagnosticContext;
107}
108
109LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
110 return pImpl->DiagnosticHandler;
111}
112
113void *LLVMContext::getDiagnosticContext() const {
114 return pImpl->DiagnosticContext;
115}
116
Chris Lattnere22e6132012-01-03 23:47:05 +0000117void LLVMContext::emitError(const Twine &ErrorStr) {
Chris Lattner1e457892010-04-07 23:40:44 +0000118 emitError(0U, ErrorStr);
119}
120
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000121void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
Chris Lattner1e457892010-04-07 23:40:44 +0000122 unsigned LocCookie = 0;
123 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
124 if (SrcLoc->getNumOperands() != 0)
125 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
126 LocCookie = CI->getZExtValue();
127 }
128 return emitError(LocCookie, ErrorStr);
129}
130
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000131void LLVMContext::diagnose(const DiagnosticInfo &DI) {
132 // If there is a report handler, use it.
133 if (pImpl->DiagnosticHandler != 0) {
134 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
135 return;
136 }
137 // Otherwise, print the message with a prefix based on the severity.
138 std::string MsgStorage;
139 raw_string_ostream Stream(MsgStorage);
140 DiagnosticPrinterRawOStream DP(Stream);
141 DI.print(DP);
142 Stream.flush();
143 switch (DI.getSeverity()) {
144 case DS_Error:
145 errs() << "error: " << MsgStorage << "\n";
146 exit(1);
147 case DS_Warning:
148 errs() << "warning: " << MsgStorage << "\n";
149 break;
150 case DS_Note:
151 errs() << "note: " << MsgStorage << "\n";
152 break;
153 }
154}
155
Chris Lattnere22e6132012-01-03 23:47:05 +0000156void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
Chris Lattner1e457892010-04-07 23:40:44 +0000157 // If there is no error handler installed, just print the error and exit.
Bob Wilsona594fab2013-02-11 05:37:07 +0000158 if (pImpl->InlineAsmDiagHandler == 0) {
Chris Lattner1e457892010-04-07 23:40:44 +0000159 errs() << "error: " << ErrorStr << "\n";
160 exit(1);
161 }
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000162
Chris Lattner1e457892010-04-07 23:40:44 +0000163 // If we do have an error handler, we can report the error and keep going.
Chris Lattner03b80a42011-10-16 05:43:57 +0000164 SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000165
Bob Wilsona594fab2013-02-11 05:37:07 +0000166 pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
Chris Lattner1e457892010-04-07 23:40:44 +0000167}
168
169//===----------------------------------------------------------------------===//
170// Metadata Kind Uniquing
171//===----------------------------------------------------------------------===//
172
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000173#ifndef NDEBUG
174/// isValidName - Return true if Name is a valid custom metadata handler name.
175static bool isValidName(StringRef MDName) {
176 if (MDName.empty())
177 return false;
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000178
Guy Benyei83c74e92013-02-12 21:21:59 +0000179 if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000180 return false;
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000181
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000182 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
183 ++I) {
Guy Benyei83c74e92013-02-12 21:21:59 +0000184 if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
185 *I != '-' && *I != '.')
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000186 return false;
187 }
188 return true;
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000189}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000190#endif
191
192/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
193unsigned LLVMContext::getMDKindID(StringRef Name) const {
194 assert(isValidName(Name) && "Invalid MDNode name");
Dan Gohman43aa8f02010-07-20 21:42:28 +0000195
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000196 // If this is new, assign it its ID.
Dan Gohman43aa8f02010-07-20 21:42:28 +0000197 return
198 pImpl->CustomMDKindNames.GetOrCreateValue(
199 Name, pImpl->CustomMDKindNames.size()).second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000200}
201
202/// getHandlerNames - Populate client supplied smallvector using custome
203/// metadata name and ID.
204void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000205 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000206 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
207 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000208 Names[I->second] = I->first();
209}