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<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 /// 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 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
178 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
179 /// corresponds to the MDNode mapped with the subprogram DIE.
180 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
182 typedef SmallVector<DbgScope *, 2> ScopeVector;
183 SmallPtrSet<const MachineInstr *, 8> InsnsBeginScopeSet;
184 SmallPtrSet<const MachineInstr *, 8> InsnsEndScopeSet;
186 /// InlineInfo - Keep track of inlined functions and their location. This
187 /// information is used to populate debug_inlined section.
188 typedef std::pair<MCSymbol *, DIE *> InlineInfoLabels;
189 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
190 SmallVector<const MDNode *, 4> InlinedSPNodes;
192 /// LabelsBeforeInsn - Maps instruction with label emitted before
194 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
196 /// LabelsAfterInsn - Maps instruction with label emitted after
198 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
200 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
202 /// Previous instruction's location information. This is used to determine
203 /// label location to indicate scope boundries in dwarf debug info.
204 DebugLoc PrevInstLoc;
207 struct FunctionDebugFrameInfo {
209 std::vector<MachineMove> Moves;
211 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
212 : Number(Num), Moves(M) {}
215 std::vector<FunctionDebugFrameInfo> DebugFrames;
217 // Section Symbols: these are assembler temporary labels that are emitted at
218 // the beginning of each supported dwarf section. These are used to form
219 // section offsets and are created by EmitSectionLabels.
220 MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
221 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
223 MCSymbol *FunctionBeginSym;
226 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
227 /// maps to the source id. Source id starts at 1.
228 std::pair<unsigned, unsigned>
229 getSourceDirectoryAndFileIds(unsigned SId) const {
230 return SourceIds[SId-1];
233 /// getNumSourceDirectories - Return the number of source directories in the
235 unsigned getNumSourceDirectories() const {
236 return DirectoryNames.size();
239 /// getSourceDirectoryName - Return the name of the directory corresponding
241 const std::string &getSourceDirectoryName(unsigned Id) const {
242 return DirectoryNames[Id - 1];
245 /// getSourceFileName - Return the name of the source file corresponding
247 const std::string &getSourceFileName(unsigned Id) const {
248 return SourceFileNames[Id - 1];
251 /// getNumSourceIds - Return the number of unique source ids.
252 unsigned getNumSourceIds() const {
253 return SourceIds.size();
256 /// assignAbbrevNumber - Define a unique number for the abbreviation.
258 void assignAbbrevNumber(DIEAbbrev &Abbrev);
260 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
261 /// information entry.
262 DIEEntry *createDIEEntry(DIE *Entry);
264 /// addUInt - Add an unsigned integer attribute data and value.
266 void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
268 /// addSInt - Add an signed integer attribute data and value.
270 void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
272 /// addString - Add a string attribute data and value.
274 void addString(DIE *Die, unsigned Attribute, unsigned Form,
275 const StringRef Str);
277 /// addLabel - Add a Dwarf label attribute data and value.
279 void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
280 const MCSymbol *Label);
282 /// addDelta - Add a label delta attribute data and value.
284 void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
285 const MCSymbol *Hi, const MCSymbol *Lo);
287 /// addDIEEntry - Add a DIE attribute data and value.
289 void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
291 /// addBlock - Add block data.
293 void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
295 /// addSourceLine - Add location information to specified debug information
297 void addSourceLine(DIE *Die, const DIVariable *V);
298 void addSourceLine(DIE *Die, const DIGlobal *G);
299 void addSourceLine(DIE *Die, const DISubprogram *SP);
300 void addSourceLine(DIE *Die, const DIType *Ty);
301 void addSourceLine(DIE *Die, const DINameSpace *NS);
303 /// addAddress - Add an address attribute to a die based on the location
305 void addAddress(DIE *Die, unsigned Attribute,
306 const MachineLocation &Location);
308 /// addRegisterAddress - Add register location entry in variable DIE.
309 bool addRegisterAddress(DIE *Die, DbgVariable *DV, const MachineOperand &MO);
311 /// addConstantValue - Add constant value entry in variable DIE.
312 bool addConstantValue(DIE *Die, DbgVariable *DV, const MachineOperand &MO);
314 /// addConstantFPValue - Add constant value entry in variable DIE.
315 bool addConstantFPValue(DIE *Die, DbgVariable *DV, const MachineOperand &MO);
317 /// addComplexAddress - Start with the address based on the location provided,
318 /// and generate the DWARF information necessary to find the actual variable
319 /// (navigating the extra location information encoded in the type) based on
320 /// the starting location. Add the DWARF information to the die.
322 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
323 const MachineLocation &Location);
325 // FIXME: Should be reformulated in terms of addComplexAddress.
326 /// addBlockByrefAddress - Start with the address based on the location
327 /// provided, and generate the DWARF information necessary to find the
328 /// actual Block variable (navigating the Block struct) based on the
329 /// starting location. Add the DWARF information to the die. Obsolete,
330 /// please use addComplexAddress instead.
332 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
333 const MachineLocation &Location);
335 /// addToContextOwner - Add Die into the list of its context owner's children.
336 void addToContextOwner(DIE *Die, DIDescriptor Context);
338 /// addType - Add a new type attribute to the specified entity.
339 void addType(DIE *Entity, DIType Ty);
342 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
343 DIE *getOrCreateNameSpace(DINameSpace NS);
345 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
347 DIE *getOrCreateTypeDIE(DIType Ty);
349 void addPubTypes(DISubprogram SP);
351 /// constructTypeDIE - Construct basic type die from DIBasicType.
352 void constructTypeDIE(DIE &Buffer,
355 /// constructTypeDIE - Construct derived type die from DIDerivedType.
356 void constructTypeDIE(DIE &Buffer,
359 /// constructTypeDIE - Construct type DIE from DICompositeType.
360 void constructTypeDIE(DIE &Buffer,
361 DICompositeType CTy);
363 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
364 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
366 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
367 void constructArrayTypeDIE(DIE &Buffer,
368 DICompositeType *CTy);
370 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
371 DIE *constructEnumTypeDIE(DIEnumerator ETy);
373 /// createGlobalVariableDIE - Create new DIE using GV.
374 DIE *createGlobalVariableDIE(const DIGlobalVariable &GV);
376 /// createMemberDIE - Create new member DIE.
377 DIE *createMemberDIE(const DIDerivedType &DT);
379 /// createSubprogramDIE - Create new DIE using SP.
380 DIE *createSubprogramDIE(const DISubprogram &SP, bool MakeDecl = false);
382 /// getOrCreateDbgScope - Create DbgScope for the scope.
383 DbgScope *getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt);
385 DbgScope *getOrCreateAbstractScope(const MDNode *N);
387 /// findAbstractVariable - Find abstract variable associated with Var.
388 DbgVariable *findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
390 DbgVariable *findAbstractVariable(DIVariable &Var, const MachineInstr *MI,
393 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
394 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
395 /// If there are global variables in this scope then create and insert
396 /// DIEs for these variables.
397 DIE *updateSubprogramScopeDIE(const MDNode *SPNode);
399 /// constructLexicalScope - Construct new DW_TAG_lexical_block
400 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
401 DIE *constructLexicalScopeDIE(DbgScope *Scope);
403 /// constructInlinedScopeDIE - This scope represents inlined body of
404 /// a function. Construct DIE to represent this concrete inlined copy
406 DIE *constructInlinedScopeDIE(DbgScope *Scope);
408 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
409 DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
411 /// constructScopeDIE - Construct a DIE for this scope.
412 DIE *constructScopeDIE(DbgScope *Scope);
414 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
415 /// the start of each one.
416 void EmitSectionLabels();
418 /// emitDIE - Recusively Emits a debug information entry.
420 void emitDIE(DIE *Die);
422 /// computeSizeAndOffset - Compute the size and offset of a DIE.
424 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
426 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
428 void computeSizeAndOffsets();
430 /// EmitDebugInfo - Emit the debug info section.
432 void emitDebugInfo();
434 /// emitAbbreviations - Emit the abbreviation section.
436 void emitAbbreviations() const;
438 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
441 void emitEndOfLineMatrix(unsigned SectionEnd);
443 /// emitDebugLines - Emit source line information.
445 void emitDebugLines();
447 /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
449 void emitCommonDebugFrame();
451 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
453 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
455 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
457 void emitDebugPubNames();
459 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
461 void emitDebugPubTypes();
463 /// emitDebugStr - Emit visible names into a debug str section.
467 /// emitDebugLoc - Emit visible names into a debug loc section.
471 /// EmitDebugARanges - Emit visible names into a debug aranges section.
473 void EmitDebugARanges();
475 /// emitDebugRanges - Emit visible names into a debug ranges section.
477 void emitDebugRanges();
479 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
481 void emitDebugMacInfo();
483 /// emitDebugInlineInfo - Emit inline info using following format.
485 /// 1. length of section
486 /// 2. Dwarf version number
489 /// Entries (one "entry" for each function that was inlined):
491 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
492 /// otherwise offset into __debug_str for regular function name.
493 /// 2. offset into __debug_str section for regular function name.
494 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
495 /// instances for the function.
497 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
498 /// inlined instance; the die_offset points to the inlined_subroutine die in
499 /// the __debug_info section, and the low_pc is the starting address for the
500 /// inlining instance.
501 void emitDebugInlineInfo();
503 /// GetOrCreateSourceID - Look up the source id with the given directory and
504 /// source file names. If none currently exists, create a new id and insert it
505 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames
507 unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName);
509 void constructCompileUnit(const MDNode *N);
511 void constructGlobalVariableDIE(const MDNode *N);
513 void constructSubprogramDIE(const MDNode *N);
515 // FIXME: This should go away in favor of complex addresses.
516 /// Find the type the programmer originally declared the variable to be
517 /// and return that type. Obsolete, use GetComplexAddrType instead.
519 DIType getBlockByrefType(DIType Ty, std::string Name);
521 /// recordSourceLine - Register a source line with debug info. Returns the
522 /// unique label that was emitted and which provides correspondence to
523 /// the source line list.
524 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope);
526 /// getSourceLineCount - Return the number of source lines in the debug
528 unsigned getSourceLineCount() const {
532 /// identifyScopeMarkers() - Indentify instructions that are marking
533 /// beginning of or end of a scope.
534 void identifyScopeMarkers();
536 /// extractScopeInformation - Scan machine instructions in this function
537 /// and collect DbgScopes. Return true, if atleast one scope was found.
538 bool extractScopeInformation();
540 /// collectVariableInfo - Populate DbgScope entries with variables' info.
541 void collectVariableInfo();
544 //===--------------------------------------------------------------------===//
545 // Main entry points.
547 DwarfDebug(AsmPrinter *A, Module *M);
550 /// beginModule - Emit all Dwarf sections that should come prior to the
552 void beginModule(Module *M);
554 /// endModule - Emit all Dwarf sections that should come after the content.
558 /// beginFunction - Gather pre-function debug information. Assumes being
559 /// emitted immediately after the function entry point.
560 void beginFunction(const MachineFunction *MF);
562 /// endFunction - Gather and emit post-function debug information.
564 void endFunction(const MachineFunction *MF);
566 /// beginScope - Process beginning of a scope.
567 void beginScope(const MachineInstr *MI);
569 /// endScope - Prcess end of a scope.
570 void endScope(const MachineInstr *MI);
572 } // End of namespace llvm