DwarfCompileUnit.h revision 8594d429e02c688d428036f8563f09572da3fbff
1//===-- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit ---*- C++ -*--===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf compile unit.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15#define CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
16
17#include "DIE.h"
18#include "llvm/Analysis/DebugInfo.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/ADT/OwningPtr.h"
22
23namespace llvm {
24
25class DwarfDebug;
26class MachineLocation;
27class MachineOperand;
28class ConstantInt;
29class DbgVariable;
30
31//===----------------------------------------------------------------------===//
32/// CompileUnit - This dwarf writer support class manages information associate
33/// with a source file.
34class CompileUnit {
35  /// ID - File identifier for source.
36  ///
37  unsigned ID;
38
39  /// Die - Compile unit debug information entry.
40  ///
41  const OwningPtr<DIE> CUDie;
42
43  /// Asm - Target of Dwarf emission.
44  AsmPrinter *Asm;
45
46  DwarfDebug *DD;
47
48  /// IndexTyDie - An anonymous type for index type.  Owned by CUDie.
49  DIE *IndexTyDie;
50
51  /// MDNodeToDieMap - Tracks the mapping of unit level debug informaton
52  /// variables to debug information entries.
53  DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
54
55  /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug informaton
56  /// descriptors to debug information entries using a DIEEntry proxy.
57  DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
58
59  /// Globals - A map of globally visible named entities for this unit.
60  ///
61  StringMap<DIE*> Globals;
62
63  /// GlobalTypes - A map of globally visible types for this unit.
64  ///
65  StringMap<DIE*> GlobalTypes;
66
67  /// DIEBlocks - A list of all the DIEBlocks in use.
68  std::vector<DIEBlock *> DIEBlocks;
69
70public:
71  CompileUnit(unsigned I, DIE *D, AsmPrinter *A, DwarfDebug *DW);
72  ~CompileUnit();
73
74  // Accessors.
75  unsigned getID()                  const { return ID; }
76  DIE* getCUDie()                   const { return CUDie.get(); }
77  const StringMap<DIE*> &getGlobals()     const { return Globals; }
78  const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
79
80  /// hasContent - Return true if this compile unit has something to write out.
81  ///
82  bool hasContent() const { return !CUDie->getChildren().empty(); }
83
84  /// addGlobal - Add a new global entity to the compile unit.
85  ///
86  void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
87
88  /// addGlobalType - Add a new global type to the compile unit.
89  ///
90  void addGlobalType(DIType Ty);
91
92  /// getDIE - Returns the debug information entry map slot for the
93  /// specified debug variable.
94  DIE *getDIE(const MDNode *N) { return MDNodeToDieMap.lookup(N); }
95
96  DIEBlock *getDIEBlock() {
97    return new (DIEValueAllocator) DIEBlock();
98  }
99
100  /// insertDIE - Insert DIE into the map.
101  void insertDIE(const MDNode *N, DIE *D) {
102    MDNodeToDieMap.insert(std::make_pair(N, D));
103  }
104
105  /// getDIEEntry - Returns the debug information entry for the specified
106  /// debug variable.
107  DIEEntry *getDIEEntry(const MDNode *N) {
108    DenseMap<const MDNode *, DIEEntry *>::iterator I =
109      MDNodeToDIEEntryMap.find(N);
110    if (I == MDNodeToDIEEntryMap.end())
111      return NULL;
112    return I->second;
113  }
114
115  /// insertDIEEntry - Insert debug information entry into the map.
116  void insertDIEEntry(const MDNode *N, DIEEntry *E) {
117    MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
118  }
119
120  /// addDie - Adds or interns the DIE to the compile unit.
121  ///
122  void addDie(DIE *Buffer) {
123    this->CUDie->addChild(Buffer);
124  }
125
126  // getIndexTyDie - Get an anonymous type for index type.
127  DIE *getIndexTyDie() {
128    return IndexTyDie;
129  }
130
131  // setIndexTyDie - Set D as anonymous type for index which can be reused
132  // later.
133  void setIndexTyDie(DIE *D) {
134    IndexTyDie = D;
135  }
136public:
137
138  /// addUInt - Add an unsigned integer attribute data and value.
139  ///
140  void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
141
142  /// addSInt - Add an signed integer attribute data and value.
143  ///
144  void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
145
146  /// addString - Add a string attribute data and value.
147  ///
148  void addString(DIE *Die, unsigned Attribute, unsigned Form,
149                 const StringRef Str);
150
151  /// addLabel - Add a Dwarf label attribute data and value.
152  ///
153  void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
154                const MCSymbol *Label);
155
156  /// addDelta - Add a label delta attribute data and value.
157  ///
158  void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
159                const MCSymbol *Hi, const MCSymbol *Lo);
160
161  /// addDIEEntry - Add a DIE attribute data and value.
162  ///
163  void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
164
165  /// addBlock - Add block data.
166  ///
167  void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
168
169  /// addSourceLine - Add location information to specified debug information
170  /// entry.
171  void addSourceLine(DIE *Die, DIVariable V);
172  void addSourceLine(DIE *Die, DIGlobalVariable G);
173  void addSourceLine(DIE *Die, DISubprogram SP);
174  void addSourceLine(DIE *Die, DIType Ty);
175  void addSourceLine(DIE *Die, DINameSpace NS);
176
177  /// addAddress - Add an address attribute to a die based on the location
178  /// provided.
179  void addAddress(DIE *Die, unsigned Attribute,
180                  const MachineLocation &Location);
181
182  /// addConstantValue - Add constant value entry in variable DIE.
183  bool addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
184  bool addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
185
186  /// addConstantFPValue - Add constant value entry in variable DIE.
187  bool addConstantFPValue(DIE *Die, const MachineOperand &MO);
188
189  /// addTemplateParams - Add template parameters in buffer.
190  void addTemplateParams(DIE &Buffer, DIArray TParams);
191
192  /// addRegisterOp - Add register operand.
193  void addRegisterOp(DIE *TheDie, unsigned Reg);
194
195  /// addRegisterOffset - Add register offset.
196  void addRegisterOffset(DIE *TheDie, unsigned Reg, int64_t Offset);
197
198  /// addComplexAddress - Start with the address based on the location provided,
199  /// and generate the DWARF information necessary to find the actual variable
200  /// (navigating the extra location information encoded in the type) based on
201  /// the starting location.  Add the DWARF information to the die.
202  ///
203  void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
204                         const MachineLocation &Location);
205
206  // FIXME: Should be reformulated in terms of addComplexAddress.
207  /// addBlockByrefAddress - Start with the address based on the location
208  /// provided, and generate the DWARF information necessary to find the
209  /// actual Block variable (navigating the Block struct) based on the
210  /// starting location.  Add the DWARF information to the die.  Obsolete,
211  /// please use addComplexAddress instead.
212  ///
213  void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
214                            const MachineLocation &Location);
215
216  /// addVariableAddress - Add DW_AT_location attribute for a
217  /// DbgVariable based on provided MachineLocation.
218  void addVariableAddress(DbgVariable *&DV, DIE *Die, MachineLocation Location);
219
220  /// addToContextOwner - Add Die into the list of its context owner's children.
221  void addToContextOwner(DIE *Die, DIDescriptor Context);
222
223  /// addType - Add a new type attribute to the specified entity.
224  void addType(DIE *Entity, DIType Ty);
225
226  /// getOrCreateNameSpace - Create a DIE for DINameSpace.
227  DIE *getOrCreateNameSpace(DINameSpace NS);
228
229  /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
230  /// given DIType.
231  DIE *getOrCreateTypeDIE(DIType Ty);
232
233  /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
234  /// for the given DITemplateTypeParameter.
235  DIE *getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP);
236
237  /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
238  /// for the given DITemplateValueParameter.
239  DIE *getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TVP);
240
241  /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
242  /// information entry.
243  DIEEntry *createDIEEntry(DIE *Entry);
244
245  void addPubTypes(DISubprogram SP);
246
247  /// constructTypeDIE - Construct basic type die from DIBasicType.
248  void constructTypeDIE(DIE &Buffer,
249                        DIBasicType BTy);
250
251  /// constructTypeDIE - Construct derived type die from DIDerivedType.
252  void constructTypeDIE(DIE &Buffer,
253                        DIDerivedType DTy);
254
255  /// constructTypeDIE - Construct type DIE from DICompositeType.
256  void constructTypeDIE(DIE &Buffer,
257                        DICompositeType CTy);
258
259  /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
260  void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
261
262  /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
263  void constructArrayTypeDIE(DIE &Buffer,
264                             DICompositeType *CTy);
265
266  /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
267  DIE *constructEnumTypeDIE(DIEnumerator ETy);
268
269  /// createMemberDIE - Create new member DIE.
270  DIE *createMemberDIE(DIDerivedType DT);
271
272private:
273
274  // DIEValueAllocator - All DIEValues are allocated through this allocator.
275  BumpPtrAllocator DIEValueAllocator;
276  DIEInteger *DIEIntegerOne;
277};
278
279} // end llvm namespace
280#endif
281