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.
86 DenseMap <const MDNode *, CompileUnit *> CUMap;
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<const MDNode *, DbgScope *> DbgScopeMap;
151 /// ConcreteScopes - Tracks the concrete scopees in the current function.
152 /// These scopes are also included in DbgScopeMap.
153 DenseMap<const 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<const 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<const 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 /// DbgVariableToFrameIndexMap - Tracks frame index used to find
174 /// variable's value.
175 DenseMap<const DbgVariable *, int> DbgVariableToFrameIndexMap;
177 /// DbgVariableToDbgInstMap - Maps DbgVariable to corresponding DBG_VALUE
178 /// machine instruction.
179 DenseMap<const DbgVariable *, const MachineInstr *> DbgVariableToDbgInstMap;
181 /// DbgVariableLabelsMap - Maps DbgVariable to corresponding MCSymbol.
182 DenseMap<const DbgVariable *, const MCSymbol *> DbgVariableLabelsMap;
184 /// VarToAbstractVarMap - Maps DbgVariable with corresponding Abstract
185 /// DbgVariable, if any.
186 DenseMap<const DbgVariable *, const DbgVariable *> VarToAbstractVarMap;
188 /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
189 /// (at the end of the module) as DW_AT_inline.
190 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
192 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
193 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
194 /// corresponds to the MDNode mapped with the subprogram DIE.
195 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
197 typedef SmallVector<DbgScope *, 2> ScopeVector;
198 SmallPtrSet<const MachineInstr *, 8> InsnsBeginScopeSet;
199 SmallPtrSet<const MachineInstr *, 8> InsnsEndScopeSet;
201 /// InlineInfo - Keep track of inlined functions and their location. This
202 /// information is used to populate debug_inlined section.
203 typedef std::pair<MCSymbol *, DIE *> InlineInfoLabels;
204 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
205 SmallVector<const MDNode *, 4> InlinedSPNodes;
207 /// LabelsBeforeInsn - Maps instruction with label emitted before
209 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
211 /// LabelsAfterInsn - Maps instruction with label emitted after
213 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
215 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
217 /// Previous instruction's location information. This is used to determine
218 /// label location to indicate scope boundries in dwarf debug info.
219 DebugLoc PrevInstLoc;
222 struct FunctionDebugFrameInfo {
224 std::vector<MachineMove> Moves;
226 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
227 : Number(Num), Moves(M) {}
230 std::vector<FunctionDebugFrameInfo> DebugFrames;
232 // Section Symbols: these are assembler temporary labels that are emitted at
233 // the beginning of each supported dwarf section. These are used to form
234 // section offsets and are created by EmitSectionLabels.
235 MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
236 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
238 MCSymbol *FunctionBeginSym;
241 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
242 /// maps to the source id. Source id starts at 1.
243 std::pair<unsigned, unsigned>
244 getSourceDirectoryAndFileIds(unsigned SId) const {
245 return SourceIds[SId-1];
248 /// getNumSourceDirectories - Return the number of source directories in the
250 unsigned getNumSourceDirectories() const {
251 return DirectoryNames.size();
254 /// getSourceDirectoryName - Return the name of the directory corresponding
256 const std::string &getSourceDirectoryName(unsigned Id) const {
257 return DirectoryNames[Id - 1];
260 /// getSourceFileName - Return the name of the source file corresponding
262 const std::string &getSourceFileName(unsigned Id) const {
263 return SourceFileNames[Id - 1];
266 /// getNumSourceIds - Return the number of unique source ids.
267 unsigned getNumSourceIds() const {
268 return SourceIds.size();
271 /// assignAbbrevNumber - Define a unique number for the abbreviation.
273 void assignAbbrevNumber(DIEAbbrev &Abbrev);
275 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
276 /// information entry.
277 DIEEntry *createDIEEntry(DIE *Entry);
279 /// addUInt - Add an unsigned integer attribute data and value.
281 void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
283 /// addSInt - Add an signed integer attribute data and value.
285 void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
287 /// addString - Add a string attribute data and value.
289 void addString(DIE *Die, unsigned Attribute, unsigned Form,
290 const StringRef Str);
292 /// addLabel - Add a Dwarf label attribute data and value.
294 void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
295 const MCSymbol *Label);
297 /// addDelta - Add a label delta attribute data and value.
299 void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
300 const MCSymbol *Hi, const MCSymbol *Lo);
302 /// addDIEEntry - Add a DIE attribute data and value.
304 void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
306 /// addBlock - Add block data.
308 void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
310 /// addSourceLine - Add location information to specified debug information
312 void addSourceLine(DIE *Die, const DIVariable *V);
313 void addSourceLine(DIE *Die, const DIGlobalVariable *G);
314 void addSourceLine(DIE *Die, const DISubprogram *SP);
315 void addSourceLine(DIE *Die, const DIType *Ty);
316 void addSourceLine(DIE *Die, const DINameSpace *NS);
318 /// addAddress - Add an address attribute to a die based on the location
320 void addAddress(DIE *Die, unsigned Attribute,
321 const MachineLocation &Location);
323 /// addRegisterAddress - Add register location entry in variable DIE.
324 bool addRegisterAddress(DIE *Die, const MCSymbol *VS, const MachineOperand &MO);
326 /// addConstantValue - Add constant value entry in variable DIE.
327 bool addConstantValue(DIE *Die, const MCSymbol *VS, const MachineOperand &MO);
329 /// addConstantFPValue - Add constant value entry in variable DIE.
330 bool addConstantFPValue(DIE *Die, const MCSymbol *VS, const MachineOperand &MO);
332 /// addComplexAddress - Start with the address based on the location provided,
333 /// and generate the DWARF information necessary to find the actual variable
334 /// (navigating the extra location information encoded in the type) based on
335 /// the starting location. Add the DWARF information to the die.
337 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
338 const MachineLocation &Location);
340 // FIXME: Should be reformulated in terms of addComplexAddress.
341 /// addBlockByrefAddress - Start with the address based on the location
342 /// provided, and generate the DWARF information necessary to find the
343 /// actual Block variable (navigating the Block struct) based on the
344 /// starting location. Add the DWARF information to the die. Obsolete,
345 /// please use addComplexAddress instead.
347 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
348 const MachineLocation &Location);
350 /// addToContextOwner - Add Die into the list of its context owner's children.
351 void addToContextOwner(DIE *Die, DIDescriptor Context);
353 /// addType - Add a new type attribute to the specified entity.
354 void addType(DIE *Entity, DIType Ty);
357 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
358 DIE *getOrCreateNameSpace(DINameSpace NS);
360 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
362 DIE *getOrCreateTypeDIE(DIType Ty);
364 void addPubTypes(DISubprogram SP);
366 /// constructTypeDIE - Construct basic type die from DIBasicType.
367 void constructTypeDIE(DIE &Buffer,
370 /// constructTypeDIE - Construct derived type die from DIDerivedType.
371 void constructTypeDIE(DIE &Buffer,
374 /// constructTypeDIE - Construct type DIE from DICompositeType.
375 void constructTypeDIE(DIE &Buffer,
376 DICompositeType CTy);
378 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
379 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
381 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
382 void constructArrayTypeDIE(DIE &Buffer,
383 DICompositeType *CTy);
385 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
386 DIE *constructEnumTypeDIE(DIEnumerator ETy);
388 /// createGlobalVariableDIE - Create new DIE using GV.
389 DIE *createGlobalVariableDIE(const DIGlobalVariable &GV);
391 /// createMemberDIE - Create new member DIE.
392 DIE *createMemberDIE(const DIDerivedType &DT);
394 /// createSubprogramDIE - Create new DIE using SP.
395 DIE *createSubprogramDIE(const DISubprogram &SP, bool MakeDecl = false);
397 /// getOrCreateDbgScope - Create DbgScope for the scope.
398 DbgScope *getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt);
400 DbgScope *getOrCreateAbstractScope(const MDNode *N);
402 /// findAbstractVariable - Find abstract variable associated with Var.
403 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
405 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
406 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
407 /// If there are global variables in this scope then create and insert
408 /// DIEs for these variables.
409 DIE *updateSubprogramScopeDIE(const MDNode *SPNode);
411 /// constructLexicalScope - Construct new DW_TAG_lexical_block
412 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
413 DIE *constructLexicalScopeDIE(DbgScope *Scope);
415 /// constructInlinedScopeDIE - This scope represents inlined body of
416 /// a function. Construct DIE to represent this concrete inlined copy
418 DIE *constructInlinedScopeDIE(DbgScope *Scope);
420 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
421 DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
423 /// constructScopeDIE - Construct a DIE for this scope.
424 DIE *constructScopeDIE(DbgScope *Scope);
426 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
427 /// the start of each one.
428 void EmitSectionLabels();
430 /// emitDIE - Recusively Emits a debug information entry.
432 void emitDIE(DIE *Die);
434 /// computeSizeAndOffset - Compute the size and offset of a DIE.
436 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
438 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
440 void computeSizeAndOffsets();
442 /// EmitDebugInfo - Emit the debug info section.
444 void emitDebugInfo();
446 /// emitAbbreviations - Emit the abbreviation section.
448 void emitAbbreviations() const;
450 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
453 void emitEndOfLineMatrix(unsigned SectionEnd);
455 /// emitDebugLines - Emit source line information.
457 void emitDebugLines();
459 /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
461 void emitCommonDebugFrame();
463 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
465 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
467 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
469 void emitDebugPubNames();
471 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
473 void emitDebugPubTypes();
475 /// emitDebugStr - Emit visible names into a debug str section.
479 /// emitDebugLoc - Emit visible names into a debug loc section.
483 /// EmitDebugARanges - Emit visible names into a debug aranges section.
485 void EmitDebugARanges();
487 /// emitDebugRanges - Emit visible names into a debug ranges section.
489 void emitDebugRanges();
491 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
493 void emitDebugMacInfo();
495 /// emitDebugInlineInfo - Emit inline info using following format.
497 /// 1. length of section
498 /// 2. Dwarf version number
501 /// Entries (one "entry" for each function that was inlined):
503 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
504 /// otherwise offset into __debug_str for regular function name.
505 /// 2. offset into __debug_str section for regular function name.
506 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
507 /// instances for the function.
509 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
510 /// inlined instance; the die_offset points to the inlined_subroutine die in
511 /// the __debug_info section, and the low_pc is the starting address for the
512 /// inlining instance.
513 void emitDebugInlineInfo();
515 /// GetOrCreateSourceID - Look up the source id with the given directory and
516 /// source file names. If none currently exists, create a new id and insert it
517 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames
519 unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName);
521 /// constructCompileUnit - Create new CompileUnit for the given
522 /// metadata node with tag DW_TAG_compile_unit.
523 void constructCompileUnit(const MDNode *N);
525 /// getCompielUnit - Get CompileUnit DIE.
526 CompileUnit *getCompileUnit(const MDNode *N) const;
528 /// constructGlobalVariableDIE - Construct global variable DIE.
529 void constructGlobalVariableDIE(const MDNode *N);
531 /// construct SubprogramDIE - Construct subprogram DIE.
532 void constructSubprogramDIE(const MDNode *N);
534 // FIXME: This should go away in favor of complex addresses.
535 /// Find the type the programmer originally declared the variable to be
536 /// and return that type. Obsolete, use GetComplexAddrType instead.
538 DIType getBlockByrefType(DIType Ty, std::string Name);
540 /// recordSourceLine - Register a source line with debug info. Returns the
541 /// unique label that was emitted and which provides correspondence to
542 /// the source line list.
543 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope);
545 /// getSourceLineCount - Return the number of source lines in the debug
547 unsigned getSourceLineCount() const {
551 /// recordVariableFrameIndex - Record a variable's index.
552 void recordVariableFrameIndex(const DbgVariable *V, int Index);
554 /// findVariableFrameIndex - Return true if frame index for the variable
555 /// is found. Update FI to hold value of the index.
556 bool findVariableFrameIndex(const DbgVariable *V, int *FI);
558 /// findVariableLabel - Find MCSymbol for the variable.
559 const MCSymbol *findVariableLabel(const DbgVariable *V);
561 /// findDbgScope - Find DbgScope for the debug loc attached with an
563 DbgScope *findDbgScope(const MachineInstr *MI);
565 /// identifyScopeMarkers() - Indentify instructions that are marking
566 /// beginning of or end of a scope.
567 void identifyScopeMarkers();
569 /// extractScopeInformation - Scan machine instructions in this function
570 /// and collect DbgScopes. Return true, if atleast one scope was found.
571 bool extractScopeInformation();
573 /// collectVariableInfo - Populate DbgScope entries with variables' info.
574 void collectVariableInfo(const MachineFunction *);
576 /// collectVariableInfoFromMMITable - Collect variable information from
577 /// side table maintained by MMI.
578 void collectVariableInfoFromMMITable(const MachineFunction * MF,
579 SmallPtrSet<const MDNode *, 16> &P);
581 //===--------------------------------------------------------------------===//
582 // Main entry points.
584 DwarfDebug(AsmPrinter *A, Module *M);
587 /// beginModule - Emit all Dwarf sections that should come prior to the
589 void beginModule(Module *M);
591 /// endModule - Emit all Dwarf sections that should come after the content.
595 /// beginFunction - Gather pre-function debug information. Assumes being
596 /// emitted immediately after the function entry point.
597 void beginFunction(const MachineFunction *MF);
599 /// endFunction - Gather and emit post-function debug information.
601 void endFunction(const MachineFunction *MF);
603 /// beginScope - Process beginning of a scope.
604 void beginScope(const MachineInstr *MI);
606 /// endScope - Prcess end of a scope.
607 void endScope(const MachineInstr *MI);
609 } // End of namespace llvm