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();
203 /// Type - Type of data stored in the value.
207 explicit DIEValue(unsigned T) : Type(T) {}
208 virtual ~DIEValue() {}
211 unsigned getType() const { return Type; }
213 /// EmitValue - Emit value via the Dwarf writer.
215 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
217 /// SizeOf - Return the size of a value in bytes.
219 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
222 virtual void print(raw_ostream &O) const = 0;
227 //===--------------------------------------------------------------------===//
228 /// DIEInteger - An integer value DIE.
230 class DIEInteger : public DIEValue {
233 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
235 /// BestForm - Choose the best form for integer.
237 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
239 const int64_t SignedInt = Int;
240 if ((char)Int == SignedInt) return dwarf::DW_FORM_data1;
241 if ((short)Int == SignedInt) return dwarf::DW_FORM_data2;
242 if ((int)Int == SignedInt) return dwarf::DW_FORM_data4;
244 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
245 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
246 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
248 return dwarf::DW_FORM_data8;
251 /// EmitValue - Emit integer of appropriate size.
253 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
255 uint64_t getValue() const { return Integer; }
257 /// SizeOf - Determine size of integer value in bytes.
259 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
261 // Implement isa/cast/dyncast.
262 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
265 virtual void print(raw_ostream &O) const;
269 //===--------------------------------------------------------------------===//
270 /// DIEExpr - An expression DIE.
272 class DIEExpr : public DIEValue {
275 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
277 /// EmitValue - Emit expression value.
279 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
281 /// getValue - Get MCExpr.
283 const MCExpr *getValue() const { return Expr; }
285 /// SizeOf - Determine size of expression value in bytes.
287 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
289 // Implement isa/cast/dyncast.
290 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
293 virtual void print(raw_ostream &O) const;
297 //===--------------------------------------------------------------------===//
298 /// DIELabel - A label DIE.
300 class DIELabel : public DIEValue {
301 const MCSymbol *Label;
303 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
305 /// EmitValue - Emit label value.
307 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
309 /// getValue - Get MCSymbol.
311 const MCSymbol *getValue() const { return Label; }
313 /// SizeOf - Determine size of label value in bytes.
315 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
317 // Implement isa/cast/dyncast.
318 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
321 virtual void print(raw_ostream &O) const;
325 //===--------------------------------------------------------------------===//
326 /// DIEDelta - A simple label difference DIE.
328 class DIEDelta : public DIEValue {
329 const MCSymbol *LabelHi;
330 const MCSymbol *LabelLo;
332 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
333 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
335 /// EmitValue - Emit delta value.
337 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
339 /// SizeOf - Determine size of delta value in bytes.
341 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
343 // Implement isa/cast/dyncast.
344 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
347 virtual void print(raw_ostream &O) const;
351 //===--------------------------------------------------------------------===//
352 /// DIEString - A container for string values.
354 class DIEString : public DIEValue {
355 const DIEValue *Access;
359 DIEString(const DIEValue *Acc, const StringRef S)
360 : DIEValue(isString), Access(Acc), Str(S) {}
362 /// getString - Grab the string out of the object.
363 StringRef getString() const { return Str; }
365 /// EmitValue - Emit delta value.
367 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
369 /// SizeOf - Determine size of delta value in bytes.
371 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
373 // Implement isa/cast/dyncast.
374 static bool classof(const DIEValue *D) { return D->getType() == isString; }
377 virtual void print(raw_ostream &O) const;
381 //===--------------------------------------------------------------------===//
382 /// DIEEntry - A pointer to another debug information entry. An instance of
383 /// this class can also be used as a proxy for a debug information entry not
384 /// yet defined (ie. types.)
385 class DIEEntry : public DIEValue {
388 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
389 assert(E && "Cannot construct a DIEEntry with a null DIE");
392 DIE *getEntry() const { return Entry; }
394 /// EmitValue - Emit debug information entry offset.
396 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
398 /// SizeOf - Determine size of debug information entry in bytes.
400 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
401 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
405 /// Returns size of a ref_addr entry.
406 static unsigned getRefAddrSize(AsmPrinter *AP);
408 // Implement isa/cast/dyncast.
409 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
412 virtual void print(raw_ostream &O) const;
416 //===--------------------------------------------------------------------===//
417 /// \brief A signature reference to a type unit.
418 class DIETypeSignature : public DIEValue {
419 const DwarfTypeUnit &Unit;
421 explicit DIETypeSignature(const DwarfTypeUnit &Unit)
422 : DIEValue(isTypeSignature), Unit(Unit) {}
424 /// \brief Emit type unit signature.
425 virtual void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const;
427 /// Returns size of a ref_sig8 entry.
428 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
429 assert(Form == dwarf::DW_FORM_ref_sig8);
433 // \brief Implement isa/cast/dyncast.
434 static bool classof(const DIEValue *E) {
435 return E->getType() == isTypeSignature;
438 virtual void print(raw_ostream &O) const;
443 //===--------------------------------------------------------------------===//
444 /// DIEBlock - A block of values. Primarily used for location expressions.
446 class DIEBlock : public DIEValue, public DIE {
447 unsigned Size; // Size in bytes excluding size header.
449 DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
451 /// ComputeSize - calculate the size of the block.
453 unsigned ComputeSize(AsmPrinter *AP);
455 /// BestForm - Choose the best form for data.
457 dwarf::Form BestForm() const {
458 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
459 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
460 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
461 return dwarf::DW_FORM_block;
464 /// EmitValue - Emit block data.
466 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
468 /// SizeOf - Determine size of block data in bytes.
470 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
472 // Implement isa/cast/dyncast.
473 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
476 virtual void print(raw_ostream &O) const;
480 } // end llvm namespace