LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LLVMContext.cpp
Go to the documentation of this file.
1 //===-- 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 //===----------------------------------------------------------------------===//
9 //
10 // This file implements LLVMContext, as a wrapper around the opaque
11 // class LLVMContextImpl.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/IR/LLVMContext.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Instruction.h"
19 #include "llvm/IR/Metadata.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include <cctype>
23 using namespace llvm;
24 
26 
28  return *GlobalContext;
29 }
30 
32  // Create the fixed metadata kinds. This is done in the same order as the
33  // MD_* enum values so that they correspond.
34 
35  // Create the 'dbg' metadata kind.
36  unsigned DbgID = getMDKindID("dbg");
37  assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
38 
39  // Create the 'tbaa' metadata kind.
40  unsigned TBAAID = getMDKindID("tbaa");
41  assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
42 
43  // Create the 'prof' metadata kind.
44  unsigned ProfID = getMDKindID("prof");
45  assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
46 
47  // Create the 'fpmath' metadata kind.
48  unsigned FPAccuracyID = getMDKindID("fpmath");
49  assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
50  (void)FPAccuracyID;
51 
52  // Create the 'range' metadata kind.
53  unsigned RangeID = getMDKindID("range");
54  assert(RangeID == MD_range && "range kind id drifted");
55  (void)RangeID;
56 
57  // Create the 'tbaa.struct' metadata kind.
58  unsigned TBAAStructID = getMDKindID("tbaa.struct");
59  assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
60  (void)TBAAStructID;
61 
62  // Create the 'invariant.load' metadata kind.
63  unsigned InvariantLdId = getMDKindID("invariant.load");
64  assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
65  (void)InvariantLdId;
66 }
68 
69 void LLVMContext::addModule(Module *M) {
71 }
72 
73 void LLVMContext::removeModule(Module *M) {
75 }
76 
77 //===----------------------------------------------------------------------===//
78 // Recoverable Backend Errors
79 //===----------------------------------------------------------------------===//
80 
81 void LLVMContext::
82 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
83  void *DiagContext) {
84  pImpl->InlineAsmDiagHandler = DiagHandler;
85  pImpl->InlineAsmDiagContext = DiagContext;
86 }
87 
88 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
89 /// setInlineAsmDiagnosticHandler.
93 }
94 
95 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
96 /// setInlineAsmDiagnosticHandler.
99 }
100 
101 void LLVMContext::emitError(const Twine &ErrorStr) {
102  emitError(0U, ErrorStr);
103 }
104 
105 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
106  unsigned LocCookie = 0;
107  if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
108  if (SrcLoc->getNumOperands() != 0)
109  if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
110  LocCookie = CI->getZExtValue();
111  }
112  return emitError(LocCookie, ErrorStr);
113 }
114 
115 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
116  // If there is no error handler installed, just print the error and exit.
117  if (pImpl->InlineAsmDiagHandler == 0) {
118  errs() << "error: " << ErrorStr << "\n";
119  exit(1);
120  }
121 
122  // If we do have an error handler, we can report the error and keep going.
123  SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
124 
126 }
127 
128 //===----------------------------------------------------------------------===//
129 // Metadata Kind Uniquing
130 //===----------------------------------------------------------------------===//
131 
132 #ifndef NDEBUG
133 /// isValidName - Return true if Name is a valid custom metadata handler name.
134 static bool isValidName(StringRef MDName) {
135  if (MDName.empty())
136  return false;
137 
138  if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
139  return false;
140 
141  for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
142  ++I) {
143  if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
144  *I != '-' && *I != '.')
145  return false;
146  }
147  return true;
148 }
149 #endif
150 
151 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
153  assert(isValidName(Name) && "Invalid MDNode name");
154 
155  // If this is new, assign it its ID.
156  return
158  Name, pImpl->CustomMDKindNames.size()).second;
159 }
160 
161 /// getHandlerNames - Populate client supplied smallvector using custome
162 /// metadata name and ID.
166  E = pImpl->CustomMDKindNames.end(); I != E; ++I)
167  Names[I->second] = I->first();
168 }
raw_ostream & errs()
void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, void *DiagContext=0)
Definition: LLVMContext.cpp:82
InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const
Definition: LLVMContext.cpp:91
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
bool insert(PtrType Ptr)
Definition: SmallPtrSet.h:253
static bool isValidName(StringRef MDName)
isValidName - Return true if Name is a valid custom metadata handler name.
MDNode - a tuple of other values.
Definition: Metadata.h:69
LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler
std::string str() const
str - Return the twine contents as a std::string.
Definition: Twine.cpp:16
void emitError(unsigned LocCookie, const Twine &ErrorStr)
static ManagedStatic< LLVMContext > GlobalContext
Definition: LLVMContext.cpp:25
iterator begin() const
Definition: StringRef.h:97
void getMDKindNames(SmallVectorImpl< StringRef > &Result) const
StringMap< unsigned > CustomMDKindNames
CustomMDKindNames - Map to hold the metadata string to ID mapping.
unsigned size() const
Definition: StringMap.h:104
unsigned getMDKindID(StringRef Name) const
getMDKindID - Return a unique non-zero ID for the specified metadata kind.
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:39
const char * iterator
Definition: StringRef.h:43
bool erase(PtrType Ptr)
Definition: SmallPtrSet.h:259
Class for constant integers.
Definition: Constants.h:51
MDNode * getMetadata(unsigned KindID) const
Definition: Instruction.h:140
SmallPtrSet< Module *, 4 > OwnedModules
void(* InlineAsmDiagHandlerTy)(const SMDiagnostic &, void *Context, unsigned LocCookie)
Definition: LLVMContext.h:64
MapEntryTy & GetOrCreateValue(StringRef Key, InitTy Val)
Definition: StringMap.h:361
void * getInlineAsmDiagnosticContext() const
Definition: LLVMContext.cpp:97
iterator begin()
Definition: StringMap.h:278
#define I(x, y, z)
Definition: MD5.cpp:54
void resize(unsigned N)
Definition: SmallVector.h:401
iterator end() const
Definition: StringRef.h:99
iterator end()
Definition: StringMap.h:281
LLVMContext & getGlobalContext()
Definition: LLVMContext.cpp:27
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110