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/MC/MCExpr.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Dwarf.h"
27 class MCSymbolRefExpr;
31 //===--------------------------------------------------------------------===//
32 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
33 /// Dwarf abbreviation.
35 /// Attribute - Dwarf attribute code.
37 dwarf::Attribute Attribute;
39 /// Form - Dwarf form code.
44 DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
47 dwarf::Attribute getAttribute() const { return Attribute; }
48 dwarf::Form getForm() const { return Form; }
50 /// Profile - Used to gather unique data for the abbreviation folding set.
52 void Profile(FoldingSetNodeID &ID) const;
55 //===--------------------------------------------------------------------===//
56 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
57 /// information object.
58 class DIEAbbrev : public FoldingSetNode {
59 /// Unique number for node.
63 /// Tag - Dwarf tag code.
67 /// Children - Whether or not this node has children.
69 // This cheats a bit in all of the uses since the values in the standard
70 // are 0 and 1 for no children and children respectively.
73 /// Data - Raw data bytes for abbreviation.
75 SmallVector<DIEAbbrevData, 12> Data;
78 DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
81 dwarf::Tag getTag() const { return Tag; }
82 unsigned getNumber() const { return Number; }
83 bool hasChildren() const { return Children; }
84 const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
85 void setChildrenFlag(bool hasChild) { Children = hasChild; }
86 void setNumber(unsigned N) { Number = N; }
88 /// AddAttribute - Adds another set of attribute information to the
90 void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
91 Data.push_back(DIEAbbrevData(Attribute, Form));
94 /// Profile - Used to gather unique data for the abbreviation folding set.
96 void Profile(FoldingSetNodeID &ID) const;
98 /// Emit - Print the abbreviation using the specified asm printer.
100 void Emit(AsmPrinter *AP) const;
103 void print(raw_ostream &O);
108 //===--------------------------------------------------------------------===//
109 /// DIE - A structured debug information entry. Has an abbreviation which
110 /// describes its organization.
115 /// Offset - Offset in debug info section.
119 /// Size - Size of instance + children.
123 /// Abbrev - Buffer for constructing abbreviation.
129 std::vector<DIE *> Children;
133 /// Attribute values.
135 SmallVector<DIEValue *, 12> Values;
138 explicit DIE(unsigned Tag)
139 : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
144 DIEAbbrev &getAbbrev() { return Abbrev; }
145 const DIEAbbrev &getAbbrev() const { return Abbrev; }
146 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
147 dwarf::Tag getTag() const { return Abbrev.getTag(); }
148 unsigned getOffset() const { return Offset; }
149 unsigned getSize() const { return Size; }
150 const std::vector<DIE *> &getChildren() const { return Children; }
151 const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
152 DIE *getParent() const { return Parent; }
153 /// Climb up the parent chain to get the compile or type unit DIE this DIE
155 const DIE *getUnit() const;
156 /// Similar to getUnit, returns null when DIE is not added to an
158 const DIE *getUnitOrNull() const;
159 void setOffset(unsigned O) { Offset = O; }
160 void setSize(unsigned S) { Size = S; }
162 /// addValue - Add a value and attributes to a DIE.
164 void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
165 Abbrev.AddAttribute(Attribute, Form);
166 Values.push_back(Value);
169 /// addChild - Add a child to the DIE.
171 void addChild(DIE *Child) {
172 assert(!Child->getParent());
173 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
174 Children.push_back(Child);
175 Child->Parent = this;
178 /// findAttribute - Find a value in the DIE with the attribute given,
179 /// returns NULL if no such attribute exists.
180 DIEValue *findAttribute(dwarf::Attribute Attribute) const;
183 void print(raw_ostream &O, unsigned IndentCount = 0) const;
188 //===--------------------------------------------------------------------===//
189 /// DIEValue - A debug information entry value. Some of these roughly correlate
190 /// to DWARF attribute classes.
193 virtual void anchor();
210 /// Ty - Type of data stored in the value.
214 explicit DIEValue(Type T) : Ty(T) {}
215 virtual ~DIEValue() {}
219 Type getType() const { return Ty; }
221 /// EmitValue - Emit value via the Dwarf writer.
223 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
225 /// SizeOf - Return the size of a value in bytes.
227 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
230 virtual void print(raw_ostream &O) const = 0;
235 //===--------------------------------------------------------------------===//
236 /// DIEInteger - An integer value DIE.
238 class DIEInteger : public DIEValue {
242 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
244 /// BestForm - Choose the best form for integer.
246 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
248 const int64_t SignedInt = Int;
249 if ((char)Int == SignedInt)
250 return dwarf::DW_FORM_data1;
251 if ((short)Int == SignedInt)
252 return dwarf::DW_FORM_data2;
253 if ((int)Int == SignedInt)
254 return dwarf::DW_FORM_data4;
256 if ((unsigned char)Int == Int)
257 return dwarf::DW_FORM_data1;
258 if ((unsigned short)Int == Int)
259 return dwarf::DW_FORM_data2;
260 if ((unsigned int)Int == Int)
261 return dwarf::DW_FORM_data4;
263 return dwarf::DW_FORM_data8;
266 /// EmitValue - Emit integer of appropriate size.
268 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
270 uint64_t getValue() const { return Integer; }
272 /// SizeOf - Determine size of integer value in bytes.
274 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
276 // Implement isa/cast/dyncast.
277 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
280 void print(raw_ostream &O) const override;
284 //===--------------------------------------------------------------------===//
285 /// DIEExpr - An expression DIE.
287 class DIEExpr : public DIEValue {
291 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
293 /// EmitValue - Emit expression value.
295 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
297 /// getValue - Get MCExpr.
299 const MCExpr *getValue() const { return Expr; }
301 /// SizeOf - Determine size of expression value in bytes.
303 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
305 // Implement isa/cast/dyncast.
306 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
309 void print(raw_ostream &O) const override;
313 //===--------------------------------------------------------------------===//
314 /// DIELabel - A label DIE.
316 class DIELabel : public DIEValue {
317 const MCSymbol *Label;
320 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
322 /// EmitValue - Emit label value.
324 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
326 /// getValue - Get MCSymbol.
328 const MCSymbol *getValue() const { return Label; }
330 /// SizeOf - Determine size of label value in bytes.
332 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
334 // Implement isa/cast/dyncast.
335 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
338 void print(raw_ostream &O) const override;
342 //===--------------------------------------------------------------------===//
343 /// DIEDelta - A simple label difference DIE.
345 class DIEDelta : public DIEValue {
346 const MCSymbol *LabelHi;
347 const MCSymbol *LabelLo;
350 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
351 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
353 /// EmitValue - Emit delta value.
355 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
357 /// SizeOf - Determine size of delta value in bytes.
359 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
361 // Implement isa/cast/dyncast.
362 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
365 void print(raw_ostream &O) const override;
369 //===--------------------------------------------------------------------===//
370 /// DIEString - A container for string values.
372 class DIEString : public DIEValue {
373 const DIEValue *Access;
377 DIEString(const DIEValue *Acc, const StringRef S)
378 : DIEValue(isString), Access(Acc), Str(S) {}
380 /// getString - Grab the string out of the object.
381 StringRef getString() const { return Str; }
383 /// EmitValue - Emit delta value.
385 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
387 /// SizeOf - Determine size of delta value in bytes.
389 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
391 // Implement isa/cast/dyncast.
392 static bool classof(const DIEValue *D) { return D->getType() == isString; }
395 void print(raw_ostream &O) const override;
399 //===--------------------------------------------------------------------===//
400 /// DIEEntry - A pointer to another debug information entry. An instance of
401 /// this class can also be used as a proxy for a debug information entry not
402 /// yet defined (ie. types.)
403 class DIEEntry : public DIEValue {
407 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
408 assert(E && "Cannot construct a DIEEntry with a null DIE");
411 DIE *getEntry() const { return Entry; }
413 /// EmitValue - Emit debug information entry offset.
415 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
417 /// SizeOf - Determine size of debug information entry in bytes.
419 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
420 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
424 /// Returns size of a ref_addr entry.
425 static unsigned getRefAddrSize(AsmPrinter *AP);
427 // Implement isa/cast/dyncast.
428 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
431 void print(raw_ostream &O) const override;
435 //===--------------------------------------------------------------------===//
436 /// \brief A signature reference to a type unit.
437 class DIETypeSignature : public DIEValue {
438 const DwarfTypeUnit &Unit;
441 explicit DIETypeSignature(const DwarfTypeUnit &Unit)
442 : DIEValue(isTypeSignature), Unit(Unit) {}
444 /// \brief Emit type unit signature.
445 void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const override;
447 /// Returns size of a ref_sig8 entry.
448 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
449 assert(Form == dwarf::DW_FORM_ref_sig8);
453 // \brief Implement isa/cast/dyncast.
454 static bool classof(const DIEValue *E) {
455 return E->getType() == isTypeSignature;
458 void print(raw_ostream &O) const override;
463 //===--------------------------------------------------------------------===//
464 /// DIELoc - Represents an expression location.
466 class DIELoc : public DIEValue, public DIE {
467 mutable unsigned Size; // Size in bytes excluding size header.
469 DIELoc() : DIEValue(isLoc), DIE(0), Size(0) {}
471 /// ComputeSize - Calculate the size of the location expression.
473 unsigned ComputeSize(AsmPrinter *AP) const;
475 /// BestForm - Choose the best form for data.
477 dwarf::Form BestForm(unsigned DwarfVersion) const {
478 if (DwarfVersion > 3)
479 return dwarf::DW_FORM_exprloc;
480 // Pre-DWARF4 location expressions were blocks and not exprloc.
481 if ((unsigned char)Size == Size)
482 return dwarf::DW_FORM_block1;
483 if ((unsigned short)Size == Size)
484 return dwarf::DW_FORM_block2;
485 if ((unsigned int)Size == Size)
486 return dwarf::DW_FORM_block4;
487 return dwarf::DW_FORM_block;
490 /// EmitValue - Emit location data.
492 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
494 /// SizeOf - Determine size of location data in bytes.
496 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
498 // Implement isa/cast/dyncast.
499 static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
502 void print(raw_ostream &O) const override;
506 //===--------------------------------------------------------------------===//
507 /// DIEBlock - Represents a block of values.
509 class DIEBlock : public DIEValue, public DIE {
510 mutable unsigned Size; // Size in bytes excluding size header.
512 DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
514 /// ComputeSize - Calculate the size of the location expression.
516 unsigned ComputeSize(AsmPrinter *AP) const;
518 /// BestForm - Choose the best form for data.
520 dwarf::Form BestForm() const {
521 if ((unsigned char)Size == Size)
522 return dwarf::DW_FORM_block1;
523 if ((unsigned short)Size == Size)
524 return dwarf::DW_FORM_block2;
525 if ((unsigned int)Size == Size)
526 return dwarf::DW_FORM_block4;
527 return dwarf::DW_FORM_block;
530 /// EmitValue - Emit location data.
532 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
534 /// SizeOf - Determine size of location data in bytes.
536 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
538 // Implement isa/cast/dyncast.
539 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
542 void print(raw_ostream &O) const override;
546 //===--------------------------------------------------------------------===//
547 /// DIELocList - Represents a pointer to a location list in the debug_loc
550 class DIELocList : public DIEValue {
551 // Index into the .debug_loc vector.
555 DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
557 /// getValue - Grab the current index out.
558 size_t getValue() const { return Index; }
560 /// EmitValue - Emit location data.
562 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
564 /// SizeOf - Determine size of location data in bytes.
566 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
568 // Implement isa/cast/dyncast.
569 static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
572 void print(raw_ostream &O) const override;
576 } // end llvm namespace