blob: d81ebbf5075f569cd226a7bb3c6249c37cb7bae9 [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework ------*- 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 debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CODEGEN_ASMPRINTER_DWARFDEBUG_H__
15#define CODEGEN_ASMPRINTER_DWARFDEBUG_H__
16
17#include "DIE.h"
18#include "DwarfPrinter.h"
19#include "llvm/CodeGen/AsmPrinter.h"
20#include "llvm/CodeGen/MachineLocation.h"
21#include "llvm/Analysis/DebugInfo.h"
22#include "llvm/Support/raw_ostream.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/FoldingSet.h"
Bill Wendling6679ee42009-05-18 22:02:36 +000025#include "llvm/ADT/SmallSet.h"
Bill Wendling0310d762009-05-15 09:23:25 +000026#include "llvm/ADT/StringMap.h"
27#include "llvm/ADT/UniqueVector.h"
28#include <string>
29
30namespace llvm {
31
32class CompileUnit;
33class DbgVariable;
34class DbgScope;
35class DbgConcreteScope;
36class MachineFrameInfo;
37class MachineModuleInfo;
38class TargetAsmInfo;
39class Timer;
40
41//===----------------------------------------------------------------------===//
42/// SrcLineInfo - This class is used to record source line correspondence.
43///
44class VISIBILITY_HIDDEN SrcLineInfo {
45 unsigned Line; // Source line number.
46 unsigned Column; // Source column.
47 unsigned SourceID; // Source ID number.
48 unsigned LabelID; // Label in code ID number.
49public:
50 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
51 : Line(L), Column(C), SourceID(S), LabelID(I) {}
52
53 // Accessors
54 unsigned getLine() const { return Line; }
55 unsigned getColumn() const { return Column; }
56 unsigned getSourceID() const { return SourceID; }
57 unsigned getLabelID() const { return LabelID; }
58};
59
60class VISIBILITY_HIDDEN DwarfDebug : public Dwarf {
61 //===--------------------------------------------------------------------===//
62 // Attributes used to construct specific Dwarf sections.
63 //
64
65 /// CompileUnitMap - A map of global variables representing compile units to
66 /// compile units.
67 DenseMap<Value *, CompileUnit *> CompileUnitMap;
68
69 /// CompileUnits - All the compile units in this module.
70 ///
71 SmallVector<CompileUnit *, 8> CompileUnits;
72
73 /// MainCU - Some platform prefers one compile unit per .o file. In such
74 /// cases, all dies are inserted in MainCU.
75 CompileUnit *MainCU;
76
77 /// AbbreviationsSet - Used to uniquely define abbreviations.
78 ///
79 FoldingSet<DIEAbbrev> AbbreviationsSet;
80
81 /// Abbreviations - A list of all the unique abbreviations in use.
82 ///
83 std::vector<DIEAbbrev *> Abbreviations;
84
85 /// DirectoryIdMap - Directory name to directory id map.
86 ///
87 StringMap<unsigned> DirectoryIdMap;
88
89 /// DirectoryNames - A list of directory names.
90 SmallVector<std::string, 8> DirectoryNames;
91
92 /// SourceFileIdMap - Source file name to source file id map.
93 ///
94 StringMap<unsigned> SourceFileIdMap;
95
96 /// SourceFileNames - A list of source file names.
97 SmallVector<std::string, 8> SourceFileNames;
98
99 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
100 /// id mapped to a unique id.
101 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
102
103 /// SourceIds - Reverse map from source id to directory id + file id pair.
104 ///
105 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
106
107 /// Lines - List of of source line correspondence.
108 std::vector<SrcLineInfo> Lines;
109
110 /// ValuesSet - Used to uniquely define values.
111 ///
112 FoldingSet<DIEValue> ValuesSet;
113
114 /// Values - A list of all the unique values in use.
115 ///
116 std::vector<DIEValue *> Values;
117
118 /// StringPool - A UniqueVector of strings used by indirect references.
119 ///
120 UniqueVector<std::string> StringPool;
121
122 /// SectionMap - Provides a unique id per text section.
123 ///
124 UniqueVector<const Section*> SectionMap;
125
126 /// SectionSourceLines - Tracks line numbers per text section.
127 ///
128 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
129
130 /// didInitial - Flag to indicate if initial emission has been done.
131 ///
132 bool didInitial;
133
134 /// shouldEmit - Flag to indicate if debug information should be emitted.
135 ///
136 bool shouldEmit;
137
138 // FunctionDbgScope - Top level scope for the current function.
139 //
140 DbgScope *FunctionDbgScope;
141
142 /// DbgScopeMap - Tracks the scopes in the current function.
143 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
144
145 /// DbgAbstractScopeMap - Tracks abstract instance scopes in the current
146 /// function.
147 DenseMap<GlobalVariable *, DbgScope *> DbgAbstractScopeMap;
148
149 /// DbgConcreteScopeMap - Tracks concrete instance scopes in the current
150 /// function.
151 DenseMap<GlobalVariable *,
152 SmallVector<DbgScope *, 8> > DbgConcreteScopeMap;
153
154 /// InlineInfo - Keep track of inlined functions and their location. This
155 /// information is used to populate debug_inlined section.
156 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
157
158 /// InlinedVariableScopes - Scopes information for the inlined subroutine
159 /// variables.
160 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
161
162 /// AbstractInstanceRootMap - Map of abstract instance roots of inlined
163 /// functions. These are subroutine entries that contain a DW_AT_inline
164 /// attribute.
165 DenseMap<const GlobalVariable *, DbgScope *> AbstractInstanceRootMap;
166
Bill Wendling6679ee42009-05-18 22:02:36 +0000167 /// InlinedParamMap - A map keeping track of which parameters are assigned to
168 /// which abstract instance.
169 DenseMap<const GlobalVariable *,
170 SmallSet<const GlobalVariable *, 32> > InlinedParamMap;
171
Bill Wendling0310d762009-05-15 09:23:25 +0000172 /// AbstractInstanceRootList - List of abstract instance roots of inlined
173 /// functions. These are subroutine entries that contain a DW_AT_inline
174 /// attribute.
175 SmallVector<DbgScope *, 32> AbstractInstanceRootList;
176
177 /// LexicalScopeStack - A stack of lexical scopes. The top one is the current
178 /// scope.
179 SmallVector<DbgScope *, 16> LexicalScopeStack;
180
181 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
182 /// used when calculating the "origin" of a concrete instance of an inlined
183 /// function.
184 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
185
186 /// DebugTimer - Timer for the Dwarf debug writer.
187 Timer *DebugTimer;
188
189 struct FunctionDebugFrameInfo {
190 unsigned Number;
191 std::vector<MachineMove> Moves;
192
193 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
194 : Number(Num), Moves(M) {}
195 };
196
197 std::vector<FunctionDebugFrameInfo> DebugFrames;
198
199 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
200 /// maps to the source id. Source id starts at 1.
201 std::pair<unsigned, unsigned>
202 getSourceDirectoryAndFileIds(unsigned SId) const {
203 return SourceIds[SId-1];
204 }
205
206 /// getNumSourceDirectories - Return the number of source directories in the
207 /// debug info.
208 unsigned getNumSourceDirectories() const {
209 return DirectoryNames.size();
210 }
211
212 /// getSourceDirectoryName - Return the name of the directory corresponding
213 /// to the id.
214 const std::string &getSourceDirectoryName(unsigned Id) const {
215 return DirectoryNames[Id - 1];
216 }
217
218 /// getSourceFileName - Return the name of the source file corresponding
219 /// to the id.
220 const std::string &getSourceFileName(unsigned Id) const {
221 return SourceFileNames[Id - 1];
222 }
223
224 /// getNumSourceIds - Return the number of unique source ids.
225 unsigned getNumSourceIds() const {
226 return SourceIds.size();
227 }
228
229 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
230 ///
231 void AssignAbbrevNumber(DIEAbbrev &Abbrev);
232
Bill Wendling995f80a2009-05-20 23:24:48 +0000233 /// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug
234 /// information entry.
235 DIEEntry *CreateDIEEntry(DIE *Entry = NULL);
Bill Wendling0310d762009-05-15 09:23:25 +0000236
237 /// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
238 ///
239 void SetDIEEntry(DIEEntry *Value, DIE *Entry);
240
241 /// AddUInt - Add an unsigned integer attribute data and value.
242 ///
243 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
244
245 /// AddSInt - Add an signed integer attribute data and value.
246 ///
247 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
248
249 /// AddString - Add a string attribute data and value.
250 ///
251 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
252 const std::string &String);
253
254 /// AddLabel - Add a Dwarf label attribute data and value.
255 ///
256 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
257 const DWLabel &Label);
258
259 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
260 ///
261 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
262 const std::string &Label);
263
264 /// AddSectionOffset - Add a section offset label attribute data and value.
265 ///
266 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
267 const DWLabel &Label, const DWLabel &Section,
268 bool isEH = false, bool useSet = true);
269
270 /// AddDelta - Add a label delta attribute data and value.
271 ///
272 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
273 const DWLabel &Hi, const DWLabel &Lo);
274
275 /// AddDIEEntry - Add a DIE attribute data and value.
276 ///
277 void AddDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
Bill Wendling995f80a2009-05-20 23:24:48 +0000278 Die->AddValue(Attribute, Form, CreateDIEEntry(Entry));
Bill Wendling0310d762009-05-15 09:23:25 +0000279 }
280
281 /// AddBlock - Add block data.
282 ///
283 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
284
285 /// AddSourceLine - Add location information to specified debug information
286 /// entry.
287 void AddSourceLine(DIE *Die, const DIVariable *V);
288
289 /// AddSourceLine - Add location information to specified debug information
290 /// entry.
291 void AddSourceLine(DIE *Die, const DIGlobal *G);
292
293 void AddSourceLine(DIE *Die, const DIType *Ty);
294
295 /// AddAddress - Add an address attribute to a die based on the location
296 /// provided.
297 void AddAddress(DIE *Die, unsigned Attribute,
298 const MachineLocation &Location);
299
300 /// AddType - Add a new type attribute to the specified entity.
301 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty);
302
303 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
304 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
305 DIBasicType BTy);
306
307 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
308 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
309 DIDerivedType DTy);
310
311 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
312 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
313 DICompositeType CTy);
314
315 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
316 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
317
318 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
319 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
320 DICompositeType *CTy);
321
322 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
323 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy);
324
325 /// CreateGlobalVariableDIE - Create new DIE using GV.
326 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit,
327 const DIGlobalVariable &GV);
328
329 /// CreateMemberDIE - Create new member DIE.
330 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT);
331
332 /// CreateSubprogramDIE - Create new DIE using SP.
333 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
334 const DISubprogram &SP,
Bill Wendling6679ee42009-05-18 22:02:36 +0000335 bool IsConstructor = false,
336 bool IsInlined = false);
Bill Wendling0310d762009-05-15 09:23:25 +0000337
338 /// FindCompileUnit - Get the compile unit for the given descriptor.
339 ///
340 CompileUnit &FindCompileUnit(DICompileUnit Unit) const;
341
Bill Wendling995f80a2009-05-20 23:24:48 +0000342 /// CreateDbgScopeVariable - Create a new scope variable.
Bill Wendling0310d762009-05-15 09:23:25 +0000343 ///
Bill Wendling995f80a2009-05-20 23:24:48 +0000344 DIE *CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit);
Bill Wendling0310d762009-05-15 09:23:25 +0000345
346 /// getOrCreateScope - Returns the scope associated with the given descriptor.
347 ///
348 DbgScope *getOrCreateScope(GlobalVariable *V);
349
350 /// ConstructDbgScope - Construct the components of a scope.
351 ///
352 void ConstructDbgScope(DbgScope *ParentScope,
353 unsigned ParentStartID, unsigned ParentEndID,
354 DIE *ParentDie, CompileUnit *Unit);
355
356 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
357 ///
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000358 void ConstructFunctionDbgScope(DbgScope *RootScope);
359
360 /// ConstructFunctionDbgScope - Construct the scope for the abstract debug
361 /// scope.
362 ///
363 void ConstructAbstractDbgScope(DbgScope *AbsScope);
Bill Wendling0310d762009-05-15 09:23:25 +0000364
365 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
366 ///
367 void ConstructDefaultDbgScope(MachineFunction *MF);
368
369 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
370 /// tools to recognize the object file contains Dwarf information.
371 void EmitInitial();
372
373 /// EmitDIE - Recusively Emits a debug information entry.
374 ///
375 void EmitDIE(DIE *Die);
376
377 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
378 ///
379 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
380
381 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
382 ///
383 void SizeAndOffsets();
384
385 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
386 ///
387 void EmitDebugInfoPerCU(CompileUnit *Unit);
388
389 void EmitDebugInfo();
390
391 /// EmitAbbreviations - Emit the abbreviation section.
392 ///
393 void EmitAbbreviations() const;
394
395 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
396 /// the line matrix.
397 ///
398 void EmitEndOfLineMatrix(unsigned SectionEnd);
399
400 /// EmitDebugLines - Emit source line information.
401 ///
402 void EmitDebugLines();
403
404 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
405 ///
406 void EmitCommonDebugFrame();
407
408 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
409 /// section.
410 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
411
412 void EmitDebugPubNamesPerCU(CompileUnit *Unit);
413
414 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
415 ///
416 void EmitDebugPubNames();
417
418 /// EmitDebugStr - Emit visible names into a debug str section.
419 ///
420 void EmitDebugStr();
421
422 /// EmitDebugLoc - Emit visible names into a debug loc section.
423 ///
424 void EmitDebugLoc();
425
426 /// EmitDebugARanges - Emit visible names into a debug aranges section.
427 ///
428 void EmitDebugARanges();
429
430 /// EmitDebugRanges - Emit visible names into a debug ranges section.
431 ///
432 void EmitDebugRanges();
433
434 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
435 ///
436 void EmitDebugMacInfo();
437
438 /// EmitDebugInlineInfo - Emit inline info using following format.
439 /// Section Header:
440 /// 1. length of section
441 /// 2. Dwarf version number
442 /// 3. address size.
443 ///
444 /// Entries (one "entry" for each function that was inlined):
445 ///
446 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
447 /// otherwise offset into __debug_str for regular function name.
448 /// 2. offset into __debug_str section for regular function name.
449 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
450 /// instances for the function.
451 ///
452 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
453 /// inlined instance; the die_offset points to the inlined_subroutine die in
454 /// the __debug_info section, and the low_pc is the starting address for the
455 /// inlining instance.
456 void EmitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000457
458 /// GetOrCreateSourceID - Look up the source id with the given directory and
459 /// source file names. If none currently exists, create a new id and insert it
460 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
461 /// as well.
462 unsigned GetOrCreateSourceID(const std::string &DirName,
463 const std::string &FileName);
464
465 void ConstructCompileUnit(GlobalVariable *GV);
466
467 /// ConstructCompileUnits - Create a compile unit DIEs.
468 void ConstructCompileUnits();
469
470 bool ConstructGlobalVariableDIE(GlobalVariable *GV);
471
472 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
473 /// visible global variables. Return true if at least one global DIE is
474 /// created.
475 bool ConstructGlobalVariableDIEs();
476
477 bool ConstructSubprogram(GlobalVariable *GV);
478
479 /// ConstructSubprograms - Create DIEs for each of the externally visible
480 /// subprograms. Return true if at least one subprogram DIE is created.
481 bool ConstructSubprograms();
Bill Wendling0310d762009-05-15 09:23:25 +0000482public:
483 //===--------------------------------------------------------------------===//
484 // Main entry points.
485 //
486 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T);
487 virtual ~DwarfDebug();
488
489 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
490 /// be emitted.
491 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
492
493 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
494 /// This is inovked by the target AsmPrinter.
495 void SetDebugInfo(MachineModuleInfo *mmi);
496
497 /// BeginModule - Emit all Dwarf sections that should come prior to the
498 /// content.
499 void BeginModule(Module *M) {
500 this->M = M;
501 }
502
503 /// EndModule - Emit all Dwarf sections that should come after the content.
504 ///
505 void EndModule();
506
507 /// BeginFunction - Gather pre-function debug information. Assumes being
508 /// emitted immediately after the function entry point.
509 void BeginFunction(MachineFunction *MF);
510
511 /// EndFunction - Gather and emit post-function debug information.
512 ///
513 void EndFunction(MachineFunction *MF);
514
515 /// RecordSourceLine - Records location information and associates it with a
516 /// label. Returns a unique label ID used to generate a label and provide
517 /// correspondence to the source line list.
518 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col);
519
520 /// RecordSourceLine - Records location information and associates it with a
521 /// label. Returns a unique label ID used to generate a label and provide
522 /// correspondence to the source line list.
523 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU);
524
525 /// getRecordSourceLineCount - Return the number of source lines in the debug
526 /// info.
527 unsigned getRecordSourceLineCount() const {
528 return Lines.size();
529 }
530
531 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
532 /// timed. Look up the source id with the given directory and source file
533 /// names. If none currently exists, create a new id and insert it in the
534 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
535 /// well.
536 unsigned getOrCreateSourceID(const std::string &DirName,
537 const std::string &FileName);
538
539 /// RecordRegionStart - Indicate the start of a region.
540 unsigned RecordRegionStart(GlobalVariable *V);
541
542 /// RecordRegionEnd - Indicate the end of a region.
543 unsigned RecordRegionEnd(GlobalVariable *V);
544
545 /// RecordVariable - Indicate the declaration of a local variable.
546 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
547 const MachineInstr *MI);
548
549 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
550 unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
551 unsigned Line, unsigned Col);
552
553 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
554 unsigned RecordInlinedFnEnd(DISubprogram &SP);
555
556 /// RecordVariableScope - Record scope for the variable declared by
557 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE. Record scopes
558 /// for only inlined subroutine variables. Other variables's scopes are
559 /// determined during RecordVariable().
560 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI);
561};
562
563} // End of namespace llvm
564
565#endif