blob: a6dc9b690c23a07241a823ef25319f3728933606 [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;
Devang Patel6404e4e2009-12-15 19:16:48 +000071
Bill Wendling88423ee2009-05-15 00:11:17 +000072 public:
73 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
74 virtual ~DIEAbbrev() {}
75
76 // Accessors.
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; }
84
85 /// AddAttribute - Adds another set of attribute information to the
86 /// abbreviation.
87 void AddAttribute(unsigned Attribute, unsigned Form) {
88 Data.push_back(DIEAbbrevData(Attribute, Form));
89 }
90
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));
95 }
96
97 /// Profile - Used to gather unique data for the abbreviation folding set.
98 ///
99 void Profile(FoldingSetNodeID &ID) const;
100
101 /// Emit - Print the abbreviation using the specified asm printer.
102 ///
103 void Emit(const AsmPrinter *Asm) const;
104
105#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000106 void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000107 void dump();
108#endif
109 };
110
111 //===--------------------------------------------------------------------===//
112 /// DIE - A structured debug information entry. Has an abbreviation which
113 /// describes it's organization.
114 class CompileUnit;
115 class DIEValue;
116
Devang Patel6f01d9c2009-11-21 00:31:03 +0000117 class DIE {
Bill Wendling88423ee2009-05-15 00:11:17 +0000118 protected:
119 /// Abbrev - Buffer for constructing abbreviation.
120 ///
121 DIEAbbrev Abbrev;
122
123 /// Offset - Offset in debug info section.
124 ///
125 unsigned Offset;
126
127 /// Size - Size of instance + children.
128 ///
129 unsigned Size;
130
131 /// Children DIEs.
132 ///
133 std::vector<DIE *> Children;
134
Devang Patel6404e4e2009-12-15 19:16:48 +0000135 DIE *Parent;
136
Bill Wendling88423ee2009-05-15 00:11:17 +0000137 /// Attributes values.
138 ///
139 SmallVector<DIEValue*, 32> Values;
140
Owen Andersond5509f22009-06-24 23:13:56 +0000141 // 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),
Devang Patel6404e4e2009-12-15 19:16:48 +0000146 Size(0), Parent (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; }
Devang Patel6404e4e2009-12-15 19:16:48 +0000157 DIE *getParent() const { return Parent; }
Bill Wendling88423ee2009-05-15 00:11:17 +0000158 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
159 void setOffset(unsigned O) { Offset = O; }
160 void setSize(unsigned S) { Size = S; }
Devang Patel6404e4e2009-12-15 19:16:48 +0000161 void setParent(DIE *P) { Parent = P; }
162
Devang Patel2c4ceb12009-11-21 02:48:08 +0000163 /// addValue - Add a value and attributes to a DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000164 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000165 void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000166 Abbrev.AddAttribute(Attribute, Form);
167 Values.push_back(Value);
168 }
169
170 /// SiblingOffset - Return the offset of the debug information entry's
171 /// sibling.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000172 unsigned getSiblingOffset() const { return Offset + Size; }
Bill Wendling88423ee2009-05-15 00:11:17 +0000173
Devang Patel2c4ceb12009-11-21 02:48:08 +0000174 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000175 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000176 void addSiblingOffset();
Bill Wendling88423ee2009-05-15 00:11:17 +0000177
Devang Patel2c4ceb12009-11-21 02:48:08 +0000178 /// addChild - Add a child to the DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000179 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000180 void addChild(DIE *Child) {
Devang Patel6404e4e2009-12-15 19:16:48 +0000181 if (Child->getParent()) {
182 assert (Child->getParent() == this && "Unexpected DIE Parent!");
183 return;
184 }
Bill Wendling88423ee2009-05-15 00:11:17 +0000185 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
186 Children.push_back(Child);
Devang Patel6404e4e2009-12-15 19:16:48 +0000187 Child->setParent(this);
Bill Wendling88423ee2009-05-15 00:11:17 +0000188 }
189
Bill Wendling88423ee2009-05-15 00:11:17 +0000190#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000191 void print(raw_ostream &O, unsigned IncIndent = 0);
Bill Wendling88423ee2009-05-15 00:11:17 +0000192 void dump();
193#endif
194 };
195
196 //===--------------------------------------------------------------------===//
197 /// DIEValue - A debug information entry value.
198 ///
Devang Patel6f01d9c2009-11-21 00:31:03 +0000199 class DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000200 public:
201 enum {
202 isInteger,
203 isString,
204 isLabel,
205 isAsIsLabel,
206 isSectionOffset,
207 isDelta,
208 isEntry,
209 isBlock
210 };
211 protected:
212 /// Type - Type of data stored in the value.
213 ///
214 unsigned Type;
215 public:
216 explicit DIEValue(unsigned T) : Type(T) {}
217 virtual ~DIEValue() {}
218
219 // Accessors
220 unsigned getType() const { return Type; }
221
222 /// EmitValue - Emit value via the Dwarf writer.
223 ///
224 virtual void EmitValue(Dwarf *D, unsigned Form) const = 0;
225
226 /// SizeOf - Return the size of a value in bytes.
227 ///
228 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const = 0;
229
Bill Wendling88423ee2009-05-15 00:11:17 +0000230 // Implement isa/cast/dyncast.
231 static bool classof(const DIEValue *) { return true; }
232
233#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000234 virtual void print(raw_ostream &O) = 0;
Bill Wendling88423ee2009-05-15 00:11:17 +0000235 void dump();
236#endif
237 };
238
239 //===--------------------------------------------------------------------===//
240 /// DIEInteger - An integer value DIE.
241 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000242 class DIEInteger : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000243 uint64_t Integer;
244 public:
245 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
246
247 /// BestForm - Choose the best form for integer.
248 ///
249 static unsigned BestForm(bool IsSigned, uint64_t Int) {
250 if (IsSigned) {
251 if ((char)Int == (signed)Int) return dwarf::DW_FORM_data1;
252 if ((short)Int == (signed)Int) return dwarf::DW_FORM_data2;
253 if ((int)Int == (signed)Int) return dwarf::DW_FORM_data4;
254 } else {
255 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
256 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
257 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
258 }
259 return dwarf::DW_FORM_data8;
260 }
261
262 /// EmitValue - Emit integer of appropriate size.
263 ///
264 virtual void EmitValue(Dwarf *D, unsigned Form) const;
265
266 /// SizeOf - Determine size of integer value in bytes.
267 ///
268 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
269
Bill Wendling88423ee2009-05-15 00:11:17 +0000270
271 // Implement isa/cast/dyncast.
272 static bool classof(const DIEInteger *) { return true; }
273 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
274
275#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000276 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000277#endif
278 };
279
280 //===--------------------------------------------------------------------===//
Devang Patel69f57b12009-12-02 15:25:16 +0000281 /// DIEString - A string value DIE. This DIE keeps string reference only.
Bill Wendling88423ee2009-05-15 00:11:17 +0000282 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000283 class DIEString : public DIEValue {
Devang Patele9a05972009-11-24 19:42:17 +0000284 const StringRef Str;
Bill Wendling88423ee2009-05-15 00:11:17 +0000285 public:
Devang Patele9a05972009-11-24 19:42:17 +0000286 explicit DIEString(const StringRef S) : DIEValue(isString), Str(S) {}
Bill Wendling88423ee2009-05-15 00:11:17 +0000287
288 /// EmitValue - Emit string value.
289 ///
290 virtual void EmitValue(Dwarf *D, unsigned Form) const;
291
292 /// SizeOf - Determine size of string value in bytes.
293 ///
294 virtual unsigned SizeOf(const TargetData *, unsigned /*Form*/) const {
295 return Str.size() + sizeof(char); // sizeof('\0');
296 }
297
Bill Wendling88423ee2009-05-15 00:11:17 +0000298 // Implement isa/cast/dyncast.
299 static bool classof(const DIEString *) { return true; }
300 static bool classof(const DIEValue *S) { return S->getType() == isString; }
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 //===--------------------------------------------------------------------===//
308 /// DIEDwarfLabel - A Dwarf internal label expression DIE.
309 //
Nick Lewycky381afae2009-11-17 09:17:08 +0000310 class DIEDwarfLabel : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000311 const DWLabel Label;
312 public:
313 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
314
315 /// EmitValue - Emit label value.
316 ///
317 virtual void EmitValue(Dwarf *D, unsigned Form) const;
318
319 /// SizeOf - Determine size of label value in bytes.
320 ///
321 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
322
Bill Wendling88423ee2009-05-15 00:11:17 +0000323 // Implement isa/cast/dyncast.
324 static bool classof(const DIEDwarfLabel *) { return true; }
325 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
326
327#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000328 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000329#endif
330 };
331
332 //===--------------------------------------------------------------------===//
333 /// DIEObjectLabel - A label to an object in code or data.
334 //
Nick Lewycky381afae2009-11-17 09:17:08 +0000335 class DIEObjectLabel : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000336 const std::string Label;
337 public:
338 explicit DIEObjectLabel(const std::string &L)
339 : DIEValue(isAsIsLabel), Label(L) {}
340
341 /// EmitValue - Emit label value.
342 ///
343 virtual void EmitValue(Dwarf *D, unsigned Form) const;
344
345 /// SizeOf - Determine size of label value in bytes.
346 ///
347 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
348
Bill Wendling88423ee2009-05-15 00:11:17 +0000349 // Implement isa/cast/dyncast.
350 static bool classof(const DIEObjectLabel *) { return true; }
351 static bool classof(const DIEValue *L) {
352 return L->getType() == isAsIsLabel;
353 }
354
355#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000356 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000357#endif
358 };
359
360 //===--------------------------------------------------------------------===//
361 /// DIESectionOffset - A section offset DIE.
362 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000363 class DIESectionOffset : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000364 const DWLabel Label;
365 const DWLabel Section;
366 bool IsEH : 1;
367 bool UseSet : 1;
368 public:
369 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
370 bool isEH = false, bool useSet = true)
371 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
372 IsEH(isEH), UseSet(useSet) {}
373
374 /// EmitValue - Emit section offset.
375 ///
376 virtual void EmitValue(Dwarf *D, unsigned Form) const;
377
378 /// SizeOf - Determine size of section offset value in bytes.
379 ///
380 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
381
Bill Wendling88423ee2009-05-15 00:11:17 +0000382 // Implement isa/cast/dyncast.
383 static bool classof(const DIESectionOffset *) { return true; }
384 static bool classof(const DIEValue *D) {
385 return D->getType() == isSectionOffset;
386 }
387
388#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000389 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000390#endif
391 };
392
393 //===--------------------------------------------------------------------===//
394 /// DIEDelta - A simple label difference DIE.
395 ///
Nick Lewycky381afae2009-11-17 09:17:08 +0000396 class DIEDelta : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000397 const DWLabel LabelHi;
398 const DWLabel LabelLo;
399 public:
400 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
401 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
402
403 /// EmitValue - Emit delta value.
404 ///
405 virtual void EmitValue(Dwarf *D, unsigned Form) const;
406
407 /// SizeOf - Determine size of delta value in bytes.
408 ///
409 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
410
Bill Wendling88423ee2009-05-15 00:11:17 +0000411 // Implement isa/cast/dyncast.
412 static bool classof(const DIEDelta *) { return true; }
413 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
414
415#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000416 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000417#endif
418 };
419
420 //===--------------------------------------------------------------------===//
421 /// DIEntry - A pointer to another debug information entry. An instance of
422 /// this class can also be used as a proxy for a debug information entry not
423 /// yet defined (ie. types.)
Nick Lewycky381afae2009-11-17 09:17:08 +0000424 class DIEEntry : public DIEValue {
Bill Wendling88423ee2009-05-15 00:11:17 +0000425 DIE *Entry;
426 public:
427 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
428
429 DIE *getEntry() const { return Entry; }
430 void setEntry(DIE *E) { Entry = E; }
431
432 /// EmitValue - Emit debug information entry offset.
433 ///
434 virtual void EmitValue(Dwarf *D, unsigned Form) const;
435
436 /// SizeOf - Determine size of debug information entry in bytes.
437 ///
438 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const {
439 return sizeof(int32_t);
440 }
441
Bill Wendling88423ee2009-05-15 00:11:17 +0000442 // Implement isa/cast/dyncast.
443 static bool classof(const DIEEntry *) { return true; }
444 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
445
446#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000447 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000448#endif
449 };
450
451 //===--------------------------------------------------------------------===//
452 /// DIEBlock - A block of values. Primarily used for location expressions.
453 //
Nick Lewycky381afae2009-11-17 09:17:08 +0000454 class DIEBlock : public DIEValue, public DIE {
Bill Wendling88423ee2009-05-15 00:11:17 +0000455 unsigned Size; // Size in bytes excluding size header.
456 public:
457 DIEBlock()
458 : DIEValue(isBlock), DIE(0), Size(0) {}
459 virtual ~DIEBlock() {}
460
461 /// ComputeSize - calculate the size of the block.
462 ///
463 unsigned ComputeSize(const TargetData *TD);
464
465 /// BestForm - Choose the best form for data.
466 ///
467 unsigned BestForm() const {
468 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
469 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
470 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
471 return dwarf::DW_FORM_block;
472 }
473
474 /// EmitValue - Emit block data.
475 ///
476 virtual void EmitValue(Dwarf *D, unsigned Form) const;
477
478 /// SizeOf - Determine size of block data in bytes.
479 ///
480 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
481
Bill Wendling88423ee2009-05-15 00:11:17 +0000482 // Implement isa/cast/dyncast.
483 static bool classof(const DIEBlock *) { return true; }
484 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
485
486#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000487 virtual void print(raw_ostream &O);
Bill Wendling88423ee2009-05-15 00:11:17 +0000488#endif
489 };
490
491} // end llvm namespace
492
493#endif