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