blob: 774c591a3c0605500ae673cf536b5ddc0e4d1381 [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;
Chris Lattnerec39f092010-03-30 23:03:27 +000061}
Owen Anderson2bc29dc2009-06-30 00:48:55 +000062LLVMContext::~LLVMContext() { delete pImpl; }
Owen Anderson48b2f3e2009-08-04 22:41:48 +000063
Owen Anderson30268be2010-09-08 18:03:32 +000064void LLVMContext::addModule(Module *M) {
65 pImpl->OwnedModules.insert(M);
66}
67
68void LLVMContext::removeModule(Module *M) {
69 pImpl->OwnedModules.erase(M);
70}
71
Chris Lattner38686bd2010-04-07 23:40:44 +000072//===----------------------------------------------------------------------===//
73// Recoverable Backend Errors
74//===----------------------------------------------------------------------===//
75
Bob Wilson04de3152012-12-25 00:07:12 +000076void LLVMContext::setDiagnosticHandler(DiagHandlerTy DiagHandler,
77 void *DiagContext) {
78 pImpl->DiagHandler = DiagHandler;
79 pImpl->DiagContext = DiagContext;
Chris Lattner42a4ee02010-04-06 00:44:45 +000080}
81
Bob Wilson04de3152012-12-25 00:07:12 +000082/// getDiagnosticHandler - Return the diagnostic handler set by
83/// setDiagnosticHandler.
84LLVMContext::DiagHandlerTy LLVMContext::getDiagnosticHandler() const {
85 return pImpl->DiagHandler;
Chris Lattner42a4ee02010-04-06 00:44:45 +000086}
87
Bob Wilson04de3152012-12-25 00:07:12 +000088/// getDiagnosticContext - Return the diagnostic context set by
89/// setDiagnosticHandler.
90void *LLVMContext::getDiagnosticContext() const {
91 return pImpl->DiagContext;
Chris Lattner42a4ee02010-04-06 00:44:45 +000092}
Chris Lattner04e3b1e2010-03-30 20:48:48 +000093
Chris Lattner3a4c60c2012-01-03 23:47:05 +000094void LLVMContext::emitError(const Twine &ErrorStr) {
Chris Lattner38686bd2010-04-07 23:40:44 +000095 emitError(0U, ErrorStr);
96}
97
Bob Wilson58446912013-02-08 21:48:29 +000098void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
Chris Lattner38686bd2010-04-07 23:40:44 +000099 unsigned LocCookie = 0;
100 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
101 if (SrcLoc->getNumOperands() != 0)
102 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
103 LocCookie = CI->getZExtValue();
104 }
105 return emitError(LocCookie, ErrorStr);
106}
107
Chris Lattner3a4c60c2012-01-03 23:47:05 +0000108void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
Chris Lattner38686bd2010-04-07 23:40:44 +0000109 // If there is no error handler installed, just print the error and exit.
Bob Wilson04de3152012-12-25 00:07:12 +0000110 if (pImpl->DiagHandler == 0) {
Chris Lattner38686bd2010-04-07 23:40:44 +0000111 errs() << "error: " << ErrorStr << "\n";
112 exit(1);
113 }
Bob Wilson26156452010-12-17 23:06:32 +0000114
Chris Lattner38686bd2010-04-07 23:40:44 +0000115 // If we do have an error handler, we can report the error and keep going.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000116 SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
Bob Wilson26156452010-12-17 23:06:32 +0000117
Bob Wilson04de3152012-12-25 00:07:12 +0000118 pImpl->DiagHandler(Diag, pImpl->DiagContext, LocCookie);
Chris Lattner38686bd2010-04-07 23:40:44 +0000119}
120
121//===----------------------------------------------------------------------===//
122// Metadata Kind Uniquing
123//===----------------------------------------------------------------------===//
124
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000125#ifndef NDEBUG
126/// isValidName - Return true if Name is a valid custom metadata handler name.
127static bool isValidName(StringRef MDName) {
128 if (MDName.empty())
129 return false;
Bob Wilson26156452010-12-17 23:06:32 +0000130
Nick Lewycky24021232010-12-19 20:42:43 +0000131 if (!std::isalpha(MDName[0]))
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000132 return false;
Bob Wilson26156452010-12-17 23:06:32 +0000133
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000134 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
135 ++I) {
Nick Lewycky24021232010-12-19 20:42:43 +0000136 if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000137 return false;
138 }
139 return true;
Owen Anderson48b2f3e2009-08-04 22:41:48 +0000140}
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000141#endif
142
143/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
144unsigned LLVMContext::getMDKindID(StringRef Name) const {
145 assert(isValidName(Name) && "Invalid MDNode name");
Dan Gohman19538d12010-07-20 21:42:28 +0000146
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000147 // If this is new, assign it its ID.
Dan Gohman19538d12010-07-20 21:42:28 +0000148 return
149 pImpl->CustomMDKindNames.GetOrCreateValue(
150 Name, pImpl->CustomMDKindNames.size()).second;
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000151}
152
153/// getHandlerNames - Populate client supplied smallvector using custome
154/// metadata name and ID.
155void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman19538d12010-07-20 21:42:28 +0000156 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000157 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
158 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000159 Names[I->second] = I->first();
160}