blob: 68c56212bc6cd83d7dc2599de938c0e6e19b516f [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;
Rafael Espindolaef9f5502012-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;
Chris Lattnerc263b422010-03-30 23:03:27 +000056}
Owen Anderson8e66e0b2009-06-30 00:48:55 +000057LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +000058
Owen Anderson8e89e412010-09-08 18:03:32 +000059void LLVMContext::addModule(Module *M) {
60 pImpl->OwnedModules.insert(M);
61}
62
63void LLVMContext::removeModule(Module *M) {
64 pImpl->OwnedModules.erase(M);
65}
66
Chris Lattner1e457892010-04-07 23:40:44 +000067//===----------------------------------------------------------------------===//
68// Recoverable Backend Errors
69//===----------------------------------------------------------------------===//
70
Chris Lattnerb0e36082010-11-17 08:13:01 +000071void LLVMContext::
Bob Wilsonec3ff9c2010-12-17 23:06:32 +000072setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
Chris Lattnerb0e36082010-11-17 08:13:01 +000073 void *DiagContext) {
Chris Lattner60955d42010-04-06 00:44:45 +000074 pImpl->InlineAsmDiagHandler = DiagHandler;
75 pImpl->InlineAsmDiagContext = DiagContext;
76}
77
78/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
79/// setInlineAsmDiagnosticHandler.
Chris Lattnerb0e36082010-11-17 08:13:01 +000080LLVMContext::InlineAsmDiagHandlerTy
81LLVMContext::getInlineAsmDiagnosticHandler() const {
Chris Lattner60955d42010-04-06 00:44:45 +000082 return pImpl->InlineAsmDiagHandler;
83}
84
85/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
86/// setInlineAsmDiagnosticHandler.
87void *LLVMContext::getInlineAsmDiagnosticContext() const {
88 return pImpl->InlineAsmDiagContext;
89}
Chris Lattnera3b94ba2010-03-30 20:48:48 +000090
Chris Lattnere22e6132012-01-03 23:47:05 +000091void LLVMContext::emitError(const Twine &ErrorStr) {
Chris Lattner1e457892010-04-07 23:40:44 +000092 emitError(0U, ErrorStr);
93}
94
Chris Lattnere22e6132012-01-03 23:47:05 +000095void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
Chris Lattner1e457892010-04-07 23:40:44 +000096 unsigned LocCookie = 0;
97 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
98 if (SrcLoc->getNumOperands() != 0)
99 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
100 LocCookie = CI->getZExtValue();
101 }
102 return emitError(LocCookie, ErrorStr);
103}
104
Chris Lattnere22e6132012-01-03 23:47:05 +0000105void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
Chris Lattner1e457892010-04-07 23:40:44 +0000106 // If there is no error handler installed, just print the error and exit.
107 if (pImpl->InlineAsmDiagHandler == 0) {
108 errs() << "error: " << ErrorStr << "\n";
109 exit(1);
110 }
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000111
Chris Lattner1e457892010-04-07 23:40:44 +0000112 // If we do have an error handler, we can report the error and keep going.
Chris Lattner03b80a42011-10-16 05:43:57 +0000113 SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000114
Chris Lattnerb0e36082010-11-17 08:13:01 +0000115 pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
Chris Lattner1e457892010-04-07 23:40:44 +0000116}
117
118//===----------------------------------------------------------------------===//
119// Metadata Kind Uniquing
120//===----------------------------------------------------------------------===//
121
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000122#ifndef NDEBUG
123/// isValidName - Return true if Name is a valid custom metadata handler name.
124static bool isValidName(StringRef MDName) {
125 if (MDName.empty())
126 return false;
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000127
Nick Lewyckyb71afe82010-12-19 20:42:43 +0000128 if (!std::isalpha(MDName[0]))
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000129 return false;
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000130
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000131 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
132 ++I) {
Nick Lewyckyb71afe82010-12-19 20:42:43 +0000133 if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000134 return false;
135 }
136 return true;
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000137}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000138#endif
139
140/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
141unsigned LLVMContext::getMDKindID(StringRef Name) const {
142 assert(isValidName(Name) && "Invalid MDNode name");
Dan Gohman43aa8f02010-07-20 21:42:28 +0000143
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000144 // If this is new, assign it its ID.
Dan Gohman43aa8f02010-07-20 21:42:28 +0000145 return
146 pImpl->CustomMDKindNames.GetOrCreateValue(
147 Name, pImpl->CustomMDKindNames.size()).second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000148}
149
150/// getHandlerNames - Populate client supplied smallvector using custome
151/// metadata name and ID.
152void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000153 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000154 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
155 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000156 Names[I->second] = I->first();
157}