blob: 3ed2c2c7e9ea74e15bfb4aece248662c996211de [file] [log] [blame]
Owen Anderson8e66e0b2009-06-30 00:48:55 +00001//===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
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 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
15#include "llvm/LLVMContext.h"
Devang Patelf7188322009-09-03 01:39:20 +000016#include "llvm/Metadata.h"
Owen Anderson8e66e0b2009-06-30 00:48:55 +000017#include "llvm/Constants.h"
Owen Anderson53a52212009-07-13 04:09:18 +000018#include "llvm/Instruction.h"
Owen Anderson1938fb12009-06-30 23:39:59 +000019#include "llvm/Support/ManagedStatic.h"
Chris Lattner1e457892010-04-07 23:40:44 +000020#include "llvm/Support/SourceMgr.h"
Owen Anderson8e66e0b2009-06-30 00:48:55 +000021#include "LLVMContextImpl.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000022#include <cctype>
Owen Anderson8e66e0b2009-06-30 00:48:55 +000023using namespace llvm;
24
Owen Anderson1938fb12009-06-30 23:39:59 +000025static ManagedStatic<LLVMContext> GlobalContext;
26
Owen Anderson2a154432009-07-01 23:13:44 +000027LLVMContext& llvm::getGlobalContext() {
Owen Anderson1cf085d2009-07-01 21:22:36 +000028 return *GlobalContext;
Owen Anderson1938fb12009-06-30 23:39:59 +000029}
30
Chris Lattnerc263b422010-03-30 23:03:27 +000031LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
Dan Gohman41f14cf2010-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 Wilsonec3ff9c2010-12-17 23:06:32 +000035 // Create the 'dbg' metadata kind.
Chris Lattnerc263b422010-03-30 23:03:27 +000036 unsigned DbgID = getMDKindID("dbg");
37 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
Dan Gohman41f14cf2010-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 Staszak3f158fd2011-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;
Chris Lattnerc263b422010-03-30 23:03:27 +000046}
Owen Anderson8e66e0b2009-06-30 00:48:55 +000047LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +000048
Owen Anderson8e89e412010-09-08 18:03:32 +000049void LLVMContext::addModule(Module *M) {
50 pImpl->OwnedModules.insert(M);
51}
52
53void LLVMContext::removeModule(Module *M) {
54 pImpl->OwnedModules.erase(M);
55}
56
Chris Lattner1e457892010-04-07 23:40:44 +000057//===----------------------------------------------------------------------===//
58// Recoverable Backend Errors
59//===----------------------------------------------------------------------===//
60
Chris Lattnerb0e36082010-11-17 08:13:01 +000061void LLVMContext::
Bob Wilsonec3ff9c2010-12-17 23:06:32 +000062setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
Chris Lattnerb0e36082010-11-17 08:13:01 +000063 void *DiagContext) {
Chris Lattner60955d42010-04-06 00:44:45 +000064 pImpl->InlineAsmDiagHandler = DiagHandler;
65 pImpl->InlineAsmDiagContext = DiagContext;
66}
67
68/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
69/// setInlineAsmDiagnosticHandler.
Chris Lattnerb0e36082010-11-17 08:13:01 +000070LLVMContext::InlineAsmDiagHandlerTy
71LLVMContext::getInlineAsmDiagnosticHandler() const {
Chris Lattner60955d42010-04-06 00:44:45 +000072 return pImpl->InlineAsmDiagHandler;
73}
74
75/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
76/// setInlineAsmDiagnosticHandler.
77void *LLVMContext::getInlineAsmDiagnosticContext() const {
78 return pImpl->InlineAsmDiagContext;
79}
Chris Lattnera3b94ba2010-03-30 20:48:48 +000080
Chris Lattner1e457892010-04-07 23:40:44 +000081void LLVMContext::emitError(StringRef ErrorStr) {
82 emitError(0U, ErrorStr);
83}
84
85void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
86 unsigned LocCookie = 0;
87 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
88 if (SrcLoc->getNumOperands() != 0)
89 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
90 LocCookie = CI->getZExtValue();
91 }
92 return emitError(LocCookie, ErrorStr);
93}
94
95void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
96 // If there is no error handler installed, just print the error and exit.
97 if (pImpl->InlineAsmDiagHandler == 0) {
98 errs() << "error: " << ErrorStr << "\n";
99 exit(1);
100 }
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000101
Chris Lattner1e457892010-04-07 23:40:44 +0000102 // If we do have an error handler, we can report the error and keep going.
Chris Lattner03b80a42011-10-16 05:43:57 +0000103 SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000104
Chris Lattnerb0e36082010-11-17 08:13:01 +0000105 pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
Chris Lattner1e457892010-04-07 23:40:44 +0000106}
107
108//===----------------------------------------------------------------------===//
109// Metadata Kind Uniquing
110//===----------------------------------------------------------------------===//
111
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000112#ifndef NDEBUG
113/// isValidName - Return true if Name is a valid custom metadata handler name.
114static bool isValidName(StringRef MDName) {
115 if (MDName.empty())
116 return false;
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000117
Nick Lewyckyb71afe82010-12-19 20:42:43 +0000118 if (!std::isalpha(MDName[0]))
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000119 return false;
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000120
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000121 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
122 ++I) {
Nick Lewyckyb71afe82010-12-19 20:42:43 +0000123 if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000124 return false;
125 }
126 return true;
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000127}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000128#endif
129
130/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
131unsigned LLVMContext::getMDKindID(StringRef Name) const {
132 assert(isValidName(Name) && "Invalid MDNode name");
Dan Gohman43aa8f02010-07-20 21:42:28 +0000133
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000134 // If this is new, assign it its ID.
Dan Gohman43aa8f02010-07-20 21:42:28 +0000135 return
136 pImpl->CustomMDKindNames.GetOrCreateValue(
137 Name, pImpl->CustomMDKindNames.size()).second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000138}
139
140/// getHandlerNames - Populate client supplied smallvector using custome
141/// metadata name and ID.
142void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000143 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000144 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
145 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000146 Names[I->second] = I->first();
147}