blob: 1bd497d05d4ecccc6e6d54db4a38a7d7c70962be [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;
Chris Lattnerc263b422010-03-30 23:03:27 +000042}
Owen Anderson8e66e0b2009-06-30 00:48:55 +000043LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +000044
Owen Anderson8e89e412010-09-08 18:03:32 +000045void LLVMContext::addModule(Module *M) {
46 pImpl->OwnedModules.insert(M);
47}
48
49void LLVMContext::removeModule(Module *M) {
50 pImpl->OwnedModules.erase(M);
51}
52
Chris Lattner1e457892010-04-07 23:40:44 +000053//===----------------------------------------------------------------------===//
54// Recoverable Backend Errors
55//===----------------------------------------------------------------------===//
56
Chris Lattnerb0e36082010-11-17 08:13:01 +000057void LLVMContext::
Bob Wilsonec3ff9c2010-12-17 23:06:32 +000058setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
Chris Lattnerb0e36082010-11-17 08:13:01 +000059 void *DiagContext) {
Chris Lattner60955d42010-04-06 00:44:45 +000060 pImpl->InlineAsmDiagHandler = DiagHandler;
61 pImpl->InlineAsmDiagContext = DiagContext;
62}
63
64/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
65/// setInlineAsmDiagnosticHandler.
Chris Lattnerb0e36082010-11-17 08:13:01 +000066LLVMContext::InlineAsmDiagHandlerTy
67LLVMContext::getInlineAsmDiagnosticHandler() const {
Chris Lattner60955d42010-04-06 00:44:45 +000068 return pImpl->InlineAsmDiagHandler;
69}
70
71/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
72/// setInlineAsmDiagnosticHandler.
73void *LLVMContext::getInlineAsmDiagnosticContext() const {
74 return pImpl->InlineAsmDiagContext;
75}
Chris Lattnera3b94ba2010-03-30 20:48:48 +000076
Chris Lattner1e457892010-04-07 23:40:44 +000077void LLVMContext::emitError(StringRef ErrorStr) {
78 emitError(0U, ErrorStr);
79}
80
81void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
82 unsigned LocCookie = 0;
83 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
84 if (SrcLoc->getNumOperands() != 0)
85 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
86 LocCookie = CI->getZExtValue();
87 }
88 return emitError(LocCookie, ErrorStr);
89}
90
91void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
92 // If there is no error handler installed, just print the error and exit.
93 if (pImpl->InlineAsmDiagHandler == 0) {
94 errs() << "error: " << ErrorStr << "\n";
95 exit(1);
96 }
Bob Wilsonec3ff9c2010-12-17 23:06:32 +000097
Chris Lattner1e457892010-04-07 23:40:44 +000098 // If we do have an error handler, we can report the error and keep going.
99 SMDiagnostic Diag("", "error: " + ErrorStr.str());
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000100
Chris Lattnerb0e36082010-11-17 08:13:01 +0000101 pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
Chris Lattner1e457892010-04-07 23:40:44 +0000102}
103
104//===----------------------------------------------------------------------===//
105// Metadata Kind Uniquing
106//===----------------------------------------------------------------------===//
107
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000108#ifndef NDEBUG
109/// isValidName - Return true if Name is a valid custom metadata handler name.
110static bool isValidName(StringRef MDName) {
111 if (MDName.empty())
112 return false;
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000113
Nick Lewyckyb71afe82010-12-19 20:42:43 +0000114 if (!std::isalpha(MDName[0]))
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000115 return false;
Bob Wilsonec3ff9c2010-12-17 23:06:32 +0000116
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000117 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
118 ++I) {
Nick Lewyckyb71afe82010-12-19 20:42:43 +0000119 if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000120 return false;
121 }
122 return true;
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000123}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000124#endif
125
126/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
127unsigned LLVMContext::getMDKindID(StringRef Name) const {
128 assert(isValidName(Name) && "Invalid MDNode name");
Dan Gohman43aa8f02010-07-20 21:42:28 +0000129
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000130 // If this is new, assign it its ID.
Dan Gohman43aa8f02010-07-20 21:42:28 +0000131 return
132 pImpl->CustomMDKindNames.GetOrCreateValue(
133 Name, pImpl->CustomMDKindNames.size()).second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000134}
135
136/// getHandlerNames - Populate client supplied smallvector using custome
137/// metadata name and ID.
138void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000139 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000140 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
141 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000142 Names[I->second] = I->first();
143}