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();
209 /// Ty - Type of data stored in the value.
213 explicit DIEValue(Type T) : Ty(T) {}
214 virtual ~DIEValue() {}
218 Type getType() const { return Ty; }
220 /// EmitValue - Emit value via the Dwarf writer.
222 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
224 /// SizeOf - Return the size of a value in bytes.
226 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
229 virtual void print(raw_ostream &O) const = 0;
234 //===--------------------------------------------------------------------===//
235 /// DIEInteger - An integer value DIE.
237 class DIEInteger : public DIEValue {
241 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
243 /// BestForm - Choose the best form for integer.
245 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
247 const int64_t SignedInt = Int;
248 if ((char)Int == SignedInt)
249 return dwarf::DW_FORM_data1;
250 if ((short)Int == SignedInt)
251 return dwarf::DW_FORM_data2;
252 if ((int)Int == SignedInt)
253 return dwarf::DW_FORM_data4;
255 if ((unsigned char)Int == Int)
256 return dwarf::DW_FORM_data1;
257 if ((unsigned short)Int == Int)
258 return dwarf::DW_FORM_data2;
259 if ((unsigned int)Int == Int)
260 return dwarf::DW_FORM_data4;
262 return dwarf::DW_FORM_data8;
265 /// EmitValue - Emit integer of appropriate size.
267 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
269 uint64_t getValue() const { return Integer; }
271 /// SizeOf - Determine size of integer value in bytes.
273 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
275 // Implement isa/cast/dyncast.
276 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
279 virtual void print(raw_ostream &O) const;
283 //===--------------------------------------------------------------------===//
284 /// DIEExpr - An expression DIE.
286 class DIEExpr : public DIEValue {
290 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
292 /// EmitValue - Emit expression value.
294 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
296 /// getValue - Get MCExpr.
298 const MCExpr *getValue() const { return Expr; }
300 /// SizeOf - Determine size of expression value in bytes.
302 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
304 // Implement isa/cast/dyncast.
305 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
308 virtual void print(raw_ostream &O) const;
312 //===--------------------------------------------------------------------===//
313 /// DIELabel - A label DIE.
315 class DIELabel : public DIEValue {
316 const MCSymbol *Label;
319 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
321 /// EmitValue - Emit label value.
323 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
325 /// getValue - Get MCSymbol.
327 const MCSymbol *getValue() const { return Label; }
329 /// SizeOf - Determine size of label value in bytes.
331 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
333 // Implement isa/cast/dyncast.
334 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
337 virtual void print(raw_ostream &O) const;
341 //===--------------------------------------------------------------------===//
342 /// DIEDelta - A simple label difference DIE.
344 class DIEDelta : public DIEValue {
345 const MCSymbol *LabelHi;
346 const MCSymbol *LabelLo;
349 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
350 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
352 /// EmitValue - Emit delta value.
354 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
356 /// SizeOf - Determine size of delta value in bytes.
358 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
360 // Implement isa/cast/dyncast.
361 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
364 virtual void print(raw_ostream &O) const;
368 //===--------------------------------------------------------------------===//
369 /// DIEString - A container for string values.
371 class DIEString : public DIEValue {
372 const DIEValue *Access;
376 DIEString(const DIEValue *Acc, const StringRef S)
377 : DIEValue(isString), Access(Acc), Str(S) {}
379 /// getString - Grab the string out of the object.
380 StringRef getString() const { return Str; }
382 /// EmitValue - Emit delta value.
384 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
386 /// SizeOf - Determine size of delta value in bytes.
388 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
390 // Implement isa/cast/dyncast.
391 static bool classof(const DIEValue *D) { return D->getType() == isString; }
394 virtual void print(raw_ostream &O) const;
398 //===--------------------------------------------------------------------===//
399 /// DIEEntry - A pointer to another debug information entry. An instance of
400 /// this class can also be used as a proxy for a debug information entry not
401 /// yet defined (ie. types.)
402 class DIEEntry : public DIEValue {
406 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
407 assert(E && "Cannot construct a DIEEntry with a null DIE");
410 DIE *getEntry() const { return Entry; }
412 /// EmitValue - Emit debug information entry offset.
414 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
416 /// SizeOf - Determine size of debug information entry in bytes.
418 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
419 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
423 /// Returns size of a ref_addr entry.
424 static unsigned getRefAddrSize(AsmPrinter *AP);
426 // Implement isa/cast/dyncast.
427 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
430 virtual void print(raw_ostream &O) const;
434 //===--------------------------------------------------------------------===//
435 /// \brief A signature reference to a type unit.
436 class DIETypeSignature : public DIEValue {
437 const DwarfTypeUnit &Unit;
440 explicit DIETypeSignature(const DwarfTypeUnit &Unit)
441 : DIEValue(isTypeSignature), Unit(Unit) {}
443 /// \brief Emit type unit signature.
444 virtual void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const;
446 /// Returns size of a ref_sig8 entry.
447 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
448 assert(Form == dwarf::DW_FORM_ref_sig8);
452 // \brief Implement isa/cast/dyncast.
453 static bool classof(const DIEValue *E) {
454 return E->getType() == isTypeSignature;
457 virtual void print(raw_ostream &O) const;
462 //===--------------------------------------------------------------------===//
463 /// DIELoc - Represents an expression location.
465 class DIELoc : public DIEValue, public DIE {
466 mutable unsigned Size; // Size in bytes excluding size header.
468 DIELoc() : DIEValue(isLoc), DIE(0), Size(0) {}
470 /// ComputeSize - Calculate the size of the location expression.
472 unsigned ComputeSize(AsmPrinter *AP) const;
474 /// BestForm - Choose the best form for data.
476 dwarf::Form BestForm(unsigned DwarfVersion) const {
477 if (DwarfVersion > 3)
478 return dwarf::DW_FORM_exprloc;
479 // Pre-DWARF4 location expressions were blocks and not exprloc.
480 if ((unsigned char)Size == Size)
481 return dwarf::DW_FORM_block1;
482 if ((unsigned short)Size == Size)
483 return dwarf::DW_FORM_block2;
484 if ((unsigned int)Size == Size)
485 return dwarf::DW_FORM_block4;
486 return dwarf::DW_FORM_block;
489 /// EmitValue - Emit location data.
491 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
493 /// SizeOf - Determine size of location data in bytes.
495 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
497 // Implement isa/cast/dyncast.
498 static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
501 virtual void print(raw_ostream &O) const;
505 //===--------------------------------------------------------------------===//
506 /// DIEBlock - Represents a block of values.
508 class DIEBlock : public DIEValue, public DIE {
509 mutable unsigned Size; // Size in bytes excluding size header.
511 DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
513 /// ComputeSize - Calculate the size of the location expression.
515 unsigned ComputeSize(AsmPrinter *AP) const;
517 /// BestForm - Choose the best form for data.
519 dwarf::Form BestForm() const {
520 if ((unsigned char)Size == Size)
521 return dwarf::DW_FORM_block1;
522 if ((unsigned short)Size == Size)
523 return dwarf::DW_FORM_block2;
524 if ((unsigned int)Size == Size)
525 return dwarf::DW_FORM_block4;
526 return dwarf::DW_FORM_block;
529 /// EmitValue - Emit location data.
531 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
533 /// SizeOf - Determine size of location data in bytes.
535 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
537 // Implement isa/cast/dyncast.
538 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
541 virtual void print(raw_ostream &O) const;
544 } // end llvm namespace