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;
139 // Private data for print()
140 mutable unsigned IndentCount;
143 explicit DIE(unsigned Tag)
144 : Offset(0), Size(0), Abbrev(Tag, dwarf::DW_CHILDREN_no), Parent(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 SmallVectorImpl<DIEValue*> &getValues() const { return Values; }
155 DIE *getParent() const { return Parent; }
156 /// Climb up the parent chain to get the compile unit DIE this DIE belongs
158 DIE *getCompileUnit();
159 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
160 void setOffset(unsigned O) { Offset = O; }
161 void setSize(unsigned S) { Size = S; }
163 /// addValue - Add a value and attributes to a DIE.
165 void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
166 Abbrev.AddAttribute(Attribute, Form);
167 Values.push_back(Value);
170 /// addChild - Add a child to the DIE.
172 void addChild(DIE *Child) {
173 if (Child->getParent()) {
174 assert (Child->getParent() == this && "Unexpected DIE Parent!");
177 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
178 Children.push_back(Child);
179 Child->Parent = this;
183 void print(raw_ostream &O, unsigned IndentCount = 0) const;
188 //===--------------------------------------------------------------------===//
189 /// DIEValue - A debug information entry value.
192 virtual void anchor();
203 /// Type - Type of data stored in the value.
207 explicit DIEValue(unsigned T) : Type(T) {}
208 virtual ~DIEValue() {}
211 unsigned getType() const { return Type; }
213 /// EmitValue - Emit value via the Dwarf writer.
215 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const = 0;
217 /// SizeOf - Return the size of a value in bytes.
219 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const = 0;
222 virtual void print(raw_ostream &O) const = 0;
227 //===--------------------------------------------------------------------===//
228 /// DIEInteger - An integer value DIE.
230 class DIEInteger : public DIEValue {
233 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
235 /// BestForm - Choose the best form for integer.
237 static unsigned BestForm(bool IsSigned, uint64_t Int) {
239 const int64_t SignedInt = Int;
240 if ((char)Int == SignedInt) return dwarf::DW_FORM_data1;
241 if ((short)Int == SignedInt) return dwarf::DW_FORM_data2;
242 if ((int)Int == SignedInt) return dwarf::DW_FORM_data4;
244 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
245 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
246 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
248 return dwarf::DW_FORM_data8;
251 /// EmitValue - Emit integer of appropriate size.
253 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
255 uint64_t getValue() const { return Integer; }
257 /// SizeOf - Determine size of integer value in bytes.
259 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
261 // Implement isa/cast/dyncast.
262 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
265 virtual void print(raw_ostream &O) const;
269 //===--------------------------------------------------------------------===//
270 /// DIELabel - A label expression DIE.
272 class DIELabel : public DIEValue {
273 const MCSymbol *Label;
275 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
277 /// EmitValue - Emit label value.
279 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
281 /// getValue - Get MCSymbol.
283 const MCSymbol *getValue() const { return Label; }
285 /// SizeOf - Determine size of label value in bytes.
287 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
289 // Implement isa/cast/dyncast.
290 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
293 virtual void print(raw_ostream &O) const;
297 //===--------------------------------------------------------------------===//
298 /// DIEDelta - A simple label difference DIE.
300 class DIEDelta : public DIEValue {
301 const MCSymbol *LabelHi;
302 const MCSymbol *LabelLo;
304 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
305 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
307 /// EmitValue - Emit delta value.
309 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
311 /// SizeOf - Determine size of delta value in bytes.
313 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
315 // Implement isa/cast/dyncast.
316 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
319 virtual void print(raw_ostream &O) const;
323 //===--------------------------------------------------------------------===//
324 /// DIEEntry - A pointer to another debug information entry. An instance of
325 /// this class can also be used as a proxy for a debug information entry not
326 /// yet defined (ie. types.)
327 class DIEEntry : public DIEValue {
330 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
331 assert(E && "Cannot construct a DIEEntry with a null DIE");
334 DIE *getEntry() const { return Entry; }
336 /// EmitValue - Emit debug information entry offset.
338 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
340 /// SizeOf - Determine size of debug information entry in bytes.
342 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const {
343 return sizeof(int32_t);
346 // Implement isa/cast/dyncast.
347 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
350 virtual void print(raw_ostream &O) const;
354 //===--------------------------------------------------------------------===//
355 /// DIEBlock - A block of values. Primarily used for location expressions.
357 class DIEBlock : public DIEValue, public DIE {
358 unsigned Size; // Size in bytes excluding size header.
361 : DIEValue(isBlock), DIE(0), Size(0) {}
362 virtual ~DIEBlock() {}
364 /// ComputeSize - calculate the size of the block.
366 unsigned ComputeSize(AsmPrinter *AP);
368 /// BestForm - Choose the best form for data.
370 unsigned BestForm() const {
371 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
372 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
373 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
374 return dwarf::DW_FORM_block;
377 /// EmitValue - Emit block data.
379 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
381 /// SizeOf - Determine size of block data in bytes.
383 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
385 // Implement isa/cast/dyncast.
386 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
389 virtual void print(raw_ostream &O) const;
393 } // end llvm namespace