1 //===--- llvm/Analysis/DebugInfo.h - Debug Information Helpers --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a bunch of datatypes that are useful for creating and
11 // walking debug info in LLVM IR form.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_DEBUGINFO_H
16 #define LLVM_SUPPORT_DEBUGINFO_H
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/DenseMap.h"
32 Version6 = 6 << 16, // Current version of debug information.
33 Version5 = 5 << 16, // Constant for version 5.
34 Version4 = 4 << 16, // Constant for version 4.
35 VersionMask = 0xffff0000 // Mask for version number.
41 /// DIDescriptor constructor. If the specified GV is non-null, this checks
42 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
43 /// not, the debug info is corrupt and we ignore it.
44 DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
46 unsigned getTag() const {
47 return getUnsignedField(0) & ~VersionMask;
49 unsigned getVersion() const {
50 return getUnsignedField(0) & VersionMask;
53 std::string getStringField(unsigned Elt) const;
54 unsigned getUnsignedField(unsigned Elt) const {
55 return (unsigned)getUInt64Field(Elt);
57 uint64_t getUInt64Field(unsigned Elt) const;
58 DIDescriptor getDescriptorField(unsigned Elt) const;
60 template <typename DescTy>
61 DescTy getFieldAs(unsigned Elt) const {
62 return DescTy(getDescriptorField(6).getGV());
65 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
68 explicit DIDescriptor() : GV(0) {}
69 explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
71 bool isNull() const { return GV == 0; }
73 GlobalVariable *getGV() const { return GV; }
75 /// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
76 Constant *getCastToEmpty() const;
79 /// DIAnchor - A wrapper for various anchor descriptors.
80 class DIAnchor : public DIDescriptor {
82 explicit DIAnchor(GlobalVariable *GV = 0);
84 unsigned getAnchorTag() const { return getUnsignedField(1); }
87 /// DIArray - This descriptor holds an array of descriptors.
88 class DIArray : public DIDescriptor {
90 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
92 unsigned getNumElements() const;
93 DIDescriptor getElement(unsigned Idx) const;
96 /// DICompileUnit - A wrapper for a compile unit.
97 class DICompileUnit : public DIDescriptor {
99 explicit DICompileUnit(GlobalVariable *GV = 0);
101 unsigned getLanguage() const { return getUnsignedField(2); }
102 std::string getFilename() const { return getStringField(3); }
103 std::string getDirectory() const { return getStringField(4); }
104 std::string getProducer() const { return getStringField(5); }
107 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
108 /// FIXME: it seems strange that this doesn't have either a reference to the
109 /// type/precision or a file/line pair for location info.
110 class DIEnumerator : public DIDescriptor {
112 explicit DIEnumerator(GlobalVariable *GV = 0);
114 std::string getName() const { return getStringField(1); }
115 uint64_t getLanguage() const { return getUInt64Field(2); }
118 /// DISubrange - This is used to represent ranges, for array bounds.
119 class DISubrange : public DIDescriptor {
121 explicit DISubrange(GlobalVariable *GV = 0);
123 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
124 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
127 /// DIType - This is a wrapper for a type.
128 /// FIXME: Types should be factored much better so that CV qualifiers and
129 /// others do not require a huge and empty descriptor full of zeros.
130 class DIType : public DIDescriptor {
132 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
133 // This ctor is used when the Tag has already been validated by a derived
135 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
137 explicit DIType(GlobalVariable *GV);
140 DIDescriptor getContext() const { return getDescriptorField(1); }
141 std::string getName() const { return getStringField(2); }
142 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
143 unsigned getLineNumber() const { return getUnsignedField(4); }
144 uint64_t getSizeInBits() const { return getUInt64Field(5); }
145 uint64_t getAlignInBits() const { return getUInt64Field(6); }
146 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
147 // carry this is just plain insane.
148 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
149 unsigned getFlags() const { return getUnsignedField(8); }
152 /// DIBasicType - A basic type, like 'int' or 'float'.
153 class DIBasicType : public DIType {
155 explicit DIBasicType(GlobalVariable *GV);
157 unsigned getEncoding() const { return getUnsignedField(9); }
160 /// DIDerivedType - A simple derived type, like a const qualified type,
161 /// a typedef, a pointer or reference, etc.
162 class DIDerivedType : public DIType {
164 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
165 : DIType(GV, true, true) {}
167 explicit DIDerivedType(GlobalVariable *GV);
169 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
171 /// isDerivedType - Return true if the specified tag is legal for
173 static bool isDerivedType(unsigned TAG);
177 /// DICompositeType - This descriptor holds a type that can refer to multiple
178 /// other types, like a function or struct.
179 /// FIXME: Why is this a DIDerivedType??
180 class DICompositeType : public DIDerivedType {
182 explicit DICompositeType(GlobalVariable *GV);
184 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
186 /// isCompositeType - Return true if the specified tag is legal for
188 static bool isCompositeType(unsigned TAG);
191 /// DIGlobal - This is a common class for global variables and subprograms.
192 class DIGlobal : public DIDescriptor {
194 explicit DIGlobal(GlobalVariable *GV = 0, unsigned RequiredTag)
195 : DIDescriptor(GV, RequiredTag) {}
198 DIDescriptor getContext() const { return getDescriptorField(2); }
199 std::string getName() const { return getStringField(3); }
200 std::string getDisplayName() const { return getStringField(4); }
201 std::string getLinkageName() const { return getStringField(5); }
203 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
204 unsigned getLineNumber() const { return getUnsignedField(7); }
205 DIType getType() const { return getFieldAs<DIType>(8); }
207 /// isLocalToUnit - Return true if this subprogram is local to the current
208 /// compile unit, like 'static' in C.
209 unsigned isLocalToUnit() const { return getUnsignedField(9); }
210 unsigned isDefinition() const { return getUnsignedField(10); }
214 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
215 class DISubprogram : public DIGlobal {
217 explicit DISubprogram(GlobalVariable *GV = 0);
221 /// DIGlobalVariable - This is a wrapper for a global variable.
222 class DIGlobalVariable : public DIGlobal {
224 explicit DIGlobalVariable(GlobalVariable *GV = 0);
226 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
230 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
232 class DIVariable : public DIDescriptor {
234 explicit DIVariable(GlobalVariable *GV = 0);
236 DIDescriptor getContext() const { return getDescriptorField(1); }
237 std::string getName() const { return getStringField(2); }
239 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
240 unsigned getLineNumber() const { return getUnsignedField(4); }
241 DIType getType() const { return getFieldAs<DIType>(5); }
243 /// isVariable - Return true if the specified tag is legal for DIVariable.
244 static bool isVariable(unsigned Tag);
248 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
249 class DIBlock : public DIDescriptor {
251 explicit DIBlock(GlobalVariable *GV = 0);
253 DIDescriptor getContext() const { return getDescriptorField(1); }
256 /// DIFactory - This object assists with the construction of the various
260 // Cached values for uniquing and faster lookups.
261 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
262 Function *StopPointFn; // llvm.dbg.stoppoint
263 Function *FuncStartFn; // llvm.dbg.func.start
264 Function *RegionStartFn; // llvm.dbg.region.start
265 Function *RegionEndFn; // llvm.dbg.region.end
266 Function *DeclareFn; // llvm.dbg.declare
267 StringMap<Constant*> StringCache;
268 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
270 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
271 void operator=(const DIFactory&); // DO NOT IMPLEMENT
273 explicit DIFactory(Module &m) : M(m) {
274 StopPointFn = FuncStartFn = RegionStartFn = RegionEndFn = DeclareFn = 0;
277 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
278 /// creating a new one if there isn't already one in the module.
279 DIAnchor GetOrCreateCompileUnitAnchor();
281 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
282 /// creating a new one if there isn't already one in the module.
283 DIAnchor GetOrCreateSubprogramAnchor();
285 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
286 /// creating a new one if there isn't already one in the module.
287 DIAnchor GetOrCreateGlobalVariableAnchor();
289 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
290 /// This implicitly uniques the arrays created.
291 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
293 /// GetOrCreateSubrange - Create a descriptor for a value range. This
294 /// implicitly uniques the values returned.
295 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
298 /// CreateCompileUnit - Create a new descriptor for the specified compile
300 DICompileUnit CreateCompileUnit(unsigned LangID,
301 const std::string &Filename,
302 const std::string &Directory,
303 const std::string &Producer);
305 /// CreateEnumerator - Create a single enumerator value.
306 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
308 /// CreateBasicType - Create a basic type like int, float, etc.
309 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
310 DICompileUnit CompileUnit, unsigned LineNumber,
311 uint64_t SizeInBits, uint64_t AlignInBits,
312 uint64_t OffsetInBits, unsigned Flags,
315 /// CreateDerivedType - Create a derived type like const qualified type,
316 /// pointer, typedef, etc.
317 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
318 const std::string &Name,
319 DICompileUnit CompileUnit,
321 uint64_t SizeInBits, uint64_t AlignInBits,
322 uint64_t OffsetInBits, unsigned Flags,
325 /// CreateCompositeType - Create a composite type like array, struct, etc.
326 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
327 const std::string &Name,
328 DICompileUnit CompileUnit,
331 uint64_t AlignInBits,
332 uint64_t OffsetInBits, unsigned Flags,
336 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
337 /// See comments in DISubprogram for descriptions of these fields.
338 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
339 const std::string &DisplayName,
340 const std::string &LinkageName,
341 DICompileUnit CompileUnit, unsigned LineNo,
342 DIType Type, bool isLocalToUnit,
345 /// CreateGlobalVariable - Create a new descriptor for the specified global.
347 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
348 const std::string &DisplayName,
349 const std::string &LinkageName,
350 DICompileUnit CompileUnit,
351 unsigned LineNo, DIType Type, bool isLocalToUnit,
352 bool isDefinition, llvm::GlobalVariable *GV);
355 /// CreateVariable - Create a new descriptor for the specified variable.
356 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
357 const std::string &Name,
358 DICompileUnit CompileUnit, unsigned LineNo,
361 /// CreateBlock - This creates a descriptor for a lexical block with the
362 /// specified parent context.
363 DIBlock CreateBlock(DIDescriptor Context);
365 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
366 /// inserting it at the end of the specified basic block.
367 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
370 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
371 /// mark the start of the specified subprogram.
372 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
374 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
375 /// mark the start of a region for the specified scoping descriptor.
376 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
378 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
379 /// mark the end of a region for the specified scoping descriptor.
380 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
382 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
383 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
386 Constant *GetTagConstant(unsigned TAG);
387 Constant *GetStringConstant(const std::string &String);
388 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
392 } // end namespace llvm