blob: 563c651315a3370f1ea6f69b4a010c82dc8ca7ff [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
Chris Lattner1e457892010-04-07 23:40:44 +000037//===----------------------------------------------------------------------===//
38// Recoverable Backend Errors
39//===----------------------------------------------------------------------===//
40
Chris Lattner60955d42010-04-06 00:44:45 +000041void LLVMContext::setInlineAsmDiagnosticHandler(void *DiagHandler,
42 void *DiagContext) {
43 pImpl->InlineAsmDiagHandler = DiagHandler;
44 pImpl->InlineAsmDiagContext = DiagContext;
45}
46
47/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
48/// setInlineAsmDiagnosticHandler.
49void *LLVMContext::getInlineAsmDiagnosticHandler() const {
50 return pImpl->InlineAsmDiagHandler;
51}
52
53/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
54/// setInlineAsmDiagnosticHandler.
55void *LLVMContext::getInlineAsmDiagnosticContext() const {
56 return pImpl->InlineAsmDiagContext;
57}
Chris Lattnera3b94ba2010-03-30 20:48:48 +000058
Chris Lattner1e457892010-04-07 23:40:44 +000059void LLVMContext::emitError(StringRef ErrorStr) {
60 emitError(0U, ErrorStr);
61}
62
63void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
64 unsigned LocCookie = 0;
65 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
66 if (SrcLoc->getNumOperands() != 0)
67 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
68 LocCookie = CI->getZExtValue();
69 }
70 return emitError(LocCookie, ErrorStr);
71}
72
73void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
74 // If there is no error handler installed, just print the error and exit.
75 if (pImpl->InlineAsmDiagHandler == 0) {
76 errs() << "error: " << ErrorStr << "\n";
77 exit(1);
78 }
79
80 // If we do have an error handler, we can report the error and keep going.
81 SMDiagnostic Diag("", "error: " + ErrorStr.str());
82
83 ((SourceMgr::DiagHandlerTy)(intptr_t)pImpl->InlineAsmDiagHandler)
84 (Diag, pImpl->InlineAsmDiagContext, LocCookie);
85
86}
87
88//===----------------------------------------------------------------------===//
89// Metadata Kind Uniquing
90//===----------------------------------------------------------------------===//
91
Chris Lattnera3b94ba2010-03-30 20:48:48 +000092#ifndef NDEBUG
93/// isValidName - Return true if Name is a valid custom metadata handler name.
94static bool isValidName(StringRef MDName) {
95 if (MDName.empty())
96 return false;
97
98 if (!isalpha(MDName[0]))
99 return false;
100
101 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
102 ++I) {
103 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
104 return false;
105 }
106 return true;
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000107}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000108#endif
109
110/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
111unsigned LLVMContext::getMDKindID(StringRef Name) const {
112 assert(isValidName(Name) && "Invalid MDNode name");
Dan Gohman43aa8f02010-07-20 21:42:28 +0000113
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000114 // If this is new, assign it its ID.
Dan Gohman43aa8f02010-07-20 21:42:28 +0000115 return
116 pImpl->CustomMDKindNames.GetOrCreateValue(
117 Name, pImpl->CustomMDKindNames.size()).second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000118}
119
120/// getHandlerNames - Populate client supplied smallvector using custome
121/// metadata name and ID.
122void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000123 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000124 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
125 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000126 Names[I->second] = I->first();
127}