blob: b73cd03ddd61d57f7c45295152065c58248b496a [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 Wilsonf64c8892013-02-11 05:37:07 +000076void LLVMContext::
77setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
78 void *DiagContext) {
79 pImpl->InlineAsmDiagHandler = DiagHandler;
80 pImpl->InlineAsmDiagContext = DiagContext;
Chris Lattner42a4ee02010-04-06 00:44:45 +000081}
82
Bob Wilsonf64c8892013-02-11 05:37:07 +000083/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
84/// setInlineAsmDiagnosticHandler.
85LLVMContext::InlineAsmDiagHandlerTy
86LLVMContext::getInlineAsmDiagnosticHandler() const {
87 return pImpl->InlineAsmDiagHandler;
Chris Lattner42a4ee02010-04-06 00:44:45 +000088}
89
Bob Wilsonf64c8892013-02-11 05:37:07 +000090/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
91/// setInlineAsmDiagnosticHandler.
92void *LLVMContext::getInlineAsmDiagnosticContext() const {
93 return pImpl->InlineAsmDiagContext;
Chris Lattner42a4ee02010-04-06 00:44:45 +000094}
Chris Lattner04e3b1e2010-03-30 20:48:48 +000095
Chris Lattner3a4c60c2012-01-03 23:47:05 +000096void LLVMContext::emitError(const Twine &ErrorStr) {
Chris Lattner38686bd2010-04-07 23:40:44 +000097 emitError(0U, ErrorStr);
98}
99
Bob Wilson58446912013-02-08 21:48:29 +0000100void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
Chris Lattner38686bd2010-04-07 23:40:44 +0000101 unsigned LocCookie = 0;
102 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
103 if (SrcLoc->getNumOperands() != 0)
104 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
105 LocCookie = CI->getZExtValue();
106 }
107 return emitError(LocCookie, ErrorStr);
108}
109
Chris Lattner3a4c60c2012-01-03 23:47:05 +0000110void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
Chris Lattner38686bd2010-04-07 23:40:44 +0000111 // If there is no error handler installed, just print the error and exit.
Bob Wilsonf64c8892013-02-11 05:37:07 +0000112 if (pImpl->InlineAsmDiagHandler == 0) {
Chris Lattner38686bd2010-04-07 23:40:44 +0000113 errs() << "error: " << ErrorStr << "\n";
114 exit(1);
115 }
Bob Wilson26156452010-12-17 23:06:32 +0000116
Chris Lattner38686bd2010-04-07 23:40:44 +0000117 // If we do have an error handler, we can report the error and keep going.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000118 SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
Bob Wilson26156452010-12-17 23:06:32 +0000119
Bob Wilsonf64c8892013-02-11 05:37:07 +0000120 pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
Chris Lattner38686bd2010-04-07 23:40:44 +0000121}
122
123//===----------------------------------------------------------------------===//
124// Metadata Kind Uniquing
125//===----------------------------------------------------------------------===//
126
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000127#ifndef NDEBUG
128/// isValidName - Return true if Name is a valid custom metadata handler name.
129static bool isValidName(StringRef MDName) {
130 if (MDName.empty())
131 return false;
Bob Wilson26156452010-12-17 23:06:32 +0000132
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000133 if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000134 return false;
Bob Wilson26156452010-12-17 23:06:32 +0000135
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000136 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
137 ++I) {
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000138 if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
139 *I != '-' && *I != '.')
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000140 return false;
141 }
142 return true;
Owen Anderson48b2f3e2009-08-04 22:41:48 +0000143}
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000144#endif
145
146/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
147unsigned LLVMContext::getMDKindID(StringRef Name) const {
148 assert(isValidName(Name) && "Invalid MDNode name");
Dan Gohman19538d12010-07-20 21:42:28 +0000149
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000150 // If this is new, assign it its ID.
Dan Gohman19538d12010-07-20 21:42:28 +0000151 return
152 pImpl->CustomMDKindNames.GetOrCreateValue(
153 Name, pImpl->CustomMDKindNames.size()).second;
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000154}
155
156/// getHandlerNames - Populate client supplied smallvector using custome
157/// metadata name and ID.
158void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman19538d12010-07-20 21:42:28 +0000159 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000160 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
161 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000162 Names[I->second] = I->first();
163}