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 "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Dwarf.h"
28 //===--------------------------------------------------------------------===//
29 /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
30 /// Dwarf abbreviation.
32 /// Attribute - Dwarf attribute code.
36 /// Form - Dwarf form code.
40 DIEAbbrevData(unsigned A
, unsigned F
) : Attribute(A
), Form(F
) {}
43 unsigned getAttribute() const { return Attribute
; }
44 unsigned getForm() const { return Form
; }
46 /// Profile - Used to gather unique data for the abbreviation folding set.
48 void Profile(FoldingSetNodeID
&ID
) const;
51 //===--------------------------------------------------------------------===//
52 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
53 /// information object.
54 class DIEAbbrev
: public FoldingSetNode
{
55 /// Tag - Dwarf tag code.
59 /// Unique number for node.
63 /// ChildrenFlag - Dwarf children flag.
65 unsigned ChildrenFlag
;
67 /// Data - Raw data bytes for abbreviation.
69 SmallVector
<DIEAbbrevData
, 8> Data
;
72 DIEAbbrev(unsigned T
, unsigned C
) : Tag(T
), ChildrenFlag(C
), Data() {}
75 unsigned getTag() const { return Tag
; }
76 unsigned getNumber() const { return Number
; }
77 unsigned getChildrenFlag() const { return ChildrenFlag
; }
78 const SmallVector
<DIEAbbrevData
, 8> &getData() const { return Data
; }
79 void setTag(unsigned T
) { Tag
= T
; }
80 void setChildrenFlag(unsigned CF
) { ChildrenFlag
= CF
; }
81 void setNumber(unsigned N
) { Number
= N
; }
83 /// AddAttribute - Adds another set of attribute information to the
85 void AddAttribute(unsigned Attribute
, unsigned Form
) {
86 Data
.push_back(DIEAbbrevData(Attribute
, Form
));
89 /// AddFirstAttribute - Adds a set of attribute information to the front
90 /// of the abbreviation.
91 void AddFirstAttribute(unsigned Attribute
, unsigned Form
) {
92 Data
.insert(Data
.begin(), DIEAbbrevData(Attribute
, Form
));
95 /// Profile - Used to gather unique data for the abbreviation folding set.
97 void Profile(FoldingSetNodeID
&ID
) const;
99 /// Emit - Print the abbreviation using the specified asm printer.
101 void Emit(AsmPrinter
*AP
) const;
104 void print(raw_ostream
&O
);
109 //===--------------------------------------------------------------------===//
110 /// DIE - A structured debug information entry. Has an abbreviation which
111 /// describes it's organization.
116 /// Abbrev - Buffer for constructing abbreviation.
120 /// Offset - Offset in debug info section.
124 /// Size - Size of instance + children.
130 std::vector
<DIE
*> Children
;
134 /// Attributes values.
136 SmallVector
<DIEValue
*, 32> Values
;
138 // Private data for print()
139 mutable unsigned IndentCount
;
141 explicit DIE(unsigned Tag
)
142 : Abbrev(Tag
, dwarf::DW_CHILDREN_no
), Offset(0),
143 Size(0), Parent (0), IndentCount(0) {}
147 DIEAbbrev
&getAbbrev() { return Abbrev
; }
148 unsigned getAbbrevNumber() const { return Abbrev
.getNumber(); }
149 unsigned getTag() const { return Abbrev
.getTag(); }
150 unsigned getOffset() const { return Offset
; }
151 unsigned getSize() const { return Size
; }
152 const std::vector
<DIE
*> &getChildren() const { return Children
; }
153 const SmallVector
<DIEValue
*, 32> &getValues() const { return Values
; }
154 DIE
*getParent() const { return Parent
; }
155 void setTag(unsigned Tag
) { Abbrev
.setTag(Tag
); }
156 void setOffset(unsigned O
) { Offset
= O
; }
157 void setSize(unsigned S
) { Size
= S
; }
159 /// addValue - Add a value and attributes to a DIE.
161 void addValue(unsigned Attribute
, unsigned Form
, DIEValue
*Value
) {
162 Abbrev
.AddAttribute(Attribute
, Form
);
163 Values
.push_back(Value
);
166 /// SiblingOffset - Return the offset of the debug information entry's
168 unsigned getSiblingOffset() const { return Offset
+ Size
; }
170 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
171 /// The caller is responsible for deleting the return value at or after the
172 /// same time it destroys this DIE.
174 DIEValue
*addSiblingOffset(BumpPtrAllocator
&A
);
176 /// addChild - Add a child to the DIE.
178 void addChild(DIE
*Child
) {
179 if (Child
->getParent()) {
180 assert (Child
->getParent() == this && "Unexpected DIE Parent!");
183 Abbrev
.setChildrenFlag(dwarf::DW_CHILDREN_yes
);
184 Children
.push_back(Child
);
185 Child
->Parent
= this;
189 void print(raw_ostream
&O
, unsigned IncIndent
= 0);
194 //===--------------------------------------------------------------------===//
195 /// DIEValue - A debug information entry value.
209 /// Type - Type of data stored in the value.
213 explicit DIEValue(unsigned T
) : Type(T
) {}
214 virtual ~DIEValue() {}
217 unsigned getType() const { return Type
; }
219 /// EmitValue - Emit value via the Dwarf writer.
221 virtual void EmitValue(AsmPrinter
*AP
, unsigned Form
) const = 0;
223 /// SizeOf - Return the size of a value in bytes.
225 virtual unsigned SizeOf(AsmPrinter
*AP
, unsigned Form
) const = 0;
227 // Implement isa/cast/dyncast.
228 static bool classof(const DIEValue
*) { return true; }
231 virtual void print(raw_ostream
&O
) = 0;
236 //===--------------------------------------------------------------------===//
237 /// DIEInteger - An integer value DIE.
239 class DIEInteger
: public DIEValue
{
242 explicit DIEInteger(uint64_t I
) : DIEValue(isInteger
), Integer(I
) {}
244 /// BestForm - Choose the best form for integer.
246 static unsigned BestForm(bool IsSigned
, uint64_t Int
) {
248 if ((char)Int
== (signed)Int
) return dwarf::DW_FORM_data1
;
249 if ((short)Int
== (signed)Int
) return dwarf::DW_FORM_data2
;
250 if ((int)Int
== (signed)Int
) return dwarf::DW_FORM_data4
;
252 if ((unsigned char)Int
== Int
) return dwarf::DW_FORM_data1
;
253 if ((unsigned short)Int
== Int
) return dwarf::DW_FORM_data2
;
254 if ((unsigned int)Int
== Int
) return dwarf::DW_FORM_data4
;
256 return dwarf::DW_FORM_data8
;
259 /// EmitValue - Emit integer of appropriate size.
261 virtual void EmitValue(AsmPrinter
*AP
, unsigned Form
) const;
263 uint64_t getValue() const { return Integer
; }
265 /// SizeOf - Determine size of integer value in bytes.
267 virtual unsigned SizeOf(AsmPrinter
*AP
, unsigned Form
) const;
269 // Implement isa/cast/dyncast.
270 static bool classof(const DIEInteger
*) { return true; }
271 static bool classof(const DIEValue
*I
) { return I
->getType() == isInteger
; }
274 virtual void print(raw_ostream
&O
);
278 //===--------------------------------------------------------------------===//
279 /// DIEString - A string value DIE. This DIE keeps string reference only.
281 class DIEString
: public DIEValue
{
284 explicit DIEString(const StringRef S
) : DIEValue(isString
), Str(S
) {}
286 /// EmitValue - Emit string value.
288 virtual void EmitValue(AsmPrinter
*AP
, unsigned Form
) const;
290 /// SizeOf - Determine size of string value in bytes.
292 virtual unsigned SizeOf(AsmPrinter
*AP
, unsigned /*Form*/) const {
293 return Str
.size() + sizeof(char); // sizeof('\0');
296 // Implement isa/cast/dyncast.
297 static bool classof(const DIEString
*) { return true; }
298 static bool classof(const DIEValue
*S
) { return S
->getType() == isString
; }
301 virtual void print(raw_ostream
&O
);
305 //===--------------------------------------------------------------------===//
306 /// DIELabel - A label expression DIE.
308 class DIELabel
: public DIEValue
{
309 const MCSymbol
*Label
;
311 explicit DIELabel(const MCSymbol
*L
) : DIEValue(isLabel
), Label(L
) {}
313 /// EmitValue - Emit label value.
315 virtual void EmitValue(AsmPrinter
*AP
, unsigned Form
) const;
317 /// getValue - Get MCSymbol.
319 const MCSymbol
*getValue() const { return Label
; }
321 /// SizeOf - Determine size of label value in bytes.
323 virtual unsigned SizeOf(AsmPrinter
*AP
, unsigned Form
) const;
325 // Implement isa/cast/dyncast.
326 static bool classof(const DIELabel
*) { return true; }
327 static bool classof(const DIEValue
*L
) { return L
->getType() == isLabel
; }
330 virtual void print(raw_ostream
&O
);
334 //===--------------------------------------------------------------------===//
335 /// DIEDelta - A simple label difference DIE.
337 class DIEDelta
: public DIEValue
{
338 const MCSymbol
*LabelHi
;
339 const MCSymbol
*LabelLo
;
341 DIEDelta(const MCSymbol
*Hi
, const MCSymbol
*Lo
)
342 : DIEValue(isDelta
), LabelHi(Hi
), LabelLo(Lo
) {}
344 /// EmitValue - Emit delta value.
346 virtual void EmitValue(AsmPrinter
*AP
, unsigned Form
) const;
348 /// SizeOf - Determine size of delta value in bytes.
350 virtual unsigned SizeOf(AsmPrinter
*AP
, unsigned Form
) const;
352 // Implement isa/cast/dyncast.
353 static bool classof(const DIEDelta
*) { return true; }
354 static bool classof(const DIEValue
*D
) { return D
->getType() == isDelta
; }
357 virtual void print(raw_ostream
&O
);
361 //===--------------------------------------------------------------------===//
362 /// DIEntry - A pointer to another debug information entry. An instance of
363 /// this class can also be used as a proxy for a debug information entry not
364 /// yet defined (ie. types.)
365 class DIEEntry
: public DIEValue
{
368 explicit DIEEntry(DIE
*E
) : DIEValue(isEntry
), Entry(E
) {}
370 DIE
*getEntry() const { return Entry
; }
372 /// EmitValue - Emit debug information entry offset.
374 virtual void EmitValue(AsmPrinter
*AP
, unsigned Form
) const;
376 /// SizeOf - Determine size of debug information entry in bytes.
378 virtual unsigned SizeOf(AsmPrinter
*AP
, unsigned Form
) const {
379 return sizeof(int32_t);
382 // Implement isa/cast/dyncast.
383 static bool classof(const DIEEntry
*) { return true; }
384 static bool classof(const DIEValue
*E
) { return E
->getType() == isEntry
; }
387 virtual void print(raw_ostream
&O
);
391 //===--------------------------------------------------------------------===//
392 /// DIEBlock - A block of values. Primarily used for location expressions.
394 class DIEBlock
: public DIEValue
, public DIE
{
395 unsigned Size
; // Size in bytes excluding size header.
398 : DIEValue(isBlock
), DIE(0), Size(0) {}
399 virtual ~DIEBlock() {}
401 /// ComputeSize - calculate the size of the block.
403 unsigned ComputeSize(AsmPrinter
*AP
);
405 /// BestForm - Choose the best form for data.
407 unsigned BestForm() const {
408 if ((unsigned char)Size
== Size
) return dwarf::DW_FORM_block1
;
409 if ((unsigned short)Size
== Size
) return dwarf::DW_FORM_block2
;
410 if ((unsigned int)Size
== Size
) return dwarf::DW_FORM_block4
;
411 return dwarf::DW_FORM_block
;
414 /// EmitValue - Emit block data.
416 virtual void EmitValue(AsmPrinter
*AP
, unsigned Form
) const;
418 /// SizeOf - Determine size of block data in bytes.
420 virtual unsigned SizeOf(AsmPrinter
*AP
, unsigned Form
) const;
422 // Implement isa/cast/dyncast.
423 static bool classof(const DIEBlock
*) { return true; }
424 static bool classof(const DIEValue
*E
) { return E
->getType() == isBlock
; }
427 virtual void print(raw_ostream
&O
);
431 } // end llvm namespace