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;
136 explicit DIE(unsigned Tag)
137 : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
142 DIEAbbrev &getAbbrev() { return Abbrev; }
143 const DIEAbbrev &getAbbrev() const { return Abbrev; }
144 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
145 dwarf::Tag getTag() const { return Abbrev.getTag(); }
146 unsigned getOffset() const { return Offset; }
147 unsigned getSize() const { return Size; }
148 const std::vector<DIE *> &getChildren() const { return Children; }
149 const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
150 DIE *getParent() const { return Parent; }
151 /// Climb up the parent chain to get the compile or type unit DIE this DIE
153 const DIE *getUnit() const;
154 /// Similar to getUnit, returns null when DIE is not added to an
156 const DIE *getUnitOrNull() const;
157 void setOffset(unsigned O) { Offset = O; }
158 void setSize(unsigned S) { Size = S; }
160 /// addValue - Add a value and attributes to a DIE.
162 void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
163 Abbrev.AddAttribute(Attribute, Form);
164 Values.push_back(Value);
167 /// addChild - Add a child to the DIE.
169 void addChild(DIE *Child) {
170 assert(!Child->getParent());
171 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
172 Children.push_back(Child);
173 Child->Parent = this;
176 /// findAttribute - Find a value in the DIE with the attribute given,
177 /// returns NULL if no such attribute exists.
178 DIEValue *findAttribute(dwarf::Attribute Attribute) const;
181 void print(raw_ostream &O, unsigned IndentCount = 0) const;
186 //===--------------------------------------------------------------------===//
187 /// DIEValue - A debug information entry value. Some of these roughly correlate
188 /// to DWARF attribute classes.
191 virtual void anchor();
208 /// Ty - Type of data stored in the value.
212 explicit DIEValue(Type T) : Ty(T) {}
213 virtual ~DIEValue() {}
217 Type getType() const { return Ty; }
219 /// EmitValue - Emit value via the Dwarf writer.
221 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
223 /// SizeOf - Return the size of a value in bytes.
225 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
228 virtual void print(raw_ostream &O) const = 0;
233 //===--------------------------------------------------------------------===//
234 /// DIEInteger - An integer value DIE.
236 class DIEInteger : public DIEValue {
240 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
242 /// BestForm - Choose the best form for integer.
244 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
246 const int64_t SignedInt = Int;
247 if ((char)Int == SignedInt)
248 return dwarf::DW_FORM_data1;
249 if ((short)Int == SignedInt)
250 return dwarf::DW_FORM_data2;
251 if ((int)Int == SignedInt)
252 return dwarf::DW_FORM_data4;
254 if ((unsigned char)Int == Int)
255 return dwarf::DW_FORM_data1;
256 if ((unsigned short)Int == Int)
257 return dwarf::DW_FORM_data2;
258 if ((unsigned int)Int == Int)
259 return dwarf::DW_FORM_data4;
261 return dwarf::DW_FORM_data8;
264 /// EmitValue - Emit integer of appropriate size.
266 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
268 uint64_t getValue() const { return Integer; }
270 /// SizeOf - Determine size of integer value in bytes.
272 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
274 // Implement isa/cast/dyncast.
275 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
278 void print(raw_ostream &O) const override;
282 //===--------------------------------------------------------------------===//
283 /// DIEExpr - An expression DIE.
285 class DIEExpr : public DIEValue {
289 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
291 /// EmitValue - Emit expression value.
293 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
295 /// getValue - Get MCExpr.
297 const MCExpr *getValue() const { return Expr; }
299 /// SizeOf - Determine size of expression value in bytes.
301 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
303 // Implement isa/cast/dyncast.
304 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
307 void print(raw_ostream &O) const override;
311 //===--------------------------------------------------------------------===//
312 /// DIELabel - A label DIE.
314 class DIELabel : public DIEValue {
315 const MCSymbol *Label;
318 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
320 /// EmitValue - Emit label value.
322 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
324 /// getValue - Get MCSymbol.
326 const MCSymbol *getValue() const { return Label; }
328 /// SizeOf - Determine size of label value in bytes.
330 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
332 // Implement isa/cast/dyncast.
333 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
336 void print(raw_ostream &O) const override;
340 //===--------------------------------------------------------------------===//
341 /// DIEDelta - A simple label difference DIE.
343 class DIEDelta : public DIEValue {
344 const MCSymbol *LabelHi;
345 const MCSymbol *LabelLo;
348 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
349 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
351 /// EmitValue - Emit delta value.
353 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
355 /// SizeOf - Determine size of delta value in bytes.
357 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
359 // Implement isa/cast/dyncast.
360 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
363 void print(raw_ostream &O) const override;
367 //===--------------------------------------------------------------------===//
368 /// DIEString - A container for string values.
370 class DIEString : public DIEValue {
371 const DIEValue *Access;
375 DIEString(const DIEValue *Acc, const StringRef S)
376 : DIEValue(isString), Access(Acc), Str(S) {}
378 /// getString - Grab the string out of the object.
379 StringRef getString() const { return Str; }
381 /// EmitValue - Emit delta value.
383 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
385 /// SizeOf - Determine size of delta value in bytes.
387 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
389 // Implement isa/cast/dyncast.
390 static bool classof(const DIEValue *D) { return D->getType() == isString; }
393 void print(raw_ostream &O) const override;
397 //===--------------------------------------------------------------------===//
398 /// DIEEntry - A pointer to another debug information entry. An instance of
399 /// this class can also be used as a proxy for a debug information entry not
400 /// yet defined (ie. types.)
401 class DIEEntry : public DIEValue {
405 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
406 assert(E && "Cannot construct a DIEEntry with a null DIE");
409 DIE *getEntry() const { return Entry; }
411 /// EmitValue - Emit debug information entry offset.
413 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
415 /// SizeOf - Determine size of debug information entry in bytes.
417 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
418 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
422 /// Returns size of a ref_addr entry.
423 static unsigned getRefAddrSize(AsmPrinter *AP);
425 // Implement isa/cast/dyncast.
426 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
429 void print(raw_ostream &O) const override;
433 //===--------------------------------------------------------------------===//
434 /// \brief A signature reference to a type unit.
435 class DIETypeSignature : public DIEValue {
436 const DwarfTypeUnit &Unit;
439 explicit DIETypeSignature(const DwarfTypeUnit &Unit)
440 : DIEValue(isTypeSignature), Unit(Unit) {}
442 /// \brief Emit type unit signature.
443 void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const override;
445 /// Returns size of a ref_sig8 entry.
446 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
447 assert(Form == dwarf::DW_FORM_ref_sig8);
451 // \brief Implement isa/cast/dyncast.
452 static bool classof(const DIEValue *E) {
453 return E->getType() == isTypeSignature;
456 void print(raw_ostream &O) const override;
461 //===--------------------------------------------------------------------===//
462 /// DIELoc - Represents an expression location.
464 class DIELoc : public DIEValue, public DIE {
465 mutable unsigned Size; // Size in bytes excluding size header.
467 DIELoc() : DIEValue(isLoc), DIE(0), Size(0) {}
469 /// ComputeSize - Calculate the size of the location expression.
471 unsigned ComputeSize(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 /// EmitValue - Emit location data.
490 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
492 /// SizeOf - Determine size of location data in bytes.
494 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
496 // Implement isa/cast/dyncast.
497 static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
500 void print(raw_ostream &O) const override;
504 //===--------------------------------------------------------------------===//
505 /// DIEBlock - Represents a block of values.
507 class DIEBlock : public DIEValue, public DIE {
508 mutable unsigned Size; // Size in bytes excluding size header.
510 DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
512 /// ComputeSize - Calculate the size of the location expression.
514 unsigned ComputeSize(AsmPrinter *AP) const;
516 /// BestForm - Choose the best form for data.
518 dwarf::Form BestForm() const {
519 if ((unsigned char)Size == Size)
520 return dwarf::DW_FORM_block1;
521 if ((unsigned short)Size == Size)
522 return dwarf::DW_FORM_block2;
523 if ((unsigned int)Size == Size)
524 return dwarf::DW_FORM_block4;
525 return dwarf::DW_FORM_block;
528 /// EmitValue - Emit location data.
530 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
532 /// SizeOf - Determine size of location data in bytes.
534 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
536 // Implement isa/cast/dyncast.
537 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
540 void print(raw_ostream &O) const override;
544 //===--------------------------------------------------------------------===//
545 /// DIELocList - Represents a pointer to a location list in the debug_loc
548 class DIELocList : public DIEValue {
549 // Index into the .debug_loc vector.
553 DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
555 /// getValue - Grab the current index out.
556 size_t getValue() const { return Index; }
558 /// EmitValue - Emit location data.
560 void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
562 /// SizeOf - Determine size of location data in bytes.
564 unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
566 // Implement isa/cast/dyncast.
567 static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
570 void print(raw_ostream &O) const override;
574 } // end llvm namespace