1 //===-- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework ------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf debug info into asm files.
12 //===----------------------------------------------------------------------===//
14 #ifndef CODEGEN_ASMPRINTER_DWARFDEBUG_H__
15 #define CODEGEN_ASMPRINTER_DWARFDEBUG_H__
17 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/UniqueVector.h"
24 #include "llvm/Support/Allocator.h"
29 class DbgConcreteScope;
32 class MachineFrameInfo;
33 class MachineLocation;
34 class MachineModuleInfo;
46 class DIGlobalVariable;
53 class DICompositeType;
55 //===----------------------------------------------------------------------===//
56 /// SrcLineInfo - This class is used to record source line correspondence.
59 unsigned Line; // Source line number.
60 unsigned Column; // Source column.
61 unsigned SourceID; // Source ID number.
62 MCSymbol *Label; // Label in code ID number.
64 SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
65 : Line(L), Column(C), SourceID(S), Label(label) {}
68 unsigned getLine() const { return Line; }
69 unsigned getColumn() const { return Column; }
70 unsigned getSourceID() const { return SourceID; }
71 MCSymbol *getLabel() const { return Label; }
75 /// Asm - Target of Dwarf emission.
78 /// MMI - Collected machine module information.
79 MachineModuleInfo *MMI;
81 //===--------------------------------------------------------------------===//
82 // Attributes used to construct specific Dwarf sections.
85 /// ModuleCU - All DIEs are inserted in ModuleCU.
86 CompileUnit *ModuleCU;
88 /// AbbreviationsSet - Used to uniquely define abbreviations.
90 FoldingSet<DIEAbbrev> AbbreviationsSet;
92 /// Abbreviations - A list of all the unique abbreviations in use.
94 std::vector<DIEAbbrev *> Abbreviations;
96 /// DirectoryIdMap - Directory name to directory id map.
98 StringMap<unsigned> DirectoryIdMap;
100 /// DirectoryNames - A list of directory names.
101 SmallVector<std::string, 8> DirectoryNames;
103 /// SourceFileIdMap - Source file name to source file id map.
105 StringMap<unsigned> SourceFileIdMap;
107 /// SourceFileNames - A list of source file names.
108 SmallVector<std::string, 8> SourceFileNames;
110 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
111 /// id mapped to a unique id.
112 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
114 /// SourceIds - Reverse map from source id to directory id + file id pair.
116 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
118 /// Lines - List of source line correspondence.
119 std::vector<SrcLineInfo> Lines;
121 /// DIEBlocks - A list of all the DIEBlocks in use.
122 std::vector<DIEBlock *> DIEBlocks;
124 // DIEValueAllocator - All DIEValues are allocated through this allocator.
125 BumpPtrAllocator DIEValueAllocator;
127 /// StringPool - A String->Symbol mapping of strings used by indirect
129 StringMap<std::pair<MCSymbol*, unsigned> > StringPool;
130 unsigned NextStringPoolNumber;
132 MCSymbol *getStringPoolEntry(StringRef Str);
134 /// SectionMap - Provides a unique id per text section.
136 UniqueVector<const MCSection*> SectionMap;
138 /// SectionSourceLines - Tracks line numbers per text section.
140 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
142 // CurrentFnDbgScope - Top level scope for the current function.
144 DbgScope *CurrentFnDbgScope;
146 /// DbgScopeMap - Tracks the scopes in the current function. Owns the
147 /// contained DbgScope*s.
149 DenseMap<MDNode *, DbgScope *> DbgScopeMap;
151 /// ConcreteScopes - Tracks the concrete scopees in the current function.
152 /// These scopes are also included in DbgScopeMap.
153 DenseMap<MDNode *, DbgScope *> ConcreteScopes;
155 /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
156 /// not included DbgScopeMap. AbstractScopes owns its DbgScope*s.
157 DenseMap<MDNode *, DbgScope *> AbstractScopes;
159 /// AbstractScopesList - Tracks abstract scopes constructed while processing
160 /// a function. This list is cleared during endFunction().
161 SmallVector<DbgScope *, 4>AbstractScopesList;
163 /// AbstractVariables - Collection on abstract variables. Owned by the
164 /// DbgScopes in AbstractScopes.
165 DenseMap<MDNode *, DbgVariable *> AbstractVariables;
167 /// DbgValueStartMap - Tracks starting scope of variable DIEs.
168 /// If the scope of an object begins sometime after the low pc value for the
169 /// scope most closely enclosing the object, the object entry may have a
170 /// DW_AT_start_scope attribute.
171 DenseMap<const MachineInstr *, DbgVariable *> DbgValueStartMap;
173 /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
174 /// (at the end of the module) as DW_AT_inline.
175 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
177 DenseMap<DIE *, MDNode *> ContainingTypeMap;
179 /// AbstractSubprogramDIEs - Collection of abstruct subprogram DIEs.
180 SmallPtrSet<DIE *, 4> AbstractSubprogramDIEs;
182 /// TopLevelDIEs - Collection of top level DIEs.
183 SmallPtrSet<DIE *, 4> TopLevelDIEs;
184 SmallVector<DIE *, 4> TopLevelDIEsVector;
186 typedef SmallVector<DbgScope *, 2> ScopeVector;
187 typedef DenseMap<const MachineInstr *, ScopeVector>
190 /// DbgScopeBeginMap - Maps instruction with a list of DbgScopes it starts.
191 InsnToDbgScopeMapTy DbgScopeBeginMap;
193 /// DbgScopeEndMap - Maps instruction with a list DbgScopes it ends.
194 InsnToDbgScopeMapTy DbgScopeEndMap;
196 /// InlineInfo - Keep track of inlined functions and their location. This
197 /// information is used to populate debug_inlined section.
198 typedef std::pair<MCSymbol*, DIE *> InlineInfoLabels;
199 DenseMap<MDNode*, SmallVector<InlineInfoLabels, 4> > InlineInfo;
200 SmallVector<MDNode *, 4> InlinedSPNodes;
202 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
203 /// used when calculating the "origin" of a concrete instance of an inlined
205 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
207 /// Previous instruction's location information. This is used to determine
208 /// label location to indicate scope boundries in dwarf debug info.
209 DebugLoc PrevInstLoc;
211 /// DebugTimer - Timer for the Dwarf debug writer.
214 struct FunctionDebugFrameInfo {
216 std::vector<MachineMove> Moves;
218 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
219 : Number(Num), Moves(M) {}
222 std::vector<FunctionDebugFrameInfo> DebugFrames;
224 // Section Symbols: these are assembler temporary labels that are emitted at
225 // the beginning of each supported dwarf section. These are used to form
226 // section offsets and are created by EmitSectionLabels.
227 MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
228 MCSymbol *DwarfStrSectionSym, *TextSectionSym;
232 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
233 /// maps to the source id. Source id starts at 1.
234 std::pair<unsigned, unsigned>
235 getSourceDirectoryAndFileIds(unsigned SId) const {
236 return SourceIds[SId-1];
239 /// getNumSourceDirectories - Return the number of source directories in the
241 unsigned getNumSourceDirectories() const {
242 return DirectoryNames.size();
245 /// getSourceDirectoryName - Return the name of the directory corresponding
247 const std::string &getSourceDirectoryName(unsigned Id) const {
248 return DirectoryNames[Id - 1];
251 /// getSourceFileName - Return the name of the source file corresponding
253 const std::string &getSourceFileName(unsigned Id) const {
254 return SourceFileNames[Id - 1];
257 /// getNumSourceIds - Return the number of unique source ids.
258 unsigned getNumSourceIds() const {
259 return SourceIds.size();
262 /// assignAbbrevNumber - Define a unique number for the abbreviation.
264 void assignAbbrevNumber(DIEAbbrev &Abbrev);
266 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
267 /// information entry.
268 DIEEntry *createDIEEntry(DIE *Entry);
270 /// addUInt - Add an unsigned integer attribute data and value.
272 void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
274 /// addSInt - Add an signed integer attribute data and value.
276 void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
278 /// addString - Add a string attribute data and value.
280 void addString(DIE *Die, unsigned Attribute, unsigned Form,
281 const StringRef Str);
283 /// addLabel - Add a Dwarf label attribute data and value.
285 void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
286 const MCSymbol *Label);
288 /// addDelta - Add a label delta attribute data and value.
290 void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
291 const MCSymbol *Hi, const MCSymbol *Lo);
293 /// addDIEEntry - Add a DIE attribute data and value.
295 void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
297 /// addBlock - Add block data.
299 void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
301 /// addSourceLine - Add location information to specified debug information
303 void addSourceLine(DIE *Die, const DIVariable *V);
304 void addSourceLine(DIE *Die, const DIGlobal *G);
305 void addSourceLine(DIE *Die, const DISubprogram *SP);
306 void addSourceLine(DIE *Die, const DIType *Ty);
307 void addSourceLine(DIE *Die, const DINameSpace *NS);
309 /// addAddress - Add an address attribute to a die based on the location
311 void addAddress(DIE *Die, unsigned Attribute,
312 const MachineLocation &Location);
314 /// addComplexAddress - Start with the address based on the location provided,
315 /// and generate the DWARF information necessary to find the actual variable
316 /// (navigating the extra location information encoded in the type) based on
317 /// the starting location. Add the DWARF information to the die.
319 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
320 const MachineLocation &Location);
322 // FIXME: Should be reformulated in terms of addComplexAddress.
323 /// addBlockByrefAddress - Start with the address based on the location
324 /// provided, and generate the DWARF information necessary to find the
325 /// actual Block variable (navigating the Block struct) based on the
326 /// starting location. Add the DWARF information to the die. Obsolete,
327 /// please use addComplexAddress instead.
329 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
330 const MachineLocation &Location);
332 /// addToContextOwner - Add Die into the list of its context owner's children.
333 void addToContextOwner(DIE *Die, DIDescriptor Context);
335 /// addType - Add a new type attribute to the specified entity.
336 void addType(DIE *Entity, DIType Ty);
339 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
340 DIE *getOrCreateNameSpace(DINameSpace NS);
342 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
344 DIE *getOrCreateTypeDIE(DIType Ty);
346 void addPubTypes(DISubprogram SP);
348 /// constructTypeDIE - Construct basic type die from DIBasicType.
349 void constructTypeDIE(DIE &Buffer,
352 /// constructTypeDIE - Construct derived type die from DIDerivedType.
353 void constructTypeDIE(DIE &Buffer,
356 /// constructTypeDIE - Construct type DIE from DICompositeType.
357 void constructTypeDIE(DIE &Buffer,
358 DICompositeType CTy);
360 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
361 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
363 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
364 void constructArrayTypeDIE(DIE &Buffer,
365 DICompositeType *CTy);
367 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
368 DIE *constructEnumTypeDIE(DIEnumerator ETy);
370 /// createGlobalVariableDIE - Create new DIE using GV.
371 DIE *createGlobalVariableDIE(const DIGlobalVariable &GV);
373 /// createMemberDIE - Create new member DIE.
374 DIE *createMemberDIE(const DIDerivedType &DT);
376 /// createSubprogramDIE - Create new DIE using SP.
377 DIE *createSubprogramDIE(const DISubprogram &SP, bool MakeDecl = false);
379 /// getUpdatedDbgScope - Find or create DbgScope assicated with
380 /// the instruction. Initialize scope and update scope hierarchy.
381 DbgScope *getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
384 /// createDbgScope - Create DbgScope for the scope.
385 void createDbgScope(MDNode *Scope, MDNode *InlinedAt);
387 DbgScope *getOrCreateAbstractScope(MDNode *N);
389 /// findAbstractVariable - Find abstract variable associated with Var.
390 DbgVariable *findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
392 DbgVariable *findAbstractVariable(DIVariable &Var, const MachineInstr *MI,
395 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
396 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
397 /// If there are global variables in this scope then create and insert
398 /// DIEs for these variables.
399 DIE *updateSubprogramScopeDIE(MDNode *SPNode);
401 /// constructLexicalScope - Construct new DW_TAG_lexical_block
402 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
403 DIE *constructLexicalScopeDIE(DbgScope *Scope);
405 /// constructInlinedScopeDIE - This scope represents inlined body of
406 /// a function. Construct DIE to represent this concrete inlined copy
408 DIE *constructInlinedScopeDIE(DbgScope *Scope);
410 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
411 DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
413 /// constructScopeDIE - Construct a DIE for this scope.
414 DIE *constructScopeDIE(DbgScope *Scope);
416 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
417 /// the start of each one.
418 void EmitSectionLabels();
420 /// emitDIE - Recusively Emits a debug information entry.
422 void emitDIE(DIE *Die);
424 /// computeSizeAndOffset - Compute the size and offset of a DIE.
426 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
428 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
430 void computeSizeAndOffsets();
432 /// EmitDebugInfo - Emit the debug info section.
434 void emitDebugInfo();
436 /// emitAbbreviations - Emit the abbreviation section.
438 void emitAbbreviations() const;
440 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
443 void emitEndOfLineMatrix(unsigned SectionEnd);
445 /// emitDebugLines - Emit source line information.
447 void emitDebugLines();
449 /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
451 void emitCommonDebugFrame();
453 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
455 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
457 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
459 void emitDebugPubNames();
461 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
463 void emitDebugPubTypes();
465 /// emitDebugStr - Emit visible names into a debug str section.
469 /// emitDebugLoc - Emit visible names into a debug loc section.
473 /// EmitDebugARanges - Emit visible names into a debug aranges section.
475 void EmitDebugARanges();
477 /// emitDebugRanges - Emit visible names into a debug ranges section.
479 void emitDebugRanges();
481 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
483 void emitDebugMacInfo();
485 /// emitDebugInlineInfo - Emit inline info using following format.
487 /// 1. length of section
488 /// 2. Dwarf version number
491 /// Entries (one "entry" for each function that was inlined):
493 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
494 /// otherwise offset into __debug_str for regular function name.
495 /// 2. offset into __debug_str section for regular function name.
496 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
497 /// instances for the function.
499 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
500 /// inlined instance; the die_offset points to the inlined_subroutine die in
501 /// the __debug_info section, and the low_pc is the starting address for the
502 /// inlining instance.
503 void emitDebugInlineInfo();
505 /// GetOrCreateSourceID - Look up the source id with the given directory and
506 /// source file names. If none currently exists, create a new id and insert it
507 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames
509 unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName);
511 void constructCompileUnit(MDNode *N);
513 void constructGlobalVariableDIE(MDNode *N);
515 void constructSubprogramDIE(MDNode *N);
517 // FIXME: This should go away in favor of complex addresses.
518 /// Find the type the programmer originally declared the variable to be
519 /// and return that type. Obsolete, use GetComplexAddrType instead.
521 DIType getBlockByrefType(DIType Ty, std::string Name);
523 /// recordSourceLine - Register a source line with debug info. Returns the
524 /// unique label that was emitted and which provides correspondence to
525 /// the source line list.
526 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, MDNode *Scope);
528 /// getSourceLineCount - Return the number of source lines in the debug
530 unsigned getSourceLineCount() const {
534 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
535 /// timed. Look up the source id with the given directory and source file
536 /// names. If none currently exists, create a new id and insert it in the
537 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
539 unsigned getOrCreateSourceID(const std::string &DirName,
540 const std::string &FileName);
542 /// extractScopeInformation - Scan machine instructions in this function
543 /// and collect DbgScopes. Return true, if atleast one scope was found.
544 bool extractScopeInformation();
546 /// collectVariableInfo - Populate DbgScope entries with variables' info.
547 void collectVariableInfo();
550 //===--------------------------------------------------------------------===//
551 // Main entry points.
553 DwarfDebug(AsmPrinter *A, Module *M);
556 /// beginModule - Emit all Dwarf sections that should come prior to the
558 void beginModule(Module *M);
560 /// endModule - Emit all Dwarf sections that should come after the content.
564 /// beginFunction - Gather pre-function debug information. Assumes being
565 /// emitted immediately after the function entry point.
566 void beginFunction(const MachineFunction *MF);
568 /// endFunction - Gather and emit post-function debug information.
570 void endFunction(const MachineFunction *MF);
572 /// beginScope - Process beginning of a scope.
573 void beginScope(const MachineInstr *MI);
575 /// endScope - Prcess end of a scope.
576 void endScope(const MachineInstr *MI);
578 } // End of namespace llvm