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;
45 class DIGlobalVariable;
52 class DICompositeType;
54 //===----------------------------------------------------------------------===//
55 /// SrcLineInfo - This class is used to record source line correspondence.
58 unsigned Line; // Source line number.
59 unsigned Column; // Source column.
60 unsigned SourceID; // Source ID number.
61 MCSymbol *Label; // Label in code ID number.
63 SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
64 : Line(L), Column(C), SourceID(S), Label(label) {}
67 unsigned getLine() const { return Line; }
68 unsigned getColumn() const { return Column; }
69 unsigned getSourceID() const { return SourceID; }
70 MCSymbol *getLabel() const { return Label; }
74 /// Asm - Target of Dwarf emission.
77 /// MMI - Collected machine module information.
78 MachineModuleInfo *MMI;
80 //===--------------------------------------------------------------------===//
81 // Attributes used to construct specific Dwarf sections.
84 /// ModuleCU - All DIEs are inserted in ModuleCU.
85 CompileUnit *ModuleCU;
87 /// AbbreviationsSet - Used to uniquely define abbreviations.
89 FoldingSet<DIEAbbrev> AbbreviationsSet;
91 /// Abbreviations - A list of all the unique abbreviations in use.
93 std::vector<DIEAbbrev *> Abbreviations;
95 /// DirectoryIdMap - Directory name to directory id map.
97 StringMap<unsigned> DirectoryIdMap;
99 /// DirectoryNames - A list of directory names.
100 SmallVector<std::string, 8> DirectoryNames;
102 /// SourceFileIdMap - Source file name to source file id map.
104 StringMap<unsigned> SourceFileIdMap;
106 /// SourceFileNames - A list of source file names.
107 SmallVector<std::string, 8> SourceFileNames;
109 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
110 /// id mapped to a unique id.
111 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
113 /// SourceIds - Reverse map from source id to directory id + file id pair.
115 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
117 /// Lines - List of source line correspondence.
118 std::vector<SrcLineInfo> Lines;
120 /// DIEBlocks - A list of all the DIEBlocks in use.
121 std::vector<DIEBlock *> DIEBlocks;
123 // DIEValueAllocator - All DIEValues are allocated through this allocator.
124 BumpPtrAllocator DIEValueAllocator;
126 /// StringPool - A String->Symbol mapping of strings used by indirect
128 StringMap<std::pair<MCSymbol*, unsigned> > StringPool;
129 unsigned NextStringPoolNumber;
131 MCSymbol *getStringPoolEntry(StringRef Str);
133 /// SectionMap - Provides a unique id per text section.
135 UniqueVector<const MCSection*> SectionMap;
137 /// SectionSourceLines - Tracks line numbers per text section.
139 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
141 // CurrentFnDbgScope - Top level scope for the current function.
143 DbgScope *CurrentFnDbgScope;
145 /// DbgScopeMap - Tracks the scopes in the current function. Owns the
146 /// contained DbgScope*s.
148 DenseMap<MDNode *, DbgScope *> DbgScopeMap;
150 /// ConcreteScopes - Tracks the concrete scopees in the current function.
151 /// These scopes are also included in DbgScopeMap.
152 DenseMap<MDNode *, DbgScope *> ConcreteScopes;
154 /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
155 /// not included DbgScopeMap. AbstractScopes owns its DbgScope*s.
156 DenseMap<MDNode *, DbgScope *> AbstractScopes;
158 /// AbstractScopesList - Tracks abstract scopes constructed while processing
159 /// a function. This list is cleared during endFunction().
160 SmallVector<DbgScope *, 4>AbstractScopesList;
162 /// AbstractVariables - Collection on abstract variables. Owned by the
163 /// DbgScopes in AbstractScopes.
164 DenseMap<MDNode *, DbgVariable *> AbstractVariables;
166 /// DbgValueStartMap - Tracks starting scope of variable DIEs.
167 /// If the scope of an object begins sometime after the low pc value for the
168 /// scope most closely enclosing the object, the object entry may have a
169 /// DW_AT_start_scope attribute.
170 DenseMap<const MachineInstr *, DbgVariable *> DbgValueStartMap;
172 /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
173 /// (at the end of the module) as DW_AT_inline.
174 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
176 DenseMap<DIE *, MDNode *> ContainingTypeMap;
178 /// AbstractSubprogramDIEs - Collection of abstruct subprogram DIEs.
179 SmallPtrSet<DIE *, 4> AbstractSubprogramDIEs;
181 /// TopLevelDIEs - Collection of top level DIEs.
182 SmallPtrSet<DIE *, 4> TopLevelDIEs;
183 SmallVector<DIE *, 4> TopLevelDIEsVector;
185 typedef SmallVector<DbgScope *, 2> ScopeVector;
186 typedef DenseMap<const MachineInstr *, ScopeVector>
189 /// DbgScopeBeginMap - Maps instruction with a list of DbgScopes it starts.
190 InsnToDbgScopeMapTy DbgScopeBeginMap;
192 /// DbgScopeEndMap - Maps instruction with a list DbgScopes it ends.
193 InsnToDbgScopeMapTy DbgScopeEndMap;
195 /// InlineInfo - Keep track of inlined functions and their location. This
196 /// information is used to populate debug_inlined section.
197 typedef std::pair<MCSymbol*, DIE *> InlineInfoLabels;
198 DenseMap<MDNode*, SmallVector<InlineInfoLabels, 4> > InlineInfo;
199 SmallVector<MDNode *, 4> InlinedSPNodes;
201 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
202 /// used when calculating the "origin" of a concrete instance of an inlined
204 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
206 /// Previous instruction's location information. This is used to determine
207 /// label location to indicate scope boundries in dwarf debug info.
208 DebugLoc PrevInstLoc;
210 struct FunctionDebugFrameInfo {
212 std::vector<MachineMove> Moves;
214 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
215 : Number(Num), Moves(M) {}
218 std::vector<FunctionDebugFrameInfo> DebugFrames;
220 // Section Symbols: these are assembler temporary labels that are emitted at
221 // the beginning of each supported dwarf section. These are used to form
222 // section offsets and are created by EmitSectionLabels.
223 MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
224 MCSymbol *DwarfStrSectionSym, *TextSectionSym;
228 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
229 /// maps to the source id. Source id starts at 1.
230 std::pair<unsigned, unsigned>
231 getSourceDirectoryAndFileIds(unsigned SId) const {
232 return SourceIds[SId-1];
235 /// getNumSourceDirectories - Return the number of source directories in the
237 unsigned getNumSourceDirectories() const {
238 return DirectoryNames.size();
241 /// getSourceDirectoryName - Return the name of the directory corresponding
243 const std::string &getSourceDirectoryName(unsigned Id) const {
244 return DirectoryNames[Id - 1];
247 /// getSourceFileName - Return the name of the source file corresponding
249 const std::string &getSourceFileName(unsigned Id) const {
250 return SourceFileNames[Id - 1];
253 /// getNumSourceIds - Return the number of unique source ids.
254 unsigned getNumSourceIds() const {
255 return SourceIds.size();
258 /// assignAbbrevNumber - Define a unique number for the abbreviation.
260 void assignAbbrevNumber(DIEAbbrev &Abbrev);
262 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
263 /// information entry.
264 DIEEntry *createDIEEntry(DIE *Entry);
266 /// addUInt - Add an unsigned integer attribute data and value.
268 void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
270 /// addSInt - Add an signed integer attribute data and value.
272 void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
274 /// addString - Add a string attribute data and value.
276 void addString(DIE *Die, unsigned Attribute, unsigned Form,
277 const StringRef Str);
279 /// addLabel - Add a Dwarf label attribute data and value.
281 void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
282 const MCSymbol *Label);
284 /// addDelta - Add a label delta attribute data and value.
286 void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
287 const MCSymbol *Hi, const MCSymbol *Lo);
289 /// addDIEEntry - Add a DIE attribute data and value.
291 void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
293 /// addBlock - Add block data.
295 void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
297 /// addSourceLine - Add location information to specified debug information
299 void addSourceLine(DIE *Die, const DIVariable *V);
300 void addSourceLine(DIE *Die, const DIGlobal *G);
301 void addSourceLine(DIE *Die, const DISubprogram *SP);
302 void addSourceLine(DIE *Die, const DIType *Ty);
303 void addSourceLine(DIE *Die, const DINameSpace *NS);
305 /// addAddress - Add an address attribute to a die based on the location
307 void addAddress(DIE *Die, unsigned Attribute,
308 const MachineLocation &Location);
310 /// addComplexAddress - Start with the address based on the location provided,
311 /// and generate the DWARF information necessary to find the actual variable
312 /// (navigating the extra location information encoded in the type) based on
313 /// the starting location. Add the DWARF information to the die.
315 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
316 const MachineLocation &Location);
318 // FIXME: Should be reformulated in terms of addComplexAddress.
319 /// addBlockByrefAddress - Start with the address based on the location
320 /// provided, and generate the DWARF information necessary to find the
321 /// actual Block variable (navigating the Block struct) based on the
322 /// starting location. Add the DWARF information to the die. Obsolete,
323 /// please use addComplexAddress instead.
325 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
326 const MachineLocation &Location);
328 /// addToContextOwner - Add Die into the list of its context owner's children.
329 void addToContextOwner(DIE *Die, DIDescriptor Context);
331 /// addType - Add a new type attribute to the specified entity.
332 void addType(DIE *Entity, DIType Ty);
335 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
336 DIE *getOrCreateNameSpace(DINameSpace NS);
338 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
340 DIE *getOrCreateTypeDIE(DIType Ty);
342 void addPubTypes(DISubprogram SP);
344 /// constructTypeDIE - Construct basic type die from DIBasicType.
345 void constructTypeDIE(DIE &Buffer,
348 /// constructTypeDIE - Construct derived type die from DIDerivedType.
349 void constructTypeDIE(DIE &Buffer,
352 /// constructTypeDIE - Construct type DIE from DICompositeType.
353 void constructTypeDIE(DIE &Buffer,
354 DICompositeType CTy);
356 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
357 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
359 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
360 void constructArrayTypeDIE(DIE &Buffer,
361 DICompositeType *CTy);
363 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
364 DIE *constructEnumTypeDIE(DIEnumerator ETy);
366 /// createGlobalVariableDIE - Create new DIE using GV.
367 DIE *createGlobalVariableDIE(const DIGlobalVariable &GV);
369 /// createMemberDIE - Create new member DIE.
370 DIE *createMemberDIE(const DIDerivedType &DT);
372 /// createSubprogramDIE - Create new DIE using SP.
373 DIE *createSubprogramDIE(const DISubprogram &SP, bool MakeDecl = false);
375 /// getUpdatedDbgScope - Find or create DbgScope assicated with
376 /// the instruction. Initialize scope and update scope hierarchy.
377 DbgScope *getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
380 /// createDbgScope - Create DbgScope for the scope.
381 void createDbgScope(MDNode *Scope, MDNode *InlinedAt);
383 DbgScope *getOrCreateAbstractScope(MDNode *N);
385 /// findAbstractVariable - Find abstract variable associated with Var.
386 DbgVariable *findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
388 DbgVariable *findAbstractVariable(DIVariable &Var, const MachineInstr *MI,
391 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
392 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
393 /// If there are global variables in this scope then create and insert
394 /// DIEs for these variables.
395 DIE *updateSubprogramScopeDIE(MDNode *SPNode);
397 /// constructLexicalScope - Construct new DW_TAG_lexical_block
398 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
399 DIE *constructLexicalScopeDIE(DbgScope *Scope);
401 /// constructInlinedScopeDIE - This scope represents inlined body of
402 /// a function. Construct DIE to represent this concrete inlined copy
404 DIE *constructInlinedScopeDIE(DbgScope *Scope);
406 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
407 DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
409 /// constructScopeDIE - Construct a DIE for this scope.
410 DIE *constructScopeDIE(DbgScope *Scope);
412 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
413 /// the start of each one.
414 void EmitSectionLabels();
416 /// emitDIE - Recusively Emits a debug information entry.
418 void emitDIE(DIE *Die);
420 /// computeSizeAndOffset - Compute the size and offset of a DIE.
422 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
424 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
426 void computeSizeAndOffsets();
428 /// EmitDebugInfo - Emit the debug info section.
430 void emitDebugInfo();
432 /// emitAbbreviations - Emit the abbreviation section.
434 void emitAbbreviations() const;
436 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
439 void emitEndOfLineMatrix(unsigned SectionEnd);
441 /// emitDebugLines - Emit source line information.
443 void emitDebugLines();
445 /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
447 void emitCommonDebugFrame();
449 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
451 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
453 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
455 void emitDebugPubNames();
457 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
459 void emitDebugPubTypes();
461 /// emitDebugStr - Emit visible names into a debug str section.
465 /// emitDebugLoc - Emit visible names into a debug loc section.
469 /// EmitDebugARanges - Emit visible names into a debug aranges section.
471 void EmitDebugARanges();
473 /// emitDebugRanges - Emit visible names into a debug ranges section.
475 void emitDebugRanges();
477 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
479 void emitDebugMacInfo();
481 /// emitDebugInlineInfo - Emit inline info using following format.
483 /// 1. length of section
484 /// 2. Dwarf version number
487 /// Entries (one "entry" for each function that was inlined):
489 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
490 /// otherwise offset into __debug_str for regular function name.
491 /// 2. offset into __debug_str section for regular function name.
492 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
493 /// instances for the function.
495 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
496 /// inlined instance; the die_offset points to the inlined_subroutine die in
497 /// the __debug_info section, and the low_pc is the starting address for the
498 /// inlining instance.
499 void emitDebugInlineInfo();
501 /// GetOrCreateSourceID - Look up the source id with the given directory and
502 /// source file names. If none currently exists, create a new id and insert it
503 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames
505 unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName);
507 void constructCompileUnit(MDNode *N);
509 void constructGlobalVariableDIE(MDNode *N);
511 void constructSubprogramDIE(MDNode *N);
513 // FIXME: This should go away in favor of complex addresses.
514 /// Find the type the programmer originally declared the variable to be
515 /// and return that type. Obsolete, use GetComplexAddrType instead.
517 DIType getBlockByrefType(DIType Ty, std::string Name);
519 /// recordSourceLine - Register a source line with debug info. Returns the
520 /// unique label that was emitted and which provides correspondence to
521 /// the source line list.
522 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, MDNode *Scope);
524 /// getSourceLineCount - Return the number of source lines in the debug
526 unsigned getSourceLineCount() const {
530 /// extractScopeInformation - Scan machine instructions in this function
531 /// and collect DbgScopes. Return true, if atleast one scope was found.
532 bool extractScopeInformation();
534 /// collectVariableInfo - Populate DbgScope entries with variables' info.
535 void collectVariableInfo();
538 //===--------------------------------------------------------------------===//
539 // Main entry points.
541 DwarfDebug(AsmPrinter *A, Module *M);
544 /// beginModule - Emit all Dwarf sections that should come prior to the
546 void beginModule(Module *M);
548 /// endModule - Emit all Dwarf sections that should come after the content.
552 /// beginFunction - Gather pre-function debug information. Assumes being
553 /// emitted immediately after the function entry point.
554 void beginFunction(const MachineFunction *MF);
556 /// endFunction - Gather and emit post-function debug information.
558 void endFunction(const MachineFunction *MF);
560 /// beginScope - Process beginning of a scope.
561 void beginScope(const MachineInstr *MI);
563 /// endScope - Prcess end of a scope.
564 void endScope(const MachineInstr *MI);
566 } // End of namespace llvm