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 CODEGEN_ASMPRINTER_DIE_H__
15 #define 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(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 std::vector<std::unique_ptr<DIE>> Children;
131 /// Attribute values.
133 SmallVector<DIEValue *, 12> Values;
137 : Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
141 explicit DIE(dwarf::Tag Tag)
142 : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
146 DIEAbbrev &getAbbrev() { return Abbrev; }
147 const DIEAbbrev &getAbbrev() const { return Abbrev; }
148 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
149 dwarf::Tag getTag() const { return Abbrev.getTag(); }
150 unsigned getOffset() const { return Offset; }
151 unsigned getSize() const { return Size; }
152 const std::vector<std::unique_ptr<DIE>> &getChildren() const {
155 const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
156 DIE *getParent() const { return Parent; }
157 /// Climb up the parent chain to get the compile or type unit DIE this DIE
159 const DIE *getUnit() const;
160 /// Similar to getUnit, returns null when DIE is not added to an
162 const DIE *getUnitOrNull() const;
163 void setOffset(unsigned O) { Offset = O; }
164 void setSize(unsigned S) { Size = S; }
166 /// addValue - Add a value and attributes to a DIE.
168 void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
169 Abbrev.AddAttribute(Attribute, Form);
170 Values.push_back(Value);
173 /// addChild - Add a child to the DIE.
175 void addChild(DIE *Child) {
176 assert(!Child->getParent());
177 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
178 Children.push_back(std::unique_ptr<DIE>(Child));
179 Child->Parent = this;
182 /// findAttribute - Find a value in the DIE with the attribute given,
183 /// returns NULL if no such attribute exists.
184 DIEValue *findAttribute(dwarf::Attribute Attribute) const;
187 void print(raw_ostream &O, unsigned IndentCount = 0) const;
192 //===--------------------------------------------------------------------===//
193 /// DIEValue - A debug information entry value. Some of these roughly correlate
194 /// to DWARF attribute classes.
197 virtual void anchor();
214 /// Ty - Type of data stored in the value.
218 explicit DIEValue(Type T) : Ty(T) {}
219 virtual ~DIEValue() {}
223 Type getType() const { return Ty; }
225 /// EmitValue - Emit value via the Dwarf writer.
227 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
229 /// SizeOf - Return the size of a value in bytes.
231 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
234 virtual void print(raw_ostream &O) const = 0;
239 //===--------------------------------------------------------------------===//
240 /// DIEInteger - An integer value DIE.
242 class DIEInteger : public DIEValue {
246 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
248 /// BestForm - Choose the best form for integer.
250 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
252 const int64_t SignedInt = Int;
253 if ((char)Int == SignedInt)
254 return dwarf::DW_FORM_data1;
255 if ((short)Int == SignedInt)
256 return dwarf::DW_FORM_data2;
257 if ((int)Int == SignedInt)
258 return dwarf::DW_FORM_data4;
260 if ((unsigned char)Int == Int)
261 return dwarf::DW_FORM_data1;
262 if ((unsigned short)Int == Int)
263 return dwarf::DW_FORM_data2;
264 if ((unsigned int)Int == Int)
265 return dwarf::DW_FORM_data4;
267 return dwarf::DW_FORM_data8;
270 /// EmitValue - Emit integer of appropriate size.
272 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
274 uint64_t getValue() const { return Integer; }
276 /// SizeOf - Determine size of integer value in bytes.
278 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
280 // Implement isa/cast/dyncast.
281 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
284 void print(raw_ostream &O) const override;
288 //===--------------------------------------------------------------------===//
289 /// DIEExpr - An expression DIE.
291 class DIEExpr : public DIEValue {
295 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
297 /// EmitValue - Emit expression value.
299 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
301 /// getValue - Get MCExpr.
303 const MCExpr *getValue() const { return Expr; }
305 /// SizeOf - Determine size of expression value in bytes.
307 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
309 // Implement isa/cast/dyncast.
310 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
313 void print(raw_ostream &O) const override;
317 //===--------------------------------------------------------------------===//
318 /// DIELabel - A label DIE.
320 class DIELabel : public DIEValue {
321 const MCSymbol *Label;
324 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
326 /// EmitValue - Emit label value.
328 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
330 /// getValue - Get MCSymbol.
332 const MCSymbol *getValue() const { return Label; }
334 /// SizeOf - Determine size of label value in bytes.
336 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
338 // Implement isa/cast/dyncast.
339 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
342 void print(raw_ostream &O) const override;
346 //===--------------------------------------------------------------------===//
347 /// DIEDelta - A simple label difference DIE.
349 class DIEDelta : public DIEValue {
350 const MCSymbol *LabelHi;
351 const MCSymbol *LabelLo;
354 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
355 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
357 /// EmitValue - Emit delta value.
359 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
361 /// SizeOf - Determine size of delta value in bytes.
363 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
365 // Implement isa/cast/dyncast.
366 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
369 void print(raw_ostream &O) const override;
373 //===--------------------------------------------------------------------===//
374 /// DIEString - A container for string values.
376 class DIEString : public DIEValue {
377 const DIEValue *Access;
381 DIEString(const DIEValue *Acc, const StringRef S)
382 : DIEValue(isString), Access(Acc), Str(S) {}
384 /// getString - Grab the string out of the object.
385 StringRef getString() const { return Str; }
387 /// EmitValue - Emit delta value.
389 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
391 /// SizeOf - Determine size of delta value in bytes.
393 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
395 // Implement isa/cast/dyncast.
396 static bool classof(const DIEValue *D) { return D->getType() == isString; }
399 void print(raw_ostream &O) const override;
403 //===--------------------------------------------------------------------===//
404 /// DIEEntry - A pointer to another debug information entry. An instance of
405 /// this class can also be used as a proxy for a debug information entry not
406 /// yet defined (ie. types.)
407 class DIEEntry : public DIEValue {
411 explicit DIEEntry(DIE &E) : DIEValue(isEntry), Entry(E) {
414 DIE &getEntry() const { return Entry; }
416 /// EmitValue - Emit debug information entry offset.
418 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
420 /// SizeOf - Determine size of debug information entry in bytes.
422 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
423 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
427 /// Returns size of a ref_addr entry.
428 static unsigned getRefAddrSize(AsmPrinter *AP);
430 // Implement isa/cast/dyncast.
431 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
434 void print(raw_ostream &O) const override;
438 //===--------------------------------------------------------------------===//
439 /// \brief A signature reference to a type unit.
440 class DIETypeSignature : public DIEValue {
441 const DwarfTypeUnit &Unit;
444 explicit DIETypeSignature(const DwarfTypeUnit &Unit)
445 : DIEValue(isTypeSignature), Unit(Unit) {}
447 /// \brief Emit type unit signature.
448 void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const override;
450 /// Returns size of a ref_sig8 entry.
451 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
452 assert(Form == dwarf::DW_FORM_ref_sig8);
456 // \brief Implement isa/cast/dyncast.
457 static bool classof(const DIEValue *E) {
458 return E->getType() == isTypeSignature;
461 void print(raw_ostream &O) const override;
466 //===--------------------------------------------------------------------===//
467 /// DIELoc - Represents an expression location.
469 class DIELoc : public DIEValue, public DIE {
470 mutable unsigned Size; // Size in bytes excluding size header.
472 DIELoc() : DIEValue(isLoc), Size(0) {}
474 /// ComputeSize - Calculate the size of the location expression.
476 unsigned ComputeSize(AsmPrinter *AP) const;
478 /// BestForm - Choose the best form for data.
480 dwarf::Form BestForm(unsigned DwarfVersion) const {
481 if (DwarfVersion > 3)
482 return dwarf::DW_FORM_exprloc;
483 // Pre-DWARF4 location expressions were blocks and not exprloc.
484 if ((unsigned char)Size == Size)
485 return dwarf::DW_FORM_block1;
486 if ((unsigned short)Size == Size)
487 return dwarf::DW_FORM_block2;
488 if ((unsigned int)Size == Size)
489 return dwarf::DW_FORM_block4;
490 return dwarf::DW_FORM_block;
493 /// EmitValue - Emit location data.
495 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
497 /// SizeOf - Determine size of location data in bytes.
499 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
501 // Implement isa/cast/dyncast.
502 static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
505 void print(raw_ostream &O) const override;
509 //===--------------------------------------------------------------------===//
510 /// DIEBlock - Represents a block of values.
512 class DIEBlock : public DIEValue, public DIE {
513 mutable unsigned Size; // Size in bytes excluding size header.
515 DIEBlock() : DIEValue(isBlock), Size(0) {}
517 /// ComputeSize - Calculate the size of the location expression.
519 unsigned ComputeSize(AsmPrinter *AP) const;
521 /// BestForm - Choose the best form for data.
523 dwarf::Form BestForm() const {
524 if ((unsigned char)Size == Size)
525 return dwarf::DW_FORM_block1;
526 if ((unsigned short)Size == Size)
527 return dwarf::DW_FORM_block2;
528 if ((unsigned int)Size == Size)
529 return dwarf::DW_FORM_block4;
530 return dwarf::DW_FORM_block;
533 /// EmitValue - Emit location data.
535 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
537 /// SizeOf - Determine size of location data in bytes.
539 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
541 // Implement isa/cast/dyncast.
542 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
545 void print(raw_ostream &O) const override;
549 //===--------------------------------------------------------------------===//
550 /// DIELocList - Represents a pointer to a location list in the debug_loc
553 class DIELocList : public DIEValue {
554 // Index into the .debug_loc vector.
558 DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
560 /// getValue - Grab the current index out.
561 size_t getValue() const { return Index; }
563 /// EmitValue - Emit location data.
565 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
567 /// SizeOf - Determine size of location data in bytes.
569 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
571 // Implement isa/cast/dyncast.
572 static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
575 void print(raw_ostream &O) const override;
579 } // end llvm namespace