DwarfCompileUnit.h revision dfa30e1ab243990eda4732a6dffb91e965e7a755
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 associated
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  /// GlobalTypes - A map of globally visible types for this unit.
60  ///
61  StringMap<DIE*> GlobalTypes;
62
63  /// AccelNames - A map of names for the name accelerator table.
64  ///
65  StringMap<DIE*> AccelNames;
66  StringMap<std::vector<DIE*> > AccelObjC;
67  StringMap<DIE*> AccelNamespace;
68  StringMap<DIE*> AccelTypes;
69
70  /// DIEBlocks - A list of all the DIEBlocks in use.
71  std::vector<DIEBlock *> DIEBlocks;
72
73  /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
74  /// need DW_AT_containing_type attribute. This attribute points to a DIE that
75  /// corresponds to the MDNode mapped with the subprogram DIE.
76  DenseMap<DIE *, const MDNode *> ContainingTypeMap;
77
78public:
79  CompileUnit(unsigned I, DIE *D, AsmPrinter *A, DwarfDebug *DW);
80  ~CompileUnit();
81
82  // Accessors.
83  unsigned getID()                  const { return ID; }
84  DIE* getCUDie()                   const { return CUDie.get(); }
85  const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
86
87  const StringMap<DIE*> &getAccelNames() const { return AccelNames; }
88  const StringMap<std::vector<DIE*> > &getAccelObjC() const {
89    return AccelObjC;
90  }
91  const StringMap<DIE*> &getAccelNamespace() const { return AccelNamespace; }
92  const StringMap<DIE*> &getAccelTypes() const { return AccelTypes; }
93
94  /// hasContent - Return true if this compile unit has something to write out.
95  ///
96  bool hasContent() const { return !CUDie->getChildren().empty(); }
97
98  /// addGlobalType - Add a new global type to the compile unit.
99  ///
100  void addGlobalType(DIType Ty);
101
102
103  /// addAccelName - Add a new name to the name accelerator table.
104  void addAccelName(StringRef Name, DIE *Die) { AccelNames[Name] = Die; }
105  void addAccelObjC(StringRef Name, DIE *Die) {
106    std::vector<DIE*> &DIEs = AccelObjC[Name];
107    DIEs.push_back(Die);
108  }
109  void addAccelNamespace(StringRef Name, DIE *Die) {
110    AccelNamespace[Name] = Die;
111  }
112  void addAccelType(StringRef Name, DIE *Die) {
113    AccelTypes[Name] = Die;
114  }
115
116  /// getDIE - Returns the debug information entry map slot for the
117  /// specified debug variable.
118  DIE *getDIE(const MDNode *N) { return MDNodeToDieMap.lookup(N); }
119
120  DIEBlock *getDIEBlock() {
121    return new (DIEValueAllocator) DIEBlock();
122  }
123
124  /// insertDIE - Insert DIE into the map.
125  void insertDIE(const MDNode *N, DIE *D) {
126    MDNodeToDieMap.insert(std::make_pair(N, D));
127  }
128
129  /// getDIEEntry - Returns the debug information entry for the specified
130  /// debug variable.
131  DIEEntry *getDIEEntry(const MDNode *N) {
132    DenseMap<const MDNode *, DIEEntry *>::iterator I =
133      MDNodeToDIEEntryMap.find(N);
134    if (I == MDNodeToDIEEntryMap.end())
135      return NULL;
136    return I->second;
137  }
138
139  /// insertDIEEntry - Insert debug information entry into the map.
140  void insertDIEEntry(const MDNode *N, DIEEntry *E) {
141    MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
142  }
143
144  /// addDie - Adds or interns the DIE to the compile unit.
145  ///
146  void addDie(DIE *Buffer) {
147    this->CUDie->addChild(Buffer);
148  }
149
150  // getIndexTyDie - Get an anonymous type for index type.
151  DIE *getIndexTyDie() {
152    return IndexTyDie;
153  }
154
155  // setIndexTyDie - Set D as anonymous type for index which can be reused
156  // later.
157  void setIndexTyDie(DIE *D) {
158    IndexTyDie = D;
159  }
160public:
161
162  /// addUInt - Add an unsigned integer attribute data and value.
163  ///
164  void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
165
166  /// addSInt - Add an signed integer attribute data and value.
167  ///
168  void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
169
170  /// addString - Add a string attribute data and value.
171  ///
172  void addString(DIE *Die, unsigned Attribute, const StringRef Str);
173
174  /// addLabel - Add a Dwarf label attribute data and value.
175  ///
176  void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
177                const MCSymbol *Label);
178
179  /// addDelta - Add a label delta attribute data and value.
180  ///
181  void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
182                const MCSymbol *Hi, const MCSymbol *Lo);
183
184  /// addDIEEntry - Add a DIE attribute data and value.
185  ///
186  void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
187
188  /// addBlock - Add block data.
189  ///
190  void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
191
192  /// addSourceLine - Add location information to specified debug information
193  /// entry.
194  void addSourceLine(DIE *Die, DIVariable V);
195  void addSourceLine(DIE *Die, DIGlobalVariable G);
196  void addSourceLine(DIE *Die, DISubprogram SP);
197  void addSourceLine(DIE *Die, DIType Ty);
198  void addSourceLine(DIE *Die, DINameSpace NS);
199
200  /// addAddress - Add an address attribute to a die based on the location
201  /// provided.
202  void addAddress(DIE *Die, unsigned Attribute,
203                  const MachineLocation &Location);
204
205  /// addConstantValue - Add constant value entry in variable DIE.
206  bool addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
207  bool addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
208
209  /// addConstantFPValue - Add constant value entry in variable DIE.
210  bool addConstantFPValue(DIE *Die, const MachineOperand &MO);
211
212  /// addTemplateParams - Add template parameters in buffer.
213  void addTemplateParams(DIE &Buffer, DIArray TParams);
214
215  /// addRegisterOp - Add register operand.
216  void addRegisterOp(DIE *TheDie, unsigned Reg);
217
218  /// addRegisterOffset - Add register offset.
219  void addRegisterOffset(DIE *TheDie, unsigned Reg, int64_t Offset);
220
221  /// addComplexAddress - Start with the address based on the location provided,
222  /// and generate the DWARF information necessary to find the actual variable
223  /// (navigating the extra location information encoded in the type) based on
224  /// the starting location.  Add the DWARF information to the die.
225  ///
226  void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
227                         const MachineLocation &Location);
228
229  // FIXME: Should be reformulated in terms of addComplexAddress.
230  /// addBlockByrefAddress - Start with the address based on the location
231  /// provided, and generate the DWARF information necessary to find the
232  /// actual Block variable (navigating the Block struct) based on the
233  /// starting location.  Add the DWARF information to the die.  Obsolete,
234  /// please use addComplexAddress instead.
235  ///
236  void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
237                            const MachineLocation &Location);
238
239  /// addVariableAddress - Add DW_AT_location attribute for a
240  /// DbgVariable based on provided MachineLocation.
241  void addVariableAddress(DbgVariable *&DV, DIE *Die, MachineLocation Location);
242
243  /// addToContextOwner - Add Die into the list of its context owner's children.
244  void addToContextOwner(DIE *Die, DIDescriptor Context);
245
246  /// addType - Add a new type attribute to the specified entity.
247  void addType(DIE *Entity, DIType Ty);
248
249  /// getOrCreateNameSpace - Create a DIE for DINameSpace.
250  DIE *getOrCreateNameSpace(DINameSpace NS);
251
252  /// getOrCreateSubprogramDIE - Create new DIE using SP.
253  DIE *getOrCreateSubprogramDIE(DISubprogram SP);
254
255  /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
256  /// given DIType.
257  DIE *getOrCreateTypeDIE(const MDNode *N);
258
259  /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
260  /// for the given DITemplateTypeParameter.
261  DIE *getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP);
262
263  /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
264  /// for the given DITemplateValueParameter.
265  DIE *getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TVP);
266
267  /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
268  /// information entry.
269  DIEEntry *createDIEEntry(DIE *Entry);
270
271  /// createGlobalVariableDIE - create global variable DIE.
272  void createGlobalVariableDIE(const MDNode *N);
273
274  void addPubTypes(DISubprogram SP);
275
276  /// constructTypeDIE - Construct basic type die from DIBasicType.
277  void constructTypeDIE(DIE &Buffer,
278                        DIBasicType BTy);
279
280  /// constructTypeDIE - Construct derived type die from DIDerivedType.
281  void constructTypeDIE(DIE &Buffer,
282                        DIDerivedType DTy);
283
284  /// constructTypeDIE - Construct type DIE from DICompositeType.
285  void constructTypeDIE(DIE &Buffer,
286                        DICompositeType CTy);
287
288  /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
289  void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
290
291  /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
292  void constructArrayTypeDIE(DIE &Buffer,
293                             DICompositeType *CTy);
294
295  /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
296  DIE *constructEnumTypeDIE(DIEnumerator ETy);
297
298  /// constructContainingTypeDIEs - Construct DIEs for types that contain
299  /// vtables.
300  void constructContainingTypeDIEs();
301
302  /// constructVariableDIE - Construct a DIE for the given DbgVariable.
303  DIE *constructVariableDIE(DbgVariable *DV, bool isScopeAbstract);
304
305  /// createMemberDIE - Create new member DIE.
306  DIE *createMemberDIE(DIDerivedType DT);
307
308private:
309
310  // DIEValueAllocator - All DIEValues are allocated through this allocator.
311  BumpPtrAllocator DIEValueAllocator;
312  DIEInteger *DIEIntegerOne;
313};
314
315} // end llvm namespace
316#endif
317