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<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),
147 DIEAbbrev &getAbbrev() { return Abbrev; }
148 const DIEAbbrev &getAbbrev() const { return Abbrev; }
149 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
150 dwarf::Tag getTag() const { return Abbrev.getTag(); }
151 unsigned getOffset() const { return Offset; }
152 unsigned getSize() const { return Size; }
153 const std::vector<DIE *> &getChildren() const { return Children; }
154 const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
155 DIE *getParent() const { return Parent; }
156 /// Climb up the parent chain to get the compile or type unit DIE this DIE
158 const DIE *getUnit() const;
159 /// Similar to getUnit, returns null when DIE is not added to an
161 const DIE *getUnitOrNull() const;
162 void setOffset(unsigned O) { Offset = O; }
163 void setSize(unsigned S) { Size = S; }
165 /// addValue - Add a value and attributes to a DIE.
167 void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
168 Abbrev.AddAttribute(Attribute, Form);
169 Values.push_back(Value);
172 /// addChild - Add a child to the DIE.
174 void addChild(DIE *Child) {
175 assert(!Child->getParent());
176 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
177 Children.push_back(Child);
178 Child->Parent = this;
181 /// findAttribute - Find a value in the DIE with the attribute given,
182 /// returns NULL if no such attribute exists.
183 DIEValue *findAttribute(dwarf::Attribute Attribute) const;
186 void print(raw_ostream &O, unsigned IndentCount = 0) const;
191 //===--------------------------------------------------------------------===//
192 /// DIEValue - A debug information entry value. Some of these roughly correlate
193 /// to DWARF attribute classes.
196 virtual void anchor();
213 /// Ty - Type of data stored in the value.
217 explicit DIEValue(Type T) : Ty(T) {}
218 virtual ~DIEValue() {}
222 Type getType() const { return Ty; }
224 /// EmitValue - Emit value via the Dwarf writer.
226 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
228 /// SizeOf - Return the size of a value in bytes.
230 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
233 virtual void print(raw_ostream &O) const = 0;
238 //===--------------------------------------------------------------------===//
239 /// DIEInteger - An integer value DIE.
241 class DIEInteger : public DIEValue {
245 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
247 /// BestForm - Choose the best form for integer.
249 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
251 const int64_t SignedInt = Int;
252 if ((char)Int == SignedInt)
253 return dwarf::DW_FORM_data1;
254 if ((short)Int == SignedInt)
255 return dwarf::DW_FORM_data2;
256 if ((int)Int == SignedInt)
257 return dwarf::DW_FORM_data4;
259 if ((unsigned char)Int == Int)
260 return dwarf::DW_FORM_data1;
261 if ((unsigned short)Int == Int)
262 return dwarf::DW_FORM_data2;
263 if ((unsigned int)Int == Int)
264 return dwarf::DW_FORM_data4;
266 return dwarf::DW_FORM_data8;
269 /// EmitValue - Emit integer of appropriate size.
271 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
273 uint64_t getValue() const { return Integer; }
275 /// SizeOf - Determine size of integer value in bytes.
277 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
279 // Implement isa/cast/dyncast.
280 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
283 void print(raw_ostream &O) const override;
287 //===--------------------------------------------------------------------===//
288 /// DIEExpr - An expression DIE.
290 class DIEExpr : public DIEValue {
294 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
296 /// EmitValue - Emit expression value.
298 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
300 /// getValue - Get MCExpr.
302 const MCExpr *getValue() const { return Expr; }
304 /// SizeOf - Determine size of expression value in bytes.
306 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
308 // Implement isa/cast/dyncast.
309 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
312 void print(raw_ostream &O) const override;
316 //===--------------------------------------------------------------------===//
317 /// DIELabel - A label DIE.
319 class DIELabel : public DIEValue {
320 const MCSymbol *Label;
323 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
325 /// EmitValue - Emit label value.
327 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
329 /// getValue - Get MCSymbol.
331 const MCSymbol *getValue() const { return Label; }
333 /// SizeOf - Determine size of label value in bytes.
335 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
337 // Implement isa/cast/dyncast.
338 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
341 void print(raw_ostream &O) const override;
345 //===--------------------------------------------------------------------===//
346 /// DIEDelta - A simple label difference DIE.
348 class DIEDelta : public DIEValue {
349 const MCSymbol *LabelHi;
350 const MCSymbol *LabelLo;
353 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
354 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
356 /// EmitValue - Emit delta value.
358 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
360 /// SizeOf - Determine size of delta value in bytes.
362 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
364 // Implement isa/cast/dyncast.
365 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
368 void print(raw_ostream &O) const override;
372 //===--------------------------------------------------------------------===//
373 /// DIEString - A container for string values.
375 class DIEString : public DIEValue {
376 const DIEValue *Access;
380 DIEString(const DIEValue *Acc, const 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 /// EmitValue - Emit delta value.
388 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
390 /// SizeOf - Determine size of delta value in bytes.
392 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
394 // Implement isa/cast/dyncast.
395 static bool classof(const DIEValue *D) { return D->getType() == isString; }
398 void print(raw_ostream &O) const override;
402 //===--------------------------------------------------------------------===//
403 /// DIEEntry - A pointer to another debug information entry. An instance of
404 /// this class can also be used as a proxy for a debug information entry not
405 /// yet defined (ie. types.)
406 class DIEEntry : public DIEValue {
410 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
411 assert(E && "Cannot construct a DIEEntry with a null DIE");
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