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 unit DIE this DIE belongs
151 const DIE *getCompileUnit() const;
152 void setOffset(unsigned O) { Offset = O; }
153 void setSize(unsigned S) { Size = S; }
155 /// addValue - Add a value and attributes to a DIE.
157 void addValue(dwarf::Attribute Attribute, dwarf::Form Form,
159 Abbrev.AddAttribute(Attribute, Form);
160 Values.push_back(Value);
163 /// addChild - Add a child to the DIE.
165 void addChild(DIE *Child) {
166 assert(!Child->getParent());
167 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
168 Children.push_back(Child);
169 Child->Parent = this;
172 /// findAttribute - Find a value in the DIE with the attribute given, returns NULL
173 /// if no such attribute exists.
174 DIEValue *findAttribute(uint16_t Attribute);
177 void print(raw_ostream &O, unsigned IndentCount = 0) const;
182 //===--------------------------------------------------------------------===//
183 /// DIEValue - A debug information entry value.
186 virtual void anchor();
198 /// Type - Type of data stored in the value.
202 explicit DIEValue(unsigned T) : Type(T) {}
203 virtual ~DIEValue() {}
206 unsigned getType() const { return Type; }
208 /// EmitValue - Emit value via the Dwarf writer.
210 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
212 /// SizeOf - Return the size of a value in bytes.
214 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
217 virtual void print(raw_ostream &O) const = 0;
222 //===--------------------------------------------------------------------===//
223 /// DIEInteger - An integer value DIE.
225 class DIEInteger : public DIEValue {
228 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
230 /// BestForm - Choose the best form for integer.
232 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
234 const int64_t SignedInt = Int;
235 if ((char)Int == SignedInt) return dwarf::DW_FORM_data1;
236 if ((short)Int == SignedInt) return dwarf::DW_FORM_data2;
237 if ((int)Int == SignedInt) return dwarf::DW_FORM_data4;
239 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
240 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
241 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
243 return dwarf::DW_FORM_data8;
246 /// EmitValue - Emit integer of appropriate size.
248 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
250 uint64_t getValue() const { return Integer; }
252 /// SizeOf - Determine size of integer value in bytes.
254 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
256 // Implement isa/cast/dyncast.
257 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
260 virtual void print(raw_ostream &O) const;
264 //===--------------------------------------------------------------------===//
265 /// DIEExpr - An expression DIE.
267 class DIEExpr : public DIEValue {
270 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
272 /// EmitValue - Emit expression value.
274 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
276 /// getValue - Get MCExpr.
278 const MCExpr *getValue() const { return Expr; }
280 /// SizeOf - Determine size of expression value in bytes.
282 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
284 // Implement isa/cast/dyncast.
285 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
288 virtual void print(raw_ostream &O) const;
292 //===--------------------------------------------------------------------===//
293 /// DIELabel - A label DIE.
295 class DIELabel : public DIEValue {
296 const MCSymbol *Label;
298 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
300 /// EmitValue - Emit label value.
302 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
304 /// getValue - Get MCSymbol.
306 const MCSymbol *getValue() const { return Label; }
308 /// SizeOf - Determine size of label value in bytes.
310 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
312 // Implement isa/cast/dyncast.
313 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
316 virtual void print(raw_ostream &O) const;
320 //===--------------------------------------------------------------------===//
321 /// DIEDelta - A simple label difference DIE.
323 class DIEDelta : public DIEValue {
324 const MCSymbol *LabelHi;
325 const MCSymbol *LabelLo;
327 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
328 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
330 /// EmitValue - Emit delta value.
332 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
334 /// SizeOf - Determine size of delta value in bytes.
336 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
338 // Implement isa/cast/dyncast.
339 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
342 virtual void print(raw_ostream &O) const;
346 //===--------------------------------------------------------------------===//
347 /// DIEString - A container for string values.
349 class DIEString : public DIEValue {
350 const DIEValue *Access;
354 DIEString(const DIEValue *Acc, const StringRef S)
355 : DIEValue(isString), Access(Acc), Str(S) {}
357 /// getString - Grab the string out of the object.
358 StringRef getString() const { return Str; }
360 /// EmitValue - Emit delta value.
362 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
364 /// SizeOf - Determine size of delta value in bytes.
366 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
368 // Implement isa/cast/dyncast.
369 static bool classof(const DIEValue *D) { return D->getType() == isString; }
372 virtual void print(raw_ostream &O) const;
376 //===--------------------------------------------------------------------===//
377 /// DIEEntry - A pointer to another debug information entry. An instance of
378 /// this class can also be used as a proxy for a debug information entry not
379 /// yet defined (ie. types.)
380 class DIEEntry : public DIEValue {
383 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
384 assert(E && "Cannot construct a DIEEntry with a null DIE");
387 DIE *getEntry() const { return Entry; }
389 /// EmitValue - Emit debug information entry offset.
391 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
393 /// SizeOf - Determine size of debug information entry in bytes.
395 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
396 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
400 /// Returns size of a ref_addr entry.
401 static unsigned getRefAddrSize(AsmPrinter *AP);
403 // Implement isa/cast/dyncast.
404 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
407 virtual void print(raw_ostream &O) const;
411 //===--------------------------------------------------------------------===//
412 /// DIEBlock - A block of values. Primarily used for location expressions.
414 class DIEBlock : public DIEValue, public DIE {
415 unsigned Size; // Size in bytes excluding size header.
417 DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
419 /// ComputeSize - calculate the size of the block.
421 unsigned ComputeSize(AsmPrinter *AP);
423 /// BestForm - Choose the best form for data.
425 dwarf::Form BestForm() const {
426 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
427 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
428 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
429 return dwarf::DW_FORM_block;
432 /// EmitValue - Emit block data.
434 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
436 /// SizeOf - Determine size of block data in bytes.
438 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
440 // Implement isa/cast/dyncast.
441 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
444 virtual void print(raw_ostream &O) const;
448 } // end llvm namespace