blob: dc6a70a6bd6a3cf8c199158e3608031a4bdfc5ba [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
17#include "DwarfLabel.h"
18#include "llvm/ADT/FoldingSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/Dwarf.h"
Chris Lattnerb01acfa2009-08-23 01:01:17 +000022#include <vector>
Bill Wendling88423ee2009-05-15 00:11:17 +000023
24namespace llvm {
25 class AsmPrinter;
26 class Dwarf;
27 class TargetData;
28
29 //===--------------------------------------------------------------------===//
30 /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
31 /// Dwarf abbreviation.
Nick Lewycky381afae2009-11-17 09:17:08 +000032 class DIEAbbrevData {
Bill Wendling88423ee2009-05-15 00:11:17 +000033 /// Attribute - Dwarf attribute code.
34 ///
35 unsigned Attribute;
36
37 /// Form - Dwarf form code.
38 ///
39 unsigned Form;
40 public:
41 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
42
43 // Accessors.
44 unsigned getAttribute() const { return Attribute; }
45 unsigned getForm() const { return Form; }
46
47 /// Profile - Used to gather unique data for the abbreviation folding set.
48 ///
49 void Profile(FoldingSetNodeID &ID) const;
50 };
51
52 //===--------------------------------------------------------------------===//
53 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
54 /// information object.
Nick Lewycky381afae2009-11-17 09:17:08 +000055 class DIEAbbrev : public FoldingSetNode {
Bill Wendling88423ee2009-05-15 00:11:17 +000056 /// Tag - Dwarf tag code.
57 ///
58 unsigned Tag;
59
60 /// Unique number for node.
61 ///
62 unsigned Number;
63
64 /// ChildrenFlag - Dwarf children flag.
65 ///
66 unsigned ChildrenFlag;
67
68 /// Data - Raw data bytes for abbreviation.
69 ///
70 SmallVector<DIEAbbrevData, 8> Data;
71 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 ///
102 void Emit(const AsmPrinter *Asm) const;
103
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.
113 class CompileUnit;
114 class DIEValue;
115
Devang Patel6f01d9c2009-11-21 00:31:03 +0000116 class DIE {
Bill Wendling88423ee2009-05-15 00:11:17 +0000117 protected:
118 /// Abbrev - Buffer for constructing abbreviation.
119 ///
120 DIEAbbrev Abbrev;
121
122 /// Offset - Offset in debug info section.
123 ///
124 unsigned Offset;
125
126 /// Size - Size of instance + children.
127 ///
128 unsigned Size;
129
130 /// Children DIEs.
131 ///
132 std::vector<DIE *> Children;
133
134 /// Attributes values.
135 ///
136 SmallVector<DIEValue*, 32> Values;
137
138 /// Abstract compile unit.
139 CompileUnit *AbstractCU;
Owen Andersond5509f22009-06-24 23:13:56 +0000140
141 // Private data for print()
142 mutable unsigned IndentCount;
Bill Wendling88423ee2009-05-15 00:11:17 +0000143 public:
144 explicit DIE(unsigned Tag)
Owen Andersond5509f22009-06-24 23:13:56 +0000145 : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0),
146 Size(0), IndentCount(0) {}
Bill Wendling88423ee2009-05-15 00:11:17 +0000147 virtual ~DIE();
148
149 // Accessors.
150 DIEAbbrev &getAbbrev() { return Abbrev; }
151 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
152 unsigned getTag() const { return Abbrev.getTag(); }
153 unsigned getOffset() const { return Offset; }
154 unsigned getSize() const { return Size; }
155 const std::vector<DIE *> &getChildren() const { return Children; }
156 SmallVector<DIEValue*, 32> &getValues() { return Values; }
157 CompileUnit *getAbstractCompileUnit() const { return AbstractCU; }
158
159 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
160 void setOffset(unsigned O) { Offset = O; }
161 void setSize(unsigned S) { Size = S; }
162 void setAbstractCompileUnit(CompileUnit *CU) { AbstractCU = CU; }
163
Devang Patel2c4ceb12009-11-21 02:48:08 +0000164 /// addValue - Add a value and attributes to a DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000165 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000166 void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000167 Abbrev.AddAttribute(Attribute, Form);
168 Values.push_back(Value);
169 }
170
171 /// SiblingOffset - Return the offset of the debug information entry's
172 /// sibling.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000173 unsigned getSiblingOffset() const { return Offset + Size; }
Bill Wendling88423ee2009-05-15 00:11:17 +0000174
Devang Patel2c4ceb12009-11-21 02:48:08 +0000175 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000176 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000177 void addSiblingOffset();
Bill Wendling88423ee2009-05-15 00:11:17 +0000178
Devang Patel2c4ceb12009-11-21 02:48:08 +0000179 /// addChild - Add a child to the DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000180 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000181 void addChild(DIE *Child) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000182 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
183 Children.push_back(Child);
184 }
185
Bill Wendling88423ee2009-05-15 00:11:17 +0000186#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000187 void print(raw_ostream &O, unsigned IncIndent = 0);
Bill Wendling88423ee2009-05-15 00:11:17 +0000188 void dump();
189#endif
190 };
191
192 //===--------------------------------------------------------------------===//
193 /// DIEValue - A debug information entry value.
194 ///
Devang Patel6f01d9c2009-11-21 00:31:03 +0000195 class DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000196 public:
197 enum {
198 isInteger,
199 isString,
200 isLabel,
201 isAsIsLabel,
202 isSectionOffset,
203 isDelta,
204 isEntry,
205 isBlock
206 };
207 protected:
208 /// Type - Type of data stored in the value.
209 ///
210 unsigned Type;
211 public:
212 explicit DIEValue(unsigned T) : Type(T) {}
213 virtual ~DIEValue() {}
214
215 // Accessors
216 unsigned getType() const { return Type; }
217
218 /// EmitValue - Emit value via the Dwarf writer.
219 ///
220 virtual void EmitValue(Dwarf *D, unsigned Form) const = 0;
221
222 /// SizeOf - Return the size of a value in bytes.
223 ///
224 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const = 0;
225
Bill Wendling88423ee2009-05-15 00:11:17 +0000226 // Implement isa/cast/dyncast.
227 static bool classof(const DIEValue *) { return true; }
228
229#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000230 virtual void print(raw_ostream &O) = 0;
Bill Wendling88423ee2009-05-15 00:11:17 +0000231 void dump();
232#endif
233 };
234
235 //===--------------------------------------------------------------------===//
236 /// DIEInteger - An integer value DIE.
237 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000238 class DIEInteger : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000239 uint64_t Integer;
240 public:
241 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
242
243 /// BestForm - Choose the best form for integer.
244 ///
245 static unsigned BestForm(bool IsSigned, uint64_t Int) {
246 if (IsSigned) {
247 if ((char)Int == (signed)Int) return dwarf::DW_FORM_data1;
248 if ((short)Int == (signed)Int) return dwarf::DW_FORM_data2;
249 if ((int)Int == (signed)Int) return dwarf::DW_FORM_data4;
250 } else {
251 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
252 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
253 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
254 }
255 return dwarf::DW_FORM_data8;
256 }
257
258 /// EmitValue - Emit integer of appropriate size.
259 ///
260 virtual void EmitValue(Dwarf *D, unsigned Form) const;
261
262 /// SizeOf - Determine size of integer value in bytes.
263 ///
264 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
265
Bill Wendling88423ee2009-05-15 00:11:17 +0000266
267 // Implement isa/cast/dyncast.
268 static bool classof(const DIEInteger *) { return true; }
269 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
270
271#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000272 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000273#endif
274 };
275
276 //===--------------------------------------------------------------------===//
277 /// DIEString - A string value DIE.
278 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000279 class DIEString : public DIEValue {
Devang Patele9a05972009-11-24 19:42:17 +0000280 const StringRef Str;
Bill Wendling88423ee2009-05-15 00:11:17 +0000281 public:
Devang Patele9a05972009-11-24 19:42:17 +0000282 explicit DIEString(const StringRef S) : DIEValue(isString), Str(S) {}
Bill Wendling88423ee2009-05-15 00:11:17 +0000283
284 /// EmitValue - Emit string value.
285 ///
286 virtual void EmitValue(Dwarf *D, unsigned Form) const;
287
288 /// SizeOf - Determine size of string value in bytes.
289 ///
290 virtual unsigned SizeOf(const TargetData *, unsigned /*Form*/) const {
291 return Str.size() + sizeof(char); // sizeof('\0');
292 }
293
Bill Wendling88423ee2009-05-15 00:11:17 +0000294 // Implement isa/cast/dyncast.
295 static bool classof(const DIEString *) { return true; }
296 static bool classof(const DIEValue *S) { return S->getType() == isString; }
297
298#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000299 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000300#endif
301 };
302
303 //===--------------------------------------------------------------------===//
304 /// DIEDwarfLabel - A Dwarf internal label expression DIE.
305 //
Nick Lewycky381afae2009-11-17 09:17:08 +0000306 class DIEDwarfLabel : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000307 const DWLabel Label;
308 public:
309 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
310
311 /// EmitValue - Emit label value.
312 ///
313 virtual void EmitValue(Dwarf *D, unsigned Form) const;
314
315 /// SizeOf - Determine size of label value in bytes.
316 ///
317 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
318
Bill Wendling88423ee2009-05-15 00:11:17 +0000319 // Implement isa/cast/dyncast.
320 static bool classof(const DIEDwarfLabel *) { return true; }
321 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
322
323#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000324 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000325#endif
326 };
327
328 //===--------------------------------------------------------------------===//
329 /// DIEObjectLabel - A label to an object in code or data.
330 //
Nick Lewycky381afae2009-11-17 09:17:08 +0000331 class DIEObjectLabel : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000332 const std::string Label;
333 public:
334 explicit DIEObjectLabel(const std::string &L)
335 : DIEValue(isAsIsLabel), Label(L) {}
336
337 /// EmitValue - Emit label value.
338 ///
339 virtual void EmitValue(Dwarf *D, unsigned Form) const;
340
341 /// SizeOf - Determine size of label value in bytes.
342 ///
343 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
344
Bill Wendling88423ee2009-05-15 00:11:17 +0000345 // Implement isa/cast/dyncast.
346 static bool classof(const DIEObjectLabel *) { return true; }
347 static bool classof(const DIEValue *L) {
348 return L->getType() == isAsIsLabel;
349 }
350
351#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000352 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000353#endif
354 };
355
356 //===--------------------------------------------------------------------===//
357 /// DIESectionOffset - A section offset DIE.
358 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000359 class DIESectionOffset : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000360 const DWLabel Label;
361 const DWLabel Section;
362 bool IsEH : 1;
363 bool UseSet : 1;
364 public:
365 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
366 bool isEH = false, bool useSet = true)
367 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
368 IsEH(isEH), UseSet(useSet) {}
369
370 /// EmitValue - Emit section offset.
371 ///
372 virtual void EmitValue(Dwarf *D, unsigned Form) const;
373
374 /// SizeOf - Determine size of section offset value in bytes.
375 ///
376 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
377
Bill Wendling88423ee2009-05-15 00:11:17 +0000378 // Implement isa/cast/dyncast.
379 static bool classof(const DIESectionOffset *) { return true; }
380 static bool classof(const DIEValue *D) {
381 return D->getType() == isSectionOffset;
382 }
383
384#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000385 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000386#endif
387 };
388
389 //===--------------------------------------------------------------------===//
390 /// DIEDelta - A simple label difference DIE.
391 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000392 class DIEDelta : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000393 const DWLabel LabelHi;
394 const DWLabel LabelLo;
395 public:
396 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
397 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
398
399 /// EmitValue - Emit delta value.
400 ///
401 virtual void EmitValue(Dwarf *D, unsigned Form) const;
402
403 /// SizeOf - Determine size of delta value in bytes.
404 ///
405 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
406
Bill Wendling88423ee2009-05-15 00:11:17 +0000407 // Implement isa/cast/dyncast.
408 static bool classof(const DIEDelta *) { return true; }
409 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
410
411#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000412 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000413#endif
414 };
415
416 //===--------------------------------------------------------------------===//
417 /// DIEntry - A pointer to another debug information entry. An instance of
418 /// this class can also be used as a proxy for a debug information entry not
419 /// yet defined (ie. types.)
Nick Lewycky381afae2009-11-17 09:17:08 +0000420 class DIEEntry : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000421 DIE *Entry;
422 public:
423 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
424
425 DIE *getEntry() const { return Entry; }
426 void setEntry(DIE *E) { Entry = E; }
427
428 /// EmitValue - Emit debug information entry offset.
429 ///
430 virtual void EmitValue(Dwarf *D, unsigned Form) const;
431
432 /// SizeOf - Determine size of debug information entry in bytes.
433 ///
434 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const {
435 return sizeof(int32_t);
436 }
437
Bill Wendling88423ee2009-05-15 00:11:17 +0000438 // Implement isa/cast/dyncast.
439 static bool classof(const DIEEntry *) { return true; }
440 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
441
442#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000443 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000444#endif
445 };
446
447 //===--------------------------------------------------------------------===//
448 /// DIEBlock - A block of values. Primarily used for location expressions.
449 //
Nick Lewycky381afae2009-11-17 09:17:08 +0000450 class DIEBlock : public DIEValue, public DIE {
Bill Wendling88423ee2009-05-15 00:11:17 +0000451 unsigned Size; // Size in bytes excluding size header.
452 public:
453 DIEBlock()
454 : DIEValue(isBlock), DIE(0), Size(0) {}
455 virtual ~DIEBlock() {}
456
457 /// ComputeSize - calculate the size of the block.
458 ///
459 unsigned ComputeSize(const TargetData *TD);
460
461 /// BestForm - Choose the best form for data.
462 ///
463 unsigned BestForm() const {
464 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
465 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
466 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
467 return dwarf::DW_FORM_block;
468 }
469
470 /// EmitValue - Emit block data.
471 ///
472 virtual void EmitValue(Dwarf *D, unsigned Form) const;
473
474 /// SizeOf - Determine size of block data in bytes.
475 ///
476 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
477
Bill Wendling88423ee2009-05-15 00:11:17 +0000478 // Implement isa/cast/dyncast.
479 static bool classof(const DIEBlock *) { return true; }
480 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
481
482#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000483 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000484#endif
485 };
486
487} // end llvm namespace
488
489#endif