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"
22 #include "llvm/Support/raw_ostream.h"
30 //===--------------------------------------------------------------------===//
31 /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
32 /// Dwarf abbreviation.
33 class VISIBILITY_HIDDEN DIEAbbrevData {
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 VISIBILITY_HIDDEN 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;
73 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
74 virtual ~DIEAbbrev() {}
77 unsigned getTag() const { return Tag; }
78 unsigned getNumber() const { return Number; }
79 unsigned getChildrenFlag() const { return ChildrenFlag; }
80 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
81 void setTag(unsigned T) { Tag = T; }
82 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
83 void setNumber(unsigned N) { Number = N; }
85 /// AddAttribute - Adds another set of attribute information to the
87 void AddAttribute(unsigned Attribute, unsigned Form) {
88 Data.push_back(DIEAbbrevData(Attribute, Form));
91 /// AddFirstAttribute - Adds a set of attribute information to the front
92 /// of the abbreviation.
93 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
94 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
97 /// Profile - Used to gather unique data for the abbreviation folding set.
99 void Profile(FoldingSetNodeID &ID) const;
101 /// Emit - Print the abbreviation using the specified asm printer.
103 void Emit(const AsmPrinter *Asm) const;
106 void print(std::ostream *O) {
109 void print(std::ostream &O);
114 //===--------------------------------------------------------------------===//
115 /// DIE - A structured debug information entry. Has an abbreviation which
116 /// describes it's organization.
120 class VISIBILITY_HIDDEN DIE : public FoldingSetNode {
122 /// Abbrev - Buffer for constructing abbreviation.
126 /// Offset - Offset in debug info section.
130 /// Size - Size of instance + children.
136 std::vector<DIE *> Children;
138 /// Attributes values.
140 SmallVector<DIEValue*, 32> Values;
142 /// Abstract compile unit.
143 CompileUnit *AbstractCU;
145 explicit DIE(unsigned Tag)
146 : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0), Size(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.
192 /// Profile - Used to gather unique data for the value folding set.
194 void Profile(FoldingSetNodeID &ID) ;
197 void print(std::ostream *O, unsigned IncIndent = 0) {
198 if (O) print(*O, IncIndent);
200 void print(std::ostream &O, unsigned IncIndent = 0);
205 //===--------------------------------------------------------------------===//
206 /// DIEValue - A debug information entry value.
208 class VISIBILITY_HIDDEN DIEValue : public FoldingSetNode {
221 /// Type - Type of data stored in the value.
225 explicit DIEValue(unsigned T) : Type(T) {}
226 virtual ~DIEValue() {}
229 unsigned getType() const { return Type; }
231 /// EmitValue - Emit value via the Dwarf writer.
233 virtual void EmitValue(Dwarf *D, unsigned Form) const = 0;
235 /// SizeOf - Return the size of a value in bytes.
237 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const = 0;
239 /// Profile - Used to gather unique data for the value folding set.
241 virtual void Profile(FoldingSetNodeID &ID) = 0;
243 // Implement isa/cast/dyncast.
244 static bool classof(const DIEValue *) { return true; }
247 void print(std::ostream *O) {
250 virtual void print(std::ostream &O) = 0;
255 //===--------------------------------------------------------------------===//
256 /// DIEInteger - An integer value DIE.
258 class VISIBILITY_HIDDEN DIEInteger : public DIEValue {
261 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
263 /// BestForm - Choose the best form for integer.
265 static unsigned BestForm(bool IsSigned, uint64_t Int) {
267 if ((char)Int == (signed)Int) return dwarf::DW_FORM_data1;
268 if ((short)Int == (signed)Int) return dwarf::DW_FORM_data2;
269 if ((int)Int == (signed)Int) return dwarf::DW_FORM_data4;
271 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
272 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
273 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
275 return dwarf::DW_FORM_data8;
278 /// EmitValue - Emit integer of appropriate size.
280 virtual void EmitValue(Dwarf *D, unsigned Form) const;
282 /// SizeOf - Determine size of integer value in bytes.
284 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
286 /// Profile - Used to gather unique data for the value folding set.
288 static void Profile(FoldingSetNodeID &ID, unsigned Int);
289 virtual void Profile(FoldingSetNodeID &ID);
291 // Implement isa/cast/dyncast.
292 static bool classof(const DIEInteger *) { return true; }
293 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
296 virtual void print(std::ostream &O);
300 //===--------------------------------------------------------------------===//
301 /// DIEString - A string value DIE.
303 class VISIBILITY_HIDDEN DIEString : public DIEValue {
304 const std::string Str;
306 explicit DIEString(const std::string &S) : DIEValue(isString), Str(S) {}
308 /// EmitValue - Emit string value.
310 virtual void EmitValue(Dwarf *D, unsigned Form) const;
312 /// SizeOf - Determine size of string value in bytes.
314 virtual unsigned SizeOf(const TargetData *, unsigned /*Form*/) const {
315 return Str.size() + sizeof(char); // sizeof('\0');
318 /// Profile - Used to gather unique data for the value folding set.
320 static void Profile(FoldingSetNodeID &ID, const std::string &Str);
321 virtual void Profile(FoldingSetNodeID &ID);
323 // Implement isa/cast/dyncast.
324 static bool classof(const DIEString *) { return true; }
325 static bool classof(const DIEValue *S) { return S->getType() == isString; }
328 virtual void print(std::ostream &O);
332 //===--------------------------------------------------------------------===//
333 /// DIEDwarfLabel - A Dwarf internal label expression DIE.
335 class VISIBILITY_HIDDEN DIEDwarfLabel : public DIEValue {
338 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
340 /// EmitValue - Emit label value.
342 virtual void EmitValue(Dwarf *D, unsigned Form) const;
344 /// SizeOf - Determine size of label value in bytes.
346 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
348 /// Profile - Used to gather unique data for the value folding set.
350 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label);
351 virtual void Profile(FoldingSetNodeID &ID);
353 // Implement isa/cast/dyncast.
354 static bool classof(const DIEDwarfLabel *) { return true; }
355 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
358 virtual void print(std::ostream &O);
362 //===--------------------------------------------------------------------===//
363 /// DIEObjectLabel - A label to an object in code or data.
365 class VISIBILITY_HIDDEN DIEObjectLabel : public DIEValue {
366 const std::string Label;
368 explicit DIEObjectLabel(const std::string &L)
369 : DIEValue(isAsIsLabel), Label(L) {}
371 /// EmitValue - Emit label value.
373 virtual void EmitValue(Dwarf *D, unsigned Form) const;
375 /// SizeOf - Determine size of label value in bytes.
377 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
379 /// Profile - Used to gather unique data for the value folding set.
381 static void Profile(FoldingSetNodeID &ID, const std::string &Label);
382 virtual void Profile(FoldingSetNodeID &ID);
384 // Implement isa/cast/dyncast.
385 static bool classof(const DIEObjectLabel *) { return true; }
386 static bool classof(const DIEValue *L) {
387 return L->getType() == isAsIsLabel;
391 virtual void print(std::ostream &O);
395 //===--------------------------------------------------------------------===//
396 /// DIESectionOffset - A section offset DIE.
398 class VISIBILITY_HIDDEN DIESectionOffset : public DIEValue {
400 const DWLabel Section;
404 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
405 bool isEH = false, bool useSet = true)
406 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
407 IsEH(isEH), UseSet(useSet) {}
409 /// EmitValue - Emit section offset.
411 virtual void EmitValue(Dwarf *D, unsigned Form) const;
413 /// SizeOf - Determine size of section offset value in bytes.
415 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
417 /// Profile - Used to gather unique data for the value folding set.
419 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
420 const DWLabel &Section);
421 virtual void Profile(FoldingSetNodeID &ID);
423 // Implement isa/cast/dyncast.
424 static bool classof(const DIESectionOffset *) { return true; }
425 static bool classof(const DIEValue *D) {
426 return D->getType() == isSectionOffset;
430 virtual void print(std::ostream &O);
434 //===--------------------------------------------------------------------===//
435 /// DIEDelta - A simple label difference DIE.
437 class VISIBILITY_HIDDEN DIEDelta : public DIEValue {
438 const DWLabel LabelHi;
439 const DWLabel LabelLo;
441 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
442 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
444 /// EmitValue - Emit delta value.
446 virtual void EmitValue(Dwarf *D, unsigned Form) const;
448 /// SizeOf - Determine size of delta value in bytes.
450 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
452 /// Profile - Used to gather unique data for the value folding set.
454 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
455 const DWLabel &LabelLo);
456 virtual void Profile(FoldingSetNodeID &ID);
458 // Implement isa/cast/dyncast.
459 static bool classof(const DIEDelta *) { return true; }
460 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
463 virtual void print(std::ostream &O);
467 //===--------------------------------------------------------------------===//
468 /// DIEntry - A pointer to another debug information entry. An instance of
469 /// this class can also be used as a proxy for a debug information entry not
470 /// yet defined (ie. types.)
471 class VISIBILITY_HIDDEN DIEEntry : public DIEValue {
474 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
476 DIE *getEntry() const { return Entry; }
477 void setEntry(DIE *E) { Entry = E; }
479 /// EmitValue - Emit debug information entry offset.
481 virtual void EmitValue(Dwarf *D, unsigned Form) const;
483 /// SizeOf - Determine size of debug information entry in bytes.
485 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const {
486 return sizeof(int32_t);
489 /// Profile - Used to gather unique data for the value folding set.
491 static void Profile(FoldingSetNodeID &ID, DIE *Entry);
492 virtual void Profile(FoldingSetNodeID &ID);
494 // Implement isa/cast/dyncast.
495 static bool classof(const DIEEntry *) { return true; }
496 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
499 virtual void print(std::ostream &O);
503 //===--------------------------------------------------------------------===//
504 /// DIEBlock - A block of values. Primarily used for location expressions.
506 class VISIBILITY_HIDDEN DIEBlock : public DIEValue, public DIE {
507 unsigned Size; // Size in bytes excluding size header.
510 : DIEValue(isBlock), DIE(0), Size(0) {}
511 virtual ~DIEBlock() {}
513 /// ComputeSize - calculate the size of the block.
515 unsigned ComputeSize(const TargetData *TD);
517 /// BestForm - Choose the best form for data.
519 unsigned BestForm() const {
520 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
521 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
522 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
523 return dwarf::DW_FORM_block;
526 /// EmitValue - Emit block data.
528 virtual void EmitValue(Dwarf *D, unsigned Form) const;
530 /// SizeOf - Determine size of block data in bytes.
532 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
534 /// Profile - Used to gather unique data for the value folding set.
536 virtual void Profile(FoldingSetNodeID &ID);
538 // Implement isa/cast/dyncast.
539 static bool classof(const DIEBlock *) { return true; }
540 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
543 virtual void print(std::ostream &O);
547 } // end llvm namespace