1 //===--- lib/CodeGen/DIE.h - DWARF Info Entries -----------------*- 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 // Data structures for DWARF info entries.
12 //===----------------------------------------------------------------------===//
14 #ifndef CODEGEN_ASMPRINTER_DIE_H__
15 #define CODEGEN_ASMPRINTER_DIE_H__
17 #include "DwarfLabel.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Dwarf.h"
29 //===--------------------------------------------------------------------===//
30 /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
31 /// Dwarf abbreviation.
33 /// Attribute - Dwarf attribute code.
37 /// Form - Dwarf form code.
41 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
44 unsigned getAttribute() const { return Attribute; }
45 unsigned getForm() const { return Form; }
47 /// Profile - Used to gather unique data for the abbreviation folding set.
49 void Profile(FoldingSetNodeID &ID) const;
52 //===--------------------------------------------------------------------===//
53 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
54 /// information object.
55 class DIEAbbrev : public FoldingSetNode {
56 /// Tag - Dwarf tag code.
60 /// Unique number for node.
64 /// ChildrenFlag - Dwarf children flag.
66 unsigned ChildrenFlag;
68 /// Data - Raw data bytes for abbreviation.
70 SmallVector<DIEAbbrevData, 8> Data;
72 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
73 virtual ~DIEAbbrev() {}
76 unsigned getTag() const { return Tag; }
77 unsigned getNumber() const { return Number; }
78 unsigned getChildrenFlag() const { return ChildrenFlag; }
79 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
80 void setTag(unsigned T) { Tag = T; }
81 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
82 void setNumber(unsigned N) { Number = N; }
84 /// AddAttribute - Adds another set of attribute information to the
86 void AddAttribute(unsigned Attribute, unsigned Form) {
87 Data.push_back(DIEAbbrevData(Attribute, Form));
90 /// AddFirstAttribute - Adds a set of attribute information to the front
91 /// of the abbreviation.
92 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
93 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
96 /// Profile - Used to gather unique data for the abbreviation folding set.
98 void Profile(FoldingSetNodeID &ID) const;
100 /// Emit - Print the abbreviation using the specified asm printer.
102 void Emit(const AsmPrinter *Asm) const;
105 void print(raw_ostream &O);
110 //===--------------------------------------------------------------------===//
111 /// DIE - A structured debug information entry. Has an abbreviation which
112 /// describes it's organization.
118 /// Abbrev - Buffer for constructing abbreviation.
122 /// Offset - Offset in debug info section.
126 /// Size - Size of instance + children.
132 std::vector<DIE *> Children;
134 /// Attributes values.
136 SmallVector<DIEValue*, 32> Values;
138 /// Abstract compile unit.
139 CompileUnit *AbstractCU;
141 // Private data for print()
142 mutable unsigned IndentCount;
144 explicit DIE(unsigned Tag)
145 : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0),
146 Size(0), IndentCount(0) {}
150 DIEAbbrev &getAbbrev() { return Abbrev; }
151 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
152 unsigned getTag() const { return Abbrev.getTag(); }
153 unsigned getOffset() const { return Offset; }
154 unsigned getSize() const { return Size; }
155 const std::vector<DIE *> &getChildren() const { return Children; }
156 SmallVector<DIEValue*, 32> &getValues() { return Values; }
157 CompileUnit *getAbstractCompileUnit() const { return AbstractCU; }
159 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
160 void setOffset(unsigned O) { Offset = O; }
161 void setSize(unsigned S) { Size = S; }
162 void setAbstractCompileUnit(CompileUnit *CU) { AbstractCU = CU; }
164 /// AddValue - Add a value and attributes to a DIE.
166 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
167 Abbrev.AddAttribute(Attribute, Form);
168 Values.push_back(Value);
171 /// SiblingOffset - Return the offset of the debug information entry's
173 unsigned SiblingOffset() const { return Offset + Size; }
175 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
177 void AddSiblingOffset();
179 /// AddChild - Add a child to the DIE.
181 void AddChild(DIE *Child) {
182 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
183 Children.push_back(Child);
186 /// Detach - Detaches objects connected to it after copying.
193 void print(raw_ostream &O, unsigned IncIndent = 0);
198 //===--------------------------------------------------------------------===//
199 /// DIEValue - A debug information entry value.
214 /// Type - Type of data stored in the value.
218 explicit DIEValue(unsigned T) : Type(T) {}
219 virtual ~DIEValue() {}
222 unsigned getType() const { return Type; }
224 /// EmitValue - Emit value via the Dwarf writer.
226 virtual void EmitValue(Dwarf *D, unsigned Form) const = 0;
228 /// SizeOf - Return the size of a value in bytes.
230 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const = 0;
232 // Implement isa/cast/dyncast.
233 static bool classof(const DIEValue *) { return true; }
236 virtual void print(raw_ostream &O) = 0;
241 //===--------------------------------------------------------------------===//
242 /// DIEInteger - An integer value DIE.
244 class DIEInteger : public DIEValue {
247 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
249 /// BestForm - Choose the best form for integer.
251 static unsigned BestForm(bool IsSigned, uint64_t Int) {
253 if ((char)Int == (signed)Int) return dwarf::DW_FORM_data1;
254 if ((short)Int == (signed)Int) return dwarf::DW_FORM_data2;
255 if ((int)Int == (signed)Int) return dwarf::DW_FORM_data4;
257 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
258 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
259 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
261 return dwarf::DW_FORM_data8;
264 /// EmitValue - Emit integer of appropriate size.
266 virtual void EmitValue(Dwarf *D, unsigned Form) const;
268 /// SizeOf - Determine size of integer value in bytes.
270 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
273 // Implement isa/cast/dyncast.
274 static bool classof(const DIEInteger *) { return true; }
275 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
278 virtual void print(raw_ostream &O);
282 //===--------------------------------------------------------------------===//
283 /// DIEString - A string value DIE.
285 class DIEString : public DIEValue {
286 const std::string Str;
288 explicit DIEString(const std::string &S) : DIEValue(isString), Str(S) {}
290 /// EmitValue - Emit string value.
292 virtual void EmitValue(Dwarf *D, unsigned Form) const;
294 /// SizeOf - Determine size of string value in bytes.
296 virtual unsigned SizeOf(const TargetData *, unsigned /*Form*/) const {
297 return Str.size() + sizeof(char); // sizeof('\0');
300 // Implement isa/cast/dyncast.
301 static bool classof(const DIEString *) { return true; }
302 static bool classof(const DIEValue *S) { return S->getType() == isString; }
305 virtual void print(raw_ostream &O);
309 //===--------------------------------------------------------------------===//
310 /// DIEDwarfLabel - A Dwarf internal label expression DIE.
312 class DIEDwarfLabel : public DIEValue {
315 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
317 /// EmitValue - Emit label value.
319 virtual void EmitValue(Dwarf *D, unsigned Form) const;
321 /// SizeOf - Determine size of label value in bytes.
323 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
325 // Implement isa/cast/dyncast.
326 static bool classof(const DIEDwarfLabel *) { return true; }
327 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
330 virtual void print(raw_ostream &O);
334 //===--------------------------------------------------------------------===//
335 /// DIEObjectLabel - A label to an object in code or data.
337 class DIEObjectLabel : public DIEValue {
338 const std::string Label;
340 explicit DIEObjectLabel(const std::string &L)
341 : DIEValue(isAsIsLabel), Label(L) {}
343 /// EmitValue - Emit label value.
345 virtual void EmitValue(Dwarf *D, unsigned Form) const;
347 /// SizeOf - Determine size of label value in bytes.
349 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
351 // Implement isa/cast/dyncast.
352 static bool classof(const DIEObjectLabel *) { return true; }
353 static bool classof(const DIEValue *L) {
354 return L->getType() == isAsIsLabel;
358 virtual void print(raw_ostream &O);
362 //===--------------------------------------------------------------------===//
363 /// DIESectionOffset - A section offset DIE.
365 class DIESectionOffset : public DIEValue {
367 const DWLabel Section;
371 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
372 bool isEH = false, bool useSet = true)
373 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
374 IsEH(isEH), UseSet(useSet) {}
376 /// EmitValue - Emit section offset.
378 virtual void EmitValue(Dwarf *D, unsigned Form) const;
380 /// SizeOf - Determine size of section offset value in bytes.
382 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
384 // Implement isa/cast/dyncast.
385 static bool classof(const DIESectionOffset *) { return true; }
386 static bool classof(const DIEValue *D) {
387 return D->getType() == isSectionOffset;
391 virtual void print(raw_ostream &O);
395 //===--------------------------------------------------------------------===//
396 /// DIEDelta - A simple label difference DIE.
398 class DIEDelta : public DIEValue {
399 const DWLabel LabelHi;
400 const DWLabel LabelLo;
402 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
403 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
405 /// EmitValue - Emit delta value.
407 virtual void EmitValue(Dwarf *D, unsigned Form) const;
409 /// SizeOf - Determine size of delta value in bytes.
411 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
413 // Implement isa/cast/dyncast.
414 static bool classof(const DIEDelta *) { return true; }
415 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
418 virtual void print(raw_ostream &O);
422 //===--------------------------------------------------------------------===//
423 /// DIEntry - A pointer to another debug information entry. An instance of
424 /// this class can also be used as a proxy for a debug information entry not
425 /// yet defined (ie. types.)
426 class DIEEntry : public DIEValue {
429 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
431 DIE *getEntry() const { return Entry; }
432 void setEntry(DIE *E) { Entry = E; }
434 /// EmitValue - Emit debug information entry offset.
436 virtual void EmitValue(Dwarf *D, unsigned Form) const;
438 /// SizeOf - Determine size of debug information entry in bytes.
440 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const {
441 return sizeof(int32_t);
444 // Implement isa/cast/dyncast.
445 static bool classof(const DIEEntry *) { return true; }
446 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
449 virtual void print(raw_ostream &O);
453 //===--------------------------------------------------------------------===//
454 /// DIEBlock - A block of values. Primarily used for location expressions.
456 class DIEBlock : public DIEValue, public DIE {
457 unsigned Size; // Size in bytes excluding size header.
460 : DIEValue(isBlock), DIE(0), Size(0) {}
461 virtual ~DIEBlock() {}
463 /// ComputeSize - calculate the size of the block.
465 unsigned ComputeSize(const TargetData *TD);
467 /// BestForm - Choose the best form for data.
469 unsigned BestForm() const {
470 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
471 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
472 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
473 return dwarf::DW_FORM_block;
476 /// EmitValue - Emit block data.
478 virtual void EmitValue(Dwarf *D, unsigned Form) const;
480 /// SizeOf - Determine size of block data in bytes.
482 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
484 // Implement isa/cast/dyncast.
485 static bool classof(const DIEBlock *) { return true; }
486 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
489 virtual void print(raw_ostream &O);
493 } // end llvm namespace