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.
223 explicit DIEValue(Type T) : Ty(T) {}
228 Type getType() const { return Ty; }
230 /// EmitValue - Emit value via the Dwarf writer.
232 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
234 /// SizeOf - Return the size of a value in bytes.
236 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
239 void print(raw_ostream &O) const;
244 //===--------------------------------------------------------------------===//
245 /// DIEInteger - An integer value DIE.
247 class DIEInteger : public DIEValue {
253 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
255 /// BestForm - Choose the best form for integer.
257 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
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;
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;
274 return dwarf::DW_FORM_data8;
277 uint64_t getValue() const { return Integer; }
278 void setValue(uint64_t Val) { Integer = Val; }
280 // Implement isa/cast/dyncast.
281 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
284 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
285 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
288 void printImpl(raw_ostream &O) const;
292 //===--------------------------------------------------------------------===//
293 /// DIEExpr - An expression DIE.
295 class DIEExpr : public DIEValue {
296 friend class DIEValue;
301 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
303 /// getValue - Get MCExpr.
305 const MCExpr *getValue() const { return Expr; }
307 // Implement isa/cast/dyncast.
308 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
311 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
312 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
315 void printImpl(raw_ostream &O) const;
319 //===--------------------------------------------------------------------===//
320 /// DIELabel - A label DIE.
322 class DIELabel : public DIEValue {
323 friend class DIEValue;
325 const MCSymbol *Label;
328 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
330 /// getValue - Get MCSymbol.
332 const MCSymbol *getValue() const { return Label; }
334 // Implement isa/cast/dyncast.
335 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
338 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
339 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
342 void printImpl(raw_ostream &O) const;
346 //===--------------------------------------------------------------------===//
347 /// DIEDelta - A simple label difference DIE.
349 class DIEDelta : public DIEValue {
350 friend class DIEValue;
352 const MCSymbol *LabelHi;
353 const MCSymbol *LabelLo;
356 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
357 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
359 // Implement isa/cast/dyncast.
360 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
363 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
364 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
367 void printImpl(raw_ostream &O) const;
371 //===--------------------------------------------------------------------===//
372 /// DIEString - A container for string values.
374 class DIEString : public DIEValue {
375 friend class DIEValue;
377 DwarfStringPoolEntryRef S;
380 DIEString(DwarfStringPoolEntryRef S) : DIEValue(isString), S(S) {}
382 /// getString - Grab the string out of the object.
383 StringRef getString() const { return S.getString(); }
385 // Implement isa/cast/dyncast.
386 static bool classof(const DIEValue *D) { return D->getType() == isString; }
389 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
390 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
393 void printImpl(raw_ostream &O) const;
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;
407 explicit DIEEntry(DIE &E) : DIEValue(isEntry), Entry(E) {
410 DIE &getEntry() const { return Entry; }
412 /// Returns size of a ref_addr entry.
413 static unsigned getRefAddrSize(const AsmPrinter *AP);
415 // Implement isa/cast/dyncast.
416 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
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)
426 void printImpl(raw_ostream &O) const;
430 //===--------------------------------------------------------------------===//
431 /// \brief A signature reference to a type unit.
432 class DIETypeSignature : public DIEValue {
433 friend class DIEValue;
435 const DwarfTypeUnit &Unit;
438 explicit DIETypeSignature(const DwarfTypeUnit &Unit)
439 : DIEValue(isTypeSignature), Unit(Unit) {}
441 // \brief Implement isa/cast/dyncast.
442 static bool classof(const DIEValue *E) {
443 return E->getType() == isTypeSignature;
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);
454 void printImpl(raw_ostream &O) const;
458 //===--------------------------------------------------------------------===//
459 /// DIELoc - Represents an expression location.
461 class DIELoc : public DIEValue, public DIE {
462 friend class DIEValue;
464 mutable unsigned Size; // Size in bytes excluding size header.
466 DIELoc() : DIEValue(isLoc), Size(0) {}
468 /// ComputeSize - Calculate the size of the location expression.
470 unsigned ComputeSize(const AsmPrinter *AP) const;
472 /// BestForm - Choose the best form for data.
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;
487 // Implement isa/cast/dyncast.
488 static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
491 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
492 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
495 void printImpl(raw_ostream &O) const;
499 //===--------------------------------------------------------------------===//
500 /// DIEBlock - Represents a block of values.
502 class DIEBlock : public DIEValue, public DIE {
503 friend class DIEValue;
505 mutable unsigned Size; // Size in bytes excluding size header.
507 DIEBlock() : DIEValue(isBlock), Size(0) {}
509 /// ComputeSize - Calculate the size of the location expression.
511 unsigned ComputeSize(const AsmPrinter *AP) const;
513 /// BestForm - Choose the best form for data.
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;
525 // Implement isa/cast/dyncast.
526 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
529 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
530 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
533 void printImpl(raw_ostream &O) const;
537 //===--------------------------------------------------------------------===//
538 /// DIELocList - Represents a pointer to a location list in the debug_loc
541 class DIELocList : public DIEValue {
542 friend class DIEValue;
544 // Index into the .debug_loc vector.
548 DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
550 /// getValue - Grab the current index out.
551 size_t getValue() const { return Index; }
553 // Implement isa/cast/dyncast.
554 static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
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