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 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
142 dwarf::Tag getTag() const { return Abbrev.getTag(); }
143 unsigned getOffset() const { return Offset; }
144 unsigned getSize() const { return Size; }
145 const std::vector<DIE *> &getChildren() const { return Children; }
146 const SmallVectorImpl<DIEValue*> &getValues() const { return Values; }
147 DIE *getParent() const { return Parent; }
148 void setOffset(unsigned O) { Offset = O; }
149 void setSize(unsigned S) { Size = S; }
151 /// addValue - Add a value and attributes to a DIE.
153 void addValue(dwarf::Attribute Attribute, dwarf::Form Form,
155 Abbrev.AddAttribute(Attribute, Form);
156 Values.push_back(Value);
159 /// addChild - Add a child to the DIE.
161 void addChild(DIE *Child) {
162 assert(!Child->getParent());
163 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
164 Children.push_back(Child);
165 Child->Parent = this;
168 /// findAttribute - Find a value in the DIE with the attribute given, returns NULL
169 /// if no such attribute exists.
170 DIEValue *findAttribute(uint16_t Attribute);
173 void print(raw_ostream &O, unsigned IndentCount = 0) const;
178 //===--------------------------------------------------------------------===//
179 /// DIEValue - A debug information entry value.
182 virtual void anchor();
194 /// Type - Type of data stored in the value.
198 explicit DIEValue(unsigned T) : Type(T) {}
199 virtual ~DIEValue() {}
202 unsigned getType() const { return Type; }
204 /// EmitValue - Emit value via the Dwarf writer.
206 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
208 /// SizeOf - Return the size of a value in bytes.
210 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
213 virtual void print(raw_ostream &O) const = 0;
218 //===--------------------------------------------------------------------===//
219 /// DIEInteger - An integer value DIE.
221 class DIEInteger : public DIEValue {
224 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
226 /// BestForm - Choose the best form for integer.
228 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
230 const int64_t SignedInt = Int;
231 if ((char)Int == SignedInt) return dwarf::DW_FORM_data1;
232 if ((short)Int == SignedInt) return dwarf::DW_FORM_data2;
233 if ((int)Int == SignedInt) return dwarf::DW_FORM_data4;
235 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
236 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
237 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
239 return dwarf::DW_FORM_data8;
242 /// EmitValue - Emit integer of appropriate size.
244 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
246 uint64_t getValue() const { return Integer; }
248 /// SizeOf - Determine size of integer value in bytes.
250 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
252 // Implement isa/cast/dyncast.
253 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
256 virtual void print(raw_ostream &O) const;
260 //===--------------------------------------------------------------------===//
261 /// DIEExpr - An expression DIE.
263 class DIEExpr : public DIEValue {
266 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
268 /// EmitValue - Emit expression value.
270 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
272 /// getValue - Get MCExpr.
274 const MCExpr *getValue() const { return Expr; }
276 /// SizeOf - Determine size of expression value in bytes.
278 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
280 // Implement isa/cast/dyncast.
281 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
284 virtual void print(raw_ostream &O) const;
288 //===--------------------------------------------------------------------===//
289 /// DIELabel - A label DIE.
291 class DIELabel : public DIEValue {
292 const MCSymbol *Label;
294 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
296 /// EmitValue - Emit label value.
298 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
300 /// getValue - Get MCSymbol.
302 const MCSymbol *getValue() const { return Label; }
304 /// SizeOf - Determine size of label value in bytes.
306 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
308 // Implement isa/cast/dyncast.
309 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
312 virtual void print(raw_ostream &O) const;
316 //===--------------------------------------------------------------------===//
317 /// DIEDelta - A simple label difference DIE.
319 class DIEDelta : public DIEValue {
320 const MCSymbol *LabelHi;
321 const MCSymbol *LabelLo;
323 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
324 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
326 /// EmitValue - Emit delta value.
328 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
330 /// SizeOf - Determine size of delta value in bytes.
332 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
334 // Implement isa/cast/dyncast.
335 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
338 virtual void print(raw_ostream &O) const;
342 //===--------------------------------------------------------------------===//
343 /// DIEString - A container for string values.
345 class DIEString : public DIEValue {
346 const DIEValue *Access;
350 DIEString(const DIEValue *Acc, const StringRef S)
351 : DIEValue(isString), Access(Acc), Str(S) {}
353 /// getString - Grab the string out of the object.
354 StringRef getString() const { return Str; }
356 /// EmitValue - Emit delta value.
358 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
360 /// SizeOf - Determine size of delta value in bytes.
362 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
364 // Implement isa/cast/dyncast.
365 static bool classof(const DIEValue *D) { return D->getType() == isString; }
368 virtual void print(raw_ostream &O) const;
372 //===--------------------------------------------------------------------===//
373 /// DIEEntry - A pointer to another debug information entry. An instance of
374 /// this class can also be used as a proxy for a debug information entry not
375 /// yet defined (ie. types.)
376 class DIEEntry : public DIEValue {
379 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
380 assert(E && "Cannot construct a DIEEntry with a null DIE");
383 DIE *getEntry() const { return Entry; }
385 /// EmitValue - Emit debug information entry offset.
387 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
389 /// SizeOf - Determine size of debug information entry in bytes.
391 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
392 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
396 /// Returns size of a ref_addr entry.
397 static unsigned getRefAddrSize(AsmPrinter *AP);
399 // Implement isa/cast/dyncast.
400 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
403 virtual void print(raw_ostream &O) const;
407 //===--------------------------------------------------------------------===//
408 /// DIEBlock - A block of values. Primarily used for location expressions.
410 class DIEBlock : public DIEValue, public DIE {
411 unsigned Size; // Size in bytes excluding size header.
413 DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
415 /// ComputeSize - calculate the size of the block.
417 unsigned ComputeSize(AsmPrinter *AP);
419 /// BestForm - Choose the best form for data.
421 dwarf::Form BestForm() const {
422 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
423 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
424 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
425 return dwarf::DW_FORM_block;
428 /// EmitValue - Emit block data.
430 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
432 /// SizeOf - Determine size of block data in bytes.
434 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
436 // Implement isa/cast/dyncast.
437 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
440 virtual void print(raw_ostream &O) const;
444 } // end llvm namespace