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.
218 /// Ty - Type of data stored in the value.
222 explicit DIEValue(Type T) : Ty(T) {}
227 Type getType() const { return Ty; }
229 /// EmitValue - Emit value via the Dwarf writer.
231 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
233 /// SizeOf - Return the size of a value in bytes.
235 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
238 void print(raw_ostream &O) const;
243 //===--------------------------------------------------------------------===//
244 /// DIEInteger - An integer value DIE.
246 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 uint64_t getValue() const { return Integer; }
277 void setValue(uint64_t Val) { Integer = Val; }
279 // Implement isa/cast/dyncast.
280 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
283 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
284 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
287 void printImpl(raw_ostream &O) const;
291 //===--------------------------------------------------------------------===//
292 /// DIEExpr - An expression DIE.
294 class DIEExpr : public DIEValue {
295 friend class DIEValue;
300 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
302 /// getValue - Get MCExpr.
304 const MCExpr *getValue() const { return Expr; }
306 // Implement isa/cast/dyncast.
307 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
310 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
311 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
314 void printImpl(raw_ostream &O) const;
318 //===--------------------------------------------------------------------===//
319 /// DIELabel - A label DIE.
321 class DIELabel : public DIEValue {
322 friend class DIEValue;
324 const MCSymbol *Label;
327 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
329 /// getValue - Get MCSymbol.
331 const MCSymbol *getValue() const { return Label; }
333 // Implement isa/cast/dyncast.
334 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
337 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
338 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
341 void printImpl(raw_ostream &O) const;
345 //===--------------------------------------------------------------------===//
346 /// DIEDelta - A simple label difference DIE.
348 class DIEDelta : public DIEValue {
349 friend class DIEValue;
351 const MCSymbol *LabelHi;
352 const MCSymbol *LabelLo;
355 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
356 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
358 // Implement isa/cast/dyncast.
359 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
362 void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
363 unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
366 void printImpl(raw_ostream &O) const;
370 //===--------------------------------------------------------------------===//
371 /// DIEString - A container for string values.
373 class DIEString : public DIEValue {
374 friend class DIEValue;
376 const DIEValue *Access;
380 DIEString(const DIEValue *Acc, StringRef S)
381 : DIEValue(isString), Access(Acc), Str(S) {}
383 /// getString - Grab the string out of the object.
384 StringRef getString() const { return Str; }
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