blob: b4996250c1b7e2d5054630f6e2ac46a07f61916c [file] [log] [blame]
Bill Wendling88423ee2009-05-15 00:11:17 +00001//===--- lib/CodeGen/DIE.h - DWARF Info Entries -----------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Data structures for DWARF info entries.
11//
12//===----------------------------------------------------------------------===//
13
Bill Wendling0310d762009-05-15 09:23:25 +000014#ifndef CODEGEN_ASMPRINTER_DIE_H__
15#define CODEGEN_ASMPRINTER_DIE_H__
Bill Wendling88423ee2009-05-15 00:11:17 +000016
Bill Wendling88423ee2009-05-15 00:11:17 +000017#include "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/Support/Compiler.h"
20#include "llvm/Support/Dwarf.h"
Chris Lattnerb01acfa2009-08-23 01:01:17 +000021#include <vector>
Bill Wendling88423ee2009-05-15 00:11:17 +000022
23namespace llvm {
24 class AsmPrinter;
Chris Lattner858431d2010-01-16 18:50:28 +000025 class MCSymbol;
Chris Lattnerb98b1bf2010-03-08 22:23:36 +000026 class raw_ostream;
Bill Wendling88423ee2009-05-15 00:11:17 +000027
28 //===--------------------------------------------------------------------===//
29 /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
30 /// Dwarf abbreviation.
Nick Lewycky381afae2009-11-17 09:17:08 +000031 class DIEAbbrevData {
Bill Wendling88423ee2009-05-15 00:11:17 +000032 /// Attribute - Dwarf attribute code.
33 ///
34 unsigned Attribute;
35
36 /// Form - Dwarf form code.
37 ///
38 unsigned Form;
39 public:
40 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
41
42 // Accessors.
43 unsigned getAttribute() const { return Attribute; }
44 unsigned getForm() const { return Form; }
45
46 /// Profile - Used to gather unique data for the abbreviation folding set.
47 ///
48 void Profile(FoldingSetNodeID &ID) const;
49 };
50
51 //===--------------------------------------------------------------------===//
52 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
53 /// information object.
Nick Lewycky381afae2009-11-17 09:17:08 +000054 class DIEAbbrev : public FoldingSetNode {
Bill Wendling88423ee2009-05-15 00:11:17 +000055 /// Tag - Dwarf tag code.
56 ///
57 unsigned Tag;
58
59 /// Unique number for node.
60 ///
61 unsigned Number;
62
63 /// ChildrenFlag - Dwarf children flag.
64 ///
65 unsigned ChildrenFlag;
66
67 /// Data - Raw data bytes for abbreviation.
68 ///
69 SmallVector<DIEAbbrevData, 8> Data;
Devang Patel6404e4e2009-12-15 19:16:48 +000070
Bill Wendling88423ee2009-05-15 00:11:17 +000071 public:
72 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
Bill Wendling88423ee2009-05-15 00:11:17 +000073
74 // Accessors.
75 unsigned getTag() const { return Tag; }
76 unsigned getNumber() const { return Number; }
77 unsigned getChildrenFlag() const { return ChildrenFlag; }
78 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
79 void setTag(unsigned T) { Tag = T; }
80 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
81 void setNumber(unsigned N) { Number = N; }
82
83 /// AddAttribute - Adds another set of attribute information to the
84 /// abbreviation.
85 void AddAttribute(unsigned Attribute, unsigned Form) {
86 Data.push_back(DIEAbbrevData(Attribute, Form));
87 }
88
89 /// AddFirstAttribute - Adds a set of attribute information to the front
90 /// of the abbreviation.
91 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
92 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
93 }
94
95 /// Profile - Used to gather unique data for the abbreviation folding set.
96 ///
97 void Profile(FoldingSetNodeID &ID) const;
98
99 /// Emit - Print the abbreviation using the specified asm printer.
100 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000101 void Emit(AsmPrinter *AP) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000102
103#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000104 void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000105 void dump();
106#endif
107 };
108
109 //===--------------------------------------------------------------------===//
110 /// DIE - A structured debug information entry. Has an abbreviation which
111 /// describes it's organization.
Bill Wendling88423ee2009-05-15 00:11:17 +0000112 class DIEValue;
113
Devang Patel6f01d9c2009-11-21 00:31:03 +0000114 class DIE {
Bill Wendling88423ee2009-05-15 00:11:17 +0000115 protected:
116 /// Abbrev - Buffer for constructing abbreviation.
117 ///
118 DIEAbbrev Abbrev;
119
120 /// Offset - Offset in debug info section.
121 ///
122 unsigned Offset;
123
124 /// Size - Size of instance + children.
125 ///
126 unsigned Size;
127
128 /// Children DIEs.
129 ///
130 std::vector<DIE *> Children;
131
Devang Patel6404e4e2009-12-15 19:16:48 +0000132 DIE *Parent;
133
Bill Wendling88423ee2009-05-15 00:11:17 +0000134 /// Attributes values.
135 ///
136 SmallVector<DIEValue*, 32> Values;
137
Owen Andersond5509f22009-06-24 23:13:56 +0000138 // Private data for print()
139 mutable unsigned IndentCount;
Bill Wendling88423ee2009-05-15 00:11:17 +0000140 public:
141 explicit DIE(unsigned Tag)
Owen Andersond5509f22009-06-24 23:13:56 +0000142 : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0),
Nick Lewycky024170f2011-10-18 22:39:43 +0000143 Size(0), Parent(0), IndentCount(0) {}
Bill Wendling88423ee2009-05-15 00:11:17 +0000144 virtual ~DIE();
145
146 // Accessors.
147 DIEAbbrev &getAbbrev() { return Abbrev; }
148 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
149 unsigned getTag() const { return Abbrev.getTag(); }
150 unsigned getOffset() const { return Offset; }
151 unsigned getSize() const { return Size; }
152 const std::vector<DIE *> &getChildren() const { return Children; }
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +0000153 const SmallVector<DIEValue*, 32> &getValues() const { return Values; }
Devang Patel6404e4e2009-12-15 19:16:48 +0000154 DIE *getParent() const { return Parent; }
Bill Wendling88423ee2009-05-15 00:11:17 +0000155 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
156 void setOffset(unsigned O) { Offset = O; }
157 void setSize(unsigned S) { Size = S; }
Devang Patel6404e4e2009-12-15 19:16:48 +0000158
Devang Patel2c4ceb12009-11-21 02:48:08 +0000159 /// addValue - Add a value and attributes to a DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000160 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000161 void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000162 Abbrev.AddAttribute(Attribute, Form);
163 Values.push_back(Value);
164 }
165
166 /// SiblingOffset - Return the offset of the debug information entry's
167 /// sibling.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000168 unsigned getSiblingOffset() const { return Offset + Size; }
Bill Wendling88423ee2009-05-15 00:11:17 +0000169
Devang Patel2c4ceb12009-11-21 02:48:08 +0000170 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +0000171 /// The caller is responsible for deleting the return value at or after the
172 /// same time it destroys this DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000173 ///
Benjamin Kramer345ef342010-03-31 19:34:01 +0000174 DIEValue *addSiblingOffset(BumpPtrAllocator &A);
Bill Wendling88423ee2009-05-15 00:11:17 +0000175
Devang Patel2c4ceb12009-11-21 02:48:08 +0000176 /// addChild - Add a child to the DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000177 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000178 void addChild(DIE *Child) {
Devang Patel6404e4e2009-12-15 19:16:48 +0000179 if (Child->getParent()) {
180 assert (Child->getParent() == this && "Unexpected DIE Parent!");
181 return;
182 }
Bill Wendling88423ee2009-05-15 00:11:17 +0000183 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
184 Children.push_back(Child);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000185 Child->Parent = this;
Bill Wendling88423ee2009-05-15 00:11:17 +0000186 }
187
Bill Wendling88423ee2009-05-15 00:11:17 +0000188#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000189 void print(raw_ostream &O, unsigned IncIndent = 0);
Bill Wendling88423ee2009-05-15 00:11:17 +0000190 void dump();
191#endif
192 };
193
194 //===--------------------------------------------------------------------===//
195 /// DIEValue - A debug information entry value.
196 ///
Devang Patel6f01d9c2009-11-21 00:31:03 +0000197 class DIEValue {
David Blaikie2d24e2a2011-12-20 02:50:00 +0000198 virtual void anchor();
Bill Wendling88423ee2009-05-15 00:11:17 +0000199 public:
200 enum {
201 isInteger,
202 isString,
203 isLabel,
Bill Wendling88423ee2009-05-15 00:11:17 +0000204 isDelta,
205 isEntry,
206 isBlock
207 };
208 protected:
209 /// Type - Type of data stored in the value.
210 ///
211 unsigned Type;
212 public:
213 explicit DIEValue(unsigned T) : Type(T) {}
214 virtual ~DIEValue() {}
215
216 // Accessors
217 unsigned getType() const { return Type; }
218
219 /// EmitValue - Emit value via the Dwarf writer.
220 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000221 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const = 0;
Bill Wendling88423ee2009-05-15 00:11:17 +0000222
223 /// SizeOf - Return the size of a value in bytes.
224 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000225 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const = 0;
Bill Wendling88423ee2009-05-15 00:11:17 +0000226
Bill Wendling88423ee2009-05-15 00:11:17 +0000227 // Implement isa/cast/dyncast.
228 static bool classof(const DIEValue *) { return true; }
229
230#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000231 virtual void print(raw_ostream &O) = 0;
Bill Wendling88423ee2009-05-15 00:11:17 +0000232 void dump();
233#endif
234 };
235
236 //===--------------------------------------------------------------------===//
237 /// DIEInteger - An integer value DIE.
238 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000239 class DIEInteger : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000240 uint64_t Integer;
241 public:
242 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
243
244 /// BestForm - Choose the best form for integer.
245 ///
246 static unsigned BestForm(bool IsSigned, uint64_t Int) {
247 if (IsSigned) {
248 if ((char)Int == (signed)Int) return dwarf::DW_FORM_data1;
249 if ((short)Int == (signed)Int) return dwarf::DW_FORM_data2;
250 if ((int)Int == (signed)Int) return dwarf::DW_FORM_data4;
251 } else {
252 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
253 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
254 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
255 }
256 return dwarf::DW_FORM_data8;
257 }
258
259 /// EmitValue - Emit integer of appropriate size.
260 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000261 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000262
Devang Patelf2548ca2010-04-16 23:33:45 +0000263 uint64_t getValue() const { return Integer; }
264
Bill Wendling88423ee2009-05-15 00:11:17 +0000265 /// SizeOf - Determine size of integer value in bytes.
266 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000267 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000268
Bill Wendling88423ee2009-05-15 00:11:17 +0000269 // Implement isa/cast/dyncast.
270 static bool classof(const DIEInteger *) { return true; }
271 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
272
273#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000274 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000275#endif
276 };
277
278 //===--------------------------------------------------------------------===//
Chris Lattner4faf59a2010-03-08 22:31:46 +0000279 /// DIELabel - A label expression DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000280 //
Chris Lattner4faf59a2010-03-08 22:31:46 +0000281 class DIELabel : public DIEValue {
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000282 const MCSymbol *Label;
Bill Wendling88423ee2009-05-15 00:11:17 +0000283 public:
Chris Lattner4faf59a2010-03-08 22:31:46 +0000284 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
Bill Wendling88423ee2009-05-15 00:11:17 +0000285
286 /// EmitValue - Emit label value.
287 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000288 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000289
Devang Patelc3f5f782010-05-25 23:40:22 +0000290 /// getValue - Get MCSymbol.
291 ///
292 const MCSymbol *getValue() const { return Label; }
293
Bill Wendling88423ee2009-05-15 00:11:17 +0000294 /// SizeOf - Determine size of label value in bytes.
295 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000296 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000297
Bill Wendling88423ee2009-05-15 00:11:17 +0000298 // Implement isa/cast/dyncast.
Chris Lattner4faf59a2010-03-08 22:31:46 +0000299 static bool classof(const DIELabel *) { return true; }
Bill Wendling88423ee2009-05-15 00:11:17 +0000300 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
301
302#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000303 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000304#endif
305 };
306
307 //===--------------------------------------------------------------------===//
Bill Wendling88423ee2009-05-15 00:11:17 +0000308 /// DIEDelta - A simple label difference DIE.
309 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000310 class DIEDelta : public DIEValue {
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000311 const MCSymbol *LabelHi;
312 const MCSymbol *LabelLo;
Bill Wendling88423ee2009-05-15 00:11:17 +0000313 public:
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000314 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
Bill Wendling88423ee2009-05-15 00:11:17 +0000315 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
316
317 /// EmitValue - Emit delta value.
318 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000319 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000320
321 /// SizeOf - Determine size of delta value in bytes.
322 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000323 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000324
Bill Wendling88423ee2009-05-15 00:11:17 +0000325 // Implement isa/cast/dyncast.
326 static bool classof(const DIEDelta *) { return true; }
327 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
328
329#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000330 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000331#endif
332 };
333
334 //===--------------------------------------------------------------------===//
Nick Lewycky024170f2011-10-18 22:39:43 +0000335 /// DIEEntry - A pointer to another debug information entry. An instance of
Bill Wendling88423ee2009-05-15 00:11:17 +0000336 /// this class can also be used as a proxy for a debug information entry not
337 /// yet defined (ie. types.)
Nick Lewycky381afae2009-11-17 09:17:08 +0000338 class DIEEntry : public DIEValue {
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000339 DIE *const Entry;
Bill Wendling88423ee2009-05-15 00:11:17 +0000340 public:
341 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
342
343 DIE *getEntry() const { return Entry; }
Bill Wendling88423ee2009-05-15 00:11:17 +0000344
345 /// EmitValue - Emit debug information entry offset.
346 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000347 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000348
349 /// SizeOf - Determine size of debug information entry in bytes.
350 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000351 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000352 return sizeof(int32_t);
353 }
354
Bill Wendling88423ee2009-05-15 00:11:17 +0000355 // Implement isa/cast/dyncast.
356 static bool classof(const DIEEntry *) { return true; }
357 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
358
359#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000360 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000361#endif
362 };
363
364 //===--------------------------------------------------------------------===//
365 /// DIEBlock - A block of values. Primarily used for location expressions.
366 //
Nick Lewycky381afae2009-11-17 09:17:08 +0000367 class DIEBlock : public DIEValue, public DIE {
Bill Wendling88423ee2009-05-15 00:11:17 +0000368 unsigned Size; // Size in bytes excluding size header.
369 public:
370 DIEBlock()
371 : DIEValue(isBlock), DIE(0), Size(0) {}
372 virtual ~DIEBlock() {}
373
374 /// ComputeSize - calculate the size of the block.
375 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000376 unsigned ComputeSize(AsmPrinter *AP);
Bill Wendling88423ee2009-05-15 00:11:17 +0000377
378 /// BestForm - Choose the best form for data.
379 ///
380 unsigned BestForm() const {
381 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
382 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
383 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
384 return dwarf::DW_FORM_block;
385 }
386
387 /// EmitValue - Emit block data.
388 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000389 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000390
391 /// SizeOf - Determine size of block data in bytes.
392 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000393 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000394
Bill Wendling88423ee2009-05-15 00:11:17 +0000395 // Implement isa/cast/dyncast.
396 static bool classof(const DIEBlock *) { return true; }
397 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
398
399#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000400 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000401#endif
402 };
403
404} // end llvm namespace
405
406#endif