Reflow some documentation and remove whitespace comments. Move
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfCompileUnit.h
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 "DwarfDebug.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/DebugInfo.h"
24 #include "llvm/MC/MCExpr.h"
25
26 namespace llvm {
27
28 class MachineLocation;
29 class MachineOperand;
30 class ConstantInt;
31 class ConstantFP;
32 class DbgVariable;
33
34 //===----------------------------------------------------------------------===//
35 /// CompileUnit - This dwarf writer support class manages information associated
36 /// with a source file.
37 class CompileUnit {
38   /// UniqueID - a numeric ID unique among all CUs in the module
39   unsigned UniqueID;
40
41   /// Node - MDNode for the compile unit.
42   DICompileUnit Node;
43
44   /// Language - Language for the translation unit associated with this CU.
45   uint16_t Language;
46
47   /// CUDie - Compile unit debug information entry.
48   const OwningPtr<DIE> CUDie;
49
50   /// Asm - Target of Dwarf emission.
51   AsmPrinter *Asm;
52
53   // Holders for some common dwarf information.
54   DwarfDebug *DD;
55   DwarfUnits *DU;
56
57   /// IndexTyDie - An anonymous type for index type.  Owned by CUDie.
58   DIE *IndexTyDie;
59
60   /// MDNodeToDieMap - Tracks the mapping of unit level debug information
61   /// variables to debug information entries.
62   DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
63
64   /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
65   /// descriptors to debug information entries using a DIEEntry proxy.
66   DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
67
68   /// GlobalNames - A map of globally visible named entities for this unit.
69   StringMap<const DIE *> GlobalNames;
70
71   /// GlobalTypes - A map of globally visible types for this unit.
72   StringMap<const DIE *> GlobalTypes;
73
74   /// AccelNames - A map of names for the name accelerator table.
75   StringMap<std::vector<const DIE *> > AccelNames;
76
77   /// AccelObjC - A map of objc spec for the objc accelerator table.
78   StringMap<std::vector<const DIE *> > AccelObjC;
79
80   /// AccelNamespace - A map of names for the namespace accelerator table.
81   StringMap<std::vector<const DIE *> > AccelNamespace;
82
83   /// AccelTypes - A map of names for the type accelerator table.
84   StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
85
86   /// DIEBlocks - A list of all the DIEBlocks in use.
87   std::vector<DIEBlock *> DIEBlocks;
88
89   /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
90   /// need DW_AT_containing_type attribute. This attribute points to a DIE that
91   /// corresponds to the MDNode mapped with the subprogram DIE.
92   DenseMap<DIE *, const MDNode *> ContainingTypeMap;
93
94   // DIEValueAllocator - All DIEValues are allocated through this allocator.
95   BumpPtrAllocator DIEValueAllocator;
96
97   // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
98   DIEInteger *DIEIntegerOne;
99
100   /// Offset of the CUDie from beginning of debug info section.
101   unsigned DebugInfoOffset;
102
103 public:
104   CompileUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A,
105               DwarfDebug *DW, DwarfUnits *DWU);
106   CompileUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
107               DwarfDebug *DW, DwarfUnits *DWU);
108   ~CompileUnit();
109
110   // Accessors.
111   unsigned getUniqueID() const { return UniqueID; }
112   uint16_t getLanguage() const { return Language; }
113   DICompileUnit getNode() const { return Node; }
114   DIE *getCUDie() const { return CUDie.get(); }
115   const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
116   const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
117
118   const StringMap<std::vector<const DIE *> > &getAccelNames() const {
119     return AccelNames;
120   }
121   const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
122     return AccelObjC;
123   }
124   const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
125     return AccelNamespace;
126   }
127   const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
128   getAccelTypes() const {
129     return AccelTypes;
130   }
131
132   unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
133   void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
134
135   /// hasContent - Return true if this compile unit has something to write out.
136   bool hasContent() const { return !CUDie->getChildren().empty(); }
137
138   /// getParentContextString - Get a string containing the language specific
139   /// context for a global name.
140   std::string getParentContextString(DIScope Context) const;
141
142   /// addGlobalName - Add a new global entity to the compile unit.
143   ///
144   void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
145
146   /// addGlobalType - Add a new global type to the compile unit.
147   void addGlobalType(DIType Ty);
148
149   /// addPubTypes - Add a set of types from the subprogram to the global types.
150   void addPubTypes(DISubprogram SP);
151
152   /// addAccelName - Add a new name to the name accelerator table.
153   void addAccelName(StringRef Name, const DIE *Die);
154
155   /// addAccelObjC - Add a new name to the ObjC accelerator table.
156   void addAccelObjC(StringRef Name, const DIE *Die);
157
158   /// addAccelNamespace - Add a new name to the namespace accelerator table.
159   void addAccelNamespace(StringRef Name, const DIE *Die);
160
161   /// addAccelType - Add a new type to the type accelerator table.
162   void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
163
164   /// getDIE - Returns the debug information entry map slot for the
165   /// specified debug variable. We delegate the request to DwarfDebug
166   /// when the MDNode can be part of the type system, since DIEs for
167   /// the type system can be shared across CUs and the mappings are
168   /// kept in DwarfDebug.
169   DIE *getDIE(DIDescriptor D) const;
170
171   /// getDIEBlock - Returns a fresh newly allocated DIEBlock.
172   DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); }
173
174   /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
175   /// when the MDNode can be part of the type system, since DIEs for
176   /// the type system can be shared across CUs and the mappings are
177   /// kept in DwarfDebug.
178   void insertDIE(DIDescriptor Desc, DIE *D);
179
180   /// addDie - Adds or interns the DIE to the compile unit.
181   ///
182   void addDie(DIE *Buffer) { CUDie->addChild(Buffer); }
183
184   /// addFlag - Add a flag that is true to the DIE.
185   void addFlag(DIE *Die, dwarf::Attribute Attribute);
186
187   /// addUInt - Add an unsigned integer attribute data and value.
188   void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
189                uint64_t Integer);
190
191   void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer);
192
193   /// addSInt - Add an signed integer attribute data and value.
194   void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
195                int64_t Integer);
196
197   void addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer);
198
199   /// addString - Add a string attribute data and value.
200   void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
201
202   /// addLocalString - Add a string attribute data and value.
203   void addLocalString(DIE *Die, dwarf::Attribute Attribute,
204                       const StringRef Str);
205
206   /// addExpr - Add a Dwarf expression attribute data and value.
207   void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr);
208
209   /// addLabel - Add a Dwarf label attribute data and value.
210   void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
211                 const MCSymbol *Label);
212
213   void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label);
214
215   /// addLabelAddress - Add a dwarf label attribute data and value using
216   /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
217   void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
218
219   /// addOpAddress - Add a dwarf op address data and value using the
220   /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
221   void addOpAddress(DIEBlock *Die, const MCSymbol *Label);
222
223   /// addDelta - Add a label delta attribute data and value.
224   void addDelta(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
225                 const MCSymbol *Hi, const MCSymbol *Lo);
226
227   /// addDIEEntry - Add a DIE attribute data and value.
228   void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
229
230   /// addDIEEntry - Add a DIE attribute data and value.
231   void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
232
233   /// addBlock - Add block data.
234   void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
235
236   /// addSourceLine - Add location information to specified debug information
237   /// entry.
238   void addSourceLine(DIE *Die, DIVariable V);
239   void addSourceLine(DIE *Die, DIGlobalVariable G);
240   void addSourceLine(DIE *Die, DISubprogram SP);
241   void addSourceLine(DIE *Die, DIType Ty);
242   void addSourceLine(DIE *Die, DINameSpace NS);
243   void addSourceLine(DIE *Die, DIObjCProperty Ty);
244
245   /// addAddress - Add an address attribute to a die based on the location
246   /// provided.
247   void addAddress(DIE *Die, dwarf::Attribute Attribute,
248                   const MachineLocation &Location, bool Indirect = false);
249
250   /// addConstantValue - Add constant value entry in variable DIE.
251   void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
252   void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
253   void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
254
255   /// addConstantFPValue - Add constant value entry in variable DIE.
256   void addConstantFPValue(DIE *Die, const MachineOperand &MO);
257   void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
258
259   /// addTemplateParams - Add template parameters in buffer.
260   void addTemplateParams(DIE &Buffer, DIArray TParams);
261
262   /// addRegisterOp - Add register operand.
263   void addRegisterOp(DIEBlock *TheDie, unsigned Reg);
264
265   /// addRegisterOffset - Add register offset.
266   void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset);
267
268   /// addComplexAddress - Start with the address based on the location provided,
269   /// and generate the DWARF information necessary to find the actual variable
270   /// (navigating the extra location information encoded in the type) based on
271   /// the starting location.  Add the DWARF information to the die.
272   void addComplexAddress(const DbgVariable &DV, DIE *Die,
273                          dwarf::Attribute Attribute,
274                          const MachineLocation &Location);
275
276   // FIXME: Should be reformulated in terms of addComplexAddress.
277   /// addBlockByrefAddress - Start with the address based on the location
278   /// provided, and generate the DWARF information necessary to find the
279   /// actual Block variable (navigating the Block struct) based on the
280   /// starting location.  Add the DWARF information to the die.  Obsolete,
281   /// please use addComplexAddress instead.
282   void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
283                             dwarf::Attribute Attribute,
284                             const MachineLocation &Location);
285
286   /// addVariableAddress - Add DW_AT_location attribute for a
287   /// DbgVariable based on provided MachineLocation.
288   void addVariableAddress(const DbgVariable &DV, DIE *Die,
289                           MachineLocation Location);
290
291   /// addType - Add a new type attribute to the specified entity. This takes
292   /// and attribute parameter because DW_AT_friend attributes are also
293   /// type references.
294   void addType(DIE *Entity, DIType Ty,
295                dwarf::Attribute Attribute = dwarf::DW_AT_type);
296
297   /// getOrCreateNameSpace - Create a DIE for DINameSpace.
298   DIE *getOrCreateNameSpace(DINameSpace NS);
299
300   /// getOrCreateSubprogramDIE - Create new DIE using SP.
301   DIE *getOrCreateSubprogramDIE(DISubprogram SP);
302
303   /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
304   /// given DIType.
305   DIE *getOrCreateTypeDIE(const MDNode *N);
306
307   /// getOrCreateContextDIE - Get context owner's DIE.
308   DIE *createTypeDIE(DICompositeType Ty);
309
310   /// getOrCreateContextDIE - Get context owner's DIE.
311   DIE *getOrCreateContextDIE(DIScope Context);
312
313   /// createGlobalVariableDIE - create global variable DIE.
314   void createGlobalVariableDIE(DIGlobalVariable GV);
315
316   /// constructContainingTypeDIEs - Construct DIEs for types that contain
317   /// vtables.
318   void constructContainingTypeDIEs();
319
320   /// constructVariableDIE - Construct a DIE for the given DbgVariable.
321   DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
322
323   /// Create a DIE with the given Tag, add the DIE to its parent, and
324   /// call insertDIE if MD is not null.
325   DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
326                        DIDescriptor N = DIDescriptor());
327
328   /// constructTypeDIEImpl - Construct type DIE that is not a type unit
329   /// reference from a DICompositeType.
330   void constructTypeDIEImpl(DIE &Buffer, DICompositeType CTy);
331
332   /// Compute the size of a header for this unit, not including the initial
333   /// length field.
334   unsigned getHeaderSize() const {
335     return sizeof(int16_t) + // DWARF version number
336            sizeof(int32_t) + // Offset Into Abbrev. Section
337            sizeof(int8_t);   // Pointer Size (in bytes)
338   }
339
340   /// Emit the header for this unit, not including the initial length field.
341   void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym);
342
343 private:
344   /// constructTypeDIE - Construct basic type die from DIBasicType.
345   void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
346
347   /// constructTypeDIE - Construct derived type die from DIDerivedType.
348   void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
349
350   /// constructTypeDIE - Construct type DIE from DICompositeType.
351   void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
352
353   /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
354   void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
355
356   /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
357   void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
358
359   /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
360   void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
361
362   /// constructMemberDIE - Construct member DIE from DIDerivedType.
363   void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
364
365   /// constructTemplateTypeParameterDIE - Construct new DIE for the given
366   /// DITemplateTypeParameter.
367   void constructTemplateTypeParameterDIE(DIE &Buffer,
368                                          DITemplateTypeParameter TP);
369
370   /// constructTemplateValueParameterDIE - Construct new DIE for the given
371   /// DITemplateValueParameter.
372   void constructTemplateValueParameterDIE(DIE &Buffer,
373                                           DITemplateValueParameter TVP);
374
375   /// getOrCreateStaticMemberDIE - Create new static data member DIE.
376   DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
377
378   /// getLowerBoundDefault - Return the default lower bound for an array. If the
379   /// DWARF version doesn't handle the language, return -1.
380   int64_t getDefaultLowerBound() const;
381
382   /// getDIEEntry - Returns the debug information entry for the specified
383   /// debug variable.
384   DIEEntry *getDIEEntry(const MDNode *N) const {
385     return MDNodeToDIEEntryMap.lookup(N);
386   }
387
388   /// insertDIEEntry - Insert debug information entry into the map.
389   void insertDIEEntry(const MDNode *N, DIEEntry *E) {
390     MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
391   }
392
393   // getIndexTyDie - Get an anonymous type for index type.
394   DIE *getIndexTyDie() { return IndexTyDie; }
395
396   // setIndexTyDie - Set D as anonymous type for index which can be reused
397   // later.
398   void setIndexTyDie(DIE *D) { IndexTyDie = D; }
399
400   /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
401   /// information entry.
402   DIEEntry *createDIEEntry(DIE *Entry);
403
404   /// resolve - Look in the DwarfDebug map for the MDNode that
405   /// corresponds to the reference.
406   template <typename T> T resolve(DIRef<T> Ref) const {
407     return DD->resolve(Ref);
408   }
409
410   /// If this is a named finished type then include it in the list of types for
411   /// the accelerator tables.
412   void updateAcceleratorTables(DIType Ty, const DIE *TyDIE);
413 };
414
415 } // end llvm namespace
416 #endif