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.
38 /// Form - Dwarf form code.
42 DIEAbbrevData(uint16_t A, uint16_t F) : Attribute(A), Form(F) {}
45 uint16_t getAttribute() const { return Attribute; }
46 uint16_t 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(uint16_t T, uint16_t C) : Tag(T), ChildrenFlag(C), Data() {}
77 uint16_t 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 setTag(uint16_t T) { Tag = T; }
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(uint16_t Attribute, uint16_t 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 // Private data for print()
136 mutable unsigned IndentCount;
139 explicit DIE(unsigned Tag)
140 : Offset(0), Size(0), Abbrev(Tag, dwarf::DW_CHILDREN_no), Parent(0) {}
144 DIEAbbrev &getAbbrev() { return Abbrev; }
145 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
146 unsigned getTag() const { return Abbrev.getTag(); }
147 unsigned getOffset() const { return Offset; }
148 unsigned getSize() const { return Size; }
149 const std::vector<DIE *> &getChildren() const { return Children; }
150 const SmallVectorImpl<DIEValue*> &getValues() const { return Values; }
151 DIE *getParent() const { return Parent; }
152 /// Climb up the parent chain to get the compile unit DIE this DIE belongs
154 DIE *getCompileUnit();
155 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
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(unsigned Attribute, unsigned Form, DIEValue *Value) {
162 Abbrev.AddAttribute(Attribute, Form);
163 Values.push_back(Value);
166 /// addChild - Add a child to the DIE.
168 void addChild(DIE *Child) {
169 if (Child->getParent()) {
170 assert (Child->getParent() == this && "Unexpected DIE Parent!");
173 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
174 Children.push_back(Child);
175 Child->Parent = this;
179 void print(raw_ostream &O, unsigned IndentCount = 0) const;
184 //===--------------------------------------------------------------------===//
185 /// DIEValue - A debug information entry value.
188 virtual void anchor();
200 /// Type - Type of data stored in the value.
204 explicit DIEValue(unsigned T) : Type(T) {}
205 virtual ~DIEValue() {}
208 unsigned getType() const { return Type; }
210 /// EmitValue - Emit value via the Dwarf writer.
212 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const = 0;
214 /// SizeOf - Return the size of a value in bytes.
216 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const = 0;
219 virtual void print(raw_ostream &O) const = 0;
224 //===--------------------------------------------------------------------===//
225 /// DIEInteger - An integer value DIE.
227 class DIEInteger : public DIEValue {
230 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
232 /// BestForm - Choose the best form for integer.
234 static unsigned BestForm(bool IsSigned, uint64_t Int) {
236 const int64_t SignedInt = Int;
237 if ((char)Int == SignedInt) return dwarf::DW_FORM_data1;
238 if ((short)Int == SignedInt) return dwarf::DW_FORM_data2;
239 if ((int)Int == SignedInt) return dwarf::DW_FORM_data4;
241 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
242 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
243 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
245 return dwarf::DW_FORM_data8;
248 /// EmitValue - Emit integer of appropriate size.
250 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
252 uint64_t getValue() const { return Integer; }
254 /// SizeOf - Determine size of integer value in bytes.
256 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
258 // Implement isa/cast/dyncast.
259 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
262 virtual void print(raw_ostream &O) const;
266 //===--------------------------------------------------------------------===//
267 /// DIEExpr - An expression DIE.
269 class DIEExpr : public DIEValue {
272 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
274 /// EmitValue - Emit expression value.
276 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
278 /// getValue - Get MCExpr.
280 const MCExpr *getValue() const { return Expr; }
282 /// SizeOf - Determine size of expression value in bytes.
284 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
286 // Implement isa/cast/dyncast.
287 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
290 virtual void print(raw_ostream &O) const;
294 //===--------------------------------------------------------------------===//
295 /// DIELabel - A label DIE.
297 class DIELabel : public DIEValue {
298 const MCSymbol *Label;
300 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
302 /// EmitValue - Emit label value.
304 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
306 /// getValue - Get MCSymbol.
308 const MCSymbol *getValue() const { return Label; }
310 /// SizeOf - Determine size of label value in bytes.
312 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
314 // Implement isa/cast/dyncast.
315 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
318 virtual void print(raw_ostream &O) const;
322 //===--------------------------------------------------------------------===//
323 /// DIEDelta - A simple label difference DIE.
325 class DIEDelta : public DIEValue {
326 const MCSymbol *LabelHi;
327 const MCSymbol *LabelLo;
329 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
330 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
332 /// EmitValue - Emit delta value.
334 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
336 /// SizeOf - Determine size of delta value in bytes.
338 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
340 // Implement isa/cast/dyncast.
341 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
344 virtual void print(raw_ostream &O) const;
348 //===--------------------------------------------------------------------===//
349 /// DIEEntry - A pointer to another debug information entry. An instance of
350 /// this class can also be used as a proxy for a debug information entry not
351 /// yet defined (ie. types.)
352 class DIEEntry : public DIEValue {
355 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
356 assert(E && "Cannot construct a DIEEntry with a null DIE");
359 DIE *getEntry() const { return Entry; }
361 /// EmitValue - Emit debug information entry offset.
363 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
365 /// SizeOf - Determine size of debug information entry in bytes.
367 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const {
368 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP) :
372 /// Returns size of a ref_addr entry.
373 static unsigned getRefAddrSize(AsmPrinter *AP);
375 // Implement isa/cast/dyncast.
376 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
379 virtual void print(raw_ostream &O) const;
383 //===--------------------------------------------------------------------===//
384 /// DIEBlock - A block of values. Primarily used for location expressions.
386 class DIEBlock : public DIEValue, public DIE {
387 unsigned Size; // Size in bytes excluding size header.
390 : DIEValue(isBlock), DIE(0), Size(0) {}
391 virtual ~DIEBlock() {}
393 /// ComputeSize - calculate the size of the block.
395 unsigned ComputeSize(AsmPrinter *AP);
397 /// BestForm - Choose the best form for data.
399 unsigned BestForm() const {
400 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
401 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
402 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
403 return dwarf::DW_FORM_block;
406 /// EmitValue - Emit block data.
408 virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
410 /// SizeOf - Determine size of block data in bytes.
412 virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
414 // Implement isa/cast/dyncast.
415 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
418 virtual void print(raw_ostream &O) const;
422 } // end llvm namespace