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 /// Tag - Dwarf tag code.
63 /// ChildrenFlag - Dwarf children flag.
65 uint16_t ChildrenFlag;
67 /// Unique number for node.
71 /// Data - Raw data bytes for abbreviation.
73 SmallVector<DIEAbbrevData, 12> Data;
76 DIEAbbrev(dwarf::Tag T, uint16_t C) : Tag(T), ChildrenFlag(C), Data() {}
79 dwarf::Tag getTag() const { return Tag; }
80 unsigned getNumber() const { return Number; }
81 uint16_t getChildrenFlag() const { return ChildrenFlag; }
82 const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
83 void setChildrenFlag(uint16_t CF) { ChildrenFlag = CF; }
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();
207 /// Type - Type of data stored in the value.
212 explicit DIEValue(unsigned T) : Type(T) {}
213 virtual ~DIEValue() {}
216 unsigned getType() const { return Type; }
218 /// EmitValue - Emit value via the Dwarf writer.
220 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
222 /// SizeOf - Return the size of a value in bytes.
224 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
227 virtual void print(raw_ostream &O) const = 0;
232 //===--------------------------------------------------------------------===//
233 /// DIEInteger - An integer value DIE.
235 class DIEInteger : public DIEValue {
239 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
241 /// BestForm - Choose the best form for integer.
243 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
245 const int64_t SignedInt = Int;
246 if ((char)Int == SignedInt)
247 return dwarf::DW_FORM_data1;
248 if ((short)Int == SignedInt)
249 return dwarf::DW_FORM_data2;
250 if ((int)Int == SignedInt)
251 return dwarf::DW_FORM_data4;
253 if ((unsigned char)Int == Int)
254 return dwarf::DW_FORM_data1;
255 if ((unsigned short)Int == Int)
256 return dwarf::DW_FORM_data2;
257 if ((unsigned int)Int == Int)
258 return dwarf::DW_FORM_data4;
260 return dwarf::DW_FORM_data8;
263 /// EmitValue - Emit integer of appropriate size.
265 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
267 uint64_t getValue() const { return Integer; }
269 /// SizeOf - Determine size of integer value in bytes.
271 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
273 // Implement isa/cast/dyncast.
274 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
277 virtual void print(raw_ostream &O) const;
281 //===--------------------------------------------------------------------===//
282 /// DIEExpr - An expression DIE.
284 class DIEExpr : public DIEValue {
288 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
290 /// EmitValue - Emit expression value.
292 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
294 /// getValue - Get MCExpr.
296 const MCExpr *getValue() const { return Expr; }
298 /// SizeOf - Determine size of expression value in bytes.
300 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
302 // Implement isa/cast/dyncast.
303 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
306 virtual void print(raw_ostream &O) const;
310 //===--------------------------------------------------------------------===//
311 /// DIELabel - A label DIE.
313 class DIELabel : public DIEValue {
314 const MCSymbol *Label;
317 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
319 /// EmitValue - Emit label value.
321 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
323 /// getValue - Get MCSymbol.
325 const MCSymbol *getValue() const { return Label; }
327 /// SizeOf - Determine size of label value in bytes.
329 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
331 // Implement isa/cast/dyncast.
332 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
335 virtual void print(raw_ostream &O) const;
339 //===--------------------------------------------------------------------===//
340 /// DIEDelta - A simple label difference DIE.
342 class DIEDelta : public DIEValue {
343 const MCSymbol *LabelHi;
344 const MCSymbol *LabelLo;
347 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
348 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
350 /// EmitValue - Emit delta value.
352 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
354 /// SizeOf - Determine size of delta value in bytes.
356 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
358 // Implement isa/cast/dyncast.
359 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
362 virtual void print(raw_ostream &O) const;
366 //===--------------------------------------------------------------------===//
367 /// DIEString - A container for string values.
369 class DIEString : public DIEValue {
370 const DIEValue *Access;
374 DIEString(const DIEValue *Acc, const StringRef S)
375 : DIEValue(isString), Access(Acc), Str(S) {}
377 /// getString - Grab the string out of the object.
378 StringRef getString() const { return Str; }
380 /// EmitValue - Emit delta value.
382 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
384 /// SizeOf - Determine size of delta value in bytes.
386 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
388 // Implement isa/cast/dyncast.
389 static bool classof(const DIEValue *D) { return D->getType() == isString; }
392 virtual void print(raw_ostream &O) const;
396 //===--------------------------------------------------------------------===//
397 /// DIEEntry - A pointer to another debug information entry. An instance of
398 /// this class can also be used as a proxy for a debug information entry not
399 /// yet defined (ie. types.)
400 class DIEEntry : public DIEValue {
404 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
405 assert(E && "Cannot construct a DIEEntry with a null DIE");
408 DIE *getEntry() const { return Entry; }
410 /// EmitValue - Emit debug information entry offset.
412 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
414 /// SizeOf - Determine size of debug information entry in bytes.
416 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
417 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
421 /// Returns size of a ref_addr entry.
422 static unsigned getRefAddrSize(AsmPrinter *AP);
424 // Implement isa/cast/dyncast.
425 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
428 virtual void print(raw_ostream &O) const;
432 //===--------------------------------------------------------------------===//
433 /// \brief A signature reference to a type unit.
434 class DIETypeSignature : public DIEValue {
435 const DwarfTypeUnit &Unit;
438 explicit DIETypeSignature(const DwarfTypeUnit &Unit)
439 : DIEValue(isTypeSignature), Unit(Unit) {}
441 /// \brief Emit type unit signature.
442 virtual void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const;
444 /// Returns size of a ref_sig8 entry.
445 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
446 assert(Form == dwarf::DW_FORM_ref_sig8);
450 // \brief Implement isa/cast/dyncast.
451 static bool classof(const DIEValue *E) {
452 return E->getType() == isTypeSignature;
455 virtual void print(raw_ostream &O) const;
460 //===--------------------------------------------------------------------===//
461 /// DIELoc - Represents an expression location.
463 class DIELoc : public DIEValue, public DIE {
464 mutable unsigned Size; // Size in bytes excluding size header.
466 DIELoc() : DIEValue(isLoc), DIE(0), Size(0) {}
468 /// ComputeSize - Calculate the size of the location expression.
470 unsigned ComputeSize(AsmPrinter *AP) const;
472 /// BestForm - Choose the best form for data.
474 dwarf::Form BestForm(unsigned DwarfVersion) const {
475 if (DwarfVersion > 3)
476 return dwarf::DW_FORM_exprloc;
477 // Pre-DWARF4 location expressions were blocks and not exprloc.
478 if ((unsigned char)Size == Size)
479 return dwarf::DW_FORM_block1;
480 if ((unsigned short)Size == Size)
481 return dwarf::DW_FORM_block2;
482 if ((unsigned int)Size == Size)
483 return dwarf::DW_FORM_block4;
484 return dwarf::DW_FORM_block;
487 /// EmitValue - Emit location data.
489 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
491 /// SizeOf - Determine size of location data in bytes.
493 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
495 // Implement isa/cast/dyncast.
496 static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
499 virtual void print(raw_ostream &O) const;
503 //===--------------------------------------------------------------------===//
504 /// DIEBlock - Represents a block of values.
506 class DIEBlock : public DIEValue, public DIE {
507 mutable unsigned Size; // Size in bytes excluding size header.
509 DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
511 /// ComputeSize - Calculate the size of the location expression.
513 unsigned ComputeSize(AsmPrinter *AP) const;
515 /// BestForm - Choose the best form for data.
517 dwarf::Form BestForm() const {
518 if ((unsigned char)Size == Size)
519 return dwarf::DW_FORM_block1;
520 if ((unsigned short)Size == Size)
521 return dwarf::DW_FORM_block2;
522 if ((unsigned int)Size == Size)
523 return dwarf::DW_FORM_block4;
524 return dwarf::DW_FORM_block;
527 /// EmitValue - Emit location data.
529 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
531 /// SizeOf - Determine size of location data in bytes.
533 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
535 // Implement isa/cast/dyncast.
536 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
539 virtual void print(raw_ostream &O) const;
542 } // end llvm namespace