blob: d56c0947795e058e3306cf6afc31bcd23030fee8 [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() {}
73 virtual ~DIEAbbrev() {}
74
75 // Accessors.
76 unsigned getTag() const { return Tag; }
77 unsigned getNumber() const { return Number; }
78 unsigned getChildrenFlag() const { return ChildrenFlag; }
79 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
80 void setTag(unsigned T) { Tag = T; }
81 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
82 void setNumber(unsigned N) { Number = N; }
83
84 /// AddAttribute - Adds another set of attribute information to the
85 /// abbreviation.
86 void AddAttribute(unsigned Attribute, unsigned Form) {
87 Data.push_back(DIEAbbrevData(Attribute, Form));
88 }
89
90 /// AddFirstAttribute - Adds a set of attribute information to the front
91 /// of the abbreviation.
92 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
93 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
94 }
95
96 /// Profile - Used to gather unique data for the abbreviation folding set.
97 ///
98 void Profile(FoldingSetNodeID &ID) const;
99
100 /// Emit - Print the abbreviation using the specified asm printer.
101 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000102 void Emit(AsmPrinter *AP) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000103
104#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000105 void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000106 void dump();
107#endif
108 };
109
110 //===--------------------------------------------------------------------===//
111 /// DIE - A structured debug information entry. Has an abbreviation which
112 /// describes it's organization.
Bill Wendling88423ee2009-05-15 00:11:17 +0000113 class DIEValue;
114
Devang Patel6f01d9c2009-11-21 00:31:03 +0000115 class DIE {
Bill Wendling88423ee2009-05-15 00:11:17 +0000116 protected:
117 /// Abbrev - Buffer for constructing abbreviation.
118 ///
119 DIEAbbrev Abbrev;
120
121 /// Offset - Offset in debug info section.
122 ///
123 unsigned Offset;
124
125 /// Size - Size of instance + children.
126 ///
127 unsigned Size;
128
129 /// Children DIEs.
130 ///
131 std::vector<DIE *> Children;
132
Devang Patel6404e4e2009-12-15 19:16:48 +0000133 DIE *Parent;
134
Bill Wendling88423ee2009-05-15 00:11:17 +0000135 /// Attributes values.
136 ///
137 SmallVector<DIEValue*, 32> Values;
138
Owen Andersond5509f22009-06-24 23:13:56 +0000139 // Private data for print()
140 mutable unsigned IndentCount;
Bill Wendling88423ee2009-05-15 00:11:17 +0000141 public:
142 explicit DIE(unsigned Tag)
Owen Andersond5509f22009-06-24 23:13:56 +0000143 : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0),
Devang Patel6404e4e2009-12-15 19:16:48 +0000144 Size(0), Parent (0), IndentCount(0) {}
Bill Wendling88423ee2009-05-15 00:11:17 +0000145 virtual ~DIE();
146
147 // Accessors.
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; }
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +0000154 const SmallVector<DIEValue*, 32> &getValues() const { return Values; }
Devang Patel6404e4e2009-12-15 19:16:48 +0000155 DIE *getParent() const { return Parent; }
Bill Wendling88423ee2009-05-15 00:11:17 +0000156 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
157 void setOffset(unsigned O) { Offset = O; }
158 void setSize(unsigned S) { Size = S; }
Devang Patel6404e4e2009-12-15 19:16:48 +0000159
Devang Patel2c4ceb12009-11-21 02:48:08 +0000160 /// addValue - Add a value and attributes to a DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000161 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000162 void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000163 Abbrev.AddAttribute(Attribute, Form);
164 Values.push_back(Value);
165 }
166
167 /// SiblingOffset - Return the offset of the debug information entry's
168 /// sibling.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000169 unsigned getSiblingOffset() const { return Offset + Size; }
Bill Wendling88423ee2009-05-15 00:11:17 +0000170
Devang Patel2c4ceb12009-11-21 02:48:08 +0000171 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +0000172 /// The caller is responsible for deleting the return value at or after the
173 /// same time it destroys this DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000174 ///
Benjamin Kramer345ef342010-03-31 19:34:01 +0000175 DIEValue *addSiblingOffset(BumpPtrAllocator &A);
Bill Wendling88423ee2009-05-15 00:11:17 +0000176
Devang Patel2c4ceb12009-11-21 02:48:08 +0000177 /// addChild - Add a child to the DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000178 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000179 void addChild(DIE *Child) {
Devang Patel6404e4e2009-12-15 19:16:48 +0000180 if (Child->getParent()) {
181 assert (Child->getParent() == this && "Unexpected DIE Parent!");
182 return;
183 }
Bill Wendling88423ee2009-05-15 00:11:17 +0000184 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
185 Children.push_back(Child);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000186 Child->Parent = this;
Bill Wendling88423ee2009-05-15 00:11:17 +0000187 }
188
Bill Wendling88423ee2009-05-15 00:11:17 +0000189#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000190 void print(raw_ostream &O, unsigned IncIndent = 0);
Bill Wendling88423ee2009-05-15 00:11:17 +0000191 void dump();
192#endif
193 };
194
195 //===--------------------------------------------------------------------===//
196 /// DIEValue - A debug information entry value.
197 ///
Devang Patel6f01d9c2009-11-21 00:31:03 +0000198 class DIEValue {
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 isSectionOffset,
205 isDelta,
206 isEntry,
207 isBlock
208 };
209 protected:
210 /// Type - Type of data stored in the value.
211 ///
212 unsigned Type;
213 public:
214 explicit DIEValue(unsigned T) : Type(T) {}
215 virtual ~DIEValue() {}
216
217 // Accessors
218 unsigned getType() const { return Type; }
219
220 /// EmitValue - Emit value via the Dwarf writer.
221 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000222 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const = 0;
Bill Wendling88423ee2009-05-15 00:11:17 +0000223
224 /// SizeOf - Return the size of a value in bytes.
225 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000226 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const = 0;
Bill Wendling88423ee2009-05-15 00:11:17 +0000227
Bill Wendling88423ee2009-05-15 00:11:17 +0000228 // Implement isa/cast/dyncast.
229 static bool classof(const DIEValue *) { return true; }
230
231#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000232 virtual void print(raw_ostream &O) = 0;
Bill Wendling88423ee2009-05-15 00:11:17 +0000233 void dump();
234#endif
235 };
236
237 //===--------------------------------------------------------------------===//
238 /// DIEInteger - An integer value DIE.
239 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000240 class DIEInteger : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000241 uint64_t Integer;
242 public:
243 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
244
245 /// BestForm - Choose the best form for integer.
246 ///
247 static unsigned BestForm(bool IsSigned, uint64_t Int) {
248 if (IsSigned) {
249 if ((char)Int == (signed)Int) return dwarf::DW_FORM_data1;
250 if ((short)Int == (signed)Int) return dwarf::DW_FORM_data2;
251 if ((int)Int == (signed)Int) return dwarf::DW_FORM_data4;
252 } else {
253 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
254 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
255 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
256 }
257 return dwarf::DW_FORM_data8;
258 }
259
260 /// EmitValue - Emit integer of appropriate size.
261 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000262 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000263
Devang Patelf2548ca2010-04-16 23:33:45 +0000264 uint64_t getValue() const { return Integer; }
265
Bill Wendling88423ee2009-05-15 00:11:17 +0000266 /// SizeOf - Determine size of integer value in bytes.
267 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000268 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000269
Bill Wendling88423ee2009-05-15 00:11:17 +0000270 // Implement isa/cast/dyncast.
271 static bool classof(const DIEInteger *) { return true; }
272 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
273
274#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000275 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000276#endif
277 };
278
279 //===--------------------------------------------------------------------===//
Devang Patel69f57b12009-12-02 15:25:16 +0000280 /// DIEString - A string value DIE. This DIE keeps string reference only.
Bill Wendling88423ee2009-05-15 00:11:17 +0000281 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000282 class DIEString : public DIEValue {
Devang Patele9a05972009-11-24 19:42:17 +0000283 const StringRef Str;
Bill Wendling88423ee2009-05-15 00:11:17 +0000284 public:
Devang Patele9a05972009-11-24 19:42:17 +0000285 explicit DIEString(const StringRef S) : DIEValue(isString), Str(S) {}
Bill Wendling88423ee2009-05-15 00:11:17 +0000286
287 /// EmitValue - Emit string value.
288 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000289 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000290
291 /// SizeOf - Determine size of string value in bytes.
292 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000293 virtual unsigned SizeOf(AsmPrinter *AP, unsigned /*Form*/) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000294 return Str.size() + sizeof(char); // sizeof('\0');
295 }
296
Bill Wendling88423ee2009-05-15 00:11:17 +0000297 // Implement isa/cast/dyncast.
298 static bool classof(const DIEString *) { return true; }
299 static bool classof(const DIEValue *S) { return S->getType() == isString; }
300
301#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000302 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000303#endif
304 };
305
306 //===--------------------------------------------------------------------===//
Chris Lattner4faf59a2010-03-08 22:31:46 +0000307 /// DIELabel - A label expression DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000308 //
Chris Lattner4faf59a2010-03-08 22:31:46 +0000309 class DIELabel : public DIEValue {
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000310 const MCSymbol *Label;
Bill Wendling88423ee2009-05-15 00:11:17 +0000311 public:
Chris Lattner4faf59a2010-03-08 22:31:46 +0000312 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
Bill Wendling88423ee2009-05-15 00:11:17 +0000313
314 /// EmitValue - Emit label value.
315 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000316 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000317
Devang Patelc3f5f782010-05-25 23:40:22 +0000318 /// getValue - Get MCSymbol.
319 ///
320 const MCSymbol *getValue() const { return Label; }
321
Bill Wendling88423ee2009-05-15 00:11:17 +0000322 /// SizeOf - Determine size of label value in bytes.
323 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000324 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000325
Bill Wendling88423ee2009-05-15 00:11:17 +0000326 // Implement isa/cast/dyncast.
Chris Lattner4faf59a2010-03-08 22:31:46 +0000327 static bool classof(const DIELabel *) { return true; }
Bill Wendling88423ee2009-05-15 00:11:17 +0000328 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
329
330#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000331 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000332#endif
333 };
334
335 //===--------------------------------------------------------------------===//
Bill Wendling88423ee2009-05-15 00:11:17 +0000336 /// DIEDelta - A simple label difference DIE.
337 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000338 class DIEDelta : public DIEValue {
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000339 const MCSymbol *LabelHi;
340 const MCSymbol *LabelLo;
Bill Wendling88423ee2009-05-15 00:11:17 +0000341 public:
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000342 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
Bill Wendling88423ee2009-05-15 00:11:17 +0000343 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
344
345 /// EmitValue - Emit delta value.
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 delta value 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
Bill Wendling88423ee2009-05-15 00:11:17 +0000353 // Implement isa/cast/dyncast.
354 static bool classof(const DIEDelta *) { return true; }
355 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
356
357#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000358 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000359#endif
360 };
361
362 //===--------------------------------------------------------------------===//
363 /// DIEntry - A pointer to another debug information entry. An instance of
364 /// this class can also be used as a proxy for a debug information entry not
365 /// yet defined (ie. types.)
Nick Lewycky381afae2009-11-17 09:17:08 +0000366 class DIEEntry : public DIEValue {
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000367 DIE *const Entry;
Bill Wendling88423ee2009-05-15 00:11:17 +0000368 public:
369 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
370
371 DIE *getEntry() const { return Entry; }
Bill Wendling88423ee2009-05-15 00:11:17 +0000372
373 /// EmitValue - Emit debug information entry offset.
374 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000375 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000376
377 /// SizeOf - Determine size of debug information entry in bytes.
378 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000379 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000380 return sizeof(int32_t);
381 }
382
Bill Wendling88423ee2009-05-15 00:11:17 +0000383 // Implement isa/cast/dyncast.
384 static bool classof(const DIEEntry *) { return true; }
385 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
386
387#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000388 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000389#endif
390 };
391
392 //===--------------------------------------------------------------------===//
393 /// DIEBlock - A block of values. Primarily used for location expressions.
394 //
Nick Lewycky381afae2009-11-17 09:17:08 +0000395 class DIEBlock : public DIEValue, public DIE {
Bill Wendling88423ee2009-05-15 00:11:17 +0000396 unsigned Size; // Size in bytes excluding size header.
397 public:
398 DIEBlock()
399 : DIEValue(isBlock), DIE(0), Size(0) {}
400 virtual ~DIEBlock() {}
401
402 /// ComputeSize - calculate the size of the block.
403 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000404 unsigned ComputeSize(AsmPrinter *AP);
Bill Wendling88423ee2009-05-15 00:11:17 +0000405
406 /// BestForm - Choose the best form for data.
407 ///
408 unsigned BestForm() const {
409 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
410 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
411 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
412 return dwarf::DW_FORM_block;
413 }
414
415 /// EmitValue - Emit block data.
416 ///
Chris Lattnerd38fee82010-04-05 00:13:49 +0000417 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000418
419 /// SizeOf - Determine size of block data in bytes.
420 ///
Chris Lattnera37d5382010-04-05 00:18:22 +0000421 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
Bill Wendling88423ee2009-05-15 00:11:17 +0000422
Bill Wendling88423ee2009-05-15 00:11:17 +0000423 // Implement isa/cast/dyncast.
424 static bool classof(const DIEBlock *) { return true; }
425 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
426
427#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000428 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000429#endif
430 };
431
432} // end llvm namespace
433
434#endif