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 /// DIEInteger - An integer value DIE.
114 explicit DIEInteger(uint64_t I) : Integer(I) {}
116 /// BestForm - Choose the best form for integer.
118 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
120 const int64_t SignedInt = Int;
121 if ((char)Int == SignedInt)
122 return dwarf::DW_FORM_data1;
123 if ((short)Int == SignedInt)
124 return dwarf::DW_FORM_data2;
125 if ((int)Int == SignedInt)
126 return dwarf::DW_FORM_data4;
128 if ((unsigned char)Int == Int)
129 return dwarf::DW_FORM_data1;
130 if ((unsigned short)Int == Int)
131 return dwarf::DW_FORM_data2;
132 if ((unsigned int)Int == Int)
133 return dwarf::DW_FORM_data4;
135 return dwarf::DW_FORM_data8;
138 uint64_t getValue() const { return Integer; }
139 void setValue(uint64_t Val) { Integer = Val; }
141 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
142 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
145 void print(raw_ostream &O) const;
149 //===--------------------------------------------------------------------===//
150 /// DIEExpr - An expression DIE.
156 explicit DIEExpr(const MCExpr *E) : Expr(E) {}
158 /// getValue - Get MCExpr.
160 const MCExpr *getValue() const { return Expr; }
162 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
163 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
166 void print(raw_ostream &O) const;
170 //===--------------------------------------------------------------------===//
171 /// DIELabel - A label DIE.
174 const MCSymbol *Label;
177 explicit DIELabel(const MCSymbol *L) : Label(L) {}
179 /// getValue - Get MCSymbol.
181 const MCSymbol *getValue() const { return Label; }
183 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
184 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
187 void print(raw_ostream &O) const;
191 //===--------------------------------------------------------------------===//
192 /// DIEDelta - A simple label difference DIE.
195 const MCSymbol *LabelHi;
196 const MCSymbol *LabelLo;
199 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
201 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
202 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
205 void print(raw_ostream &O) const;
209 //===--------------------------------------------------------------------===//
210 /// DIEString - A container for string values.
213 DwarfStringPoolEntryRef S;
216 DIEString(DwarfStringPoolEntryRef S) : S(S) {}
218 /// getString - Grab the string out of the object.
219 StringRef getString() const { return S.getString(); }
221 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
222 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
225 void print(raw_ostream &O) const;
229 //===--------------------------------------------------------------------===//
230 /// DIEEntry - A pointer to another debug information entry. An instance of
231 /// this class can also be used as a proxy for a debug information entry not
232 /// yet defined (ie. types.)
240 explicit DIEEntry(DIE &E) : Entry(&E) {}
242 DIE &getEntry() const { return *Entry; }
244 /// Returns size of a ref_addr entry.
245 static unsigned getRefAddrSize(const AsmPrinter *AP);
247 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
248 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
249 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
254 void print(raw_ostream &O) const;
258 //===--------------------------------------------------------------------===//
259 /// \brief A signature reference to a type unit.
260 class DIETypeSignature {
261 const DwarfTypeUnit *Unit;
263 DIETypeSignature() = delete;
266 explicit DIETypeSignature(const DwarfTypeUnit &Unit) : Unit(&Unit) {}
268 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
269 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
270 assert(Form == dwarf::DW_FORM_ref_sig8);
275 void print(raw_ostream &O) const;
279 //===--------------------------------------------------------------------===//
280 /// DIELocList - Represents a pointer to a location list in the debug_loc
284 // Index into the .debug_loc vector.
288 DIELocList(size_t I) : Index(I) {}
290 /// getValue - Grab the current index out.
291 size_t getValue() const { return Index; }
293 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
294 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
297 void print(raw_ostream &O) const;
301 //===--------------------------------------------------------------------===//
302 /// DIEValue - A debug information entry value. Some of these roughly correlate
303 /// to DWARF attribute classes.
311 #define HANDLE_DIEVALUE(T) is##T,
312 #include "llvm/CodeGen/DIEValue.def"
316 /// Ty - Type of data stored in the value.
319 dwarf::Attribute Attribute = (dwarf::Attribute)0;
320 dwarf::Form Form = (dwarf::Form)0;
322 /// Storage for the value.
324 /// All values that aren't standard layout (or are larger than 8 bytes)
325 /// should be stored by reference instead of by value.
326 typedef AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
327 DIEDelta *, DIEEntry, DIETypeSignature,
328 DIEBlock *, DIELoc *, DIELocList> ValTy;
329 static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
330 sizeof(ValTy) <= sizeof(void *),
331 "Expected all large types to be stored via pointer");
333 /// Underlying stored value.
336 template <class T> void construct(T V) {
337 static_assert(std::is_standard_layout<T>::value ||
338 std::is_pointer<T>::value,
339 "Expected standard layout or pointer");
340 new (reinterpret_cast<void *>(Val.buffer)) T(V);
343 template <class T> T &get() { return *reinterpret_cast<T *>(Val.buffer); }
344 template <class T> const T &get() const {
345 return *reinterpret_cast<const T *>(Val.buffer);
347 template <class T> void destruct() { get<T>().~T(); }
349 /// Destroy the underlying value.
351 /// This should get optimized down to a no-op. We could skip it if we could
352 /// add a static assert on \a std::is_trivially_copyable(), but we currently
353 /// support versions of GCC that don't understand that.
358 #define HANDLE_DIEVALUE_SMALL(T) \
362 #define HANDLE_DIEVALUE_LARGE(T) \
364 destruct<const DIE##T *>();
366 #include "llvm/CodeGen/DIEValue.def"
370 /// Copy the underlying value.
372 /// This should get optimized down to a simple copy. We need to actually
373 /// construct the value, rather than calling memcpy, to satisfy strict
375 void copyVal(const DIEValue &X) {
379 #define HANDLE_DIEVALUE_SMALL(T) \
381 construct<DIE##T>(X.get<DIE##T>()); \
383 #define HANDLE_DIEVALUE_LARGE(T) \
385 construct<const DIE##T *>(X.get<const DIE##T *>()); \
387 #include "llvm/CodeGen/DIEValue.def"
392 DIEValue() = default;
393 DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
396 DIEValue &operator=(const DIEValue &X) {
399 Attribute = X.Attribute;
404 ~DIEValue() { destroyVal(); }
406 #define HANDLE_DIEVALUE_SMALL(T) \
407 DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V) \
408 : Ty(is##T), Attribute(Attribute), Form(Form) { \
409 construct<DIE##T>(V); \
411 #define HANDLE_DIEVALUE_LARGE(T) \
412 DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V) \
413 : Ty(is##T), Attribute(Attribute), Form(Form) { \
414 assert(V && "Expected valid value"); \
415 construct<const DIE##T *>(V); \
417 #include "llvm/CodeGen/DIEValue.def"
420 Type getType() const { return Ty; }
421 dwarf::Attribute getAttribute() const { return Attribute; }
422 dwarf::Form getForm() const { return Form; }
423 explicit operator bool() const { return Ty; }
425 #define HANDLE_DIEVALUE_SMALL(T) \
426 const DIE##T &getDIE##T() const { \
427 assert(getType() == is##T && "Expected " #T); \
428 return get<DIE##T>(); \
430 #define HANDLE_DIEVALUE_LARGE(T) \
431 const DIE##T &getDIE##T() const { \
432 assert(getType() == is##T && "Expected " #T); \
433 return *get<const DIE##T *>(); \
435 #include "llvm/CodeGen/DIEValue.def"
437 /// EmitValue - Emit value via the Dwarf writer.
439 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
441 /// SizeOf - Return the size of a value in bytes.
443 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
446 void print(raw_ostream &O) const;
451 //===--------------------------------------------------------------------===//
452 /// DIE - A structured debug information entry. Has an abbreviation which
453 /// describes its organization.
456 /// Offset - Offset in debug info section.
460 /// Size - Size of instance + children.
464 unsigned AbbrevNumber = ~0u;
466 /// Tag - Dwarf tag code.
468 dwarf::Tag Tag = (dwarf::Tag)0;
472 // This can't be a vector<DIE> because pointer validity is requirent for the
473 // Parent pointer and DIEEntry.
474 // It can't be a list<DIE> because some clients need pointer validity before
475 // the object has been added to any child list
476 // (eg: DwarfUnit::constructVariableDIE). These aren't insurmountable, but may
477 // be more convoluted than beneficial.
478 std::vector<std::unique_ptr<DIE>> Children;
482 /// Attribute values.
484 SmallVector<DIEValue, 12> Values;
487 DIE() : Offset(0), Size(0), Parent(nullptr) {}
490 explicit DIE(dwarf::Tag Tag)
491 : Offset(0), Size(0), Tag(Tag), Parent(nullptr) {}
494 unsigned getAbbrevNumber() const { return AbbrevNumber; }
495 dwarf::Tag getTag() const { return Tag; }
496 unsigned getOffset() const { return Offset; }
497 unsigned getSize() const { return Size; }
498 bool hasChildren() const { return !Children.empty(); }
499 const std::vector<std::unique_ptr<DIE>> &getChildren() const {
502 const SmallVectorImpl<DIEValue> &getValues() const { return Values; }
503 void setValue(unsigned I, DIEValue New) {
504 assert(I < Values.size());
507 DIE *getParent() const { return Parent; }
509 /// Generate the abbreviation for this DIE.
511 /// Calculate the abbreviation for this, which should be uniqued and
512 /// eventually used to call \a setAbbrevNumber().
513 DIEAbbrev generateAbbrev() const;
515 /// Set the abbreviation number for this DIE.
516 void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
518 /// Climb up the parent chain to get the compile or type unit DIE this DIE
520 const DIE *getUnit() const;
521 /// Similar to getUnit, returns null when DIE is not added to an
523 const DIE *getUnitOrNull() const;
524 void setOffset(unsigned O) { Offset = O; }
525 void setSize(unsigned S) { Size = S; }
527 /// addValue - Add a value and attributes to a DIE.
529 void addValue(DIEValue Value) { Values.push_back(Value); }
531 void addValue(dwarf::Attribute Attribute, dwarf::Form Form, T &&Value) {
532 Values.emplace_back(Attribute, Form, std::forward<T>(Value));
535 /// addChild - Add a child to the DIE.
537 void addChild(std::unique_ptr<DIE> Child) {
538 assert(!Child->getParent());
539 Child->Parent = this;
540 Children.push_back(std::move(Child));
543 /// Find a value in the DIE with the attribute given.
545 /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
546 /// gives \a DIEValue::isNone) if no such attribute exists.
547 DIEValue findAttribute(dwarf::Attribute Attribute) const;
550 void print(raw_ostream &O, unsigned IndentCount = 0) const;
555 //===--------------------------------------------------------------------===//
556 /// DIELoc - Represents an expression location.
558 class DIELoc : public DIE {
559 mutable unsigned Size; // Size in bytes excluding size header.
562 DIELoc() : Size(0) {}
564 /// ComputeSize - Calculate the size of the location expression.
566 unsigned ComputeSize(const AsmPrinter *AP) const;
568 /// BestForm - Choose the best form for data.
570 dwarf::Form BestForm(unsigned DwarfVersion) const {
571 if (DwarfVersion > 3)
572 return dwarf::DW_FORM_exprloc;
573 // Pre-DWARF4 location expressions were blocks and not exprloc.
574 if ((unsigned char)Size == Size)
575 return dwarf::DW_FORM_block1;
576 if ((unsigned short)Size == Size)
577 return dwarf::DW_FORM_block2;
578 if ((unsigned int)Size == Size)
579 return dwarf::DW_FORM_block4;
580 return dwarf::DW_FORM_block;
583 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
584 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
587 void print(raw_ostream &O) const;
591 //===--------------------------------------------------------------------===//
592 /// DIEBlock - Represents a block of values.
594 class DIEBlock : public DIE {
595 mutable unsigned Size; // Size in bytes excluding size header.
598 DIEBlock() : Size(0) {}
600 /// ComputeSize - Calculate the size of the location expression.
602 unsigned ComputeSize(const AsmPrinter *AP) const;
604 /// BestForm - Choose the best form for data.
606 dwarf::Form BestForm() const {
607 if ((unsigned char)Size == Size)
608 return dwarf::DW_FORM_block1;
609 if ((unsigned short)Size == Size)
610 return dwarf::DW_FORM_block2;
611 if ((unsigned int)Size == Size)
612 return dwarf::DW_FORM_block4;
613 return dwarf::DW_FORM_block;
616 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
617 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
620 void print(raw_ostream &O) const;
624 } // end llvm namespace