1 //===--- lib/CodeGen/DIE.h - DWARF Info Entries -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Data structures for DWARF info entries.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
17 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
20 #include "llvm/Support/Dwarf.h"
30 //===--------------------------------------------------------------------===//
31 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
32 /// Dwarf abbreviation.
34 /// Attribute - Dwarf attribute code.
36 dwarf::Attribute Attribute;
38 /// Form - Dwarf form code.
43 DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
46 dwarf::Attribute getAttribute() const { return Attribute; }
47 dwarf::Form getForm() const { return Form; }
49 /// Profile - Used to gather unique data for the abbreviation folding set.
51 void Profile(FoldingSetNodeID &ID) const;
54 //===--------------------------------------------------------------------===//
55 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
56 /// information object.
57 class DIEAbbrev : public FoldingSetNode {
58 /// Unique number for node.
62 /// Tag - Dwarf tag code.
66 /// Children - Whether or not this node has children.
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.
72 /// Data - Raw data bytes for abbreviation.
74 SmallVector<DIEAbbrevData, 12> Data;
77 DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
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; }
87 /// AddAttribute - Adds another set of attribute information to the
89 void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
90 Data.push_back(DIEAbbrevData(Attribute, Form));
93 /// Profile - Used to gather unique data for the abbreviation folding set.
95 void Profile(FoldingSetNodeID &ID) const;
97 /// Emit - Print the abbreviation using the specified asm printer.
99 void Emit(const AsmPrinter *AP) const;
102 void print(raw_ostream &O);
107 //===--------------------------------------------------------------------===//
108 /// DIE - A structured debug information entry. Has an abbreviation which
109 /// describes its organization.
114 /// Offset - Offset in debug info section.
118 /// Size - Size of instance + children.
122 /// Abbrev - Buffer for constructing abbreviation.
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;
138 /// Attribute values.
140 SmallVector<DIEValue *, 12> Values;
144 : Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
148 explicit DIE(dwarf::Tag Tag)
149 : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
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 {
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
166 const DIE *getUnit() const;
167 /// Similar to getUnit, returns null when DIE is not added to an
169 const DIE *getUnitOrNull() const;
170 void setOffset(unsigned O) { Offset = O; }
171 void setSize(unsigned S) { Size = S; }
173 /// addValue - Add a value and attributes to a DIE.
175 void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
176 Abbrev.AddAttribute(Attribute, Form);
177 Values.push_back(Value);
180 /// addChild - Add a child to the DIE.
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));
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;
194 void print(raw_ostream &O, unsigned IndentCount = 0) const;
199 //===--------------------------------------------------------------------===//
200 /// DIEValue - A debug information entry value. Some of these roughly correlate
201 /// to DWARF attribute classes.
219 /// Ty - Type of data stored in the value.
224 explicit DIEValue(Type T) : Ty(T) {}
229 Type getType() const { return Ty; }
231 /// EmitValue - Emit value via the Dwarf writer.
233 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
235 /// SizeOf - Return the size of a value in bytes.
237 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
240 void print(raw_ostream &O) const;
245 //===--------------------------------------------------------------------===//
246 /// DIEInteger - An integer value DIE.
248 class DIEInteger : public DIEValue {
254 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
256 /// BestForm - Choose the best form for integer.
258 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
260 const int64_t SignedInt = Int;
261 if ((char)Int == SignedInt)
262 return dwarf::DW_FORM_data1;
263 if ((short)Int == SignedInt)
264 return dwarf::DW_FORM_data2;
265 if ((int)Int == SignedInt)
266 return dwarf::DW_FORM_data4;
268 if ((unsigned char)Int == Int)
269 return dwarf::DW_FORM_data1;
270 if ((unsigned short)Int == Int)
271 return dwarf::DW_FORM_data2;
272 if ((unsigned int)Int == Int)
273 return dwarf::DW_FORM_data4;
275 return dwarf::DW_FORM_data8;
278 uint64_t getValue() const { return Integer; }
279 void setValue(uint64_t Val) { Integer = Val; }
281 // Implement isa/cast/dyncast.
282 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
285 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
286 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
289 void printImpl(raw_ostream &O) const;
293 //===--------------------------------------------------------------------===//
294 /// DIEExpr - An expression DIE.
296 class DIEExpr : public DIEValue {
297 friend class DIEValue;
302 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
304 /// getValue - Get MCExpr.
306 const MCExpr *getValue() const { return Expr; }
308 // Implement isa/cast/dyncast.
309 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
312 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
313 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
316 void printImpl(raw_ostream &O) const;
320 //===--------------------------------------------------------------------===//
321 /// DIELabel - A label DIE.
323 class DIELabel : public DIEValue {
324 friend class DIEValue;
326 const MCSymbol *Label;
329 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
331 /// getValue - Get MCSymbol.
333 const MCSymbol *getValue() const { return Label; }
335 // Implement isa/cast/dyncast.
336 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
339 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
340 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
343 void printImpl(raw_ostream &O) const;
347 //===--------------------------------------------------------------------===//
348 /// DIEDelta - A simple label difference DIE.
350 class DIEDelta : public DIEValue {
351 friend class DIEValue;
353 const MCSymbol *LabelHi;
354 const MCSymbol *LabelLo;
357 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
358 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
360 // Implement isa/cast/dyncast.
361 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
364 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
365 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
368 void printImpl(raw_ostream &O) const;
372 //===--------------------------------------------------------------------===//
373 /// DIEString - A container for string values.
375 class DIEString : public DIEValue {
376 friend class DIEValue;
378 DwarfStringPoolEntryRef S;
381 DIEString(DwarfStringPoolEntryRef S) : DIEValue(isString), S(S) {}
383 /// getString - Grab the string out of the object.
384 StringRef getString() const { return S.getString(); }
386 // Implement isa/cast/dyncast.
387 static bool classof(const DIEValue *D) { return D->getType() == isString; }
390 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
391 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
394 void printImpl(raw_ostream &O) const;
398 //===--------------------------------------------------------------------===//
399 /// DIEEntry - A pointer to another debug information entry. An instance of
400 /// this class can also be used as a proxy for a debug information entry not
401 /// yet defined (ie. types.)
402 class DIEEntry : public DIEValue {
403 friend class DIEValue;
408 explicit DIEEntry(DIE &E) : DIEValue(isEntry), Entry(E) {
411 DIE &getEntry() const { return Entry; }
413 /// Returns size of a ref_addr entry.
414 static unsigned getRefAddrSize(const AsmPrinter *AP);
416 // Implement isa/cast/dyncast.
417 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
420 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
421 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
422 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
427 void printImpl(raw_ostream &O) const;
431 //===--------------------------------------------------------------------===//
432 /// \brief A signature reference to a type unit.
433 class DIETypeSignature : public DIEValue {
434 friend class DIEValue;
436 const DwarfTypeUnit &Unit;
439 explicit DIETypeSignature(const DwarfTypeUnit &Unit)
440 : DIEValue(isTypeSignature), Unit(Unit) {}
442 // \brief Implement isa/cast/dyncast.
443 static bool classof(const DIEValue *E) {
444 return E->getType() == isTypeSignature;
448 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
449 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
450 assert(Form == dwarf::DW_FORM_ref_sig8);
455 void printImpl(raw_ostream &O) const;
459 //===--------------------------------------------------------------------===//
460 /// DIELoc - Represents an expression location.
462 class DIELoc : public DIEValue, public DIE {
463 friend class DIEValue;
465 mutable unsigned Size; // Size in bytes excluding size header.
467 DIELoc() : DIEValue(isLoc), Size(0) {}
469 /// ComputeSize - Calculate the size of the location expression.
471 unsigned ComputeSize(const AsmPrinter *AP) const;
473 /// BestForm - Choose the best form for data.
475 dwarf::Form BestForm(unsigned DwarfVersion) const {
476 if (DwarfVersion > 3)
477 return dwarf::DW_FORM_exprloc;
478 // Pre-DWARF4 location expressions were blocks and not exprloc.
479 if ((unsigned char)Size == Size)
480 return dwarf::DW_FORM_block1;
481 if ((unsigned short)Size == Size)
482 return dwarf::DW_FORM_block2;
483 if ((unsigned int)Size == Size)
484 return dwarf::DW_FORM_block4;
485 return dwarf::DW_FORM_block;
488 // Implement isa/cast/dyncast.
489 static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
492 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
493 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
496 void printImpl(raw_ostream &O) const;
500 //===--------------------------------------------------------------------===//
501 /// DIEBlock - Represents a block of values.
503 class DIEBlock : public DIEValue, public DIE {
504 friend class DIEValue;
506 mutable unsigned Size; // Size in bytes excluding size header.
508 DIEBlock() : DIEValue(isBlock), Size(0) {}
510 /// ComputeSize - Calculate the size of the location expression.
512 unsigned ComputeSize(const AsmPrinter *AP) const;
514 /// BestForm - Choose the best form for data.
516 dwarf::Form BestForm() const {
517 if ((unsigned char)Size == Size)
518 return dwarf::DW_FORM_block1;
519 if ((unsigned short)Size == Size)
520 return dwarf::DW_FORM_block2;
521 if ((unsigned int)Size == Size)
522 return dwarf::DW_FORM_block4;
523 return dwarf::DW_FORM_block;
526 // Implement isa/cast/dyncast.
527 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
530 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
531 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
534 void printImpl(raw_ostream &O) const;
538 //===--------------------------------------------------------------------===//
539 /// DIELocList - Represents a pointer to a location list in the debug_loc
542 class DIELocList : public DIEValue {
543 friend class DIEValue;
545 // Index into the .debug_loc vector.
549 DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
551 /// getValue - Grab the current index out.
552 size_t getValue() const { return Index; }
554 // Implement isa/cast/dyncast.
555 static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
558 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
559 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
562 void printImpl(raw_ostream &O) const;
566 } // end llvm namespace