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/Support/Compiler.h"
20 #include "llvm/Support/Dwarf.h"
21 #include "llvm/MC/MCExpr.h"
27 class MCSymbolRefExpr;
30 //===--------------------------------------------------------------------===//
31 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
32 /// Dwarf abbreviation.
34 /// Attribute - Dwarf attribute code.
36 dwarf::Attribute Attribute;
38 /// Form - Dwarf form code.
42 DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
45 dwarf::Attribute getAttribute() const { return Attribute; }
46 dwarf::Form getForm() const { return Form; }
48 /// Profile - Used to gather unique data for the abbreviation folding set.
50 void Profile(FoldingSetNodeID &ID) const;
53 //===--------------------------------------------------------------------===//
54 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
55 /// information object.
56 class DIEAbbrev : public FoldingSetNode {
57 /// Tag - Dwarf tag code.
61 /// ChildrenFlag - Dwarf children flag.
63 uint16_t ChildrenFlag;
65 /// Unique number for node.
69 /// Data - Raw data bytes for abbreviation.
71 SmallVector<DIEAbbrevData, 12> Data;
74 DIEAbbrev(dwarf::Tag T, uint16_t C) : Tag(T), ChildrenFlag(C), Data() {}
77 dwarf::Tag getTag() const { return Tag; }
78 unsigned getNumber() const { return Number; }
79 uint16_t getChildrenFlag() const { return ChildrenFlag; }
80 const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
81 void setChildrenFlag(uint16_t CF) { ChildrenFlag = CF; }
82 void setNumber(unsigned N) { Number = N; }
84 /// AddAttribute - Adds another set of attribute information to the
86 void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
87 Data.push_back(DIEAbbrevData(Attribute, Form));
90 /// Profile - Used to gather unique data for the abbreviation folding set.
92 void Profile(FoldingSetNodeID &ID) const;
94 /// Emit - Print the abbreviation using the specified asm printer.
96 void Emit(AsmPrinter *AP) const;
99 void print(raw_ostream &O);
104 //===--------------------------------------------------------------------===//
105 /// DIE - A structured debug information entry. Has an abbreviation which
106 /// describes its organization.
111 /// Offset - Offset in debug info section.
115 /// Size - Size of instance + children.
119 /// Abbrev - Buffer for constructing abbreviation.
125 std::vector<DIE *> Children;
129 /// Attribute values.
131 SmallVector<DIEValue*, 12> Values;
134 explicit DIE(unsigned Tag)
135 : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
140 DIEAbbrev &getAbbrev() { return Abbrev; }
141 const DIEAbbrev &getAbbrev() const { return Abbrev; }
142 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
143 dwarf::Tag getTag() const { return Abbrev.getTag(); }
144 unsigned getOffset() const { return Offset; }
145 unsigned getSize() const { return Size; }
146 const std::vector<DIE *> &getChildren() const { return Children; }
147 const SmallVectorImpl<DIEValue*> &getValues() const { return Values; }
148 DIE *getParent() const { return Parent; }
149 /// Climb up the parent chain to get the compile or type unit DIE this DIE
151 const DIE *getUnit() const;
152 /// Similar to getUnit, returns null when DIE is not added to an
154 const DIE *getUnitOrNull() const;
155 void setOffset(unsigned O) { Offset = O; }
156 void setSize(unsigned S) { Size = S; }
158 /// addValue - Add a value and attributes to a DIE.
160 void addValue(dwarf::Attribute Attribute, dwarf::Form Form,
162 Abbrev.AddAttribute(Attribute, Form);
163 Values.push_back(Value);
166 /// addChild - Add a child to the DIE.
168 void addChild(DIE *Child) {
169 assert(!Child->getParent());
170 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
171 Children.push_back(Child);
172 Child->Parent = this;
175 /// findAttribute - Find a value in the DIE with the attribute given,
176 /// returns NULL if no such attribute exists.
177 DIEValue *findAttribute(uint16_t Attribute) const;
180 void print(raw_ostream &O, unsigned IndentCount = 0) const;
185 //===--------------------------------------------------------------------===//
186 /// DIEValue - A debug information entry value.
189 virtual void anchor();
201 /// Type - Type of data stored in the value.
205 explicit DIEValue(unsigned T) : Type(T) {}
206 virtual ~DIEValue() {}
209 unsigned getType() const { return Type; }
211 /// EmitValue - Emit value via the Dwarf writer.
213 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
215 /// SizeOf - Return the size of a value in bytes.
217 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
220 virtual void print(raw_ostream &O) const = 0;
225 //===--------------------------------------------------------------------===//
226 /// DIEInteger - An integer value DIE.
228 class DIEInteger : public DIEValue {
231 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
233 /// BestForm - Choose the best form for integer.
235 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
237 const int64_t SignedInt = Int;
238 if ((char)Int == SignedInt) return dwarf::DW_FORM_data1;
239 if ((short)Int == SignedInt) return dwarf::DW_FORM_data2;
240 if ((int)Int == SignedInt) return dwarf::DW_FORM_data4;
242 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
243 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
244 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
246 return dwarf::DW_FORM_data8;
249 /// EmitValue - Emit integer of appropriate size.
251 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
253 uint64_t getValue() const { return Integer; }
255 /// SizeOf - Determine size of integer value in bytes.
257 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
259 // Implement isa/cast/dyncast.
260 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
263 virtual void print(raw_ostream &O) const;
267 //===--------------------------------------------------------------------===//
268 /// DIEExpr - An expression DIE.
270 class DIEExpr : public DIEValue {
273 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
275 /// EmitValue - Emit expression value.
277 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
279 /// getValue - Get MCExpr.
281 const MCExpr *getValue() const { return Expr; }
283 /// SizeOf - Determine size of expression value in bytes.
285 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
287 // Implement isa/cast/dyncast.
288 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
291 virtual void print(raw_ostream &O) const;
295 //===--------------------------------------------------------------------===//
296 /// DIELabel - A label DIE.
298 class DIELabel : public DIEValue {
299 const MCSymbol *Label;
301 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
303 /// EmitValue - Emit label value.
305 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
307 /// getValue - Get MCSymbol.
309 const MCSymbol *getValue() const { return Label; }
311 /// SizeOf - Determine size of label value in bytes.
313 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
315 // Implement isa/cast/dyncast.
316 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
319 virtual void print(raw_ostream &O) const;
323 //===--------------------------------------------------------------------===//
324 /// DIEDelta - A simple label difference DIE.
326 class DIEDelta : public DIEValue {
327 const MCSymbol *LabelHi;
328 const MCSymbol *LabelLo;
330 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
331 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
333 /// EmitValue - Emit delta value.
335 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
337 /// SizeOf - Determine size of delta value in bytes.
339 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
341 // Implement isa/cast/dyncast.
342 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
345 virtual void print(raw_ostream &O) const;
349 //===--------------------------------------------------------------------===//
350 /// DIEString - A container for string values.
352 class DIEString : public DIEValue {
353 const DIEValue *Access;
357 DIEString(const DIEValue *Acc, const StringRef S)
358 : DIEValue(isString), Access(Acc), Str(S) {}
360 /// getString - Grab the string out of the object.
361 StringRef getString() const { return Str; }
363 /// EmitValue - Emit delta value.
365 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
367 /// SizeOf - Determine size of delta value in bytes.
369 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
371 // Implement isa/cast/dyncast.
372 static bool classof(const DIEValue *D) { return D->getType() == isString; }
375 virtual void print(raw_ostream &O) const;
379 //===--------------------------------------------------------------------===//
380 /// DIEEntry - A pointer to another debug information entry. An instance of
381 /// this class can also be used as a proxy for a debug information entry not
382 /// yet defined (ie. types.)
383 class DIEEntry : public DIEValue {
386 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
387 assert(E && "Cannot construct a DIEEntry with a null DIE");
390 DIE *getEntry() const { return Entry; }
392 /// EmitValue - Emit debug information entry offset.
394 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
396 /// SizeOf - Determine size of debug information entry in bytes.
398 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
399 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
403 /// Returns size of a ref_addr entry.
404 static unsigned getRefAddrSize(AsmPrinter *AP);
406 // Implement isa/cast/dyncast.
407 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
410 virtual void print(raw_ostream &O) const;
414 //===--------------------------------------------------------------------===//
415 /// DIEBlock - A block of values. Primarily used for location expressions.
417 class DIEBlock : public DIEValue, public DIE {
418 unsigned Size; // Size in bytes excluding size header.
420 DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
422 /// ComputeSize - calculate the size of the block.
424 unsigned ComputeSize(AsmPrinter *AP);
426 /// BestForm - Choose the best form for data.
428 dwarf::Form BestForm() const {
429 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
430 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
431 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
432 return dwarf::DW_FORM_block;
435 /// EmitValue - Emit block data.
437 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
439 /// SizeOf - Determine size of block data in bytes.
441 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
443 // Implement isa/cast/dyncast.
444 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
447 virtual void print(raw_ostream &O) const;
451 } // end llvm namespace