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(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();
206 /// Type - Type of data stored in the value.
211 explicit DIEValue(unsigned T) : Type(T) {}
212 virtual ~DIEValue() {}
215 unsigned getType() const { return Type; }
217 /// EmitValue - Emit value via the Dwarf writer.
219 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
221 /// SizeOf - Return the size of a value in bytes.
223 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
226 virtual void print(raw_ostream &O) const = 0;
231 //===--------------------------------------------------------------------===//
232 /// DIEInteger - An integer value DIE.
234 class DIEInteger : public DIEValue {
238 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
240 /// BestForm - Choose the best form for integer.
242 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
244 const int64_t SignedInt = Int;
245 if ((char)Int == SignedInt)
246 return dwarf::DW_FORM_data1;
247 if ((short)Int == SignedInt)
248 return dwarf::DW_FORM_data2;
249 if ((int)Int == SignedInt)
250 return dwarf::DW_FORM_data4;
252 if ((unsigned char)Int == Int)
253 return dwarf::DW_FORM_data1;
254 if ((unsigned short)Int == Int)
255 return dwarf::DW_FORM_data2;
256 if ((unsigned int)Int == Int)
257 return dwarf::DW_FORM_data4;
259 return dwarf::DW_FORM_data8;
262 /// EmitValue - Emit integer of appropriate size.
264 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
266 uint64_t getValue() const { return Integer; }
268 /// SizeOf - Determine size of integer value in bytes.
270 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
272 // Implement isa/cast/dyncast.
273 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
276 virtual void print(raw_ostream &O) const;
280 //===--------------------------------------------------------------------===//
281 /// DIEExpr - An expression DIE.
283 class DIEExpr : public DIEValue {
287 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
289 /// EmitValue - Emit expression value.
291 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
293 /// getValue - Get MCExpr.
295 const MCExpr *getValue() const { return Expr; }
297 /// SizeOf - Determine size of expression value in bytes.
299 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
301 // Implement isa/cast/dyncast.
302 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
305 virtual void print(raw_ostream &O) const;
309 //===--------------------------------------------------------------------===//
310 /// DIELabel - A label DIE.
312 class DIELabel : public DIEValue {
313 const MCSymbol *Label;
316 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
318 /// EmitValue - Emit label value.
320 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
322 /// getValue - Get MCSymbol.
324 const MCSymbol *getValue() const { return Label; }
326 /// SizeOf - Determine size of label value in bytes.
328 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
330 // Implement isa/cast/dyncast.
331 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
334 virtual void print(raw_ostream &O) const;
338 //===--------------------------------------------------------------------===//
339 /// DIEDelta - A simple label difference DIE.
341 class DIEDelta : public DIEValue {
342 const MCSymbol *LabelHi;
343 const MCSymbol *LabelLo;
346 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
347 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
349 /// EmitValue - Emit delta value.
351 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
353 /// SizeOf - Determine size of delta value in bytes.
355 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
357 // Implement isa/cast/dyncast.
358 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
361 virtual void print(raw_ostream &O) const;
365 //===--------------------------------------------------------------------===//
366 /// DIEString - A container for string values.
368 class DIEString : public DIEValue {
369 const DIEValue *Access;
373 DIEString(const DIEValue *Acc, const StringRef S)
374 : DIEValue(isString), Access(Acc), Str(S) {}
376 /// getString - Grab the string out of the object.
377 StringRef getString() const { return Str; }
379 /// EmitValue - Emit delta value.
381 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
383 /// SizeOf - Determine size of delta value in bytes.
385 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
387 // Implement isa/cast/dyncast.
388 static bool classof(const DIEValue *D) { return D->getType() == isString; }
391 virtual void print(raw_ostream &O) const;
395 //===--------------------------------------------------------------------===//
396 /// DIEEntry - A pointer to another debug information entry. An instance of
397 /// this class can also be used as a proxy for a debug information entry not
398 /// yet defined (ie. types.)
399 class DIEEntry : public DIEValue {
403 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
404 assert(E && "Cannot construct a DIEEntry with a null DIE");
407 DIE *getEntry() const { return Entry; }
409 /// EmitValue - Emit debug information entry offset.
411 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
413 /// SizeOf - Determine size of debug information entry in bytes.
415 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
416 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
420 /// Returns size of a ref_addr entry.
421 static unsigned getRefAddrSize(AsmPrinter *AP);
423 // Implement isa/cast/dyncast.
424 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
427 virtual void print(raw_ostream &O) const;
431 //===--------------------------------------------------------------------===//
432 /// \brief A signature reference to a type unit.
433 class DIETypeSignature : public DIEValue {
434 const DwarfTypeUnit &Unit;
437 explicit DIETypeSignature(const DwarfTypeUnit &Unit)
438 : DIEValue(isTypeSignature), Unit(Unit) {}
440 /// \brief Emit type unit signature.
441 virtual void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const;
443 /// Returns size of a ref_sig8 entry.
444 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
445 assert(Form == dwarf::DW_FORM_ref_sig8);
449 // \brief Implement isa/cast/dyncast.
450 static bool classof(const DIEValue *E) {
451 return E->getType() == isTypeSignature;
454 virtual void print(raw_ostream &O) const;
459 //===--------------------------------------------------------------------===//
460 /// DIELoc - Represents an expression location.
462 class DIELoc : public DIEValue, public DIE {
463 mutable unsigned Size; // Size in bytes excluding size header.
465 DIELoc() : DIEValue(isLoc), DIE(0), Size(0) {}
467 /// ComputeSize - Calculate the size of the location expression.
469 unsigned ComputeSize(AsmPrinter *AP) const;
471 /// BestForm - Choose the best form for data.
473 dwarf::Form BestForm(unsigned DwarfVersion) const {
474 if (DwarfVersion > 3)
475 return dwarf::DW_FORM_exprloc;
476 // Pre-DWARF4 location expressions were blocks and not exprloc.
477 if ((unsigned char)Size == Size)
478 return dwarf::DW_FORM_block1;
479 if ((unsigned short)Size == Size)
480 return dwarf::DW_FORM_block2;
481 if ((unsigned int)Size == Size)
482 return dwarf::DW_FORM_block4;
483 return dwarf::DW_FORM_block;
486 /// EmitValue - Emit location data.
488 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
490 /// SizeOf - Determine size of location data in bytes.
492 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
494 // Implement isa/cast/dyncast.
495 static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
498 virtual void print(raw_ostream &O) const;
502 //===--------------------------------------------------------------------===//
503 /// DIEBlock - Represents a block of values.
505 class DIEBlock : public DIEValue, public DIE {
506 mutable unsigned Size; // Size in bytes excluding size header.
508 DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
510 /// ComputeSize - Calculate the size of the location expression.
512 unsigned ComputeSize(AsmPrinter *AP) const;
514 /// BestForm - Choose the best form for data.
516 dwarf::Form BestForm() const {
517 if ((unsigned char)Size == Size)
518 return dwarf::DW_FORM_block1;
519 if ((unsigned short)Size == Size)
520 return dwarf::DW_FORM_block2;
521 if ((unsigned int)Size == Size)
522 return dwarf::DW_FORM_block4;
523 return dwarf::DW_FORM_block;
526 /// EmitValue - Emit location data.
528 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
530 /// SizeOf - Determine size of location data in bytes.
532 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
534 // Implement isa/cast/dyncast.
535 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
538 virtual void print(raw_ostream &O) const;
541 } // end llvm namespace