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"
30 //===--------------------------------------------------------------------===//
31 /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
32 /// Dwarf abbreviation.
34 /// Attribute - Dwarf attribute code.
38 /// Form - Dwarf form code.
42 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
45 unsigned getAttribute() const { return Attribute; }
46 unsigned getForm() const { return Form; }
48 /// Profile - Used to gather unique data for the abbreviation folding set.
50 void Profile(FoldingSetNodeID &ID) const;
53 //===--------------------------------------------------------------------===//
54 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
55 /// information object.
56 class DIEAbbrev : public FoldingSetNode {
57 /// Tag - Dwarf tag code.
61 /// Unique number for node.
65 /// ChildrenFlag - Dwarf children flag.
67 unsigned ChildrenFlag;
69 /// Data - Raw data bytes for abbreviation.
71 SmallVector<DIEAbbrevData, 8> Data;
74 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
75 virtual ~DIEAbbrev() {}
78 unsigned getTag() const { return Tag; }
79 unsigned getNumber() const { return Number; }
80 unsigned getChildrenFlag() const { return ChildrenFlag; }
81 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
82 void setTag(unsigned T) { Tag = T; }
83 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
84 void setNumber(unsigned N) { Number = N; }
86 /// AddAttribute - Adds another set of attribute information to the
88 void AddAttribute(unsigned Attribute, unsigned Form) {
89 Data.push_back(DIEAbbrevData(Attribute, Form));
92 /// AddFirstAttribute - Adds a set of attribute information to the front
93 /// of the abbreviation.
94 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
95 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
98 /// Profile - Used to gather unique data for the abbreviation folding set.
100 void Profile(FoldingSetNodeID &ID) const;
102 /// Emit - Print the abbreviation using the specified asm printer.
104 void Emit(const DwarfPrinter *DP) const;
107 void print(raw_ostream &O);
112 //===--------------------------------------------------------------------===//
113 /// DIE - A structured debug information entry. Has an abbreviation which
114 /// describes it's organization.
119 /// Abbrev - Buffer for constructing abbreviation.
123 /// Offset - Offset in debug info section.
127 /// Size - Size of instance + children.
133 std::vector<DIE *> Children;
137 /// Attributes values.
139 SmallVector<DIEValue*, 32> Values;
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), Parent (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 DIE *getParent() const { return Parent; }
158 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
159 void setOffset(unsigned O) { Offset = O; }
160 void setSize(unsigned S) { Size = S; }
162 /// addValue - Add a value and attributes to a DIE.
164 void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
165 Abbrev.AddAttribute(Attribute, Form);
166 Values.push_back(Value);
169 /// SiblingOffset - Return the offset of the debug information entry's
171 unsigned getSiblingOffset() const { return Offset + Size; }
173 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
175 void addSiblingOffset();
177 /// addChild - Add a child to the DIE.
179 void addChild(DIE *Child) {
180 if (Child->getParent()) {
181 assert (Child->getParent() == this && "Unexpected DIE Parent!");
184 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
185 Children.push_back(Child);
186 Child->Parent = this;
190 void print(raw_ostream &O, unsigned IncIndent = 0);
195 //===--------------------------------------------------------------------===//
196 /// DIEValue - A debug information entry value.
211 /// Type - Type of data stored in the value.
215 explicit DIEValue(unsigned T) : Type(T) {}
216 virtual ~DIEValue() {}
219 unsigned getType() const { return Type; }
221 /// EmitValue - Emit value via the Dwarf writer.
223 virtual void EmitValue(DwarfPrinter *D, unsigned Form) const = 0;
225 /// SizeOf - Return the size of a value in bytes.
227 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const = 0;
229 // Implement isa/cast/dyncast.
230 static bool classof(const DIEValue *) { return true; }
233 virtual void print(raw_ostream &O) = 0;
238 //===--------------------------------------------------------------------===//
239 /// DIEInteger - An integer value DIE.
241 class DIEInteger : public DIEValue {
244 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
246 /// BestForm - Choose the best form for integer.
248 static unsigned BestForm(bool IsSigned, uint64_t Int) {
250 if ((char)Int == (signed)Int) return dwarf::DW_FORM_data1;
251 if ((short)Int == (signed)Int) return dwarf::DW_FORM_data2;
252 if ((int)Int == (signed)Int) return dwarf::DW_FORM_data4;
254 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
255 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
256 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
258 return dwarf::DW_FORM_data8;
261 /// EmitValue - Emit integer of appropriate size.
263 virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
265 /// SizeOf - Determine size of integer value in bytes.
267 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
270 // Implement isa/cast/dyncast.
271 static bool classof(const DIEInteger *) { return true; }
272 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
275 virtual void print(raw_ostream &O);
279 //===--------------------------------------------------------------------===//
280 /// DIEString - A string value DIE. This DIE keeps string reference only.
282 class DIEString : public DIEValue {
285 explicit DIEString(const StringRef S) : DIEValue(isString), Str(S) {}
287 /// EmitValue - Emit string value.
289 virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
291 /// SizeOf - Determine size of string value in bytes.
293 virtual unsigned SizeOf(const TargetData *, unsigned /*Form*/) const {
294 return Str.size() + sizeof(char); // sizeof('\0');
297 // Implement isa/cast/dyncast.
298 static bool classof(const DIEString *) { return true; }
299 static bool classof(const DIEValue *S) { return S->getType() == isString; }
302 virtual void print(raw_ostream &O);
306 //===--------------------------------------------------------------------===//
307 /// DIEDwarfLabel - A Dwarf internal label expression DIE.
309 class DIEDwarfLabel : public DIEValue {
312 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
314 /// EmitValue - Emit label value.
316 virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
318 /// SizeOf - Determine size of label value in bytes.
320 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
322 // Implement isa/cast/dyncast.
323 static bool classof(const DIEDwarfLabel *) { return true; }
324 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
327 virtual void print(raw_ostream &O);
331 //===--------------------------------------------------------------------===//
332 /// DIEObjectLabel - A label to an object in code or data.
334 class DIEObjectLabel : public DIEValue {
337 explicit DIEObjectLabel(const MCSymbol *S)
338 : DIEValue(isAsIsLabel), Sym(S) {}
340 /// EmitValue - Emit label value.
342 virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
344 /// SizeOf - Determine size of label value in bytes.
346 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
348 // Implement isa/cast/dyncast.
349 static bool classof(const DIEObjectLabel *) { return true; }
350 static bool classof(const DIEValue *L) {
351 return L->getType() == isAsIsLabel;
355 virtual void print(raw_ostream &O);
359 //===--------------------------------------------------------------------===//
360 /// DIESectionOffset - A section offset DIE.
362 class DIESectionOffset : public DIEValue {
364 const DWLabel Section;
368 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
369 bool isEH = false, bool useSet = true)
370 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
371 IsEH(isEH), UseSet(useSet) {}
373 /// EmitValue - Emit section offset.
375 virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
377 /// SizeOf - Determine size of section offset value in bytes.
379 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
381 // Implement isa/cast/dyncast.
382 static bool classof(const DIESectionOffset *) { return true; }
383 static bool classof(const DIEValue *D) {
384 return D->getType() == isSectionOffset;
388 virtual void print(raw_ostream &O);
392 //===--------------------------------------------------------------------===//
393 /// DIEDelta - A simple label difference DIE.
395 class DIEDelta : public DIEValue {
396 const DWLabel LabelHi;
397 const DWLabel LabelLo;
399 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
400 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
402 /// EmitValue - Emit delta value.
404 virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
406 /// SizeOf - Determine size of delta value in bytes.
408 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
410 // Implement isa/cast/dyncast.
411 static bool classof(const DIEDelta *) { return true; }
412 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
415 virtual void print(raw_ostream &O);
419 //===--------------------------------------------------------------------===//
420 /// DIEntry - A pointer to another debug information entry. An instance of
421 /// this class can also be used as a proxy for a debug information entry not
422 /// yet defined (ie. types.)
423 class DIEEntry : public DIEValue {
426 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
428 DIE *getEntry() const { return Entry; }
429 void setEntry(DIE *E) { Entry = E; }
431 /// EmitValue - Emit debug information entry offset.
433 virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
435 /// SizeOf - Determine size of debug information entry in bytes.
437 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const {
438 return sizeof(int32_t);
441 // Implement isa/cast/dyncast.
442 static bool classof(const DIEEntry *) { return true; }
443 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
446 virtual void print(raw_ostream &O);
450 //===--------------------------------------------------------------------===//
451 /// DIEBlock - A block of values. Primarily used for location expressions.
453 class DIEBlock : public DIEValue, public DIE {
454 unsigned Size; // Size in bytes excluding size header.
457 : DIEValue(isBlock), DIE(0), Size(0) {}
458 virtual ~DIEBlock() {}
460 /// ComputeSize - calculate the size of the block.
462 unsigned ComputeSize(const TargetData *TD);
464 /// BestForm - Choose the best form for data.
466 unsigned BestForm() const {
467 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
468 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
469 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
470 return dwarf::DW_FORM_block;
473 /// EmitValue - Emit block data.
475 virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
477 /// SizeOf - Determine size of block data in bytes.
479 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
481 // Implement isa/cast/dyncast.
482 static bool classof(const DIEBlock *) { return true; }
483 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
486 virtual void print(raw_ostream &O);
490 } // end llvm namespace