blob: 60fb830e9b57f3c896c32f00db1d7c5f2d89d55a [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)) {
31 // Create the first metadata kind, which is always 'dbg'.
32 unsigned DbgID = getMDKindID("dbg");
33 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
34}
Owen Anderson8e66e0b2009-06-30 00:48:55 +000035LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +000036
Owen Anderson8e89e412010-09-08 18:03:32 +000037void LLVMContext::addModule(Module *M) {
38 pImpl->OwnedModules.insert(M);
39}
40
41void LLVMContext::removeModule(Module *M) {
42 pImpl->OwnedModules.erase(M);
43}
44
Chris Lattner1e457892010-04-07 23:40:44 +000045//===----------------------------------------------------------------------===//
46// Recoverable Backend Errors
47//===----------------------------------------------------------------------===//
48
Chris Lattner60955d42010-04-06 00:44:45 +000049void LLVMContext::setInlineAsmDiagnosticHandler(void *DiagHandler,
50 void *DiagContext) {
51 pImpl->InlineAsmDiagHandler = DiagHandler;
52 pImpl->InlineAsmDiagContext = DiagContext;
53}
54
55/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
56/// setInlineAsmDiagnosticHandler.
57void *LLVMContext::getInlineAsmDiagnosticHandler() const {
58 return pImpl->InlineAsmDiagHandler;
59}
60
61/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
62/// setInlineAsmDiagnosticHandler.
63void *LLVMContext::getInlineAsmDiagnosticContext() const {
64 return pImpl->InlineAsmDiagContext;
65}
Chris Lattnera3b94ba2010-03-30 20:48:48 +000066
Chris Lattner1e457892010-04-07 23:40:44 +000067void LLVMContext::emitError(StringRef ErrorStr) {
68 emitError(0U, ErrorStr);
69}
70
71void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
72 unsigned LocCookie = 0;
73 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
74 if (SrcLoc->getNumOperands() != 0)
75 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
76 LocCookie = CI->getZExtValue();
77 }
78 return emitError(LocCookie, ErrorStr);
79}
80
81void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
82 // If there is no error handler installed, just print the error and exit.
83 if (pImpl->InlineAsmDiagHandler == 0) {
84 errs() << "error: " << ErrorStr << "\n";
85 exit(1);
86 }
87
88 // If we do have an error handler, we can report the error and keep going.
89 SMDiagnostic Diag("", "error: " + ErrorStr.str());
90
91 ((SourceMgr::DiagHandlerTy)(intptr_t)pImpl->InlineAsmDiagHandler)
92 (Diag, pImpl->InlineAsmDiagContext, LocCookie);
93
94}
95
96//===----------------------------------------------------------------------===//
97// Metadata Kind Uniquing
98//===----------------------------------------------------------------------===//
99
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000100#ifndef NDEBUG
101/// isValidName - Return true if Name is a valid custom metadata handler name.
102static bool isValidName(StringRef MDName) {
103 if (MDName.empty())
104 return false;
105
106 if (!isalpha(MDName[0]))
107 return false;
108
109 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
110 ++I) {
111 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
112 return false;
113 }
114 return true;
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000115}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000116#endif
117
118/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
119unsigned LLVMContext::getMDKindID(StringRef Name) const {
120 assert(isValidName(Name) && "Invalid MDNode name");
Dan Gohman43aa8f02010-07-20 21:42:28 +0000121
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000122 // If this is new, assign it its ID.
Dan Gohman43aa8f02010-07-20 21:42:28 +0000123 return
124 pImpl->CustomMDKindNames.GetOrCreateValue(
125 Name, pImpl->CustomMDKindNames.size()).second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000126}
127
128/// getHandlerNames - Populate client supplied smallvector using custome
129/// metadata name and ID.
130void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000131 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000132 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
133 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000134 Names[I->second] = I->first();
135}