blob: c948320c20ec2e4f5dfc7a1f5863f73ed792a54f [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"
Owen Anderson8e66e0b2009-06-30 00:48:55 +000022using namespace llvm;
23
Owen Anderson1938fb12009-06-30 23:39:59 +000024static ManagedStatic<LLVMContext> GlobalContext;
25
Owen Anderson2a154432009-07-01 23:13:44 +000026LLVMContext& llvm::getGlobalContext() {
Owen Anderson1cf085d2009-07-01 21:22:36 +000027 return *GlobalContext;
Owen Anderson1938fb12009-06-30 23:39:59 +000028}
29
Chris Lattnerc263b422010-03-30 23:03:27 +000030LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
Dan Gohman41f14cf2010-09-14 21:25:10 +000031 // Create the fixed metadata kinds. This is done in the same order as the
32 // MD_* enum values so that they correspond.
33
Bob Wilsonec3ff9c2010-12-17 23:06:32 +000034 // Create the 'dbg' metadata kind.
Chris Lattnerc263b422010-03-30 23:03:27 +000035 unsigned DbgID = getMDKindID("dbg");
36 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
Dan Gohman41f14cf2010-09-14 21:25:10 +000037
38 // Create the 'tbaa' metadata kind.
39 unsigned TBAAID = getMDKindID("tbaa");
40 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
Chris Lattnerc263b422010-03-30 23:03:27 +000041}
Owen Anderson8e66e0b2009-06-30 00:48:55 +000042LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +000043
Owen Anderson8e89e412010-09-08 18:03:32 +000044void LLVMContext::addModule(Module *M) {
45 pImpl->OwnedModules.insert(M);
46}
47
48void LLVMContext::removeModule(Module *M) {
49 pImpl->OwnedModules.erase(M);
50}
51
Chris Lattner1e457892010-04-07 23:40:44 +000052//===----------------------------------------------------------------------===//
53// Recoverable Backend Errors
54//===----------------------------------------------------------------------===//
55
Chris Lattnerb0e36082010-11-17 08:13:01 +000056void LLVMContext::
Bob Wilsonec3ff9c2010-12-17 23:06:32 +000057setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
Chris Lattnerb0e36082010-11-17 08:13:01 +000058 void *DiagContext) {
Chris Lattner60955d42010-04-06 00:44:45 +000059 pImpl->InlineAsmDiagHandler = DiagHandler;
60 pImpl->InlineAsmDiagContext = DiagContext;
61}
62
63/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
64/// setInlineAsmDiagnosticHandler.
Chris Lattnerb0e36082010-11-17 08:13:01 +000065LLVMContext::InlineAsmDiagHandlerTy
66LLVMContext::getInlineAsmDiagnosticHandler() const {
Chris Lattner60955d42010-04-06 00:44:45 +000067 return pImpl->InlineAsmDiagHandler;
68}
69
70/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
71/// setInlineAsmDiagnosticHandler.
72void *LLVMContext::getInlineAsmDiagnosticContext() const {
73 return pImpl->InlineAsmDiagContext;
74}
Chris Lattnera3b94ba2010-03-30 20:48:48 +000075
Chris Lattner1e457892010-04-07 23:40:44 +000076void LLVMContext::emitError(StringRef ErrorStr) {
77 emitError(0U, ErrorStr);
78}
79
80void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
81 unsigned LocCookie = 0;
82 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
83 if (SrcLoc->getNumOperands() != 0)
84 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
85 LocCookie = CI->getZExtValue();
86 }
87 return emitError(LocCookie, ErrorStr);
88}
89
90void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
91 // If there is no error handler installed, just print the error and exit.
92 if (pImpl->InlineAsmDiagHandler == 0) {
93 errs() << "error: " << ErrorStr << "\n";
94 exit(1);
95 }
Bob Wilsonec3ff9c2010-12-17 23:06:32 +000096
Chris Lattner1e457892010-04-07 23:40:44 +000097 // If we do have an error handler, we can report the error and keep going.
98 SMDiagnostic Diag("", "error: " + ErrorStr.str());
Bob Wilsonec3ff9c2010-12-17 23:06:32 +000099
Chris Lattnerb0e36082010-11-17 08:13:01 +0000100 pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
Chris Lattner1e457892010-04-07 23:40:44 +0000101}
102
103//===----------------------------------------------------------------------===//
104// Metadata Kind Uniquing
105//===----------------------------------------------------------------------===//
106
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000107#ifndef NDEBUG
108/// isValidName - Return true if Name is a valid custom metadata handler name.
109static bool isValidName(StringRef MDName) {
110 if (MDName.empty())
111 return false;
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000112
Nick Lewyckyb71afe82010-12-19 20:42:43 +0000113 if (!std::isalpha(MDName[0]))
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000114 return false;
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000115
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000116 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
117 ++I) {
Nick Lewyckyb71afe82010-12-19 20:42:43 +0000118 if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000119 return false;
120 }
121 return true;
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000122}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000123#endif
124
125/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
126unsigned LLVMContext::getMDKindID(StringRef Name) const {
127 assert(isValidName(Name) && "Invalid MDNode name");
Dan Gohman43aa8f02010-07-20 21:42:28 +0000128
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000129 // If this is new, assign it its ID.
Dan Gohman43aa8f02010-07-20 21:42:28 +0000130 return
131 pImpl->CustomMDKindNames.GetOrCreateValue(
132 Name, pImpl->CustomMDKindNames.size()).second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000133}
134
135/// getHandlerNames - Populate client supplied smallvector using custome
136/// metadata name and ID.
137void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000138 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000139 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
140 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000141 Names[I->second] = I->first();
142}