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/Support/Dwarf.h"
29 //===--------------------------------------------------------------------===//
30 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
31 /// Dwarf abbreviation.
33 /// Attribute - Dwarf attribute code.
35 dwarf::Attribute Attribute;
37 /// Form - Dwarf form code.
42 DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
45 dwarf::Attribute getAttribute() const { return Attribute; }
46 dwarf::Form getForm() const { return Form; }
48 /// Profile - Used to gather unique data for the abbreviation folding set.
50 void Profile(FoldingSetNodeID &ID) const;
53 //===--------------------------------------------------------------------===//
54 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
55 /// information object.
56 class DIEAbbrev : public FoldingSetNode {
57 /// Unique number for node.
61 /// Tag - Dwarf tag code.
65 /// Children - Whether or not this node has children.
67 // This cheats a bit in all of the uses since the values in the standard
68 // are 0 and 1 for no children and children respectively.
71 /// Data - Raw data bytes for abbreviation.
73 SmallVector<DIEAbbrevData, 12> Data;
76 DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
79 dwarf::Tag getTag() const { return Tag; }
80 unsigned getNumber() const { return Number; }
81 bool hasChildren() const { return Children; }
82 const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
83 void setChildrenFlag(bool hasChild) { Children = hasChild; }
84 void setNumber(unsigned N) { Number = N; }
86 /// AddAttribute - Adds another set of attribute information to the
88 void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
89 Data.push_back(DIEAbbrevData(Attribute, Form));
92 /// Profile - Used to gather unique data for the abbreviation folding set.
94 void Profile(FoldingSetNodeID &ID) const;
96 /// Emit - Print the abbreviation using the specified asm printer.
98 void Emit(const AsmPrinter *AP) const;
101 void print(raw_ostream &O);
106 //===--------------------------------------------------------------------===//
107 /// DIE - A structured debug information entry. Has an abbreviation which
108 /// describes its organization.
113 /// Offset - Offset in debug info section.
117 /// Size - Size of instance + children.
121 /// Abbrev - Buffer for constructing abbreviation.
127 // This can't be a vector<DIE> because pointer validity is requirent for the
128 // Parent pointer and DIEEntry.
129 // It can't be a list<DIE> because some clients need pointer validity before
130 // the object has been added to any child list
131 // (eg: DwarfUnit::constructVariableDIE). These aren't insurmountable, but may
132 // be more convoluted than beneficial.
133 std::vector<std::unique_ptr<DIE>> Children;
137 /// Attribute values.
139 SmallVector<DIEValue *, 12> Values;
143 : Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
147 explicit DIE(dwarf::Tag Tag)
148 : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
152 DIEAbbrev &getAbbrev() { return Abbrev; }
153 const DIEAbbrev &getAbbrev() const { return Abbrev; }
154 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
155 dwarf::Tag getTag() const { return Abbrev.getTag(); }
156 unsigned getOffset() const { return Offset; }
157 unsigned getSize() const { return Size; }
158 const std::vector<std::unique_ptr<DIE>> &getChildren() const {
161 const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
162 DIE *getParent() const { return Parent; }
163 /// Climb up the parent chain to get the compile or type unit DIE this DIE
165 const DIE *getUnit() const;
166 /// Similar to getUnit, returns null when DIE is not added to an
168 const DIE *getUnitOrNull() const;
169 void setOffset(unsigned O) { Offset = O; }
170 void setSize(unsigned S) { Size = S; }
172 /// addValue - Add a value and attributes to a DIE.
174 void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
175 Abbrev.AddAttribute(Attribute, Form);
176 Values.push_back(Value);
179 /// addChild - Add a child to the DIE.
181 void addChild(std::unique_ptr<DIE> Child) {
182 assert(!Child->getParent());
183 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
184 Child->Parent = this;
185 Children.push_back(std::move(Child));
188 /// findAttribute - Find a value in the DIE with the attribute given,
189 /// returns NULL if no such attribute exists.
190 DIEValue *findAttribute(dwarf::Attribute Attribute) const;
193 void print(raw_ostream &O, unsigned IndentCount = 0) const;
198 //===--------------------------------------------------------------------===//
199 /// DIEValue - A debug information entry value. Some of these roughly correlate
200 /// to DWARF attribute classes.
203 virtual void anchor();
220 /// Ty - Type of data stored in the value.
224 explicit DIEValue(Type T) : Ty(T) {}
225 virtual ~DIEValue() {}
229 Type getType() const { return Ty; }
231 /// EmitValue - Emit value via the Dwarf writer.
233 virtual void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const = 0;
235 /// SizeOf - Return the size of a value in bytes.
237 virtual unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const = 0;
240 virtual void print(raw_ostream &O) const = 0;
245 //===--------------------------------------------------------------------===//
246 /// DIEInteger - An integer value DIE.
248 class DIEInteger : public DIEValue {
252 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
254 /// BestForm - Choose the best form for integer.
256 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
258 const int64_t SignedInt = Int;
259 if ((char)Int == SignedInt)
260 return dwarf::DW_FORM_data1;
261 if ((short)Int == SignedInt)
262 return dwarf::DW_FORM_data2;
263 if ((int)Int == SignedInt)
264 return dwarf::DW_FORM_data4;
266 if ((unsigned char)Int == Int)
267 return dwarf::DW_FORM_data1;
268 if ((unsigned short)Int == Int)
269 return dwarf::DW_FORM_data2;
270 if ((unsigned int)Int == Int)
271 return dwarf::DW_FORM_data4;
273 return dwarf::DW_FORM_data8;
276 /// EmitValue - Emit integer of appropriate size.
278 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
280 uint64_t getValue() const { return Integer; }
281 void setValue(uint64_t Val) { Integer = Val; }
283 /// SizeOf - Determine size of integer value in bytes.
285 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
287 // Implement isa/cast/dyncast.
288 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
291 void print(raw_ostream &O) const override;
295 //===--------------------------------------------------------------------===//
296 /// DIEExpr - An expression DIE.
298 class DIEExpr : public DIEValue {
302 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
304 /// EmitValue - Emit expression value.
306 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
308 /// getValue - Get MCExpr.
310 const MCExpr *getValue() const { return Expr; }
312 /// SizeOf - Determine size of expression value in bytes.
314 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
316 // Implement isa/cast/dyncast.
317 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
320 void print(raw_ostream &O) const override;
324 //===--------------------------------------------------------------------===//
325 /// DIELabel - A label DIE.
327 class DIELabel : public DIEValue {
328 const MCSymbol *Label;
331 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
333 /// EmitValue - Emit label value.
335 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
337 /// getValue - Get MCSymbol.
339 const MCSymbol *getValue() const { return Label; }
341 /// SizeOf - Determine size of label value in bytes.
343 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
345 // Implement isa/cast/dyncast.
346 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
349 void print(raw_ostream &O) const override;
353 //===--------------------------------------------------------------------===//
354 /// DIEDelta - A simple label difference DIE.
356 class DIEDelta : public DIEValue {
357 const MCSymbol *LabelHi;
358 const MCSymbol *LabelLo;
361 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
362 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
364 /// EmitValue - Emit delta value.
366 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
368 /// SizeOf - Determine size of delta value in bytes.
370 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
372 // Implement isa/cast/dyncast.
373 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
376 void print(raw_ostream &O) const override;
380 //===--------------------------------------------------------------------===//
381 /// DIEString - A container for string values.
383 class DIEString : public DIEValue {
384 const DIEValue *Access;
388 DIEString(const DIEValue *Acc, StringRef S)
389 : DIEValue(isString), Access(Acc), Str(S) {}
391 /// getString - Grab the string out of the object.
392 StringRef getString() const { return Str; }
394 /// EmitValue - Emit delta value.
396 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
398 /// SizeOf - Determine size of delta value in bytes.
400 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
402 // Implement isa/cast/dyncast.
403 static bool classof(const DIEValue *D) { return D->getType() == isString; }
406 void print(raw_ostream &O) const override;
410 //===--------------------------------------------------------------------===//
411 /// DIEEntry - A pointer to another debug information entry. An instance of
412 /// this class can also be used as a proxy for a debug information entry not
413 /// yet defined (ie. types.)
414 class DIEEntry : public DIEValue {
418 explicit DIEEntry(DIE &E) : DIEValue(isEntry), Entry(E) {
421 DIE &getEntry() const { return Entry; }
423 /// EmitValue - Emit debug information entry offset.
425 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
427 /// SizeOf - Determine size of debug information entry in bytes.
429 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override {
430 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
434 /// Returns size of a ref_addr entry.
435 static unsigned getRefAddrSize(const AsmPrinter *AP);
437 // Implement isa/cast/dyncast.
438 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
441 void print(raw_ostream &O) const override;
445 //===--------------------------------------------------------------------===//
446 /// \brief A signature reference to a type unit.
447 class DIETypeSignature : public DIEValue {
448 const DwarfTypeUnit &Unit;
451 explicit DIETypeSignature(const DwarfTypeUnit &Unit)
452 : DIEValue(isTypeSignature), Unit(Unit) {}
454 /// \brief Emit type unit signature.
455 void EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const override;
457 /// Returns size of a ref_sig8 entry.
458 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override {
459 assert(Form == dwarf::DW_FORM_ref_sig8);
463 // \brief Implement isa/cast/dyncast.
464 static bool classof(const DIEValue *E) {
465 return E->getType() == isTypeSignature;
468 void print(raw_ostream &O) const override;
473 //===--------------------------------------------------------------------===//
474 /// DIELoc - Represents an expression location.
476 class DIELoc : public DIEValue, public DIE {
477 mutable unsigned Size; // Size in bytes excluding size header.
479 DIELoc() : DIEValue(isLoc), Size(0) {}
481 /// ComputeSize - Calculate the size of the location expression.
483 unsigned ComputeSize(const AsmPrinter *AP) const;
485 /// BestForm - Choose the best form for data.
487 dwarf::Form BestForm(unsigned DwarfVersion) const {
488 if (DwarfVersion > 3)
489 return dwarf::DW_FORM_exprloc;
490 // Pre-DWARF4 location expressions were blocks and not exprloc.
491 if ((unsigned char)Size == Size)
492 return dwarf::DW_FORM_block1;
493 if ((unsigned short)Size == Size)
494 return dwarf::DW_FORM_block2;
495 if ((unsigned int)Size == Size)
496 return dwarf::DW_FORM_block4;
497 return dwarf::DW_FORM_block;
500 /// EmitValue - Emit location data.
502 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
504 /// SizeOf - Determine size of location data in bytes.
506 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
508 // Implement isa/cast/dyncast.
509 static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
512 void print(raw_ostream &O) const override;
516 //===--------------------------------------------------------------------===//
517 /// DIEBlock - Represents a block of values.
519 class DIEBlock : public DIEValue, public DIE {
520 mutable unsigned Size; // Size in bytes excluding size header.
522 DIEBlock() : DIEValue(isBlock), Size(0) {}
524 /// ComputeSize - Calculate the size of the location expression.
526 unsigned ComputeSize(const AsmPrinter *AP) const;
528 /// BestForm - Choose the best form for data.
530 dwarf::Form BestForm() const {
531 if ((unsigned char)Size == Size)
532 return dwarf::DW_FORM_block1;
533 if ((unsigned short)Size == Size)
534 return dwarf::DW_FORM_block2;
535 if ((unsigned int)Size == Size)
536 return dwarf::DW_FORM_block4;
537 return dwarf::DW_FORM_block;
540 /// EmitValue - Emit location data.
542 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
544 /// SizeOf - Determine size of location data in bytes.
546 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
548 // Implement isa/cast/dyncast.
549 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
552 void print(raw_ostream &O) const override;
556 //===--------------------------------------------------------------------===//
557 /// DIELocList - Represents a pointer to a location list in the debug_loc
560 class DIELocList : public DIEValue {
561 // Index into the .debug_loc vector.
565 DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
567 /// getValue - Grab the current index out.
568 size_t getValue() const { return Index; }
570 /// EmitValue - Emit location data.
572 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
574 /// SizeOf - Determine size of location data in bytes.
576 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
578 // Implement isa/cast/dyncast.
579 static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
582 void print(raw_ostream &O) const override;
586 } // end llvm namespace