blob: 738f117ad0b8d2e67499bb90cb1e38057cbc78a5 [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
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000017#include "DIE.h"
Devang Patel622b0262010-01-19 06:19:05 +000018#include "llvm/ADT/DenseMap.h"
Bill Wendling0310d762009-05-15 09:23:25 +000019#include "llvm/ADT/FoldingSet.h"
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +000020#include "llvm/ADT/SetVector.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000021#include "llvm/ADT/SmallPtrSet.h"
Bill Wendling0310d762009-05-15 09:23:25 +000022#include "llvm/ADT/StringMap.h"
Chandler Carrutha1514e22012-12-04 07:12:27 +000023#include "llvm/CodeGen/AsmPrinter.h"
24#include "llvm/CodeGen/LexicalScopes.h"
25#include "llvm/DebugInfo.h"
26#include "llvm/MC/MachineLocation.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000027#include "llvm/Support/Allocator.h"
Benjamin Kramer12ea7652010-09-13 20:04:49 +000028#include "llvm/Support/DebugLoc.h"
Bill Wendling0310d762009-05-15 09:23:25 +000029
30namespace llvm {
31
32class CompileUnit;
Chris Lattner5b676ce2012-01-26 20:44:57 +000033class ConstantInt;
34class ConstantFP;
Nick Lewycky381afae2009-11-17 09:17:08 +000035class DbgVariable;
Bill Wendling0310d762009-05-15 09:23:25 +000036class MachineFrameInfo;
37class MachineModuleInfo;
Devang Patela43098d2010-04-28 01:03:09 +000038class MachineOperand;
Chris Lattneraf76e592009-08-22 20:48:53 +000039class MCAsmInfo;
Chris Lattner74e41f92010-04-05 05:24:55 +000040class DIEAbbrev;
41class DIE;
42class DIEBlock;
43class DIEEntry;
Bill Wendling0310d762009-05-15 09:23:25 +000044
45//===----------------------------------------------------------------------===//
Eric Christopherb6dc8652012-11-27 22:43:45 +000046/// \brief This class is used to record source line correspondence.
Nick Lewycky381afae2009-11-17 09:17:08 +000047class SrcLineInfo {
Bill Wendling0310d762009-05-15 09:23:25 +000048 unsigned Line; // Source line number.
49 unsigned Column; // Source column.
50 unsigned SourceID; // Source ID number.
Chris Lattner25b68c62010-03-14 08:15:55 +000051 MCSymbol *Label; // Label in code ID number.
Bill Wendling0310d762009-05-15 09:23:25 +000052public:
Chris Lattner25b68c62010-03-14 08:15:55 +000053 SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
54 : Line(L), Column(C), SourceID(S), Label(label) {}
Bill Wendling0310d762009-05-15 09:23:25 +000055
56 // Accessors
57 unsigned getLine() const { return Line; }
58 unsigned getColumn() const { return Column; }
59 unsigned getSourceID() const { return SourceID; }
Chris Lattner25b68c62010-03-14 08:15:55 +000060 MCSymbol *getLabel() const { return Label; }
Bill Wendling0310d762009-05-15 09:23:25 +000061};
62
Eric Christopherb6dc8652012-11-27 22:43:45 +000063/// \brief This struct describes location entries emitted in the .debug_loc
64/// section.
Devang Patel6c3ea902011-02-04 22:57:18 +000065typedef struct DotDebugLocEntry {
66 const MCSymbol *Begin;
67 const MCSymbol *End;
68 MachineLocation Loc;
Devang Patelc26f5442011-04-28 02:22:40 +000069 const MDNode *Variable;
Devang Patel6c3ea902011-02-04 22:57:18 +000070 bool Merged;
Devang Patelc4329072011-06-01 22:03:25 +000071 bool Constant;
Devang Patel80efd4e2011-07-08 16:49:43 +000072 enum EntryType {
73 E_Location,
74 E_Integer,
75 E_ConstantFP,
76 E_ConstantInt
77 };
78 enum EntryType EntryKind;
79
80 union {
81 int64_t Int;
82 const ConstantFP *CFP;
83 const ConstantInt *CIP;
84 } Constants;
Eric Christopherc82fbf42012-11-21 00:03:28 +000085 DotDebugLocEntry()
86 : Begin(0), End(0), Variable(0), Merged(false),
Devang Patel80efd4e2011-07-08 16:49:43 +000087 Constant(false) { Constants.Int = 0;}
Devang Patelc26f5442011-04-28 02:22:40 +000088 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, MachineLocation &L,
Eric Christopherc82fbf42012-11-21 00:03:28 +000089 const MDNode *V)
90 : Begin(B), End(E), Loc(L), Variable(V), Merged(false),
Devang Patel80efd4e2011-07-08 16:49:43 +000091 Constant(false) { Constants.Int = 0; EntryKind = E_Location; }
Devang Patelc4329072011-06-01 22:03:25 +000092 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, int64_t i)
Eric Christopherc82fbf42012-11-21 00:03:28 +000093 : Begin(B), End(E), Variable(0), Merged(false),
Devang Patel80efd4e2011-07-08 16:49:43 +000094 Constant(true) { Constants.Int = i; EntryKind = E_Integer; }
95 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, const ConstantFP *FPtr)
Eric Christopherc82fbf42012-11-21 00:03:28 +000096 : Begin(B), End(E), Variable(0), Merged(false),
Devang Patel80efd4e2011-07-08 16:49:43 +000097 Constant(true) { Constants.CFP = FPtr; EntryKind = E_ConstantFP; }
Eric Christopher4984e012012-09-10 23:34:03 +000098 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E,
99 const ConstantInt *IPtr)
Eric Christopherc82fbf42012-11-21 00:03:28 +0000100 : Begin(B), End(E), Variable(0), Merged(false),
Devang Patel80efd4e2011-07-08 16:49:43 +0000101 Constant(true) { Constants.CIP = IPtr; EntryKind = E_ConstantInt; }
Devang Patelc4329072011-06-01 22:03:25 +0000102
Eric Christopherb6dc8652012-11-27 22:43:45 +0000103 /// \brief Empty entries are also used as a trigger to emit temp label. Such
Devang Patel6c3ea902011-02-04 22:57:18 +0000104 /// labels are referenced is used to find debug_loc offset for a given DIE.
105 bool isEmpty() { return Begin == 0 && End == 0; }
106 bool isMerged() { return Merged; }
107 void Merge(DotDebugLocEntry *Next) {
108 if (!(Begin && Loc == Next->Loc && End == Next->Begin))
109 return;
110 Next->Begin = Begin;
111 Merged = true;
112 }
Devang Patel80efd4e2011-07-08 16:49:43 +0000113 bool isLocation() const { return EntryKind == E_Location; }
114 bool isInt() const { return EntryKind == E_Integer; }
115 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
116 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
117 int64_t getInt() { return Constants.Int; }
118 const ConstantFP *getConstantFP() { return Constants.CFP; }
119 const ConstantInt *getConstantInt() { return Constants.CIP; }
Devang Patel6c3ea902011-02-04 22:57:18 +0000120} DotDebugLocEntry;
121
Devang Patel3cbee302011-04-12 22:53:02 +0000122//===----------------------------------------------------------------------===//
Eric Christopherb6dc8652012-11-27 22:43:45 +0000123/// \brief This class is used to track local variable information.
Devang Patel3cbee302011-04-12 22:53:02 +0000124class DbgVariable {
125 DIVariable Var; // Variable Descriptor.
126 DIE *TheDIE; // Variable DIE.
127 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
Devang Patel5a1a67c2011-08-15 19:01:20 +0000128 DbgVariable *AbsVar; // Corresponding Abstract variable, if any.
Devang Patelff9dd0a2011-08-15 21:24:36 +0000129 const MachineInstr *MInsn; // DBG_VALUE instruction of the variable.
130 int FrameIndex;
Devang Patel3cbee302011-04-12 22:53:02 +0000131public:
132 // AbsVar may be NULL.
Eric Christopherc82fbf42012-11-21 00:03:28 +0000133 DbgVariable(DIVariable V, DbgVariable *AV)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000134 : Var(V), TheDIE(0), DotDebugLocOffset(~0U), AbsVar(AV), MInsn(0),
Devang Patelc890b192011-08-15 21:35:16 +0000135 FrameIndex(~0) {}
Devang Patel3cbee302011-04-12 22:53:02 +0000136
137 // Accessors.
138 DIVariable getVariable() const { return Var; }
139 void setDIE(DIE *D) { TheDIE = D; }
140 DIE *getDIE() const { return TheDIE; }
141 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
142 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
143 StringRef getName() const { return Var.getName(); }
Devang Patel5a1a67c2011-08-15 19:01:20 +0000144 DbgVariable *getAbstractVariable() const { return AbsVar; }
Devang Patelff9dd0a2011-08-15 21:24:36 +0000145 const MachineInstr *getMInsn() const { return MInsn; }
146 void setMInsn(const MachineInstr *M) { MInsn = M; }
147 int getFrameIndex() const { return FrameIndex; }
148 void setFrameIndex(int FI) { FrameIndex = FI; }
Eric Christopherc82fbf42012-11-21 00:03:28 +0000149 // Translate tag to proper Dwarf tag.
150 unsigned getTag() const {
Devang Patel59bc4092011-08-15 18:35:42 +0000151 if (Var.getTag() == dwarf::DW_TAG_arg_variable)
152 return dwarf::DW_TAG_formal_parameter;
Eric Christopherc82fbf42012-11-21 00:03:28 +0000153
Devang Patel59bc4092011-08-15 18:35:42 +0000154 return dwarf::DW_TAG_variable;
155 }
Eric Christopherb6dc8652012-11-27 22:43:45 +0000156 /// \brief Return true if DbgVariable is artificial.
Devang Patela098c502011-08-15 18:40:16 +0000157 bool isArtificial() const {
158 if (Var.isArtificial())
159 return true;
Eric Christopher7b451cf2012-09-21 22:18:52 +0000160 if (getType().isArtificial())
Devang Patela098c502011-08-15 18:40:16 +0000161 return true;
162 return false;
163 }
Eric Christophere5212782012-09-12 23:36:19 +0000164
165 bool isObjectPointer() const {
166 if (Var.isObjectPointer())
167 return true;
Eric Christopher7b451cf2012-09-21 22:18:52 +0000168 if (getType().isObjectPointer())
Eric Christophere5212782012-09-12 23:36:19 +0000169 return true;
170 return false;
171 }
Eric Christopherc82fbf42012-11-21 00:03:28 +0000172
Devang Patel3cbee302011-04-12 22:53:02 +0000173 bool variableHasComplexAddress() const {
174 assert(Var.Verify() && "Invalid complex DbgVariable!");
175 return Var.hasComplexAddress();
176 }
177 bool isBlockByrefVariable() const {
178 assert(Var.Verify() && "Invalid complex DbgVariable!");
179 return Var.isBlockByrefVariable();
180 }
Eric Christopherc82fbf42012-11-21 00:03:28 +0000181 unsigned getNumAddrElements() const {
Devang Patel3cbee302011-04-12 22:53:02 +0000182 assert(Var.Verify() && "Invalid complex DbgVariable!");
183 return Var.getNumAddrElements();
184 }
185 uint64_t getAddrElement(unsigned i) const {
186 return Var.getAddrElement(i);
187 }
188 DIType getType() const;
189};
190
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000191/// \brief Collects and handles information specific to a particular
192/// collection of units.
193class DwarfUnits {
194 // Target of Dwarf emission, used for sizing of abbreviations.
195 AsmPrinter *Asm;
196
197 // Used to uniquely define abbreviations.
198 FoldingSet<DIEAbbrev> *AbbreviationsSet;
199
200 // A list of all the unique abbreviations in use.
201 std::vector<DIEAbbrev *> *Abbreviations;
202
203 // A pointer to all units in the section.
204 SmallVector<CompileUnit *, 1> CUs;
205
206public:
207 DwarfUnits(AsmPrinter *AP, FoldingSet<DIEAbbrev> *AS,
208 std::vector<DIEAbbrev *> *A) :
209 Asm(AP), AbbreviationsSet(AS), Abbreviations(A) {}
210
211 /// \brief Compute the size and offset of a DIE given an incoming Offset.
212 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
213
214 /// \brief Compute the size and offset of all the DIEs.
215 void computeSizeAndOffsets();
216
217 /// \brief Define a unique number for the abbreviation.
218 void assignAbbrevNumber(DIEAbbrev &Abbrev);
219
220 /// \brief Add a unit to the list of CUs.
221 void addUnit(CompileUnit *CU) { CUs.push_back(CU); }
Eric Christopherb1e66d02012-12-15 00:04:07 +0000222
223 /// \brief Emit all of the units to the section listed with the given
224 /// abbreviation section.
225 void emitUnits(DwarfDebug *, const MCSection *, const MCSection *,
226 const MCSymbol *);
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000227};
228
Eric Christopherb6dc8652012-11-27 22:43:45 +0000229/// \brief Collects and handles dwarf debug information.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000230class DwarfDebug {
Eric Christopherb6dc8652012-11-27 22:43:45 +0000231 // Target of Dwarf emission.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000232 AsmPrinter *Asm;
Chris Lattner105d6972010-04-05 05:31:04 +0000233
Eric Christopherb6dc8652012-11-27 22:43:45 +0000234 // Collected machine module information.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000235 MachineModuleInfo *MMI;
236
Eric Christopherb6dc8652012-11-27 22:43:45 +0000237 // All DIEValues are allocated through this allocator.
Benjamin Kramerf33a79c2012-06-09 10:34:15 +0000238 BumpPtrAllocator DIEValueAllocator;
239
Bill Wendling0310d762009-05-15 09:23:25 +0000240 //===--------------------------------------------------------------------===//
241 // Attributes used to construct specific Dwarf sections.
242 //
243
Devang Patel163a9f72010-05-10 22:49:55 +0000244 CompileUnit *FirstCU;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000245
Eric Christopherb6dc8652012-11-27 22:43:45 +0000246 // Maps MDNode with its corresponding CompileUnit.
Devang Patel163a9f72010-05-10 22:49:55 +0000247 DenseMap <const MDNode *, CompileUnit *> CUMap;
Bill Wendling0310d762009-05-15 09:23:25 +0000248
Eric Christopherb6dc8652012-11-27 22:43:45 +0000249 // Maps subprogram MDNode with its corresponding CompileUnit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000250 DenseMap <const MDNode *, CompileUnit *> SPMap;
251
Eric Christopherb6dc8652012-11-27 22:43:45 +0000252 // Used to uniquely define abbreviations.
Bill Wendling0310d762009-05-15 09:23:25 +0000253 FoldingSet<DIEAbbrev> AbbreviationsSet;
254
Eric Christopherb6dc8652012-11-27 22:43:45 +0000255 // A list of all the unique abbreviations in use.
Bill Wendling0310d762009-05-15 09:23:25 +0000256 std::vector<DIEAbbrev *> Abbreviations;
257
Eric Christopherb6dc8652012-11-27 22:43:45 +0000258 // Source id map, i.e. pair of source filename and directory,
259 // separated by a zero byte, mapped to a unique id.
Benjamin Kramerf33a79c2012-06-09 10:34:15 +0000260 StringMap<unsigned, BumpPtrAllocator&> SourceIdMap;
Bill Wendling0310d762009-05-15 09:23:25 +0000261
Eric Christopherb6dc8652012-11-27 22:43:45 +0000262 // A String->Symbol mapping of strings used by indirect
263 // references.
Benjamin Kramerf33a79c2012-06-09 10:34:15 +0000264 StringMap<std::pair<MCSymbol*, unsigned>, BumpPtrAllocator&> StringPool;
Chris Lattnerbc733f52010-03-13 02:17:42 +0000265 unsigned NextStringPoolNumber;
Eric Christopherc82fbf42012-11-21 00:03:28 +0000266
Eric Christopherb6dc8652012-11-27 22:43:45 +0000267 // Provides a unique id per text section.
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +0000268 SetVector<const MCSection*> SectionMap;
Bill Wendling0310d762009-05-15 09:23:25 +0000269
Eric Christopherb6dc8652012-11-27 22:43:45 +0000270 // List of Arguments (DbgValues) for current function.
Devang Patel0478c152011-03-01 22:58:55 +0000271 SmallVector<DbgVariable *, 8> CurrentFnArguments;
272
Devang Patelbf47fdb2011-08-10 20:55:27 +0000273 LexicalScopes LScopes;
Devang Patel2ddefec2010-03-25 15:09:44 +0000274
Eric Christopherb6dc8652012-11-27 22:43:45 +0000275 // Collection of abstract subprogram DIEs.
Devang Patel8aa61472010-07-07 22:20:57 +0000276 DenseMap<const MDNode *, DIE *> AbstractSPDies;
277
Eric Christopherb6dc8652012-11-27 22:43:45 +0000278 // Collection of dbg variables of a scope.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000279 DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> > ScopeVariables;
Devang Patel53bb5c92009-11-10 23:06:00 +0000280
Eric Christopherb6dc8652012-11-27 22:43:45 +0000281 // Collection of abstract variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000282 DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
Devang Patel53bb5c92009-11-10 23:06:00 +0000283
Eric Christopherb6dc8652012-11-27 22:43:45 +0000284 // Collection of DotDebugLocEntry.
Devang Patelc3f5f782010-05-25 23:40:22 +0000285 SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
286
Eric Christopherb6dc8652012-11-27 22:43:45 +0000287 // Collection of subprogram DIEs that are marked (at the end of the module)
288 // as DW_AT_inline.
Devang Patel53bb5c92009-11-10 23:06:00 +0000289 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
290
Eric Christopherb6dc8652012-11-27 22:43:45 +0000291 // Keep track of inlined functions and their location. This
292 // information is used to populate the debug_inlined section.
Devang Patelc3f5f782010-05-25 23:40:22 +0000293 typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
Devang Patele9f8f5e2010-05-07 20:54:48 +0000294 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
295 SmallVector<const MDNode *, 4> InlinedSPNodes;
Bill Wendling0310d762009-05-15 09:23:25 +0000296
Eric Christopherb6dc8652012-11-27 22:43:45 +0000297 // This is a collection of subprogram MDNodes that are processed to create DIEs.
Devang Patel4a1cad62010-06-28 18:25:03 +0000298 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
299
Eric Christopherb6dc8652012-11-27 22:43:45 +0000300 // Maps instruction with label emitted before instruction.
Devang Pateleac9c072010-04-27 19:46:33 +0000301 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
Devang Patel1c246352010-04-08 16:50:29 +0000302
Eric Christopherb6dc8652012-11-27 22:43:45 +0000303 // Maps instruction with label emitted after instruction.
Devang Pateleac9c072010-04-27 19:46:33 +0000304 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
Devang Patel1c246352010-04-08 16:50:29 +0000305
Eric Christopherb6dc8652012-11-27 22:43:45 +0000306 // Every user variable mentioned by a DBG_VALUE instruction in order of
307 // appearance.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000308 SmallVector<const MDNode*, 8> UserVariables;
Devang Patelb2b31a62010-05-26 19:37:24 +0000309
Eric Christopherb6dc8652012-11-27 22:43:45 +0000310 // For each user variable, keep a list of DBG_VALUE instructions in order.
311 // The list can also contain normal instructions that clobber the previous
312 // DBG_VALUE.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000313 typedef DenseMap<const MDNode*, SmallVector<const MachineInstr*, 4> >
314 DbgValueHistoryMap;
315 DbgValueHistoryMap DbgValues;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000316
Devang Patelf2548ca2010-04-16 23:33:45 +0000317 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
Devang Patelc04d5452010-04-22 20:56:35 +0000318
Eric Christopherb6dc8652012-11-27 22:43:45 +0000319 // Previous instruction's location information. This is used to determine
320 // label location to indicate scope boundries in dwarf debug info.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000321 DebugLoc PrevInstLoc;
Devang Patelf2548ca2010-04-16 23:33:45 +0000322 MCSymbol *PrevLabel;
Devang Patel553881b2010-03-29 17:20:31 +0000323
Eric Christopherb6dc8652012-11-27 22:43:45 +0000324 // This location indicates end of function prologue and beginning of function
325 // body.
Devang Patel4243e672011-05-11 19:22:19 +0000326 DebugLoc PrologEndLoc;
327
Bill Wendling0310d762009-05-15 09:23:25 +0000328 struct FunctionDebugFrameInfo {
329 unsigned Number;
330 std::vector<MachineMove> Moves;
331
332 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
333 : Number(Num), Moves(M) {}
334 };
335
336 std::vector<FunctionDebugFrameInfo> DebugFrames;
337
Chris Lattner9c69e285532010-04-04 22:59:04 +0000338 // Section Symbols: these are assembler temporary labels that are emitted at
339 // the beginning of each supported dwarf section. These are used to form
340 // section offsets and are created by EmitSectionLabels.
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000341 MCSymbol *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
Devang Patelf2548ca2010-04-16 23:33:45 +0000342 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
Devang Patelc3f5f782010-05-25 23:40:22 +0000343 MCSymbol *DwarfDebugLocSectionSym;
344 MCSymbol *FunctionBeginSym, *FunctionEndSym;
Devang Patelca76f6f2010-07-08 20:10:35 +0000345
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000346 // As an optimization, there is no need to emit an entry in the directory
347 // table for the same directory as DW_at_comp_dir.
348 StringRef CompilationDir;
349
Eli Benderskyd4a05e02012-12-03 18:45:45 +0000350 // Counter for assigning globally unique IDs for CUs.
351 unsigned GlobalCUIndexCount;
352
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000353 // Holder for the file specific debug information.
354 DwarfUnits InfoHolder;
355
Eric Christopher617d1832012-12-10 22:25:41 +0000356 // Holders for the various debug information flags that we might need to
357 // have exposed. See accessor functions below for description.
358
359 // Whether or not we're emitting info for older versions of gdb on darwin.
360 bool IsDarwinGDBCompat;
361
Eric Christopher5cf823c2012-11-29 22:56:13 +0000362 // DWARF5 Experimental Options
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000363 bool HasDwarfAccelTables;
Eric Christopher4daaed12012-12-10 19:51:21 +0000364 bool HasSplitDwarf;
Eric Christopher28bd25a2012-12-10 19:51:13 +0000365
Eric Christopher0b944ee2012-12-11 19:42:09 +0000366 // Separated Dwarf Variables
Eric Christopher28bd25a2012-12-10 19:51:13 +0000367 // In general these will all be for bits that are left in the
368 // original object file, rather than things that are meant
369 // to be in the .dwo sections.
370
Eric Christopher0b944ee2012-12-11 19:42:09 +0000371 // The CU left in the original object file for separated debug info.
Eric Christopher4daaed12012-12-10 19:51:21 +0000372 CompileUnit *SkeletonCU;
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000373 DwarfUnits SkeletonHolder;
Eric Christopher28bd25a2012-12-10 19:51:13 +0000374
Devang Patele62dfcf2011-03-31 16:53:49 +0000375private:
Bill Wendling0310d762009-05-15 09:23:25 +0000376
Devang Patelbf47fdb2011-08-10 20:55:27 +0000377 void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000378
Eric Christopherb6dc8652012-11-27 22:43:45 +0000379 /// \brief Find abstract variable associated with Var.
Devang Patel26c1e562010-05-20 16:36:41 +0000380 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
Devang Patel53bb5c92009-11-10 23:06:00 +0000381
Eric Christopherb6dc8652012-11-27 22:43:45 +0000382 /// \brief Find DIE for the given subprogram and attach appropriate
383 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
384 /// variables in this scope then create and insert DIEs for these
385 /// variables.
Devang Pateld3024342011-08-15 22:24:32 +0000386 DIE *updateSubprogramScopeDIE(CompileUnit *SPCU, const MDNode *SPNode);
Bill Wendling0310d762009-05-15 09:23:25 +0000387
Eric Christopherb6dc8652012-11-27 22:43:45 +0000388 /// \brief Construct new DW_TAG_lexical_block for this scope and
389 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000390 DIE *constructLexicalScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
Bill Wendling0310d762009-05-15 09:23:25 +0000391
Eric Christopherb6dc8652012-11-27 22:43:45 +0000392 /// \brief This scope represents inlined body of a function. Construct
393 /// DIE to represent this concrete inlined copy of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000394 DIE *constructInlinedScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000395
Eric Christopherb6dc8652012-11-27 22:43:45 +0000396 /// \brief Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000397 DIE *constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000398
Eric Christopherb6dc8652012-11-27 22:43:45 +0000399 /// \brief Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000400 void emitSectionLabels();
Bill Wendling0310d762009-05-15 09:23:25 +0000401
Eric Christopherb6dc8652012-11-27 22:43:45 +0000402 /// \brief Compute the size and offset of a DIE given an incoming Offset.
Eric Christopherfbd19752012-11-20 22:14:13 +0000403 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
Bill Wendling0310d762009-05-15 09:23:25 +0000404
Eric Christopherb6dc8652012-11-27 22:43:45 +0000405 /// \brief Compute the size and offset of all the DIEs.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000406 void computeSizeAndOffsets();
Bill Wendling0310d762009-05-15 09:23:25 +0000407
Eric Christopherb6dc8652012-11-27 22:43:45 +0000408 /// \brief Attach DW_AT_inline attribute with inlined subprogram DIEs.
Eric Christopher4117bec2012-11-22 00:59:49 +0000409 void computeInlinedDIEs();
410
Eric Christopherb6dc8652012-11-27 22:43:45 +0000411 /// \brief Collect info for variables that were optimized out.
Eric Christopher4117bec2012-11-22 00:59:49 +0000412 void collectDeadVariables();
413
Eric Christopherb6dc8652012-11-27 22:43:45 +0000414 /// \brief Finish off debug information after all functions have been
415 /// processed.
Eric Christopher4117bec2012-11-22 00:59:49 +0000416 void finalizeModuleInfo();
417
Eric Christopherb6dc8652012-11-27 22:43:45 +0000418 /// \brief Emit labels to close any remaining sections that have been left
419 /// open.
Eric Christopher4117bec2012-11-22 00:59:49 +0000420 void endSections();
421
Eric Christopherb6dc8652012-11-27 22:43:45 +0000422 /// \brief Emit the debug info section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000423 void emitDebugInfo();
Bill Wendling0310d762009-05-15 09:23:25 +0000424
Eric Christopherb6dc8652012-11-27 22:43:45 +0000425 /// \brief Emit the abbreviation section.
Eric Christopher7dc68db2012-11-20 23:30:11 +0000426 void emitAbbreviations();
Bill Wendling0310d762009-05-15 09:23:25 +0000427
Eric Christopherb6dc8652012-11-27 22:43:45 +0000428 /// \brief Emit the last address of the section and the end of
Bill Wendling0310d762009-05-15 09:23:25 +0000429 /// the line matrix.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000430 void emitEndOfLineMatrix(unsigned SectionEnd);
Bill Wendling0310d762009-05-15 09:23:25 +0000431
Eric Christopherb6dc8652012-11-27 22:43:45 +0000432 /// \brief Emit visible names into a hashed accelerator table section.
Eric Christopher09ac3d82011-11-07 09:24:32 +0000433 void emitAccelNames();
Eric Christopherc82fbf42012-11-21 00:03:28 +0000434
Eric Christopherb6dc8652012-11-27 22:43:45 +0000435 /// \brief Emit objective C classes and categories into a hashed
Eric Christopher09ac3d82011-11-07 09:24:32 +0000436 /// accelerator table section.
437 void emitAccelObjC();
438
Eric Christopherb6dc8652012-11-27 22:43:45 +0000439 /// \brief Emit namespace dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +0000440 void emitAccelNamespaces();
441
Eric Christopherb6dc8652012-11-27 22:43:45 +0000442 /// \brief Emit type dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +0000443 void emitAccelTypes();
Eric Christopherc82fbf42012-11-21 00:03:28 +0000444
Eric Christopherb6dc8652012-11-27 22:43:45 +0000445 /// \brief Emit visible types into a debug pubtypes section.
Devang Patel193f7202009-11-24 01:14:22 +0000446 void emitDebugPubTypes();
447
Eric Christopherb6dc8652012-11-27 22:43:45 +0000448 /// \brief Emit visible names into a debug str section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000449 void emitDebugStr();
Bill Wendling0310d762009-05-15 09:23:25 +0000450
Eric Christopherb6dc8652012-11-27 22:43:45 +0000451 /// \brief Emit visible names into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000452 void emitDebugLoc();
Bill Wendling0310d762009-05-15 09:23:25 +0000453
Eric Christopherb6dc8652012-11-27 22:43:45 +0000454 /// \brief Emit visible names into a debug aranges section.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000455 void emitDebugARanges();
Bill Wendling0310d762009-05-15 09:23:25 +0000456
Eric Christopherb6dc8652012-11-27 22:43:45 +0000457 /// \brief Emit visible names into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000458 void emitDebugRanges();
Bill Wendling0310d762009-05-15 09:23:25 +0000459
Eric Christopherb6dc8652012-11-27 22:43:45 +0000460 /// \brief Emit visible names into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000461 void emitDebugMacInfo();
Bill Wendling0310d762009-05-15 09:23:25 +0000462
Eric Christopherb6dc8652012-11-27 22:43:45 +0000463 /// \brief Emit inline info using custom format.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000464 void emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000465
Eric Christopher4daaed12012-12-10 19:51:21 +0000466 /// DWARF 5 Experimental Split Dwarf Emitters
Eric Christopher98e237f2012-11-30 23:59:06 +0000467
Eric Christopher4daaed12012-12-10 19:51:21 +0000468 /// \brief Construct the split debug info compile unit for the debug info
469 /// section.
470 CompileUnit *constructSkeletonCU(const MDNode *);
Eric Christopher98e237f2012-11-30 23:59:06 +0000471
Eric Christopher4daaed12012-12-10 19:51:21 +0000472 /// \brief Emit the local split debug info section.
473 void emitSkeletonCU(const MCSection *);
Eric Christopher98e237f2012-11-30 23:59:06 +0000474
475 /// \brief Emit the debug info dwo section.
476 void emitDebugInfoDWO();
477
Eric Christopherb6dc8652012-11-27 22:43:45 +0000478 /// \brief Create new CompileUnit for the given metadata node with tag
479 /// DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000480 CompileUnit *constructCompileUnit(const MDNode *N);
Devang Patel163a9f72010-05-10 22:49:55 +0000481
Eric Christopherb6dc8652012-11-27 22:43:45 +0000482 /// \brief Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000483 void constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000484
Eric Christopherb6dc8652012-11-27 22:43:45 +0000485 /// \brief Register a source line with debug info. Returns the unique
486 /// label that was emitted and which provides correspondence to the
487 /// source line list.
Devang Patel4243e672011-05-11 19:22:19 +0000488 void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
489 unsigned Flags);
Eric Christopherc82fbf42012-11-21 00:03:28 +0000490
Eric Christopherb6dc8652012-11-27 22:43:45 +0000491 /// \brief Indentify instructions that are marking the beginning of or
492 /// ending of a scope.
Devang Patele37b0c62010-04-08 18:43:56 +0000493 void identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +0000494
Eric Christopherb6dc8652012-11-27 22:43:45 +0000495 /// \brief If Var is an current function argument that add it in
496 /// CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000497 bool addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000498 DbgVariable *Var, LexicalScope *Scope);
Devang Patel0478c152011-03-01 22:58:55 +0000499
Eric Christopherb6dc8652012-11-27 22:43:45 +0000500 /// \brief Populate LexicalScope entries with variables' info.
Devang Patel78e127d2010-06-25 22:07:34 +0000501 void collectVariableInfo(const MachineFunction *,
502 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
Eric Christopherc82fbf42012-11-21 00:03:28 +0000503
Eric Christopherb6dc8652012-11-27 22:43:45 +0000504 /// \brief Collect variable information from the side table maintained
505 /// by MMI.
Devang Patelee432862010-05-20 19:57:06 +0000506 void collectVariableInfoFromMMITable(const MachineFunction * MF,
507 SmallPtrSet<const MDNode *, 16> &P);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000508
Eric Christopherb6dc8652012-11-27 22:43:45 +0000509 /// \brief Ensure that a label will be emitted before MI.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000510 void requestLabelBeforeInsn(const MachineInstr *MI) {
511 LabelsBeforeInsn.insert(std::make_pair(MI, (MCSymbol*)0));
512 }
513
Eric Christopherb6dc8652012-11-27 22:43:45 +0000514 /// \brief Return Label preceding the instruction.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000515 const MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
516
Eric Christopherb6dc8652012-11-27 22:43:45 +0000517 /// \brief Ensure that a label will be emitted after MI.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000518 void requestLabelAfterInsn(const MachineInstr *MI) {
519 LabelsAfterInsn.insert(std::make_pair(MI, (MCSymbol*)0));
520 }
521
Eric Christopherb6dc8652012-11-27 22:43:45 +0000522 /// \brief Return Label immediately following the instruction.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000523 const MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
524
Bill Wendling0310d762009-05-15 09:23:25 +0000525public:
526 //===--------------------------------------------------------------------===//
527 // Main entry points.
528 //
Chris Lattner49cd6642010-04-05 05:11:15 +0000529 DwarfDebug(AsmPrinter *A, Module *M);
Chris Lattner2b1b3312010-04-05 05:32:45 +0000530 ~DwarfDebug();
Bill Wendling0310d762009-05-15 09:23:25 +0000531
Eric Christopherb6dc8652012-11-27 22:43:45 +0000532 /// \brief Collect debug info from named mdnodes such as llvm.dbg.enum
533 /// and llvm.dbg.ty
Eric Christopherc4639d62012-11-19 22:42:15 +0000534 void collectInfoFromNamedMDNodes(const Module *M);
Devang Patel02e603f2011-08-15 23:47:24 +0000535
Eric Christopherb6dc8652012-11-27 22:43:45 +0000536 /// \brief Collect debug info using DebugInfoFinder.
Nick Lewycky024170f2011-10-18 22:39:43 +0000537 /// FIXME - Remove this when DragonEgg switches to DIBuilder.
Eric Christopherc4639d62012-11-19 22:42:15 +0000538 bool collectLegacyDebugInfo(const Module *M);
Devang Patel02e603f2011-08-15 23:47:24 +0000539
Eric Christopherb6dc8652012-11-27 22:43:45 +0000540 /// \brief Emit all Dwarf sections that should come prior to the
Bill Wendling0310d762009-05-15 09:23:25 +0000541 /// content.
Eric Christopherc4639d62012-11-19 22:42:15 +0000542 void beginModule();
Bill Wendling0310d762009-05-15 09:23:25 +0000543
Eric Christopherb6dc8652012-11-27 22:43:45 +0000544 /// \brief Emit all Dwarf sections that should come after the content.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000545 void endModule();
Bill Wendling0310d762009-05-15 09:23:25 +0000546
Eric Christopherb6dc8652012-11-27 22:43:45 +0000547 /// \brief Gather pre-function debug information.
Chris Lattnereec791a2010-01-26 23:18:02 +0000548 void beginFunction(const MachineFunction *MF);
Bill Wendling0310d762009-05-15 09:23:25 +0000549
Eric Christopherb6dc8652012-11-27 22:43:45 +0000550 /// \brief Gather and emit post-function debug information.
Chris Lattnereec791a2010-01-26 23:18:02 +0000551 void endFunction(const MachineFunction *MF);
Bill Wendling0310d762009-05-15 09:23:25 +0000552
Eric Christopherb6dc8652012-11-27 22:43:45 +0000553 /// \brief Process beginning of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +0000554 void beginInstruction(const MachineInstr *MI);
Bill Wendling0310d762009-05-15 09:23:25 +0000555
Eric Christopherb6dc8652012-11-27 22:43:45 +0000556 /// \brief Process end of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +0000557 void endInstruction(const MachineInstr *MI);
Devang Patel3cbee302011-04-12 22:53:02 +0000558
Eric Christopherb6dc8652012-11-27 22:43:45 +0000559 /// \brief Look up the source id with the given directory and source file
560 /// names. If none currently exists, create a new id and insert it in the
561 /// SourceIds map.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000562 unsigned getOrCreateSourceID(StringRef DirName, StringRef FullName);
Devang Patel3cbee302011-04-12 22:53:02 +0000563
Eric Christopherb6dc8652012-11-27 22:43:45 +0000564 /// \brief Returns the entry into the start of the pool.
Nick Lewycky390c40d2011-10-27 06:44:11 +0000565 MCSymbol *getStringPool();
566
Eric Christopherb6dc8652012-11-27 22:43:45 +0000567 /// \brief Returns an entry into the string pool with the given
Nick Lewycky390c40d2011-10-27 06:44:11 +0000568 /// string text.
569 MCSymbol *getStringPoolEntry(StringRef Str);
Eric Christopherc1610fa2012-08-23 22:36:36 +0000570
Eric Christopherb1e66d02012-12-15 00:04:07 +0000571 /// \brief Recursively Emits a debug information entry.
572 void emitDIE(DIE *Die);
573
Eric Christopherb6dc8652012-11-27 22:43:45 +0000574 /// \brief Returns whether or not to limit some of our debug
Eric Christopherc1610fa2012-08-23 22:36:36 +0000575 /// output to the limitations of darwin gdb.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000576 bool useDarwinGDBCompat() { return IsDarwinGDBCompat; }
Eric Christophere6ad6ac2012-11-21 00:03:31 +0000577
578 // Experimental DWARF5 features.
579
Eric Christopherb6dc8652012-11-27 22:43:45 +0000580 /// \brief Returns whether or not to emit tables that dwarf consumers can
581 /// use to accelerate lookup.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000582 bool useDwarfAccelTables() { return HasDwarfAccelTables; }
Eric Christophere6ad6ac2012-11-21 00:03:31 +0000583
Eric Christopherb6dc8652012-11-27 22:43:45 +0000584 /// \brief Returns whether or not to change the current debug info for the
Eric Christopher4daaed12012-12-10 19:51:21 +0000585 /// split dwarf proposal support.
586 bool useSplitDwarf() { return HasSplitDwarf; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000587};
Bill Wendling0310d762009-05-15 09:23:25 +0000588} // End of namespace llvm
589
590#endif