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 /// Profile - Used to gather unique data for the abbreviation folding set.
91 void Profile(FoldingSetNodeID &ID) const;
93 /// Emit - Print the abbreviation using the specified asm printer.
95 void Emit(AsmPrinter *AP) const;
98 void print(raw_ostream &O);
103 //===--------------------------------------------------------------------===//
104 /// DIE - A structured debug information entry. Has an abbreviation which
105 /// describes its organization.
110 /// Offset - Offset in debug info section.
114 /// Size - Size of instance + children.
118 /// Abbrev - Buffer for constructing abbreviation.
124 std::vector<DIE *> Children;
128 /// Attribute values.
130 SmallVector<DIEValue*, 12> Values;
133 // Private data for print()
134 mutable unsigned IndentCount;
137 explicit DIE(unsigned Tag)
138 : Offset(0), Size(0), Abbrev(Tag, dwarf::DW_CHILDREN_no), Parent(0) {}
142 DIEAbbrev &getAbbrev() { return Abbrev; }
143 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
144 unsigned getTag() const { return Abbrev.getTag(); }
145 unsigned getOffset() const { return Offset; }
146 unsigned getSize() const { return Size; }
147 const std::vector<DIE *> &getChildren() const { return Children; }
148 const SmallVectorImpl<DIEValue*> &getValues() const { return Values; }
149 DIE *getParent() const { return Parent; }
150 /// Climb up the parent chain to get the compile unit DIE this DIE belongs
152 DIE *getCompileUnit();
153 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
154 void setOffset(unsigned O) { Offset = O; }
155 void setSize(unsigned S) { Size = S; }
157 /// addValue - Add a value and attributes to a DIE.
159 void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
160 Abbrev.AddAttribute(Attribute, Form);
161 Values.push_back(Value);
164 /// addChild - Add a child to the DIE.
166 void addChild(DIE *Child) {
167 if (Child->getParent()) {
168 assert (Child->getParent() == this && "Unexpected DIE Parent!");
171 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
172 Children.push_back(Child);
173 Child->Parent = this;
177 void print(raw_ostream &O, unsigned IndentCount = 0) const;
182 //===--------------------------------------------------------------------===//
183 /// DIEValue - A debug information entry value.
186 virtual void anchor();
197 /// Type - Type of data stored in the value.
201 explicit DIEValue(unsigned T) : Type(T) {}
202 virtual ~DIEValue() {}
205 unsigned getType() const { return Type; }
207 /// EmitValue - Emit value via the Dwarf writer.
209 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const = 0;
211 /// SizeOf - Return the size of a value in bytes.
213 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const = 0;
216 virtual void print(raw_ostream &O) const = 0;
221 //===--------------------------------------------------------------------===//
222 /// DIEInteger - An integer value DIE.
224 class DIEInteger : public DIEValue {
227 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
229 /// BestForm - Choose the best form for integer.
231 static unsigned BestForm(bool IsSigned, uint64_t Int) {
233 const int64_t SignedInt = Int;
234 if ((char)Int == SignedInt) return dwarf::DW_FORM_data1;
235 if ((short)Int == SignedInt) return dwarf::DW_FORM_data2;
236 if ((int)Int == SignedInt) return dwarf::DW_FORM_data4;
238 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
239 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
240 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
242 return dwarf::DW_FORM_data8;
245 /// EmitValue - Emit integer of appropriate size.
247 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
249 uint64_t getValue() const { return Integer; }
251 /// SizeOf - Determine size of integer value in bytes.
253 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
255 // Implement isa/cast/dyncast.
256 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
259 virtual void print(raw_ostream &O) const;
263 //===--------------------------------------------------------------------===//
264 /// DIELabel - A label expression DIE.
266 class DIELabel : public DIEValue {
267 const MCSymbol *Label;
269 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
271 /// EmitValue - Emit label value.
273 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
275 /// getValue - Get MCSymbol.
277 const MCSymbol *getValue() const { return Label; }
279 /// SizeOf - Determine size of label value in bytes.
281 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
283 // Implement isa/cast/dyncast.
284 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
287 virtual void print(raw_ostream &O) const;
291 //===--------------------------------------------------------------------===//
292 /// DIEDelta - A simple label difference DIE.
294 class DIEDelta : public DIEValue {
295 const MCSymbol *LabelHi;
296 const MCSymbol *LabelLo;
298 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
299 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
301 /// EmitValue - Emit delta value.
303 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
305 /// SizeOf - Determine size of delta value in bytes.
307 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
309 // Implement isa/cast/dyncast.
310 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
313 virtual void print(raw_ostream &O) const;
317 //===--------------------------------------------------------------------===//
318 /// DIEEntry - A pointer to another debug information entry. An instance of
319 /// this class can also be used as a proxy for a debug information entry not
320 /// yet defined (ie. types.)
321 class DIEEntry : public DIEValue {
324 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
325 assert(E && "Cannot construct a DIEEntry with a null DIE");
328 DIE *getEntry() const { return Entry; }
330 /// EmitValue - Emit debug information entry offset.
332 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
334 /// SizeOf - Determine size of debug information entry in bytes.
336 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const {
337 return sizeof(int32_t);
340 // Implement isa/cast/dyncast.
341 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
344 virtual void print(raw_ostream &O) const;
348 //===--------------------------------------------------------------------===//
349 /// DIEBlock - A block of values. Primarily used for location expressions.
351 class DIEBlock : public DIEValue, public DIE {
352 unsigned Size; // Size in bytes excluding size header.
355 : DIEValue(isBlock), DIE(0), Size(0) {}
356 virtual ~DIEBlock() {}
358 /// ComputeSize - calculate the size of the block.
360 unsigned ComputeSize(AsmPrinter *AP);
362 /// BestForm - Choose the best form for data.
364 unsigned BestForm() const {
365 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
366 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
367 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
368 return dwarf::DW_FORM_block;
371 /// EmitValue - Emit block data.
373 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
375 /// SizeOf - Determine size of block data in bytes.
377 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
379 // Implement isa/cast/dyncast.
380 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
383 virtual void print(raw_ostream &O) const;
387 } // end llvm namespace