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"
29 //===--------------------------------------------------------------------===//
30 /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
31 /// Dwarf abbreviation.
33 /// Attribute - Dwarf attribute code.
37 /// Form - Dwarf form code.
41 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
44 unsigned getAttribute() const { return Attribute; }
45 unsigned getForm() const { return Form; }
47 /// Profile - Used to gather unique data for the abbreviation folding set.
49 void Profile(FoldingSetNodeID &ID) const;
52 //===--------------------------------------------------------------------===//
53 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
54 /// information object.
55 class DIEAbbrev : public FoldingSetNode {
56 /// Tag - Dwarf tag code.
60 /// Unique number for node.
64 /// ChildrenFlag - Dwarf children flag.
66 unsigned ChildrenFlag;
68 /// Data - Raw data bytes for abbreviation.
70 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(AsmPrinter *AP) const;
106 void print(raw_ostream &O);
111 //===--------------------------------------------------------------------===//
112 /// DIE - A structured debug information entry. Has an abbreviation which
113 /// describes it's organization.
118 /// Abbrev - Buffer for constructing abbreviation.
122 /// Offset - Offset in debug info section.
126 /// Size - Size of instance + children.
132 std::vector<DIE *> Children;
136 /// Attributes values.
138 SmallVector<DIEValue*, 32> Values;
140 // Private data for print()
141 mutable unsigned IndentCount;
143 explicit DIE(unsigned Tag)
144 : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0),
145 Size(0), Parent (0), IndentCount(0) {}
149 DIEAbbrev &getAbbrev() { return Abbrev; }
150 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
151 unsigned getTag() const { return Abbrev.getTag(); }
152 unsigned getOffset() const { return Offset; }
153 unsigned getSize() const { return Size; }
154 const std::vector<DIE *> &getChildren() const { return Children; }
155 const SmallVector<DIEValue*, 32> &getValues() const { return Values; }
156 DIE *getParent() const { return Parent; }
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 /// SiblingOffset - Return the offset of the debug information entry's
170 unsigned getSiblingOffset() const { return Offset + Size; }
172 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
173 /// The caller is responsible for deleting the return value at or after the
174 /// same time it destroys this DIE.
176 DIEValue *addSiblingOffset(BumpPtrAllocator &A);
178 /// addChild - Add a child to the DIE.
180 void addChild(DIE *Child) {
181 if (Child->getParent()) {
182 assert (Child->getParent() == this && "Unexpected DIE Parent!");
185 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
186 Children.push_back(Child);
187 Child->Parent = this;
191 void print(raw_ostream &O, unsigned IncIndent = 0);
196 //===--------------------------------------------------------------------===//
197 /// DIEValue - A debug information entry value.
211 /// Type - Type of data stored in the value.
215 explicit DIEValue(unsigned T) : Type(T) {}
216 virtual ~DIEValue() {}
219 unsigned getType() const { return Type; }
221 /// EmitValue - Emit value via the Dwarf writer.
223 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const = 0;
225 /// SizeOf - Return the size of a value in bytes.
227 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const = 0;
229 // Implement isa/cast/dyncast.
230 static bool classof(const DIEValue *) { return true; }
233 virtual void print(raw_ostream &O) = 0;
238 //===--------------------------------------------------------------------===//
239 /// DIEInteger - An integer value DIE.
241 class DIEInteger : public DIEValue {
244 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
246 /// BestForm - Choose the best form for integer.
248 static unsigned BestForm(bool IsSigned, uint64_t Int) {
250 if ((char)Int == (signed)Int) return dwarf::DW_FORM_data1;
251 if ((short)Int == (signed)Int) return dwarf::DW_FORM_data2;
252 if ((int)Int == (signed)Int) return dwarf::DW_FORM_data4;
254 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
255 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
256 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
258 return dwarf::DW_FORM_data8;
261 /// EmitValue - Emit integer of appropriate size.
263 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
265 /// SizeOf - Determine size of integer value in bytes.
267 virtual unsigned SizeOf(const TargetData *TD, 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(const TargetData *, 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 /// SizeOf - Determine size of label value in bytes.
320 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
322 // Implement isa/cast/dyncast.
323 static bool classof(const DIELabel *) { return true; }
324 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
327 virtual void print(raw_ostream &O);
331 //===--------------------------------------------------------------------===//
332 /// DIEDelta - A simple label difference DIE.
334 class DIEDelta : public DIEValue {
335 const MCSymbol *LabelHi;
336 const MCSymbol *LabelLo;
338 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
339 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
341 /// EmitValue - Emit delta value.
343 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
345 /// SizeOf - Determine size of delta value in bytes.
347 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
349 // Implement isa/cast/dyncast.
350 static bool classof(const DIEDelta *) { return true; }
351 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
354 virtual void print(raw_ostream &O);
358 //===--------------------------------------------------------------------===//
359 /// DIEntry - A pointer to another debug information entry. An instance of
360 /// this class can also be used as a proxy for a debug information entry not
361 /// yet defined (ie. types.)
362 class DIEEntry : public DIEValue {
365 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
367 DIE *getEntry() const { return Entry; }
369 /// EmitValue - Emit debug information entry offset.
371 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
373 /// SizeOf - Determine size of debug information entry in bytes.
375 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const {
376 return sizeof(int32_t);
379 // Implement isa/cast/dyncast.
380 static bool classof(const DIEEntry *) { return true; }
381 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
384 virtual void print(raw_ostream &O);
388 //===--------------------------------------------------------------------===//
389 /// DIEBlock - A block of values. Primarily used for location expressions.
391 class DIEBlock : public DIEValue, public DIE {
392 unsigned Size; // Size in bytes excluding size header.
395 : DIEValue(isBlock), DIE(0), Size(0) {}
396 virtual ~DIEBlock() {}
398 /// ComputeSize - calculate the size of the block.
400 unsigned ComputeSize(const TargetData *TD);
402 /// BestForm - Choose the best form for data.
404 unsigned BestForm() const {
405 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
406 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
407 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
408 return dwarf::DW_FORM_block;
411 /// EmitValue - Emit block data.
413 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
415 /// SizeOf - Determine size of block data in bytes.
417 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
419 // Implement isa/cast/dyncast.
420 static bool classof(const DIEBlock *) { return true; }
421 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
424 virtual void print(raw_ostream &O);
428 } // end llvm namespace