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() {}
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(AsmPrinter *AP) const;
105 void print(raw_ostream &O);
110 //===--------------------------------------------------------------------===//
111 /// DIE - A structured debug information entry. Has an abbreviation which
112 /// describes it's organization.
117 /// Abbrev - Buffer for constructing abbreviation.
121 /// Offset - Offset in debug info section.
125 /// Size - Size of instance + children.
131 std::vector<DIE *> Children;
135 /// Attributes values.
137 SmallVector<DIEValue*, 32> Values;
139 // Private data for print()
140 mutable unsigned IndentCount;
142 explicit DIE(unsigned Tag)
143 : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0),
144 Size(0), Parent (0), IndentCount(0) {}
148 DIEAbbrev &getAbbrev() { return Abbrev; }
149 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
150 unsigned getTag() const { return Abbrev.getTag(); }
151 unsigned getOffset() const { return Offset; }
152 unsigned getSize() const { return Size; }
153 const std::vector<DIE *> &getChildren() const { return Children; }
154 const SmallVector<DIEValue*, 32> &getValues() const { return Values; }
155 DIE *getParent() const { return Parent; }
156 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
157 void setOffset(unsigned O) { Offset = O; }
158 void setSize(unsigned S) { Size = S; }
160 /// addValue - Add a value and attributes to a DIE.
162 void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
163 Abbrev.AddAttribute(Attribute, Form);
164 Values.push_back(Value);
167 /// SiblingOffset - Return the offset of the debug information entry's
169 unsigned getSiblingOffset() const { return Offset + Size; }
171 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
172 /// The caller is responsible for deleting the return value at or after the
173 /// same time it destroys this DIE.
175 DIEValue *addSiblingOffset(BumpPtrAllocator &A);
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.
210 /// Type - Type of data stored in the value.
214 explicit DIEValue(unsigned T) : Type(T) {}
215 virtual ~DIEValue() {}
218 unsigned getType() const { return Type; }
220 /// EmitValue - Emit value via the Dwarf writer.
222 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const = 0;
224 /// SizeOf - Return the size of a value in bytes.
226 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const = 0;
228 // Implement isa/cast/dyncast.
229 static bool classof(const DIEValue *) { return true; }
232 virtual void print(raw_ostream &O) = 0;
237 //===--------------------------------------------------------------------===//
238 /// DIEInteger - An integer value DIE.
240 class DIEInteger : public DIEValue {
243 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
245 /// BestForm - Choose the best form for integer.
247 static unsigned BestForm(bool IsSigned, uint64_t Int) {
249 if ((char)Int == (signed)Int) return dwarf::DW_FORM_data1;
250 if ((short)Int == (signed)Int) return dwarf::DW_FORM_data2;
251 if ((int)Int == (signed)Int) return dwarf::DW_FORM_data4;
253 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
254 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
255 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
257 return dwarf::DW_FORM_data8;
260 /// EmitValue - Emit integer of appropriate size.
262 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
264 uint64_t getValue() const { return Integer; }
266 /// SizeOf - Determine size of integer value in bytes.
268 virtual unsigned SizeOf(AsmPrinter *AP, 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(AsmPrinter *AP, unsigned Form) const;
291 /// SizeOf - Determine size of string value in bytes.
293 virtual unsigned SizeOf(AsmPrinter *AP, 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 /// DIELabel - A label expression DIE.
309 class DIELabel : public DIEValue {
310 const MCSymbol *Label;
312 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
314 /// EmitValue - Emit label value.
316 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
318 /// getValue - Get MCSymbol.
320 const MCSymbol *getValue() const { return Label; }
322 /// SizeOf - Determine size of label value in bytes.
324 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
326 // Implement isa/cast/dyncast.
327 static bool classof(const DIELabel *) { return true; }
328 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
331 virtual void print(raw_ostream &O);
335 //===--------------------------------------------------------------------===//
336 /// DIEDelta - A simple label difference DIE.
338 class DIEDelta : public DIEValue {
339 const MCSymbol *LabelHi;
340 const MCSymbol *LabelLo;
342 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
343 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
345 /// EmitValue - Emit delta value.
347 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
349 /// SizeOf - Determine size of delta value in bytes.
351 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
353 // Implement isa/cast/dyncast.
354 static bool classof(const DIEDelta *) { return true; }
355 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
358 virtual void print(raw_ostream &O);
362 //===--------------------------------------------------------------------===//
363 /// DIEntry - A pointer to another debug information entry. An instance of
364 /// this class can also be used as a proxy for a debug information entry not
365 /// yet defined (ie. types.)
366 class DIEEntry : public DIEValue {
369 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
371 DIE *getEntry() const { return Entry; }
373 /// EmitValue - Emit debug information entry offset.
375 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
377 /// SizeOf - Determine size of debug information entry in bytes.
379 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const {
380 return sizeof(int32_t);
383 // Implement isa/cast/dyncast.
384 static bool classof(const DIEEntry *) { return true; }
385 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
388 virtual void print(raw_ostream &O);
392 //===--------------------------------------------------------------------===//
393 /// DIEBlock - A block of values. Primarily used for location expressions.
395 class DIEBlock : public DIEValue, public DIE {
396 unsigned Size; // Size in bytes excluding size header.
399 : DIEValue(isBlock), DIE(0), Size(0) {}
400 virtual ~DIEBlock() {}
402 /// ComputeSize - calculate the size of the block.
404 unsigned ComputeSize(AsmPrinter *AP);
406 /// BestForm - Choose the best form for data.
408 unsigned BestForm() const {
409 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
410 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
411 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
412 return dwarf::DW_FORM_block;
415 /// EmitValue - Emit block data.
417 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
419 /// SizeOf - Determine size of block data in bytes.
421 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
423 // Implement isa/cast/dyncast.
424 static bool classof(const DIEBlock *) { return true; }
425 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
428 virtual void print(raw_ostream &O);
432 } // end llvm namespace