AsmPrinter: Make DIEString small
[oota-llvm.git] / include / llvm / CodeGen / DIE.h
1 //===--- 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
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
16
17 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
20 #include "llvm/Support/Dwarf.h"
21 #include <vector>
22
23 namespace llvm {
24 class AsmPrinter;
25 class MCExpr;
26 class MCSymbol;
27 class raw_ostream;
28 class DwarfTypeUnit;
29
30 //===--------------------------------------------------------------------===//
31 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
32 /// Dwarf abbreviation.
33 class DIEAbbrevData {
34   /// Attribute - Dwarf attribute code.
35   ///
36   dwarf::Attribute Attribute;
37
38   /// Form - Dwarf form code.
39   ///
40   dwarf::Form Form;
41
42 public:
43   DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
44
45   // Accessors.
46   dwarf::Attribute getAttribute() const { return Attribute; }
47   dwarf::Form getForm() const { return Form; }
48
49   /// Profile - Used to gather unique data for the abbreviation folding set.
50   ///
51   void Profile(FoldingSetNodeID &ID) const;
52 };
53
54 //===--------------------------------------------------------------------===//
55 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
56 /// information object.
57 class DIEAbbrev : public FoldingSetNode {
58   /// Unique number for node.
59   ///
60   unsigned Number;
61
62   /// Tag - Dwarf tag code.
63   ///
64   dwarf::Tag Tag;
65
66   /// Children - Whether or not this node has children.
67   ///
68   // This cheats a bit in all of the uses since the values in the standard
69   // are 0 and 1 for no children and children respectively.
70   bool Children;
71
72   /// Data - Raw data bytes for abbreviation.
73   ///
74   SmallVector<DIEAbbrevData, 12> Data;
75
76 public:
77   DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
78
79   // Accessors.
80   dwarf::Tag getTag() const { return Tag; }
81   unsigned getNumber() const { return Number; }
82   bool hasChildren() const { return Children; }
83   const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
84   void setChildrenFlag(bool hasChild) { Children = hasChild; }
85   void setNumber(unsigned N) { Number = N; }
86
87   /// AddAttribute - Adds another set of attribute information to the
88   /// abbreviation.
89   void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
90     Data.push_back(DIEAbbrevData(Attribute, Form));
91   }
92
93   /// Profile - Used to gather unique data for the abbreviation folding set.
94   ///
95   void Profile(FoldingSetNodeID &ID) const;
96
97   /// Emit - Print the abbreviation using the specified asm printer.
98   ///
99   void Emit(const AsmPrinter *AP) const;
100
101 #ifndef NDEBUG
102   void print(raw_ostream &O);
103   void dump();
104 #endif
105 };
106
107 //===--------------------------------------------------------------------===//
108 /// DIE - A structured debug information entry.  Has an abbreviation which
109 /// describes its organization.
110 class DIEValue;
111
112 class DIE {
113 protected:
114   /// Offset - Offset in debug info section.
115   ///
116   unsigned Offset;
117
118   /// Size - Size of instance + children.
119   ///
120   unsigned Size;
121
122   /// Abbrev - Buffer for constructing abbreviation.
123   ///
124   DIEAbbrev Abbrev;
125
126   /// Children DIEs.
127   ///
128   // This can't be a vector<DIE> because pointer validity is requirent for the
129   // Parent pointer and DIEEntry.
130   // It can't be a list<DIE> because some clients need pointer validity before
131   // the object has been added to any child list
132   // (eg: DwarfUnit::constructVariableDIE). These aren't insurmountable, but may
133   // be more convoluted than beneficial.
134   std::vector<std::unique_ptr<DIE>> Children;
135
136   DIE *Parent;
137
138   /// Attribute values.
139   ///
140   SmallVector<DIEValue *, 12> Values;
141
142 protected:
143   DIE()
144       : Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
145         Parent(nullptr) {}
146
147 public:
148   explicit DIE(dwarf::Tag Tag)
149       : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
150         Parent(nullptr) {}
151
152   // Accessors.
153   DIEAbbrev &getAbbrev() { return Abbrev; }
154   const DIEAbbrev &getAbbrev() const { return Abbrev; }
155   unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
156   dwarf::Tag getTag() const { return Abbrev.getTag(); }
157   unsigned getOffset() const { return Offset; }
158   unsigned getSize() const { return Size; }
159   const std::vector<std::unique_ptr<DIE>> &getChildren() const {
160     return Children;
161   }
162   const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
163   DIE *getParent() const { return Parent; }
164   /// Climb up the parent chain to get the compile or type unit DIE this DIE
165   /// belongs to.
166   const DIE *getUnit() const;
167   /// Similar to getUnit, returns null when DIE is not added to an
168   /// owner yet.
169   const DIE *getUnitOrNull() const;
170   void setOffset(unsigned O) { Offset = O; }
171   void setSize(unsigned S) { Size = S; }
172
173   /// addValue - Add a value and attributes to a DIE.
174   ///
175   void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
176     Abbrev.AddAttribute(Attribute, Form);
177     Values.push_back(Value);
178   }
179
180   /// addChild - Add a child to the DIE.
181   ///
182   void addChild(std::unique_ptr<DIE> Child) {
183     assert(!Child->getParent());
184     Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
185     Child->Parent = this;
186     Children.push_back(std::move(Child));
187   }
188
189   /// findAttribute - Find a value in the DIE with the attribute given,
190   /// returns NULL if no such attribute exists.
191   DIEValue *findAttribute(dwarf::Attribute Attribute) const;
192
193 #ifndef NDEBUG
194   void print(raw_ostream &O, unsigned IndentCount = 0) const;
195   void dump();
196 #endif
197 };
198
199 //===--------------------------------------------------------------------===//
200 /// DIEValue - A debug information entry value. Some of these roughly correlate
201 /// to DWARF attribute classes.
202 ///
203 class DIEValue {
204 public:
205   enum Type {
206     isInteger,
207     isString,
208     isExpr,
209     isLabel,
210     isDelta,
211     isEntry,
212     isTypeSignature,
213     isBlock,
214     isLoc,
215     isLocList,
216   };
217
218 protected:
219   /// Ty - Type of data stored in the value.
220   ///
221   Type Ty;
222
223   explicit DIEValue(Type T) : Ty(T) {}
224   ~DIEValue() {}
225
226 public:
227   // Accessors
228   Type getType() const { return Ty; }
229
230   /// EmitValue - Emit value via the Dwarf writer.
231   ///
232   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
233
234   /// SizeOf - Return the size of a value in bytes.
235   ///
236   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
237
238 #ifndef NDEBUG
239   void print(raw_ostream &O) const;
240   void dump() const;
241 #endif
242 };
243
244 //===--------------------------------------------------------------------===//
245 /// DIEInteger - An integer value DIE.
246 ///
247 class DIEInteger : public DIEValue {
248   friend DIEValue;
249
250   uint64_t Integer;
251
252 public:
253   explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
254
255   /// BestForm - Choose the best form for integer.
256   ///
257   static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
258     if (IsSigned) {
259       const int64_t SignedInt = Int;
260       if ((char)Int == SignedInt)
261         return dwarf::DW_FORM_data1;
262       if ((short)Int == SignedInt)
263         return dwarf::DW_FORM_data2;
264       if ((int)Int == SignedInt)
265         return dwarf::DW_FORM_data4;
266     } else {
267       if ((unsigned char)Int == Int)
268         return dwarf::DW_FORM_data1;
269       if ((unsigned short)Int == Int)
270         return dwarf::DW_FORM_data2;
271       if ((unsigned int)Int == Int)
272         return dwarf::DW_FORM_data4;
273     }
274     return dwarf::DW_FORM_data8;
275   }
276
277   uint64_t getValue() const { return Integer; }
278   void setValue(uint64_t Val) { Integer = Val; }
279
280   // Implement isa/cast/dyncast.
281   static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
282
283 private:
284   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
285   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
286
287 #ifndef NDEBUG
288   void printImpl(raw_ostream &O) const;
289 #endif
290 };
291
292 //===--------------------------------------------------------------------===//
293 /// DIEExpr - An expression DIE.
294 //
295 class DIEExpr : public DIEValue {
296   friend class DIEValue;
297
298   const MCExpr *Expr;
299
300 public:
301   explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
302
303   /// getValue - Get MCExpr.
304   ///
305   const MCExpr *getValue() const { return Expr; }
306
307   // Implement isa/cast/dyncast.
308   static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
309
310 private:
311   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
312   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
313
314 #ifndef NDEBUG
315   void printImpl(raw_ostream &O) const;
316 #endif
317 };
318
319 //===--------------------------------------------------------------------===//
320 /// DIELabel - A label DIE.
321 //
322 class DIELabel : public DIEValue {
323   friend class DIEValue;
324
325   const MCSymbol *Label;
326
327 public:
328   explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
329
330   /// getValue - Get MCSymbol.
331   ///
332   const MCSymbol *getValue() const { return Label; }
333
334   // Implement isa/cast/dyncast.
335   static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
336
337 private:
338   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
339   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
340
341 #ifndef NDEBUG
342   void printImpl(raw_ostream &O) const;
343 #endif
344 };
345
346 //===--------------------------------------------------------------------===//
347 /// DIEDelta - A simple label difference DIE.
348 ///
349 class DIEDelta : public DIEValue {
350   friend class DIEValue;
351
352   const MCSymbol *LabelHi;
353   const MCSymbol *LabelLo;
354
355 public:
356   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
357       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
358
359   // Implement isa/cast/dyncast.
360   static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
361
362 private:
363   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
364   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
365
366 #ifndef NDEBUG
367   void printImpl(raw_ostream &O) const;
368 #endif
369 };
370
371 //===--------------------------------------------------------------------===//
372 /// DIEString - A container for string values.
373 ///
374 class DIEString : public DIEValue {
375   friend class DIEValue;
376
377   DwarfStringPoolEntryRef S;
378
379 public:
380   DIEString(DwarfStringPoolEntryRef S) : DIEValue(isString), S(S) {}
381
382   /// getString - Grab the string out of the object.
383   StringRef getString() const { return S.getString(); }
384
385   // Implement isa/cast/dyncast.
386   static bool classof(const DIEValue *D) { return D->getType() == isString; }
387
388 private:
389   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
390   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
391
392 #ifndef NDEBUG
393   void printImpl(raw_ostream &O) const;
394 #endif
395 };
396
397 //===--------------------------------------------------------------------===//
398 /// DIEEntry - A pointer to another debug information entry.  An instance of
399 /// this class can also be used as a proxy for a debug information entry not
400 /// yet defined (ie. types.)
401 class DIEEntry : public DIEValue {
402   friend class DIEValue;
403
404   DIE &Entry;
405
406 public:
407   explicit DIEEntry(DIE &E) : DIEValue(isEntry), Entry(E) {
408   }
409
410   DIE &getEntry() const { return Entry; }
411
412   /// Returns size of a ref_addr entry.
413   static unsigned getRefAddrSize(const AsmPrinter *AP);
414
415   // Implement isa/cast/dyncast.
416   static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
417
418 private:
419   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
420   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
421     return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
422                                            : sizeof(int32_t);
423   }
424
425 #ifndef NDEBUG
426   void printImpl(raw_ostream &O) const;
427 #endif
428 };
429
430 //===--------------------------------------------------------------------===//
431 /// \brief A signature reference to a type unit.
432 class DIETypeSignature : public DIEValue {
433   friend class DIEValue;
434
435   const DwarfTypeUnit &Unit;
436
437 public:
438   explicit DIETypeSignature(const DwarfTypeUnit &Unit)
439       : DIEValue(isTypeSignature), Unit(Unit) {}
440
441   // \brief Implement isa/cast/dyncast.
442   static bool classof(const DIEValue *E) {
443     return E->getType() == isTypeSignature;
444   }
445
446 private:
447   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
448   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
449     assert(Form == dwarf::DW_FORM_ref_sig8);
450     return 8;
451   }
452
453 #ifndef NDEBUG
454   void printImpl(raw_ostream &O) const;
455 #endif
456 };
457
458 //===--------------------------------------------------------------------===//
459 /// DIELoc - Represents an expression location.
460 //
461 class DIELoc : public DIEValue, public DIE {
462   friend class DIEValue;
463
464   mutable unsigned Size; // Size in bytes excluding size header.
465 public:
466   DIELoc() : DIEValue(isLoc), Size(0) {}
467
468   /// ComputeSize - Calculate the size of the location expression.
469   ///
470   unsigned ComputeSize(const AsmPrinter *AP) const;
471
472   /// BestForm - Choose the best form for data.
473   ///
474   dwarf::Form BestForm(unsigned DwarfVersion) const {
475     if (DwarfVersion > 3)
476       return dwarf::DW_FORM_exprloc;
477     // Pre-DWARF4 location expressions were blocks and not exprloc.
478     if ((unsigned char)Size == Size)
479       return dwarf::DW_FORM_block1;
480     if ((unsigned short)Size == Size)
481       return dwarf::DW_FORM_block2;
482     if ((unsigned int)Size == Size)
483       return dwarf::DW_FORM_block4;
484     return dwarf::DW_FORM_block;
485   }
486
487   // Implement isa/cast/dyncast.
488   static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
489
490 private:
491   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
492   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
493
494 #ifndef NDEBUG
495   void printImpl(raw_ostream &O) const;
496 #endif
497 };
498
499 //===--------------------------------------------------------------------===//
500 /// DIEBlock - Represents a block of values.
501 //
502 class DIEBlock : public DIEValue, public DIE {
503   friend class DIEValue;
504
505   mutable unsigned Size; // Size in bytes excluding size header.
506 public:
507   DIEBlock() : DIEValue(isBlock), Size(0) {}
508
509   /// ComputeSize - Calculate the size of the location expression.
510   ///
511   unsigned ComputeSize(const AsmPrinter *AP) const;
512
513   /// BestForm - Choose the best form for data.
514   ///
515   dwarf::Form BestForm() const {
516     if ((unsigned char)Size == Size)
517       return dwarf::DW_FORM_block1;
518     if ((unsigned short)Size == Size)
519       return dwarf::DW_FORM_block2;
520     if ((unsigned int)Size == Size)
521       return dwarf::DW_FORM_block4;
522     return dwarf::DW_FORM_block;
523   }
524
525   // Implement isa/cast/dyncast.
526   static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
527
528 private:
529   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
530   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
531
532 #ifndef NDEBUG
533   void printImpl(raw_ostream &O) const;
534 #endif
535 };
536
537 //===--------------------------------------------------------------------===//
538 /// DIELocList - Represents a pointer to a location list in the debug_loc
539 /// section.
540 //
541 class DIELocList : public DIEValue {
542   friend class DIEValue;
543
544   // Index into the .debug_loc vector.
545   size_t Index;
546
547 public:
548   DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
549
550   /// getValue - Grab the current index out.
551   size_t getValue() const { return Index; }
552
553   // Implement isa/cast/dyncast.
554   static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
555
556 private:
557   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
558   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
559
560 #ifndef NDEBUG
561   void printImpl(raw_ostream &O) const;
562 #endif
563 };
564
565 } // end llvm namespace
566
567 #endif