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 void setOffset(unsigned O) { Offset = O; }
150 void setSize(unsigned S) { Size = S; }
152 /// addValue - Add a value and attributes to a DIE.
154 void addValue(dwarf::Attribute Attribute, dwarf::Form Form,
156 Abbrev.AddAttribute(Attribute, Form);
157 Values.push_back(Value);
160 /// addChild - Add a child to the DIE.
162 void addChild(DIE *Child) {
163 assert(!Child->getParent());
164 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
165 Children.push_back(Child);
166 Child->Parent = this;
169 /// findAttribute - Find a value in the DIE with the attribute given, returns NULL
170 /// if no such attribute exists.
171 DIEValue *findAttribute(uint16_t Attribute);
174 void print(raw_ostream &O, unsigned IndentCount = 0) const;
179 //===--------------------------------------------------------------------===//
180 /// DIEValue - A debug information entry value.
183 virtual void anchor();
195 /// Type - Type of data stored in the value.
199 explicit DIEValue(unsigned T) : Type(T) {}
200 virtual ~DIEValue() {}
203 unsigned getType() const { return Type; }
205 /// EmitValue - Emit value via the Dwarf writer.
207 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
209 /// SizeOf - Return the size of a value in bytes.
211 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
214 virtual void print(raw_ostream &O) const = 0;
219 //===--------------------------------------------------------------------===//
220 /// DIEInteger - An integer value DIE.
222 class DIEInteger : public DIEValue {
225 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
227 /// BestForm - Choose the best form for integer.
229 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
231 const int64_t SignedInt = Int;
232 if ((char)Int == SignedInt) return dwarf::DW_FORM_data1;
233 if ((short)Int == SignedInt) return dwarf::DW_FORM_data2;
234 if ((int)Int == SignedInt) return dwarf::DW_FORM_data4;
236 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
237 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
238 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
240 return dwarf::DW_FORM_data8;
243 /// EmitValue - Emit integer of appropriate size.
245 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
247 uint64_t getValue() const { return Integer; }
249 /// SizeOf - Determine size of integer value in bytes.
251 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
253 // Implement isa/cast/dyncast.
254 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
257 virtual void print(raw_ostream &O) const;
261 //===--------------------------------------------------------------------===//
262 /// DIEExpr - An expression DIE.
264 class DIEExpr : public DIEValue {
267 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
269 /// EmitValue - Emit expression value.
271 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
273 /// getValue - Get MCExpr.
275 const MCExpr *getValue() const { return Expr; }
277 /// SizeOf - Determine size of expression value in bytes.
279 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
281 // Implement isa/cast/dyncast.
282 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
285 virtual void print(raw_ostream &O) const;
289 //===--------------------------------------------------------------------===//
290 /// DIELabel - A label DIE.
292 class DIELabel : public DIEValue {
293 const MCSymbol *Label;
295 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
297 /// EmitValue - Emit label value.
299 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
301 /// getValue - Get MCSymbol.
303 const MCSymbol *getValue() const { return Label; }
305 /// SizeOf - Determine size of label value in bytes.
307 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
309 // Implement isa/cast/dyncast.
310 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
313 virtual void print(raw_ostream &O) const;
317 //===--------------------------------------------------------------------===//
318 /// DIEDelta - A simple label difference DIE.
320 class DIEDelta : public DIEValue {
321 const MCSymbol *LabelHi;
322 const MCSymbol *LabelLo;
324 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
325 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
327 /// EmitValue - Emit delta value.
329 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
331 /// SizeOf - Determine size of delta value in bytes.
333 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
335 // Implement isa/cast/dyncast.
336 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
339 virtual void print(raw_ostream &O) const;
343 //===--------------------------------------------------------------------===//
344 /// DIEString - A container for string values.
346 class DIEString : public DIEValue {
347 const DIEValue *Access;
351 DIEString(const DIEValue *Acc, const StringRef S)
352 : DIEValue(isString), Access(Acc), Str(S) {}
354 /// getString - Grab the string out of the object.
355 StringRef getString() const { return Str; }
357 /// EmitValue - Emit delta value.
359 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
361 /// SizeOf - Determine size of delta value in bytes.
363 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
365 // Implement isa/cast/dyncast.
366 static bool classof(const DIEValue *D) { return D->getType() == isString; }
369 virtual void print(raw_ostream &O) const;
373 //===--------------------------------------------------------------------===//
374 /// DIEEntry - A pointer to another debug information entry. An instance of
375 /// this class can also be used as a proxy for a debug information entry not
376 /// yet defined (ie. types.)
377 class DIEEntry : public DIEValue {
380 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
381 assert(E && "Cannot construct a DIEEntry with a null DIE");
384 DIE *getEntry() const { return Entry; }
386 /// EmitValue - Emit debug information entry offset.
388 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
390 /// SizeOf - Determine size of debug information entry in bytes.
392 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
393 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
397 /// Returns size of a ref_addr entry.
398 static unsigned getRefAddrSize(AsmPrinter *AP);
400 // Implement isa/cast/dyncast.
401 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
404 virtual void print(raw_ostream &O) const;
408 //===--------------------------------------------------------------------===//
409 /// DIEBlock - A block of values. Primarily used for location expressions.
411 class DIEBlock : public DIEValue, public DIE {
412 unsigned Size; // Size in bytes excluding size header.
414 DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
416 /// ComputeSize - calculate the size of the block.
418 unsigned ComputeSize(AsmPrinter *AP);
420 /// BestForm - Choose the best form for data.
422 dwarf::Form BestForm() const {
423 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
424 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
425 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
426 return dwarf::DW_FORM_block;
429 /// EmitValue - Emit block data.
431 virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
433 /// SizeOf - Determine size of block data in bytes.
435 virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
437 // Implement isa/cast/dyncast.
438 static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
441 virtual void print(raw_ostream &O) const;
445 } // end llvm namespace