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 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(uint16_t Attribute, uint16_t 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 // Private data for print()
135 mutable unsigned IndentCount;
138 explicit DIE(unsigned Tag)
139 : Offset(0), Size(0), Abbrev(Tag, dwarf::DW_CHILDREN_no), Parent(0) {}
143 DIEAbbrev &getAbbrev() { return Abbrev; }
144 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
145 uint16_t getTag() const { return Abbrev.getTag(); }
146 unsigned getOffset() const { return Offset; }
147 unsigned getSize() const { return Size; }
148 const std::vector<DIE *> &getChildren() const { return Children; }
149 const SmallVectorImpl<DIEValue*> &getValues() const { return Values; }
150 DIE *getParent() const { return Parent; }
151 void setOffset(unsigned O) { Offset = O; }
152 void setSize(unsigned S) { Size = S; }
154 /// addValue - Add a value and attributes to a DIE.
156 void addValue(uint16_t Attribute, uint16_t Form, DIEValue *Value) {
157 Abbrev.AddAttribute(Attribute, Form);
158 Values.push_back(Value);
161 /// addChild - Add a child to the DIE.
163 void addChild(DIE *Child) {
164 assert(!Child->getParent());
165 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
166 Children.push_back(Child);
167 Child->Parent = this;
170 /// findAttribute - Find a value in the DIE with the attribute given, returns NULL
171 /// if no such attribute exists.
172 DIEValue *findAttribute(uint16_t Attribute);
175 void print(raw_ostream &O, unsigned IndentCount = 0) const;
180 //===--------------------------------------------------------------------===//
181 /// DIEValue - A debug information entry value.
184 virtual void anchor();
196 /// Type - Type of data stored in the value.
200 explicit DIEValue(unsigned T) : Type(T) {}
201 virtual ~DIEValue() {}
204 unsigned getType() const { return Type; }
206 /// EmitValue - Emit value via the Dwarf writer.
208 virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const = 0;
210 /// SizeOf - Return the size of a value in bytes.
212 virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const = 0;
215 virtual void print(raw_ostream &O) const = 0;
220 //===--------------------------------------------------------------------===//
221 /// DIEInteger - An integer value DIE.
223 class DIEInteger : public DIEValue {
226 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
228 /// BestForm - Choose the best form for integer.
230 static uint16_t BestForm(bool IsSigned, uint64_t Int) {
232 const int64_t SignedInt = Int;
233 if ((char)Int == SignedInt) return dwarf::DW_FORM_data1;
234 if ((short)Int == SignedInt) return dwarf::DW_FORM_data2;
235 if ((int)Int == SignedInt) return dwarf::DW_FORM_data4;
237 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
238 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
239 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
241 return dwarf::DW_FORM_data8;
244 /// EmitValue - Emit integer of appropriate size.
246 virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
248 uint64_t getValue() const { return Integer; }
250 /// SizeOf - Determine size of integer value in bytes.
252 virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
254 // Implement isa/cast/dyncast.
255 static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
258 virtual void print(raw_ostream &O) const;
262 //===--------------------------------------------------------------------===//
263 /// DIEExpr - An expression DIE.
265 class DIEExpr : public DIEValue {
268 explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
270 /// EmitValue - Emit expression value.
272 virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
274 /// getValue - Get MCExpr.
276 const MCExpr *getValue() const { return Expr; }
278 /// SizeOf - Determine size of expression value in bytes.
280 virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
282 // Implement isa/cast/dyncast.
283 static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
286 virtual void print(raw_ostream &O) const;
290 //===--------------------------------------------------------------------===//
291 /// DIELabel - A label DIE.
293 class DIELabel : public DIEValue {
294 const MCSymbol *Label;
296 explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
298 /// EmitValue - Emit label value.
300 virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
302 /// getValue - Get MCSymbol.
304 const MCSymbol *getValue() const { return Label; }
306 /// SizeOf - Determine size of label value in bytes.
308 virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
310 // Implement isa/cast/dyncast.
311 static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
314 virtual void print(raw_ostream &O) const;
318 //===--------------------------------------------------------------------===//
319 /// DIEDelta - A simple label difference DIE.
321 class DIEDelta : public DIEValue {
322 const MCSymbol *LabelHi;
323 const MCSymbol *LabelLo;
325 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
326 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
328 /// EmitValue - Emit delta value.
330 virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
332 /// SizeOf - Determine size of delta value in bytes.
334 virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
336 // Implement isa/cast/dyncast.
337 static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
340 virtual void print(raw_ostream &O) const;
344 //===--------------------------------------------------------------------===//
345 /// DIEString - A container for string values.
347 class DIEString : public DIEValue {
348 const DIEValue *Access;
352 DIEString(const DIEValue *Acc, const StringRef S)
353 : DIEValue(isString), Access(Acc), Str(S) {}
355 /// getString - Grab the string out of the object.
356 StringRef getString() const { return Str; }
358 /// EmitValue - Emit delta value.
360 virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
362 /// SizeOf - Determine size of delta value in bytes.
364 virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
366 // Implement isa/cast/dyncast.
367 static bool classof(const DIEValue *D) { return D->getType() == isString; }
370 virtual void print(raw_ostream &O) const;
374 //===--------------------------------------------------------------------===//
375 /// DIEEntry - A pointer to another debug information entry. An instance of
376 /// this class can also be used as a proxy for a debug information entry not
377 /// yet defined (ie. types.)
378 class DIEEntry : public DIEValue {
381 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
382 assert(E && "Cannot construct a DIEEntry with a null DIE");
385 DIE *getEntry() const { return Entry; }
387 /// EmitValue - Emit debug information entry offset.
389 virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
391 /// SizeOf - Determine size of debug information entry in bytes.
393 virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const {
394 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
398 /// Returns size of a ref_addr entry.
399 static unsigned getRefAddrSize(AsmPrinter *AP);
401 // Implement isa/cast/dyncast.
402 static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
405 virtual void print(raw_ostream &O) const;
409 //===--------------------------------------------------------------------===//
410 /// DIEBlock - A block of values. Primarily used for location expressions.
412 class DIEBlock : public DIEValue, public DIE {
413 unsigned Size; // Size in bytes excluding size header.
416 : DIEValue(isBlock), DIE(0), Size(0) {}
417 virtual ~DIEBlock() {}
419 /// ComputeSize - calculate the size of the block.
421 unsigned ComputeSize(AsmPrinter *AP);
423 /// BestForm - Choose the best form for data.
425 uint16_t 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, uint16_t Form) const;
436 /// SizeOf - Determine size of block data in bytes.
438 virtual unsigned SizeOf(AsmPrinter *AP, uint16_t 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