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.
43 DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
46 dwarf::Attribute getAttribute() const { return Attribute; }
47 dwarf::Form getForm() const { return Form; }
49 /// Profile - Used to gather unique data for the abbreviation folding set.
51 void Profile(FoldingSetNodeID &ID) const;
54 //===--------------------------------------------------------------------===//
55 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
56 /// information object.
57 class DIEAbbrev : public FoldingSetNode {
58 /// Tag - Dwarf tag code.
62 /// ChildrenFlag - Dwarf children flag.
64 uint16_t ChildrenFlag;
66 /// Unique number for node.
70 /// Data - Raw data bytes for abbreviation.
72 SmallVector<DIEAbbrevData, 12> Data;
75 DIEAbbrev(dwarf::Tag T, uint16_t C) : Tag(T), ChildrenFlag(C), Data() {}
78 dwarf::Tag getTag() const { return Tag; }
79 unsigned getNumber() const { return Number; }
80 uint16_t getChildrenFlag() const { return ChildrenFlag; }
81 const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
82 void setChildrenFlag(uint16_t CF) { ChildrenFlag = CF; }
83 void setNumber(unsigned N) { Number = N; }
85 /// AddAttribute - Adds another set of attribute information to the
87 void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
88 Data.push_back(DIEAbbrevData(Attribute, Form));
91 /// Profile - Used to gather unique data for the abbreviation folding set.
93 void Profile(FoldingSetNodeID &ID) const;
95 /// Emit - Print the abbreviation using the specified asm printer.
97 void Emit(AsmPrinter *AP) const;
100 void print(raw_ostream &O);
105 //===--------------------------------------------------------------------===//
106 /// DIE - A structured debug information entry. Has an abbreviation which
107 /// describes its organization.
112 /// Offset - Offset in debug info section.
116 /// Size - Size of instance + children.
120 /// Abbrev - Buffer for constructing abbreviation.
126 std::vector<DIE *> Children;
130 /// Attribute values.
132 SmallVector<DIEValue*, 12> Values;
135 explicit DIE(unsigned Tag)
136 : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
141 DIEAbbrev &getAbbrev() { return Abbrev; }
142 const DIEAbbrev &getAbbrev() const { return Abbrev; }
143 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
144 dwarf::Tag getTag() const { return Abbrev.getTag(); }
145 unsigned getOffset() const { return Offset; }
146 unsigned getSize() const { return Size; }
147 const std::vector<DIE *> &getChildren() const { return Children; }
148 const SmallVectorImpl<DIEValue*> &getValues() const { return Values; }
149 DIE *getParent() const { return Parent; }
150 /// Climb up the parent chain to get the compile or type unit DIE this DIE
152 const DIE *getUnit() const;
153 /// Similar to getUnit, returns null when DIE is not added to an
155 const DIE *getUnitOrNull() const;
156 void setOffset(unsigned O) { Offset = O; }
157 void setSize(unsigned S) { Size = S; }
159 /// addValue - Add a value and attributes to a DIE.
161 void addValue(dwarf::Attribute Attribute, dwarf::Form Form,
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(uint16_t Attribute) const;
181 void print(raw_ostream &O, unsigned IndentCount = 0) const;
186 //===--------------------------------------------------------------------===//
187 /// DIEValue - A debug information entry value.
190 virtual void anchor();
204 /// Type - Type of data stored in the value.
208 explicit DIEValue(unsigned T) : Type(T) {}
209 virtual ~DIEValue() {}
212 unsigned getType() const { return Type; }
214 /// EmitValue - Emit value via the Dwarf writer.
216 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
218 /// SizeOf - Return the size of a value in bytes.
220 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
223 virtual void print(raw_ostream &O) const = 0;
228 //===--------------------------------------------------------------------===//
229 /// DIEInteger - An integer value DIE.
231 class DIEInteger : public DIEValue {
234 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
236 /// BestForm - Choose the best form for integer.
238 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
240 const int64_t SignedInt = Int;
241 if ((char)Int == SignedInt) return dwarf::DW_FORM_data1;
242 if ((short)Int == SignedInt) return dwarf::DW_FORM_data2;
243 if ((int)Int == SignedInt) return dwarf::DW_FORM_data4;
245 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
246 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
247 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
249 return dwarf::DW_FORM_data8;
252 /// EmitValue - Emit integer of appropriate size.
254 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
256 uint64_t getValue() const { return Integer; }
258 /// SizeOf - Determine size of integer value in bytes.
260 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
262 // Implement isa/cast/dyncast.
263 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
266 virtual void print(raw_ostream &O) const;
270 //===--------------------------------------------------------------------===//
271 /// DIEExpr - An expression DIE.
273 class DIEExpr : public DIEValue {
276 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
278 /// EmitValue - Emit expression value.
280 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
282 /// getValue - Get MCExpr.
284 const MCExpr *getValue() const { return Expr; }
286 /// SizeOf - Determine size of expression value in bytes.
288 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
290 // Implement isa/cast/dyncast.
291 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
294 virtual void print(raw_ostream &O) const;
298 //===--------------------------------------------------------------------===//
299 /// DIELabel - A label DIE.
301 class DIELabel : public DIEValue {
302 const MCSymbol *Label;
304 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
306 /// EmitValue - Emit label value.
308 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
310 /// getValue - Get MCSymbol.
312 const MCSymbol *getValue() const { return Label; }
314 /// SizeOf - Determine size of label value in bytes.
316 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
318 // Implement isa/cast/dyncast.
319 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
322 virtual void print(raw_ostream &O) const;
326 //===--------------------------------------------------------------------===//
327 /// DIEDelta - A simple label difference DIE.
329 class DIEDelta : public DIEValue {
330 const MCSymbol *LabelHi;
331 const MCSymbol *LabelLo;
333 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
334 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
336 /// EmitValue - Emit delta value.
338 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
340 /// SizeOf - Determine size of delta value in bytes.
342 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
344 // Implement isa/cast/dyncast.
345 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
348 virtual void print(raw_ostream &O) const;
352 //===--------------------------------------------------------------------===//
353 /// DIEString - A container for string values.
355 class DIEString : public DIEValue {
356 const DIEValue *Access;
360 DIEString(const DIEValue *Acc, const StringRef S)
361 : DIEValue(isString), Access(Acc), Str(S) {}
363 /// getString - Grab the string out of the object.
364 StringRef getString() const { return Str; }
366 /// EmitValue - Emit delta value.
368 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
370 /// SizeOf - Determine size of delta value in bytes.
372 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
374 // Implement isa/cast/dyncast.
375 static bool classof(const DIEValue *D) { return D->getType() == isString; }
378 virtual void print(raw_ostream &O) const;
382 //===--------------------------------------------------------------------===//
383 /// DIEEntry - A pointer to another debug information entry. An instance of
384 /// this class can also be used as a proxy for a debug information entry not
385 /// yet defined (ie. types.)
386 class DIEEntry : public DIEValue {
389 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
390 assert(E && "Cannot construct a DIEEntry with a null DIE");
393 DIE *getEntry() const { return Entry; }
395 /// EmitValue - Emit debug information entry offset.
397 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
399 /// SizeOf - Determine size of debug information entry in bytes.
401 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
402 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
406 /// Returns size of a ref_addr entry.
407 static unsigned getRefAddrSize(AsmPrinter *AP);
409 // Implement isa/cast/dyncast.
410 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
413 virtual void print(raw_ostream &O) const;
417 //===--------------------------------------------------------------------===//
418 /// \brief A signature reference to a type unit.
419 class DIETypeSignature : public DIEValue {
420 const DwarfTypeUnit &Unit;
422 explicit DIETypeSignature(const DwarfTypeUnit &Unit)
423 : DIEValue(isTypeSignature), Unit(Unit) {}
425 /// \brief Emit type unit signature.
426 virtual void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const;
428 /// Returns size of a ref_sig8 entry.
429 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
430 assert(Form == dwarf::DW_FORM_ref_sig8);
434 // \brief Implement isa/cast/dyncast.
435 static bool classof(const DIEValue *E) {
436 return E->getType() == isTypeSignature;
439 virtual void print(raw_ostream &O) const;
444 //===--------------------------------------------------------------------===//
445 /// DIELoc - Represents an expression location.
447 class DIELoc : public DIEValue, public DIE {
448 mutable unsigned Size; // Size in bytes excluding size header.
450 DIELoc() : DIEValue(isLoc), DIE(0), Size(0) {}
452 /// ComputeSize - Calculate the size of the location expression.
454 unsigned ComputeSize(AsmPrinter *AP) const;
456 /// BestForm - Choose the best form for data.
458 dwarf::Form BestForm(unsigned DwarfVersion) const {
459 if (DwarfVersion > 3) return dwarf::DW_FORM_exprloc;
460 // Pre-DWARF4 location expressions were blocks and not exprloc.
461 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
462 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
463 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
464 return dwarf::DW_FORM_block;
467 /// EmitValue - Emit location data.
469 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
471 /// SizeOf - Determine size of location data in bytes.
473 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
475 // Implement isa/cast/dyncast.
476 static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
479 virtual void print(raw_ostream &O) const;
483 //===--------------------------------------------------------------------===//
484 /// DIEBlock - Represents a block of values.
486 class DIEBlock : public DIEValue, public DIE {
487 mutable unsigned Size; // Size in bytes excluding size header.
489 DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
491 /// ComputeSize - Calculate the size of the location expression.
493 unsigned ComputeSize(AsmPrinter *AP) const;
495 /// BestForm - Choose the best form for data.
497 dwarf::Form BestForm() const {
498 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
499 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
500 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
501 return dwarf::DW_FORM_block;
504 /// EmitValue - Emit location data.
506 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
508 /// SizeOf - Determine size of location data in bytes.
510 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
512 // Implement isa/cast/dyncast.
513 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
516 virtual void print(raw_ostream &O) const;
519 } // end llvm namespace