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 /// DIEValue - A debug information entry value. Some of these roughly correlate
109 /// to DWARF attribute classes.
127 /// Ty - Type of data stored in the value.
132 explicit DIEValue(Type T) : Ty(T) {}
137 Type getType() const { return Ty; }
139 /// EmitValue - Emit value via the Dwarf writer.
141 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
143 /// SizeOf - Return the size of a value in bytes.
145 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
148 void print(raw_ostream &O) const;
153 //===--------------------------------------------------------------------===//
154 /// DIEInteger - An integer value DIE.
156 class DIEInteger : public DIEValue {
162 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
164 /// BestForm - Choose the best form for integer.
166 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
168 const int64_t SignedInt = Int;
169 if ((char)Int == SignedInt)
170 return dwarf::DW_FORM_data1;
171 if ((short)Int == SignedInt)
172 return dwarf::DW_FORM_data2;
173 if ((int)Int == SignedInt)
174 return dwarf::DW_FORM_data4;
176 if ((unsigned char)Int == Int)
177 return dwarf::DW_FORM_data1;
178 if ((unsigned short)Int == Int)
179 return dwarf::DW_FORM_data2;
180 if ((unsigned int)Int == Int)
181 return dwarf::DW_FORM_data4;
183 return dwarf::DW_FORM_data8;
186 uint64_t getValue() const { return Integer; }
187 void setValue(uint64_t Val) { Integer = Val; }
189 // Implement isa/cast/dyncast.
190 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
193 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
194 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
197 void printImpl(raw_ostream &O) const;
201 //===--------------------------------------------------------------------===//
202 /// DIEExpr - An expression DIE.
204 class DIEExpr : public DIEValue {
205 friend class DIEValue;
210 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
212 /// getValue - Get MCExpr.
214 const MCExpr *getValue() const { return Expr; }
216 // Implement isa/cast/dyncast.
217 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
220 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
221 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
224 void printImpl(raw_ostream &O) const;
228 //===--------------------------------------------------------------------===//
229 /// DIELabel - A label DIE.
231 class DIELabel : public DIEValue {
232 friend class DIEValue;
234 const MCSymbol *Label;
237 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
239 /// getValue - Get MCSymbol.
241 const MCSymbol *getValue() const { return Label; }
243 // Implement isa/cast/dyncast.
244 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
247 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
248 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
251 void printImpl(raw_ostream &O) const;
255 //===--------------------------------------------------------------------===//
256 /// DIEDelta - A simple label difference DIE.
258 class DIEDelta : public DIEValue {
259 friend class DIEValue;
261 const MCSymbol *LabelHi;
262 const MCSymbol *LabelLo;
265 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
266 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
268 // Implement isa/cast/dyncast.
269 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
272 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
273 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
276 void printImpl(raw_ostream &O) const;
280 //===--------------------------------------------------------------------===//
281 /// DIEString - A container for string values.
283 class DIEString : public DIEValue {
284 friend class DIEValue;
286 DwarfStringPoolEntryRef S;
289 DIEString(DwarfStringPoolEntryRef S) : DIEValue(isString), S(S) {}
291 /// getString - Grab the string out of the object.
292 StringRef getString() const { return S.getString(); }
294 // Implement isa/cast/dyncast.
295 static bool classof(const DIEValue *D) { return D->getType() == isString; }
298 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
299 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
302 void printImpl(raw_ostream &O) const;
306 //===--------------------------------------------------------------------===//
307 /// DIEEntry - A pointer to another debug information entry. An instance of
308 /// this class can also be used as a proxy for a debug information entry not
309 /// yet defined (ie. types.)
311 class DIEEntry : public DIEValue {
312 friend class DIEValue;
317 explicit DIEEntry(DIE &E) : DIEValue(isEntry), Entry(E) {
320 DIE &getEntry() const { return Entry; }
322 /// Returns size of a ref_addr entry.
323 static unsigned getRefAddrSize(const AsmPrinter *AP);
325 // Implement isa/cast/dyncast.
326 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
329 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
330 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
331 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
336 void printImpl(raw_ostream &O) const;
340 //===--------------------------------------------------------------------===//
341 /// \brief A signature reference to a type unit.
342 class DIETypeSignature : public DIEValue {
343 friend class DIEValue;
345 const DwarfTypeUnit &Unit;
348 explicit DIETypeSignature(const DwarfTypeUnit &Unit)
349 : DIEValue(isTypeSignature), Unit(Unit) {}
351 // \brief Implement isa/cast/dyncast.
352 static bool classof(const DIEValue *E) {
353 return E->getType() == isTypeSignature;
357 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
358 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
359 assert(Form == dwarf::DW_FORM_ref_sig8);
364 void printImpl(raw_ostream &O) const;
368 //===--------------------------------------------------------------------===//
369 /// DIELocList - Represents a pointer to a location list in the debug_loc
372 class DIELocList : public DIEValue {
373 friend class DIEValue;
375 // Index into the .debug_loc vector.
379 DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
381 /// getValue - Grab the current index out.
382 size_t getValue() const { return Index; }
384 // Implement isa/cast/dyncast.
385 static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
388 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
389 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
392 void printImpl(raw_ostream &O) const;
396 //===--------------------------------------------------------------------===//
397 /// DIE - A structured debug information entry. Has an abbreviation which
398 /// describes its organization.
401 /// Offset - Offset in debug info section.
405 /// Size - Size of instance + children.
409 /// Abbrev - Buffer for constructing abbreviation.
415 // This can't be a vector<DIE> because pointer validity is requirent for the
416 // Parent pointer and DIEEntry.
417 // It can't be a list<DIE> because some clients need pointer validity before
418 // the object has been added to any child list
419 // (eg: DwarfUnit::constructVariableDIE). These aren't insurmountable, but may
420 // be more convoluted than beneficial.
421 std::vector<std::unique_ptr<DIE>> Children;
425 /// Attribute values.
427 SmallVector<DIEValue *, 12> Values;
431 : Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
435 explicit DIE(dwarf::Tag Tag)
436 : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
440 DIEAbbrev &getAbbrev() { return Abbrev; }
441 const DIEAbbrev &getAbbrev() const { return Abbrev; }
442 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
443 dwarf::Tag getTag() const { return Abbrev.getTag(); }
444 unsigned getOffset() const { return Offset; }
445 unsigned getSize() const { return Size; }
446 const std::vector<std::unique_ptr<DIE>> &getChildren() const {
449 const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
450 DIE *getParent() const { return Parent; }
451 /// Climb up the parent chain to get the compile or type unit DIE this DIE
453 const DIE *getUnit() const;
454 /// Similar to getUnit, returns null when DIE is not added to an
456 const DIE *getUnitOrNull() const;
457 void setOffset(unsigned O) { Offset = O; }
458 void setSize(unsigned S) { Size = S; }
460 /// addValue - Add a value and attributes to a DIE.
462 void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
463 Abbrev.AddAttribute(Attribute, Form);
464 Values.push_back(Value);
467 /// addChild - Add a child to the DIE.
469 void addChild(std::unique_ptr<DIE> Child) {
470 assert(!Child->getParent());
471 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
472 Child->Parent = this;
473 Children.push_back(std::move(Child));
476 /// findAttribute - Find a value in the DIE with the attribute given,
477 /// returns NULL if no such attribute exists.
478 DIEValue *findAttribute(dwarf::Attribute Attribute) const;
481 void print(raw_ostream &O, unsigned IndentCount = 0) const;
486 //===--------------------------------------------------------------------===//
487 /// DIELoc - Represents an expression location.
489 class DIELoc : public DIEValue, public DIE {
490 friend class DIEValue;
492 mutable unsigned Size; // Size in bytes excluding size header.
494 DIELoc() : DIEValue(isLoc), Size(0) {}
496 /// ComputeSize - Calculate the size of the location expression.
498 unsigned ComputeSize(const AsmPrinter *AP) const;
500 /// BestForm - Choose the best form for data.
502 dwarf::Form BestForm(unsigned DwarfVersion) const {
503 if (DwarfVersion > 3)
504 return dwarf::DW_FORM_exprloc;
505 // Pre-DWARF4 location expressions were blocks and not exprloc.
506 if ((unsigned char)Size == Size)
507 return dwarf::DW_FORM_block1;
508 if ((unsigned short)Size == Size)
509 return dwarf::DW_FORM_block2;
510 if ((unsigned int)Size == Size)
511 return dwarf::DW_FORM_block4;
512 return dwarf::DW_FORM_block;
515 // Implement isa/cast/dyncast.
516 static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
519 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
520 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
523 void printImpl(raw_ostream &O) const;
527 //===--------------------------------------------------------------------===//
528 /// DIEBlock - Represents a block of values.
530 class DIEBlock : public DIEValue, public DIE {
531 friend class DIEValue;
533 mutable unsigned Size; // Size in bytes excluding size header.
535 DIEBlock() : DIEValue(isBlock), Size(0) {}
537 /// ComputeSize - Calculate the size of the location expression.
539 unsigned ComputeSize(const AsmPrinter *AP) const;
541 /// BestForm - Choose the best form for data.
543 dwarf::Form BestForm() const {
544 if ((unsigned char)Size == Size)
545 return dwarf::DW_FORM_block1;
546 if ((unsigned short)Size == Size)
547 return dwarf::DW_FORM_block2;
548 if ((unsigned int)Size == Size)
549 return dwarf::DW_FORM_block4;
550 return dwarf::DW_FORM_block;
553 // Implement isa/cast/dyncast.
554 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
557 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
558 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
561 void printImpl(raw_ostream &O) const;
565 } // end llvm namespace