blob: d77e996b5e454da57f7b510ff068f783dd81c5cd [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;
Peter Collingbournef7d1e7b2011-10-27 19:19:14 +000046
47 // Create the 'fpaccuracy' metadata kind.
48 unsigned FPAccuracyID = getMDKindID("fpaccuracy");
49 assert(FPAccuracyID == MD_fpaccuracy && "fpaccuracy kind id drifted");
50 (void)FPAccuracyID;
Chris Lattnerc263b422010-03-30 23:03:27 +000051}
Owen Anderson8e66e0b2009-06-30 00:48:55 +000052LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +000053
Owen Anderson8e89e412010-09-08 18:03:32 +000054void LLVMContext::addModule(Module *M) {
55 pImpl->OwnedModules.insert(M);
56}
57
58void LLVMContext::removeModule(Module *M) {
59 pImpl->OwnedModules.erase(M);
60}
61
Chris Lattner1e457892010-04-07 23:40:44 +000062//===----------------------------------------------------------------------===//
63// Recoverable Backend Errors
64//===----------------------------------------------------------------------===//
65
Chris Lattnerb0e36082010-11-17 08:13:01 +000066void LLVMContext::
Bob Wilsonec3ff9c2010-12-17 23:06:32 +000067setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
Chris Lattnerb0e36082010-11-17 08:13:01 +000068 void *DiagContext) {
Chris Lattner60955d42010-04-06 00:44:45 +000069 pImpl->InlineAsmDiagHandler = DiagHandler;
70 pImpl->InlineAsmDiagContext = DiagContext;
71}
72
73/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
74/// setInlineAsmDiagnosticHandler.
Chris Lattnerb0e36082010-11-17 08:13:01 +000075LLVMContext::InlineAsmDiagHandlerTy
76LLVMContext::getInlineAsmDiagnosticHandler() const {
Chris Lattner60955d42010-04-06 00:44:45 +000077 return pImpl->InlineAsmDiagHandler;
78}
79
80/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
81/// setInlineAsmDiagnosticHandler.
82void *LLVMContext::getInlineAsmDiagnosticContext() const {
83 return pImpl->InlineAsmDiagContext;
84}
Chris Lattnera3b94ba2010-03-30 20:48:48 +000085
Chris Lattnere22e6132012-01-03 23:47:05 +000086void LLVMContext::emitError(const Twine &ErrorStr) {
Chris Lattner1e457892010-04-07 23:40:44 +000087 emitError(0U, ErrorStr);
88}
89
Chris Lattnere22e6132012-01-03 23:47:05 +000090void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
Chris Lattner1e457892010-04-07 23:40:44 +000091 unsigned LocCookie = 0;
92 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
93 if (SrcLoc->getNumOperands() != 0)
94 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
95 LocCookie = CI->getZExtValue();
96 }
97 return emitError(LocCookie, ErrorStr);
98}
99
Chris Lattnere22e6132012-01-03 23:47:05 +0000100void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
Chris Lattner1e457892010-04-07 23:40:44 +0000101 // If there is no error handler installed, just print the error and exit.
102 if (pImpl->InlineAsmDiagHandler == 0) {
103 errs() << "error: " << ErrorStr << "\n";
104 exit(1);
105 }
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000106
Chris Lattner1e457892010-04-07 23:40:44 +0000107 // If we do have an error handler, we can report the error and keep going.
Chris Lattner03b80a42011-10-16 05:43:57 +0000108 SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000109
Chris Lattnerb0e36082010-11-17 08:13:01 +0000110 pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
Chris Lattner1e457892010-04-07 23:40:44 +0000111}
112
113//===----------------------------------------------------------------------===//
114// Metadata Kind Uniquing
115//===----------------------------------------------------------------------===//
116
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000117#ifndef NDEBUG
118/// isValidName - Return true if Name is a valid custom metadata handler name.
119static bool isValidName(StringRef MDName) {
120 if (MDName.empty())
121 return false;
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000122
Nick Lewyckyb71afe82010-12-19 20:42:43 +0000123 if (!std::isalpha(MDName[0]))
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000124 return false;
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000125
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000126 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
127 ++I) {
Nick Lewyckyb71afe82010-12-19 20:42:43 +0000128 if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000129 return false;
130 }
131 return true;
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000132}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000133#endif
134
135/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
136unsigned LLVMContext::getMDKindID(StringRef Name) const {
137 assert(isValidName(Name) && "Invalid MDNode name");
Dan Gohman43aa8f02010-07-20 21:42:28 +0000138
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000139 // If this is new, assign it its ID.
Dan Gohman43aa8f02010-07-20 21:42:28 +0000140 return
141 pImpl->CustomMDKindNames.GetOrCreateValue(
142 Name, pImpl->CustomMDKindNames.size()).second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000143}
144
145/// getHandlerNames - Populate client supplied smallvector using custome
146/// metadata name and ID.
147void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000148 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000149 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
150 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000151 Names[I->second] = I->first();
152}