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(uint16_t A, uint16_t F) : Attribute(A), Form(F) {}
43 uint16_t getAttribute() const { return Attribute; }
44 uint16_t 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 /// ChildrenFlag - Dwarf children flag.
61 uint16_t ChildrenFlag;
63 /// Unique number for node.
67 /// Data - Raw data bytes for abbreviation.
69 SmallVector<DIEAbbrevData, 12> Data;
72 DIEAbbrev(uint16_t T, uint16_t C) : Tag(T), ChildrenFlag(C), Data() {}
75 uint16_t getTag() const { return Tag; }
76 unsigned getNumber() const { return Number; }
77 uint16_t getChildrenFlag() const { return ChildrenFlag; }
78 const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
79 void setTag(uint16_t T) { Tag = T; }
80 void setChildrenFlag(uint16_t CF) { ChildrenFlag = CF; }
81 void setNumber(unsigned N) { Number = N; }
83 /// AddAttribute - Adds another set of attribute information to the
85 void AddAttribute(uint16_t Attribute, uint16_t 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(uint16_t Attribute, uint16_t 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 its organization.
116 /// Offset - Offset in debug info section.
120 /// Size - Size of instance + children.
124 /// Abbrev - Buffer for constructing abbreviation.
130 std::vector<DIE *> Children;
134 /// Attribute values.
136 SmallVector<DIEValue*, 12> Values;
138 // Private data for print()
139 mutable unsigned IndentCount;
141 explicit DIE(unsigned Tag)
142 : Offset(0), Size(0), Abbrev(Tag, dwarf::DW_CHILDREN_no), Parent(0) {}
146 DIEAbbrev &getAbbrev() { return Abbrev; }
147 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
148 unsigned getTag() const { return Abbrev.getTag(); }
149 unsigned getOffset() const { return Offset; }
150 unsigned getSize() const { return Size; }
151 const std::vector<DIE *> &getChildren() const { return Children; }
152 const SmallVectorImpl<DIEValue*> &getValues() const { return Values; }
153 DIE *getParent() const { return Parent; }
154 /// Climb up the parent chain to get the compile unit DIE this DIE belongs
156 DIE *getCompileUnit();
157 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
158 void setOffset(unsigned O) { Offset = O; }
159 void setSize(unsigned S) { Size = S; }
161 /// addValue - Add a value and attributes to a DIE.
163 void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
164 Abbrev.AddAttribute(Attribute, Form);
165 Values.push_back(Value);
168 /// addChild - Add a child to the DIE.
170 void addChild(DIE *Child) {
171 if (Child->getParent()) {
172 assert (Child->getParent() == this && "Unexpected DIE Parent!");
175 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
176 Children.push_back(Child);
177 Child->Parent = this;
181 void print(raw_ostream &O, unsigned IndentCount = 0) const;
186 //===--------------------------------------------------------------------===//
187 /// DIEValue - A debug information entry value.
190 virtual void anchor();
201 /// Type - Type of data stored in the value.
205 explicit DIEValue(unsigned T) : Type(T) {}
206 virtual ~DIEValue() {}
209 unsigned getType() const { return Type; }
211 /// EmitValue - Emit value via the Dwarf writer.
213 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const = 0;
215 /// SizeOf - Return the size of a value in bytes.
217 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const = 0;
220 virtual void print(raw_ostream &O) = 0;
225 //===--------------------------------------------------------------------===//
226 /// DIEInteger - An integer value DIE.
228 class DIEInteger : public DIEValue {
231 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
233 /// BestForm - Choose the best form for integer.
235 static unsigned BestForm(bool IsSigned, uint64_t Int) {
237 const int64_t SignedInt = Int;
238 if ((char)Int == SignedInt) return dwarf::DW_FORM_data1;
239 if ((short)Int == SignedInt) return dwarf::DW_FORM_data2;
240 if ((int)Int == SignedInt) return dwarf::DW_FORM_data4;
242 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
243 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
244 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
246 return dwarf::DW_FORM_data8;
249 /// EmitValue - Emit integer of appropriate size.
251 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
253 uint64_t getValue() const { return Integer; }
255 /// SizeOf - Determine size of integer value in bytes.
257 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
259 // Implement isa/cast/dyncast.
260 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
263 virtual void print(raw_ostream &O);
267 //===--------------------------------------------------------------------===//
268 /// DIELabel - A label expression DIE.
270 class DIELabel : public DIEValue {
271 const MCSymbol *Label;
273 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
275 /// EmitValue - Emit label value.
277 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
279 /// getValue - Get MCSymbol.
281 const MCSymbol *getValue() const { return Label; }
283 /// SizeOf - Determine size of label value in bytes.
285 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
287 // Implement isa/cast/dyncast.
288 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
291 virtual void print(raw_ostream &O);
295 //===--------------------------------------------------------------------===//
296 /// DIEDelta - A simple label difference DIE.
298 class DIEDelta : public DIEValue {
299 const MCSymbol *LabelHi;
300 const MCSymbol *LabelLo;
302 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
303 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
305 /// EmitValue - Emit delta value.
307 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
309 /// SizeOf - Determine size of delta value in bytes.
311 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
313 // Implement isa/cast/dyncast.
314 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
317 virtual void print(raw_ostream &O);
321 //===--------------------------------------------------------------------===//
322 /// DIEEntry - A pointer to another debug information entry. An instance of
323 /// this class can also be used as a proxy for a debug information entry not
324 /// yet defined (ie. types.)
325 class DIEEntry : public DIEValue {
328 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
329 assert(E && "Cannot construct a DIEEntry with a null DIE");
332 DIE *getEntry() const { return Entry; }
334 /// EmitValue - Emit debug information entry offset.
336 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
338 /// SizeOf - Determine size of debug information entry in bytes.
340 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const {
341 return sizeof(int32_t);
344 // Implement isa/cast/dyncast.
345 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
348 virtual void print(raw_ostream &O);
352 //===--------------------------------------------------------------------===//
353 /// DIEBlock - A block of values. Primarily used for location expressions.
355 class DIEBlock : public DIEValue, public DIE {
356 unsigned Size; // Size in bytes excluding size header.
359 : DIEValue(isBlock), DIE(0), Size(0) {}
360 virtual ~DIEBlock() {}
362 /// ComputeSize - calculate the size of the block.
364 unsigned ComputeSize(AsmPrinter *AP);
366 /// BestForm - Choose the best form for data.
368 unsigned BestForm() const {
369 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
370 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
371 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
372 return dwarf::DW_FORM_block;
375 /// EmitValue - Emit block data.
377 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
379 /// SizeOf - Determine size of block data in bytes.
381 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
383 // Implement isa/cast/dyncast.
384 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
387 virtual void print(raw_ostream &O);
391 } // end llvm namespace