blob: 883bb9878fa5be8f9c744e292735ff32eb17daff [file] [log] [blame]
Nick Lewyckydbf50812012-12-26 22:00:49 +00001//===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
Owen Anderson2bc29dc2009-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 Anderson52170072009-06-30 17:06:46 +00009//
10// This file implements LLVMContext, as a wrapper around the opaque
Benjamin Kramer12ddd402009-08-11 17:45:13 +000011// class LLVMContextImpl.
Owen Anderson52170072009-06-30 17:06:46 +000012//
13//===----------------------------------------------------------------------===//
Owen Anderson2bc29dc2009-06-30 00:48:55 +000014
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000015#include "llvm/IR/LLVMContext.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "LLVMContextImpl.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/Constants.h"
18#include "llvm/IR/Instruction.h"
19#include "llvm/IR/Metadata.h"
Owen Andersondb47ed02009-06-30 23:39:59 +000020#include "llvm/Support/ManagedStatic.h"
Chris Lattner38686bd2010-04-07 23:40:44 +000021#include "llvm/Support/SourceMgr.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000022#include <cctype>
Owen Anderson2bc29dc2009-06-30 00:48:55 +000023using namespace llvm;
24
Owen Andersondb47ed02009-06-30 23:39:59 +000025static ManagedStatic<LLVMContext> GlobalContext;
26
Owen Anderson4434ed42009-07-01 23:13:44 +000027LLVMContext& llvm::getGlobalContext() {
Owen Anderson31895e72009-07-01 21:22:36 +000028 return *GlobalContext;
Owen Andersondb47ed02009-06-30 23:39:59 +000029}
30
Chris Lattnerec39f092010-03-30 23:03:27 +000031LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
Dan Gohmanb2143b62010-09-14 21:25:10 +000032 // Create the fixed metadata kinds. This is done in the same order as the
33 // MD_* enum values so that they correspond.
34
Bob Wilson26156452010-12-17 23:06:32 +000035 // Create the 'dbg' metadata kind.
Chris Lattnerec39f092010-03-30 23:03:27 +000036 unsigned DbgID = getMDKindID("dbg");
37 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
Dan Gohmanb2143b62010-09-14 21:25:10 +000038
39 // Create the 'tbaa' metadata kind.
40 unsigned TBAAID = getMDKindID("tbaa");
41 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
Jakub Staszak9da99342011-07-06 18:22:43 +000042
43 // Create the 'prof' metadata kind.
44 unsigned ProfID = getMDKindID("prof");
45 assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
Peter Collingbourne999f90b2011-10-27 19:19:14 +000046
Duncan Sands5e5c5f82012-04-14 12:36:06 +000047 // Create the 'fpmath' metadata kind.
48 unsigned FPAccuracyID = getMDKindID("fpmath");
49 assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
Peter Collingbourne999f90b2011-10-27 19:19:14 +000050 (void)FPAccuracyID;
Rafael Espindola39dd3282012-03-24 00:14:51 +000051
52 // Create the 'range' metadata kind.
53 unsigned RangeID = getMDKindID("range");
54 assert(RangeID == MD_range && "range kind id drifted");
55 (void)RangeID;
Dan Gohmanb54834b2012-09-13 17:56:17 +000056
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 Yang985dac62013-03-06 17:48:48 +000061
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 Lattnerec39f092010-03-30 23:03:27 +000066}
Owen Anderson2bc29dc2009-06-30 00:48:55 +000067LLVMContext::~LLVMContext() { delete pImpl; }
Owen Anderson48b2f3e2009-08-04 22:41:48 +000068
Owen Anderson30268be2010-09-08 18:03:32 +000069void LLVMContext::addModule(Module *M) {
70 pImpl->OwnedModules.insert(M);
71}
72
73void LLVMContext::removeModule(Module *M) {
74 pImpl->OwnedModules.erase(M);
75}
76
Chris Lattner38686bd2010-04-07 23:40:44 +000077//===----------------------------------------------------------------------===//
78// Recoverable Backend Errors
79//===----------------------------------------------------------------------===//
80
Bob Wilsonf64c8892013-02-11 05:37:07 +000081void LLVMContext::
82setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
83 void *DiagContext) {
84 pImpl->InlineAsmDiagHandler = DiagHandler;
85 pImpl->InlineAsmDiagContext = DiagContext;
Chris Lattner42a4ee02010-04-06 00:44:45 +000086}
87
Bob Wilsonf64c8892013-02-11 05:37:07 +000088/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
89/// setInlineAsmDiagnosticHandler.
90LLVMContext::InlineAsmDiagHandlerTy
91LLVMContext::getInlineAsmDiagnosticHandler() const {
92 return pImpl->InlineAsmDiagHandler;
Chris Lattner42a4ee02010-04-06 00:44:45 +000093}
94
Bob Wilsonf64c8892013-02-11 05:37:07 +000095/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
96/// setInlineAsmDiagnosticHandler.
97void *LLVMContext::getInlineAsmDiagnosticContext() const {
98 return pImpl->InlineAsmDiagContext;
Chris Lattner42a4ee02010-04-06 00:44:45 +000099}
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000100
Chris Lattner3a4c60c2012-01-03 23:47:05 +0000101void LLVMContext::emitError(const Twine &ErrorStr) {
Chris Lattner38686bd2010-04-07 23:40:44 +0000102 emitError(0U, ErrorStr);
103}
104
Bob Wilson58446912013-02-08 21:48:29 +0000105void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
Chris Lattner38686bd2010-04-07 23:40:44 +0000106 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 Lattner3a4c60c2012-01-03 23:47:05 +0000115void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
Chris Lattner38686bd2010-04-07 23:40:44 +0000116 // If there is no error handler installed, just print the error and exit.
Bob Wilsonf64c8892013-02-11 05:37:07 +0000117 if (pImpl->InlineAsmDiagHandler == 0) {
Chris Lattner38686bd2010-04-07 23:40:44 +0000118 errs() << "error: " << ErrorStr << "\n";
119 exit(1);
120 }
Bob Wilson26156452010-12-17 23:06:32 +0000121
Chris Lattner38686bd2010-04-07 23:40:44 +0000122 // If we do have an error handler, we can report the error and keep going.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000123 SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
Bob Wilson26156452010-12-17 23:06:32 +0000124
Bob Wilsonf64c8892013-02-11 05:37:07 +0000125 pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
Chris Lattner38686bd2010-04-07 23:40:44 +0000126}
127
128//===----------------------------------------------------------------------===//
129// Metadata Kind Uniquing
130//===----------------------------------------------------------------------===//
131
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000132#ifndef NDEBUG
133/// isValidName - Return true if Name is a valid custom metadata handler name.
134static bool isValidName(StringRef MDName) {
135 if (MDName.empty())
136 return false;
Bob Wilson26156452010-12-17 23:06:32 +0000137
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000138 if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000139 return false;
Bob Wilson26156452010-12-17 23:06:32 +0000140
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000141 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
142 ++I) {
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000143 if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
144 *I != '-' && *I != '.')
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000145 return false;
146 }
147 return true;
Owen Anderson48b2f3e2009-08-04 22:41:48 +0000148}
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000149#endif
150
151/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
152unsigned LLVMContext::getMDKindID(StringRef Name) const {
153 assert(isValidName(Name) && "Invalid MDNode name");
Dan Gohman19538d12010-07-20 21:42:28 +0000154
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000155 // If this is new, assign it its ID.
Dan Gohman19538d12010-07-20 21:42:28 +0000156 return
157 pImpl->CustomMDKindNames.GetOrCreateValue(
158 Name, pImpl->CustomMDKindNames.size()).second;
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000159}
160
161/// getHandlerNames - Populate client supplied smallvector using custome
162/// metadata name and ID.
163void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman19538d12010-07-20 21:42:28 +0000164 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000165 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
166 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000167 Names[I->second] = I->first();
168}