blob: f05802070e55259f421cef349d216eaa2f690d3b [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
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//===----------------------------------------------------------------------===//
Chris Lattner6cde3e62010-03-09 00:39:24 +000013
Devang Patele4b27562009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000016#include "DIE.h"
Eric Christopher09ac3d82011-11-07 09:24:32 +000017#include "DwarfAccelTable.h"
Devang Patel8b9df622011-04-12 17:40:32 +000018#include "DwarfCompileUnit.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/Statistic.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/ADT/Triple.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000023#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/Constants.h"
26#include "llvm/DIBuilder.h"
27#include "llvm/DataLayout.h"
28#include "llvm/DebugInfo.h"
29#include "llvm/Instructions.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000030#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000031#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000032#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000033#include "llvm/MC/MCSymbol.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000034#include "llvm/Module.h"
Devang Pateleac9c072010-04-27 19:46:33 +000035#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000038#include "llvm/Support/FormattedStream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000039#include "llvm/Support/Path.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000040#include "llvm/Support/Timer.h"
41#include "llvm/Support/ValueHandle.h"
42#include "llvm/Target/TargetFrameLowering.h"
43#include "llvm/Target/TargetLoweringObjectFile.h"
44#include "llvm/Target/TargetMachine.h"
45#include "llvm/Target/TargetOptions.h"
46#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendling0310d762009-05-15 09:23:25 +000047using namespace llvm;
48
Jim Grosbach1e20b962010-07-21 21:21:52 +000049static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel61409622010-07-07 20:12:52 +000050 cl::Hidden,
Devang Pateleac9c072010-04-27 19:46:33 +000051 cl::desc("Disable debug info printing"));
52
Dan Gohman281d65d2010-05-07 01:08:53 +000053static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner7a2bdde2011-04-15 05:18:47 +000054 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman281d65d2010-05-07 01:08:53 +000055 cl::init(false));
56
Eric Christopher20f47ab2012-08-23 22:36:40 +000057namespace {
58 enum DefaultOnOff {
59 Default, Enable, Disable
60 };
61}
Eric Christopher09ac3d82011-11-07 09:24:32 +000062
Eric Christopher20f47ab2012-08-23 22:36:40 +000063static cl::opt<DefaultOnOff> DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
64 cl::desc("Output prototype dwarf accelerator tables."),
65 cl::values(
66 clEnumVal(Default, "Default for platform"),
67 clEnumVal(Enable, "Enabled"),
68 clEnumVal(Disable, "Disabled"),
69 clEnumValEnd),
70 cl::init(Default));
71
72static cl::opt<DefaultOnOff> DarwinGDBCompat("darwin-gdb-compat", cl::Hidden,
Eric Christopher10cb7442012-08-23 07:10:46 +000073 cl::desc("Compatibility with Darwin gdb."),
Eric Christopher20f47ab2012-08-23 22:36:40 +000074 cl::values(
75 clEnumVal(Default, "Default for platform"),
76 clEnumVal(Enable, "Enabled"),
77 clEnumVal(Disable, "Disabled"),
78 clEnumValEnd),
79 cl::init(Default));
Eric Christopher10cb7442012-08-23 07:10:46 +000080
Eric Christopher4daaed12012-12-10 19:51:21 +000081static cl::opt<DefaultOnOff> SplitDwarf("split-dwarf", cl::Hidden,
82 cl::desc("Output prototype dwarf split debug info."),
Eric Christopherf5b6dcd2012-11-12 22:22:20 +000083 cl::values(
84 clEnumVal(Default, "Default for platform"),
85 clEnumVal(Enable, "Enabled"),
86 clEnumVal(Disable, "Disabled"),
87 clEnumValEnd),
88 cl::init(Default));
89
Bill Wendling5f017e82010-04-07 09:28:04 +000090namespace {
91 const char *DWARFGroupName = "DWARF Emission";
92 const char *DbgTimerName = "DWARF Debug Writer";
93} // end anonymous namespace
94
Bill Wendling0310d762009-05-15 09:23:25 +000095//===----------------------------------------------------------------------===//
96
Eric Christopherb6dc8652012-11-27 22:43:45 +000097// Configuration values for initial hash set sizes (log2).
98//
Bill Wendling0310d762009-05-15 09:23:25 +000099static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +0000100
101namespace llvm {
102
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000103DIType DbgVariable::getType() const {
Devang Patel3cbee302011-04-12 22:53:02 +0000104 DIType Ty = Var.getType();
105 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
106 // addresses instead.
107 if (Var.isBlockByrefVariable()) {
108 /* Byref variables, in Blocks, are declared by the programmer as
109 "SomeType VarName;", but the compiler creates a
110 __Block_byref_x_VarName struct, and gives the variable VarName
111 either the struct, or a pointer to the struct, as its type. This
112 is necessary for various behind-the-scenes things the compiler
113 needs to do with by-reference variables in blocks.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000114
Devang Patel3cbee302011-04-12 22:53:02 +0000115 However, as far as the original *programmer* is concerned, the
116 variable should still have type 'SomeType', as originally declared.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000117
Devang Patel3cbee302011-04-12 22:53:02 +0000118 The following function dives into the __Block_byref_x_VarName
119 struct to find the original type of the variable. This will be
120 passed back to the code generating the type for the Debug
121 Information Entry for the variable 'VarName'. 'VarName' will then
122 have the original type 'SomeType' in its debug information.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000123
Devang Patel3cbee302011-04-12 22:53:02 +0000124 The original type 'SomeType' will be the type of the field named
125 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000126
Devang Patel3cbee302011-04-12 22:53:02 +0000127 NOTE: In order for this to not completely fail on the debugger
128 side, the Debug Information Entry for the variable VarName needs to
129 have a DW_AT_location that tells the debugger how to unwind through
130 the pointers and __Block_byref_x_VarName struct to find the actual
131 value of the variable. The function addBlockByrefType does this. */
132 DIType subType = Ty;
133 unsigned tag = Ty.getTag();
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000134
Devang Patel3cbee302011-04-12 22:53:02 +0000135 if (tag == dwarf::DW_TAG_pointer_type) {
136 DIDerivedType DTy = DIDerivedType(Ty);
137 subType = DTy.getTypeDerivedFrom();
138 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000139
Devang Patel3cbee302011-04-12 22:53:02 +0000140 DICompositeType blockStruct = DICompositeType(subType);
141 DIArray Elements = blockStruct.getTypeArray();
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000142
Devang Patel3cbee302011-04-12 22:53:02 +0000143 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
144 DIDescriptor Element = Elements.getElement(i);
145 DIDerivedType DT = DIDerivedType(Element);
146 if (getName() == DT.getName())
147 return (DT.getTypeDerivedFrom());
Devang Patel8bd11de2010-08-09 21:01:39 +0000148 }
Devang Patel8bd11de2010-08-09 21:01:39 +0000149 }
Devang Patel3cbee302011-04-12 22:53:02 +0000150 return Ty;
151}
Bill Wendling0310d762009-05-15 09:23:25 +0000152
Chris Lattnerea761862010-04-05 04:09:20 +0000153} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000154
Chris Lattner49cd6642010-04-05 05:11:15 +0000155DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Eric Christopher28bd25a2012-12-10 19:51:13 +0000156 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000157 AbbreviationsSet(InitAbbreviationsSetSize),
Benjamin Kramerf33a79c2012-06-09 10:34:15 +0000158 SourceIdMap(DIEValueAllocator), StringPool(DIEValueAllocator),
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000159 PrevLabel(NULL), GlobalCUIndexCount(0),
160 InfoHolder(A, &AbbreviationsSet, &Abbreviations),
Eric Christopher6eebe472012-12-19 22:02:53 +0000161 SkeletonCU(0),
162 SkeletonAbbrevSet(InitAbbreviationsSetSize),
163 SkeletonHolder(A, &SkeletonAbbrevSet, &SkeletonAbbrevs) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000164 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000165
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000166 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000167 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000168 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Eric Christopher6eebe472012-12-19 22:02:53 +0000169 DwarfAbbrevDWOSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000170 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopher60777d82012-04-02 17:58:52 +0000171
Eric Christopher10cb7442012-08-23 07:10:46 +0000172 // Turn on accelerator tables and older gdb compatibility
173 // for Darwin.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000174 bool IsDarwin = Triple(M->getTargetTriple()).isOSDarwin();
Eric Christopher20f47ab2012-08-23 22:36:40 +0000175 if (DarwinGDBCompat == Default) {
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000176 if (IsDarwin)
177 IsDarwinGDBCompat = true;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000178 else
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000179 IsDarwinGDBCompat = false;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000180 } else
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000181 IsDarwinGDBCompat = DarwinGDBCompat == Enable ? true : false;
Eric Christopherc1610fa2012-08-23 22:36:36 +0000182
Eric Christopher20f47ab2012-08-23 22:36:40 +0000183 if (DwarfAccelTables == Default) {
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000184 if (IsDarwin)
185 HasDwarfAccelTables = true;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000186 else
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000187 HasDwarfAccelTables = false;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000188 } else
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000189 HasDwarfAccelTables = DwarfAccelTables == Enable ? true : false;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000190
Eric Christopher4daaed12012-12-10 19:51:21 +0000191 if (SplitDwarf == Default)
192 HasSplitDwarf = false;
Eric Christopherf5b6dcd2012-11-12 22:22:20 +0000193 else
Eric Christopher4daaed12012-12-10 19:51:21 +0000194 HasSplitDwarf = SplitDwarf == Enable ? true : false;
Eric Christopherf5b6dcd2012-11-12 22:22:20 +0000195
Dan Gohman03c3dc72010-06-18 15:56:31 +0000196 {
197 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopherc4639d62012-11-19 22:42:15 +0000198 beginModule();
Torok Edwin9c421072010-04-07 10:44:46 +0000199 }
Bill Wendling0310d762009-05-15 09:23:25 +0000200}
201DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000202}
203
Eric Christopherb6dc8652012-11-27 22:43:45 +0000204// Switch to the specified MCSection and emit an assembler
205// temporary label to it if SymbolStem is specified.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000206static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christopherd8a87522011-11-07 09:18:38 +0000207 const char *SymbolStem = 0) {
208 Asm->OutStreamer.SwitchSection(Section);
209 if (!SymbolStem) return 0;
210
211 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
212 Asm->OutStreamer.EmitLabel(TmpSym);
213 return TmpSym;
214}
215
Nick Lewycky390c40d2011-10-27 06:44:11 +0000216MCSymbol *DwarfDebug::getStringPool() {
217 return Asm->GetTempSymbol("section_str");
218}
219
Chris Lattnerbc733f52010-03-13 02:17:42 +0000220MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
221 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
222 if (Entry.first) return Entry.first;
223
224 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000225 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000226}
227
Eric Christopherb6dc8652012-11-27 22:43:45 +0000228// Define a unique number for the abbreviation.
229//
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000230void DwarfUnits::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000231 // Profile the node so that we can make it unique.
232 FoldingSetNodeID ID;
233 Abbrev.Profile(ID);
234
235 // Check the set for priors.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000236 DIEAbbrev *InSet = AbbreviationsSet->GetOrInsertNode(&Abbrev);
Bill Wendling0310d762009-05-15 09:23:25 +0000237
238 // If it's newly added.
239 if (InSet == &Abbrev) {
240 // Add to abbreviation list.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000241 Abbreviations->push_back(&Abbrev);
Bill Wendling0310d762009-05-15 09:23:25 +0000242
243 // Assign the vector position + 1 as its number.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000244 Abbrev.setNumber(Abbreviations->size());
Bill Wendling0310d762009-05-15 09:23:25 +0000245 } else {
246 // Assign existing abbreviation number.
247 Abbrev.setNumber(InSet->getNumber());
248 }
249}
250
Eric Christopherb6dc8652012-11-27 22:43:45 +0000251// If special LLVM prefix that is used to inform the asm
252// printer to not emit usual symbol prefix before the symbol name is used then
253// return linkage name after skipping this special LLVM prefix.
Devang Patel351ca332010-01-05 01:46:14 +0000254static StringRef getRealLinkageName(StringRef LinkageName) {
255 char One = '\1';
256 if (LinkageName.startswith(StringRef(&One, 1)))
257 return LinkageName.substr(1);
258 return LinkageName;
259}
260
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000261static bool isObjCClass(StringRef Name) {
262 return Name.startswith("+") || Name.startswith("-");
263}
264
265static bool hasObjCCategory(StringRef Name) {
266 if (!isObjCClass(Name)) return false;
267
268 size_t pos = Name.find(')');
269 if (pos != std::string::npos) {
270 if (Name[pos+1] != ' ') return false;
271 return true;
272 }
273 return false;
274}
275
276static void getObjCClassCategory(StringRef In, StringRef &Class,
277 StringRef &Category) {
278 if (!hasObjCCategory(In)) {
279 Class = In.slice(In.find('[') + 1, In.find(' '));
280 Category = "";
281 return;
282 }
283
284 Class = In.slice(In.find('[') + 1, In.find('('));
285 Category = In.slice(In.find('[') + 1, In.find(' '));
286 return;
287}
288
289static StringRef getObjCMethodName(StringRef In) {
290 return In.slice(In.find(' ') + 1, In.find(']'));
291}
292
293// Add the various names to the Dwarf accelerator table names.
294static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
295 DIE* Die) {
296 if (!SP.isDefinition()) return;
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000297
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000298 TheCU->addAccelName(SP.getName(), Die);
299
300 // If the linkage name is different than the name, go ahead and output
301 // that as well into the name table.
302 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
303 TheCU->addAccelName(SP.getLinkageName(), Die);
304
305 // If this is an Objective-C selector name add it to the ObjC accelerator
306 // too.
307 if (isObjCClass(SP.getName())) {
308 StringRef Class, Category;
309 getObjCClassCategory(SP.getName(), Class, Category);
310 TheCU->addAccelObjC(Class, Die);
311 if (Category != "")
312 TheCU->addAccelObjC(Category, Die);
313 // Also add the base method name to the name table.
314 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
315 }
316}
317
Eric Christopherb6dc8652012-11-27 22:43:45 +0000318// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
319// and DW_AT_high_pc attributes. If there are global variables in this
320// scope then create and insert DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000321DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
322 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000323 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000324
Chris Lattnerd38fee82010-04-05 00:13:49 +0000325 assert(SPDie && "Unable to find subprogram DIE!");
326 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000327
Bill Wendling168c1902012-11-07 05:19:04 +0000328 // If we're updating an abstract DIE, then we will be adding the children and
329 // object pointer later on. But what we don't want to do is process the
330 // concrete DIE twice.
Devang Patel8aa61472010-07-07 22:20:57 +0000331 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
Bill Wendling168c1902012-11-07 05:19:04 +0000332 // Pick up abstract subprogram DIE.
Devang Patel8aa61472010-07-07 22:20:57 +0000333 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000334 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
335 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000336 SPCU->addDie(SPDie);
Bill Wendlinga4c76932012-11-07 04:42:18 +0000337 } else {
338 DISubprogram SPDecl = SP.getFunctionDeclaration();
339 if (!SPDecl.isSubprogram()) {
340 // There is not any need to generate specification DIE for a function
341 // defined at compile unit level. If a function is defined inside another
342 // function then gdb prefers the definition at top level and but does not
343 // expect specification DIE in parent function. So avoid creating
344 // specification DIE for a function defined inside a function.
345 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
346 !SP.getContext().isFile() &&
347 !isSubprogramContext(SP.getContext())) {
348 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
349
350 // Add arguments.
351 DICompositeType SPTy = SP.getType();
352 DIArray Args = SPTy.getTypeArray();
353 unsigned SPTag = SPTy.getTag();
354 if (SPTag == dwarf::DW_TAG_subroutine_type)
355 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
356 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
357 DIType ATy = DIType(Args.getElement(i));
358 SPCU->addType(Arg, ATy);
359 if (ATy.isArtificial())
360 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
361 if (ATy.isObjectPointer())
362 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer,
363 dwarf::DW_FORM_ref4, Arg);
364 SPDie->addChild(Arg);
365 }
366 DIE *SPDeclDie = SPDie;
367 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000368 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification,
369 dwarf::DW_FORM_ref4, SPDeclDie);
Bill Wendlinga4c76932012-11-07 04:42:18 +0000370 SPCU->addDie(SPDie);
371 }
372 }
Devang Patel8aa61472010-07-07 22:20:57 +0000373 }
374
Devang Patel3cbee302011-04-12 22:53:02 +0000375 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
376 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
377 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
378 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000379 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
380 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000381 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000382
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000383 // Add name to the name table, we do this here because we're guaranteed
384 // to have concrete versions of our DW_TAG_subprogram nodes.
385 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000386
Chris Lattnerd38fee82010-04-05 00:13:49 +0000387 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000388}
389
Eric Christopherb6dc8652012-11-27 22:43:45 +0000390// Construct new DW_TAG_lexical_block for this scope and attach
391// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000392DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
Devang Pateld3024342011-08-15 22:24:32 +0000393 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000394 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
395 if (Scope->isAbstractScope())
396 return ScopeDIE;
397
Devang Patelbf47fdb2011-08-10 20:55:27 +0000398 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000399 if (Ranges.empty())
400 return 0;
401
Devang Patelbf47fdb2011-08-10 20:55:27 +0000402 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000403 if (Ranges.size() > 1) {
404 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000405 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000406 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000407 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000408 DebugRangeSymbols.size()
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000409 * Asm->getDataLayout().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000410 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000411 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000412 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
413 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000414 }
415 DebugRangeSymbols.push_back(NULL);
416 DebugRangeSymbols.push_back(NULL);
417 return ScopeDIE;
418 }
419
Devang Patelc3f5f782010-05-25 23:40:22 +0000420 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
421 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000422
Devang Patelc3f5f782010-05-25 23:40:22 +0000423 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000424
Chris Lattnerb7db7332010-03-09 01:58:53 +0000425 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
426 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000427
Devang Patel3cbee302011-04-12 22:53:02 +0000428 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
429 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000430
431 return ScopeDIE;
432}
433
Eric Christopherb6dc8652012-11-27 22:43:45 +0000434// This scope represents inlined body of a function. Construct DIE to
435// represent this concrete inlined copy of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000436DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
437 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000438 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000439 assert(Ranges.empty() == false &&
440 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000441
Devang Patel26a92002011-07-27 00:34:13 +0000442 if (!Scope->getScopeNode())
443 return NULL;
444 DIScope DS(Scope->getScopeNode());
445 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000446 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
447 if (!OriginDIE) {
Bill Wendlinge0aae5b2012-10-30 17:51:02 +0000448 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patel26a92002011-07-27 00:34:13 +0000449 return NULL;
450 }
451
Devang Patelbf47fdb2011-08-10 20:55:27 +0000452 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000453 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
454 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000455
Devang Patel0afbf232010-07-08 22:39:20 +0000456 if (StartLabel == 0 || EndLabel == 0) {
Bill Wendlinge0aae5b2012-10-30 17:51:02 +0000457 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000458 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000459 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000460 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000461 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000462 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000463
Devang Pateld96efb82011-05-05 17:54:26 +0000464 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000465 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
466 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000467
Devang Patel26a92002011-07-27 00:34:13 +0000468 if (Ranges.size() > 1) {
469 // .debug_range section has not been laid out yet. Emit offset in
470 // .debug_range as a uint, size 4, for now. emitDIE will handle
471 // DW_AT_ranges appropriately.
472 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000473 DebugRangeSymbols.size()
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000474 * Asm->getDataLayout().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000475 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000476 RE = Ranges.end(); RI != RE; ++RI) {
477 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
478 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
479 }
480 DebugRangeSymbols.push_back(NULL);
481 DebugRangeSymbols.push_back(NULL);
482 } else {
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000483 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel5bc942c2011-08-10 23:58:09 +0000484 StartLabel);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000485 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel5bc942c2011-08-10 23:58:09 +0000486 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000487 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000488
489 InlinedSubprogramDIEs.insert(OriginDIE);
490
491 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000492 //.debug_inlined section specification does not clearly state how
493 // to emit inlined scope that is split into multiple instruction ranges.
494 // For now, use first instruction range and emit low_pc/high_pc pair and
495 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000496 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000497 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000498
499 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000500 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000501 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000502 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000503 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000504
Devang Patel53bb5c92009-11-10 23:06:00 +0000505 DILocation DL(Scope->getInlinedAt());
Eric Christopher08212002012-03-26 21:38:38 +0000506 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000507 getOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patel3cbee302011-04-12 22:53:02 +0000508 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000509
Eric Christopher309bedd2011-12-04 06:02:38 +0000510 // Add name to the name table, we do this here because we're guaranteed
511 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
512 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000513
Devang Patel53bb5c92009-11-10 23:06:00 +0000514 return ScopeDIE;
515}
516
Eric Christopherb6dc8652012-11-27 22:43:45 +0000517// Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000518DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000519 if (!Scope || !Scope->getScopeNode())
520 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000521
Nick Lewycky746cb672011-10-26 22:55:33 +0000522 SmallVector<DIE *, 8> Children;
Eric Christophere5212782012-09-12 23:36:19 +0000523 DIE *ObjectPointer = NULL;
Devang Patel0478c152011-03-01 22:58:55 +0000524
525 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000526 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000527 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
528 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000529 if (DIE *Arg =
Eric Christophere5212782012-09-12 23:36:19 +0000530 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope())) {
Devang Patel0478c152011-03-01 22:58:55 +0000531 Children.push_back(Arg);
Eric Christophere5212782012-09-12 23:36:19 +0000532 if (ArgDV->isObjectPointer()) ObjectPointer = Arg;
533 }
Devang Patel0478c152011-03-01 22:58:55 +0000534
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000535 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000536 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000537 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000538 if (DIE *Variable =
Eric Christopher7b451cf2012-09-21 22:18:52 +0000539 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope())) {
Devang Patel5bc9fec2011-02-19 01:31:27 +0000540 Children.push_back(Variable);
Eric Christopher7b451cf2012-09-21 22:18:52 +0000541 if (Variables[i]->isObjectPointer()) ObjectPointer = Variable;
542 }
Devang Patelbf47fdb2011-08-10 20:55:27 +0000543 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000544 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000545 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000546 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000547 DIScope DS(Scope->getScopeNode());
548 DIE *ScopeDIE = NULL;
549 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000550 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000551 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000552 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000553 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000554 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000555 // Note down abstract DIE.
556 if (ScopeDIE)
557 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
558 }
Devang Patel3c91b052010-03-08 20:52:55 +0000559 else
Devang Pateld3024342011-08-15 22:24:32 +0000560 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000561 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000562 else {
563 // There is no need to emit empty lexical block DIE.
564 if (Children.empty())
565 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000566 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000567 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000568
Devang Patelaead63c2010-03-29 22:59:58 +0000569 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000570
Devang Patel5bc9fec2011-02-19 01:31:27 +0000571 // Add children
572 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
573 E = Children.end(); I != E; ++I)
574 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000575
Eric Christophere5212782012-09-12 23:36:19 +0000576 if (DS.isSubprogram() && ObjectPointer != NULL)
577 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer,
578 dwarf::DW_FORM_ref4, ObjectPointer);
579
Jim Grosbach1e20b962010-07-21 21:21:52 +0000580 if (DS.isSubprogram())
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000581 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000582
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000583 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000584}
585
Eric Christopherb6dc8652012-11-27 22:43:45 +0000586// Look up the source id with the given directory and source file names.
587// If none currently exists, create a new id and insert it in the
588// SourceIds map. This can update DirectoryNames and SourceFileNames maps
589// as well.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000590unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName,
Devang Patel23670e52011-03-24 20:30:50 +0000591 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000592 // If FE did not provide a file name, then assume stdin.
593 if (FileName.empty())
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000594 return getOrCreateSourceID("<stdin>", StringRef());
Devang Patel23670e52011-03-24 20:30:50 +0000595
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000596 // TODO: this might not belong here. See if we can factor this better.
597 if (DirName == CompilationDir)
598 DirName = "";
599
Nick Lewycky44d798d2011-10-17 23:05:28 +0000600 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel1905a182010-09-16 20:57:49 +0000601
Benjamin Kramer74612c22012-03-11 14:56:26 +0000602 // We look up the file/dir pair by concatenating them with a zero byte.
603 SmallString<128> NamePair;
604 NamePair += DirName;
605 NamePair += '\0'; // Zero bytes are not allowed in paths.
606 NamePair += FileName;
607
608 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
609 if (Ent.getValue() != SrcId)
610 return Ent.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000611
Rafael Espindola5c055632010-11-18 02:04:25 +0000612 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer74612c22012-03-11 14:56:26 +0000613 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000614
615 return SrcId;
616}
617
Eric Christopherb6dc8652012-11-27 22:43:45 +0000618// Create new CompileUnit for the given metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000619CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000620 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000621 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000622 CompilationDir = DIUnit.getDirectory();
Eli Benderskyd4a05e02012-12-03 18:45:45 +0000623 // Call this to emit a .file directive if it wasn't emitted for the source
624 // file this CU comes from yet.
625 getOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000626
627 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyd4a05e02012-12-03 18:45:45 +0000628 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
629 DIUnit.getLanguage(), Die, Asm, this);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000630 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000631 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
632 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000633 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher6635cad2012-08-01 18:19:01 +0000634 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
635 // into an entity.
636 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000637 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000638 // compile unit in debug_line section.
Rafael Espindola2241e512012-06-22 13:24:07 +0000639 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindola597a7662011-05-04 17:44:06 +0000640 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000641 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000642 else
Devang Patel3cbee302011-04-12 22:53:02 +0000643 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000644
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000645 if (!CompilationDir.empty())
646 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000647 if (DIUnit.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000648 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000649
Devang Patel65dbc902009-11-25 17:36:49 +0000650 StringRef Flags = DIUnit.getFlags();
651 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000652 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000653
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000654 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000655 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000656 dwarf::DW_FORM_data1, RVer);
657
Devang Patel163a9f72010-05-10 22:49:55 +0000658 if (!FirstCU)
659 FirstCU = NewCU;
Eric Christopher4daaed12012-12-10 19:51:21 +0000660 if (useSplitDwarf() && !SkeletonCU)
661 SkeletonCU = constructSkeletonCU(N);
Eric Christopher98e237f2012-11-30 23:59:06 +0000662
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000663 InfoHolder.addUnit(NewCU);
664
Devang Patel163a9f72010-05-10 22:49:55 +0000665 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000666 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000667}
668
Eric Christopherb6dc8652012-11-27 22:43:45 +0000669// Construct subprogram DIE.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000670void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
Devang Patel3655a212011-08-15 23:36:40 +0000671 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000672 CompileUnit *&CURef = SPMap[N];
673 if (CURef)
674 return;
675 CURef = TheCU;
676
Devang Patele4b27562009-08-28 23:24:31 +0000677 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000678 if (!SP.isDefinition())
679 // This is a method declaration which will be handled while constructing
680 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000681 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000682
Devang Pateldbc64af2011-08-15 17:24:54 +0000683 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000684
685 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000686 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000687
688 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000689 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000690
Devang Patel13e16b62009-06-26 01:49:18 +0000691 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000692}
693
Eric Christopherb6dc8652012-11-27 22:43:45 +0000694// Collect debug info from named mdnodes such as llvm.dbg.enum and llvm.dbg.ty.
Eric Christopherc4639d62012-11-19 22:42:15 +0000695void DwarfDebug::collectInfoFromNamedMDNodes(const Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000696 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
697 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
698 const MDNode *N = NMD->getOperand(i);
699 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
700 constructSubprogramDIE(CU, N);
701 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000702
Devang Patel94c7ddb2011-08-16 22:09:43 +0000703 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
704 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
705 const MDNode *N = NMD->getOperand(i);
706 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000707 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000708 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000709
Devang Patel02e603f2011-08-15 23:47:24 +0000710 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
711 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
712 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000713 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
714 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000715 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000716
Devang Patel02e603f2011-08-15 23:47:24 +0000717 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
718 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
719 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000720 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
721 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000722 }
723}
724
Eric Christopherb6dc8652012-11-27 22:43:45 +0000725// Collect debug info using DebugInfoFinder.
726// FIXME - Remove this when dragonegg switches to DIBuilder.
Eric Christopherc4639d62012-11-19 22:42:15 +0000727bool DwarfDebug::collectLegacyDebugInfo(const Module *M) {
Devang Patel02e603f2011-08-15 23:47:24 +0000728 DebugInfoFinder DbgFinder;
729 DbgFinder.processModule(*M);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000730
Devang Patel02e603f2011-08-15 23:47:24 +0000731 bool HasDebugInfo = false;
732 // Scan all the compile-units to see if there are any marked as the main
733 // unit. If not, we do not generate debug info.
734 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
735 E = DbgFinder.compile_unit_end(); I != E; ++I) {
736 if (DICompileUnit(*I).isMain()) {
737 HasDebugInfo = true;
738 break;
739 }
740 }
741 if (!HasDebugInfo) return false;
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000742
Devang Patel02e603f2011-08-15 23:47:24 +0000743 // Create all the compile unit DIEs.
744 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
745 E = DbgFinder.compile_unit_end(); I != E; ++I)
746 constructCompileUnit(*I);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000747
Devang Patel02e603f2011-08-15 23:47:24 +0000748 // Create DIEs for each global variable.
749 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
750 E = DbgFinder.global_variable_end(); I != E; ++I) {
751 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000752 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000753 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000754 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000755
Devang Patel02e603f2011-08-15 23:47:24 +0000756 // Create DIEs for each subprogram.
757 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
758 E = DbgFinder.subprogram_end(); I != E; ++I) {
759 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000760 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
761 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000762 }
763
764 return HasDebugInfo;
765}
766
Eric Christopherb6dc8652012-11-27 22:43:45 +0000767// Emit all Dwarf sections that should come prior to the content. Create
768// global DIEs and emit initial debug info sections. This is invoked by
769// the target AsmPrinter.
Eric Christopherc4639d62012-11-19 22:42:15 +0000770void DwarfDebug::beginModule() {
Devang Pateleac9c072010-04-27 19:46:33 +0000771 if (DisableDebugInfoPrinting)
772 return;
773
Eric Christopherc4639d62012-11-19 22:42:15 +0000774 const Module *M = MMI->getModule();
775
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000776 // If module has named metadata anchors then use them, otherwise scan the
777 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000778 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
779 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000780 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
781 DICompileUnit CUNode(CU_Nodes->getOperand(i));
782 CompileUnit *CU = constructCompileUnit(CUNode);
783 DIArray GVs = CUNode.getGlobalVariables();
784 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000785 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000786 DIArray SPs = CUNode.getSubprograms();
787 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
788 constructSubprogramDIE(CU, SPs.getElement(i));
789 DIArray EnumTypes = CUNode.getEnumTypes();
790 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
791 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
792 DIArray RetainedTypes = CUNode.getRetainedTypes();
793 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
794 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
795 }
Devang Patel02e603f2011-08-15 23:47:24 +0000796 } else if (!collectLegacyDebugInfo(M))
797 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000798
Devang Patel02e603f2011-08-15 23:47:24 +0000799 collectInfoFromNamedMDNodes(M);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000800
Chris Lattnerd850ac72010-04-05 02:19:28 +0000801 // Tell MMI that we have debug info.
802 MMI->setDebugInfoAvailability(true);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000803
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000804 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000805 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000806}
807
Eric Christopher4117bec2012-11-22 00:59:49 +0000808// Attach DW_AT_inline attribute with inlined subprogram DIEs.
809void DwarfDebug::computeInlinedDIEs() {
Devang Patel53bb5c92009-11-10 23:06:00 +0000810 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
811 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000812 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
Devang Patel53bb5c92009-11-10 23:06:00 +0000813 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000814 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000815 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000816 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000817 AE = AbstractSPDies.end(); AI != AE; ++AI) {
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000818 DIE *ISP = AI->second;
819 if (InlinedSubprogramDIEs.count(ISP))
820 continue;
821 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
822 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000823}
824
825// Collect info for variables that were optimized out.
826void DwarfDebug::collectDeadVariables() {
827 const Module *M = MMI->getModule();
828 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
829
830 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
831 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
832 DICompileUnit TheCU(CU_Nodes->getOperand(i));
833 DIArray Subprograms = TheCU.getSubprograms();
834 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopherbdab8002012-11-27 00:13:51 +0000835 DISubprogram SP(Subprograms.getElement(i));
836 if (ProcessedSPNodes.count(SP) != 0) continue;
837 if (!SP.Verify()) continue;
838 if (!SP.isDefinition()) continue;
839 DIArray Variables = SP.getVariables();
840 if (Variables.getNumElements() == 0) continue;
Eric Christopher4117bec2012-11-22 00:59:49 +0000841
Eric Christopherbdab8002012-11-27 00:13:51 +0000842 LexicalScope *Scope =
843 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
844 DeadFnScopeMap[SP] = Scope;
Eric Christopher4117bec2012-11-22 00:59:49 +0000845
Eric Christopherbdab8002012-11-27 00:13:51 +0000846 // Construct subprogram DIE and add variables DIEs.
847 CompileUnit *SPCU = CUMap.lookup(TheCU);
848 assert(SPCU && "Unable to find Compile Unit!");
849 constructSubprogramDIE(SPCU, SP);
850 DIE *ScopeDIE = SPCU->getDIE(SP);
851 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
852 DIVariable DV(Variables.getElement(vi));
853 if (!DV.Verify()) continue;
854 DbgVariable *NewVar = new DbgVariable(DV, NULL);
855 if (DIE *VariableDIE =
856 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
857 ScopeDIE->addChild(VariableDIE);
858 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000859 }
860 }
861 }
862 DeleteContainerSeconds(DeadFnScopeMap);
863}
864
865void DwarfDebug::finalizeModuleInfo() {
866 // Collect info for variables that were optimized out.
867 collectDeadVariables();
868
869 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
870 computeInlinedDIEs();
Devang Patel53bb5c92009-11-10 23:06:00 +0000871
Eric Christopher6635cad2012-08-01 18:19:01 +0000872 // Emit DW_AT_containing_type attribute to connect types with their
873 // vtable holding type.
Devang Pateldbc64af2011-08-15 17:24:54 +0000874 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000875 CUE = CUMap.end(); CUI != CUE; ++CUI) {
Devang Pateldbc64af2011-08-15 17:24:54 +0000876 CompileUnit *TheCU = CUI->second;
877 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000878 }
879
Eric Christopher4117bec2012-11-22 00:59:49 +0000880 // Compute DIE offsets and sizes.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000881 InfoHolder.computeSizeAndOffsets();
882 if (useSplitDwarf())
883 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher4117bec2012-11-22 00:59:49 +0000884}
885
886void DwarfDebug::endSections() {
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000887 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000888 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000889 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000890 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000891 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000892
893 // End text sections.
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +0000894 for (unsigned I = 0, E = SectionMap.size(); I != E; ++I) {
895 Asm->OutStreamer.SwitchSection(SectionMap[I]);
896 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", I+1));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000897 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000898}
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000899
Eric Christopherb6dc8652012-11-27 22:43:45 +0000900// Emit all Dwarf sections that should come after the content.
Eric Christopher4117bec2012-11-22 00:59:49 +0000901void DwarfDebug::endModule() {
902
903 if (!FirstCU) return;
904
905 // End any existing sections.
906 // TODO: Does this need to happen?
907 endSections();
908
909 // Finalize the debug info for the module.
910 finalizeModuleInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000911
Eric Christopher97c34722012-11-19 19:43:59 +0000912 // Emit initial sections.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000913 emitSectionLabels();
Eric Christopher97c34722012-11-19 19:43:59 +0000914
Eric Christopher4daaed12012-12-10 19:51:21 +0000915 if (!useSplitDwarf()) {
Eric Christopher42885022012-11-27 22:43:42 +0000916 // Emit all the DIEs into a debug info section.
917 emitDebugInfo();
Eric Christopher74802f72012-11-27 00:41:54 +0000918
Eric Christopher42885022012-11-27 22:43:42 +0000919 // Corresponding abbreviations into a abbrev section.
920 emitAbbreviations();
921
922 // Emit info into a debug loc section.
923 emitDebugLoc();
924
925 // Emit info into a debug aranges section.
926 emitDebugARanges();
927
928 // Emit info into a debug ranges section.
929 emitDebugRanges();
930
931 // Emit info into a debug macinfo section.
932 emitDebugMacInfo();
933
934 // Emit inline info.
935 // TODO: When we don't need the option anymore we
936 // can remove all of the code that this section
937 // depends upon.
938 if (useDarwinGDBCompat())
939 emitDebugInlineInfo();
940 } else {
Eric Christopher0b944ee2012-12-11 19:42:09 +0000941 // TODO: Fill this in for separated debug sections and separate
Eric Christopher42885022012-11-27 22:43:42 +0000942 // out information into new sections.
943
Eric Christopher98e237f2012-11-30 23:59:06 +0000944 // Emit the debug info section and compile units.
Eric Christopher42885022012-11-27 22:43:42 +0000945 emitDebugInfo();
Eric Christopher98e237f2012-11-30 23:59:06 +0000946 emitDebugInfoDWO();
Eric Christopher42885022012-11-27 22:43:42 +0000947
948 // Corresponding abbreviations into a abbrev section.
949 emitAbbreviations();
Eric Christopher6eebe472012-12-19 22:02:53 +0000950 emitDebugAbbrevDWO();
Eric Christopher42885022012-11-27 22:43:42 +0000951
952 // Emit info into a debug loc section.
953 emitDebugLoc();
954
955 // Emit info into a debug aranges section.
956 emitDebugARanges();
957
958 // Emit info into a debug ranges section.
959 emitDebugRanges();
960
961 // Emit info into a debug macinfo section.
962 emitDebugMacInfo();
963
964 // Emit inline info.
965 // TODO: When we don't need the option anymore we
966 // can remove all of the code that this section
967 // depends upon.
968 if (useDarwinGDBCompat())
969 emitDebugInlineInfo();
970 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000971
Eric Christopher9d9f5a52012-08-23 07:32:06 +0000972 // Emit info into the dwarf accelerator table sections.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000973 if (useDwarfAccelTables()) {
Eric Christopher09ac3d82011-11-07 09:24:32 +0000974 emitAccelNames();
975 emitAccelObjC();
976 emitAccelNamespaces();
977 emitAccelTypes();
978 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000979
Devang Patel193f7202009-11-24 01:14:22 +0000980 // Emit info into a debug pubtypes section.
Eric Christopher360f0062012-08-23 07:10:56 +0000981 // TODO: When we don't need the option anymore we can
982 // remove all of the code that adds to the table.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000983 if (useDarwinGDBCompat())
Eric Christopher360f0062012-08-23 07:10:56 +0000984 emitDebugPubTypes();
Devang Patel193f7202009-11-24 01:14:22 +0000985
Eric Christopher42885022012-11-27 22:43:42 +0000986 // Finally emit string information into a string table.
Eric Christopherfcdbecb2012-11-27 06:49:23 +0000987 emitDebugStr();
988
Devang Patele9a1cca2010-08-02 17:32:15 +0000989 // clean up.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000990 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000991 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
992 E = CUMap.end(); I != E; ++I)
993 delete I->second;
Eric Christopher9ec87b32012-12-10 19:51:18 +0000994
Eric Christopher4daaed12012-12-10 19:51:21 +0000995 delete SkeletonCU;
Eric Christopher9ec87b32012-12-10 19:51:18 +0000996
Eric Christopher98e237f2012-11-30 23:59:06 +0000997 // Reset these for the next Module if we have one.
998 FirstCU = NULL;
Eric Christopher4daaed12012-12-10 19:51:21 +0000999 SkeletonCU = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001000}
1001
Eric Christopherb6dc8652012-11-27 22:43:45 +00001002// Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +00001003DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +00001004 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +00001005 LLVMContext &Ctx = DV->getContext();
1006 // More then one inlined variable corresponds to one abstract variable.
1007 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +00001008 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001009 if (AbsDbgVariable)
1010 return AbsDbgVariable;
1011
Devang Patelbf47fdb2011-08-10 20:55:27 +00001012 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00001013 if (!Scope)
1014 return NULL;
1015
Devang Patel5a1a67c2011-08-15 19:01:20 +00001016 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001017 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00001018 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00001019 return AbsDbgVariable;
1020}
1021
Eric Christopherb6dc8652012-11-27 22:43:45 +00001022// If Var is a current function argument then add it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +00001023bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +00001024 DbgVariable *Var, LexicalScope *Scope) {
1025 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +00001026 return false;
1027 DIVariable DV = Var->getVariable();
1028 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1029 return false;
1030 unsigned ArgNo = DV.getArgNumber();
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001031 if (ArgNo == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001032 return false;
1033
Devang Patelcb3a6572011-03-03 20:02:02 +00001034 size_t Size = CurrentFnArguments.size();
1035 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001036 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +00001037 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +00001038 // arguments does the function have at source level.
1039 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +00001040 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +00001041 CurrentFnArguments[ArgNo - 1] = Var;
1042 return true;
1043}
1044
Eric Christopherb6dc8652012-11-27 22:43:45 +00001045// Collect variable information from side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001046void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001047DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +00001048 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +00001049 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1050 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1051 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00001052 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00001053 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00001054 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00001055 DIVariable DV(Var);
1056 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001057
Devang Patelbf47fdb2011-08-10 20:55:27 +00001058 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001059
Devang Patelfb0ee432009-11-10 23:20:04 +00001060 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00001061 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00001062 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001063
Devang Patel26c1e562010-05-20 16:36:41 +00001064 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001065 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +00001066 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +00001067 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001068 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001069 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001070 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +00001071 }
Devang Patelee432862010-05-20 19:57:06 +00001072}
Devang Patel90a48ad2010-03-15 18:33:46 +00001073
Eric Christopherb6dc8652012-11-27 22:43:45 +00001074// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1075// defined reg.
Devang Patelc3f5f782010-05-25 23:40:22 +00001076static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001077 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001078 return MI->getNumOperands() == 3 &&
1079 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
1080 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +00001081}
1082
Eric Christopherb6dc8652012-11-27 22:43:45 +00001083// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001084static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1085 const MCSymbol *FLabel,
Devang Patel90b40412011-07-08 17:09:57 +00001086 const MCSymbol *SLabel,
1087 const MachineInstr *MI) {
1088 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1089
1090 if (MI->getNumOperands() != 3) {
1091 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1092 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1093 }
1094 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1095 MachineLocation MLoc;
1096 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1097 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1098 }
1099 if (MI->getOperand(0).isImm())
1100 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1101 if (MI->getOperand(0).isFPImm())
1102 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1103 if (MI->getOperand(0).isCImm())
1104 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1105
Craig Topper5e25ee82012-02-05 08:31:47 +00001106 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +00001107}
1108
Eric Christopherb6dc8652012-11-27 22:43:45 +00001109// Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001110void
Devang Patel78e127d2010-06-25 22:07:34 +00001111DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1112 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001113
Eric Christopherb6dc8652012-11-27 22:43:45 +00001114 // collection info from MMI table.
Devang Patelee432862010-05-20 19:57:06 +00001115 collectVariableInfoFromMMITable(MF, Processed);
1116
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001117 for (SmallVectorImpl<const MDNode*>::const_iterator
1118 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1119 ++UVI) {
1120 const MDNode *Var = *UVI;
1121 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001122 continue;
1123
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001124 // History contains relevant DBG_VALUE instructions for Var and instructions
1125 // clobbering it.
1126 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1127 if (History.empty())
1128 continue;
1129 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001130
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001131 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001132 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001133 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1134 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001135 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001136 else {
1137 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001138 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001139 else {
1140 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001141 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001142 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001143 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001144 }
1145 }
Devang Patelee432862010-05-20 19:57:06 +00001146 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001147 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001148 continue;
1149
1150 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001151 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001152 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1153 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001154 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001155 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001156 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001157 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001158
Eric Christopherc56e3f02012-10-08 20:48:54 +00001159 // Simplify ranges that are fully coalesced.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001160 if (History.size() <= 1 || (History.size() == 2 &&
1161 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001162 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001163 continue;
1164 }
1165
1166 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001167 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001168
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001169 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1170 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1171 const MachineInstr *Begin = *HI;
1172 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001173
Devang Patel4ada1d72011-06-01 23:00:17 +00001174 // Check if DBG_VALUE is truncating a range.
1175 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1176 && !Begin->getOperand(0).getReg())
1177 continue;
1178
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001179 // Compute the range for a register location.
1180 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1181 const MCSymbol *SLabel = 0;
1182
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001183 if (HI + 1 == HE)
1184 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001185 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001186 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001187 else {
1188 const MachineInstr *End = HI[1];
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001189 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Devang Patel476df5f2011-07-07 21:44:42 +00001190 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001191 if (End->isDebugValue())
1192 SLabel = getLabelBeforeInsn(End);
1193 else {
1194 // End is a normal instruction clobbering the range.
1195 SLabel = getLabelAfterInsn(End);
1196 assert(SLabel && "Forgot label after clobber instruction");
1197 ++HI;
1198 }
1199 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001200
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001201 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001202 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1203 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001204 }
1205 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001206 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001207
1208 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001209 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1210 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1211 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1212 DIVariable DV(Variables.getElement(i));
1213 if (!DV || !DV.Verify() || !Processed.insert(DV))
1214 continue;
1215 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1216 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001217 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001218}
Devang Patel98e1cac2010-05-14 21:01:35 +00001219
Eric Christopherb6dc8652012-11-27 22:43:45 +00001220// Return Label preceding the instruction.
Devang Patelc3f5f782010-05-25 23:40:22 +00001221const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001222 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1223 assert(Label && "Didn't insert label before instruction");
1224 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001225}
1226
Eric Christopherb6dc8652012-11-27 22:43:45 +00001227// Return Label immediately following the instruction.
Devang Patelc3f5f782010-05-25 23:40:22 +00001228const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001229 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001230}
1231
Eric Christopherb6dc8652012-11-27 22:43:45 +00001232// Process beginning of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +00001233void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001234 // Check if source location changes, but ignore DBG_VALUE locations.
1235 if (!MI->isDebugValue()) {
1236 DebugLoc DL = MI->getDebugLoc();
1237 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001238 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001239 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001240 if (DL == PrologEndLoc) {
1241 Flags |= DWARF2_FLAG_PROLOGUE_END;
1242 PrologEndLoc = DebugLoc();
1243 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001244 if (PrologEndLoc.isUnknown())
1245 Flags |= DWARF2_FLAG_IS_STMT;
1246
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001247 if (!DL.isUnknown()) {
1248 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001249 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001250 } else
Devang Patel4243e672011-05-11 19:22:19 +00001251 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001252 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001253 }
Devang Patelaead63c2010-03-29 22:59:58 +00001254
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001255 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001256 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1257 LabelsBeforeInsn.find(MI);
1258
1259 // No label needed.
1260 if (I == LabelsBeforeInsn.end())
1261 return;
1262
1263 // Label already assigned.
1264 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001265 return;
Devang Patel553881b2010-03-29 17:20:31 +00001266
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001267 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001268 PrevLabel = MMI->getContext().CreateTempSymbol();
1269 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001270 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001271 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001272}
1273
Eric Christopherb6dc8652012-11-27 22:43:45 +00001274// Process end of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +00001275void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001276 // Don't create a new label after DBG_VALUE instructions.
1277 // They don't generate code.
1278 if (!MI->isDebugValue())
1279 PrevLabel = 0;
1280
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001281 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1282 LabelsAfterInsn.find(MI);
1283
1284 // No label needed.
1285 if (I == LabelsAfterInsn.end())
1286 return;
1287
1288 // Label already assigned.
1289 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001290 return;
1291
1292 // We need a label after this instruction.
1293 if (!PrevLabel) {
1294 PrevLabel = MMI->getContext().CreateTempSymbol();
1295 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001296 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001297 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001298}
1299
Eric Christopherb6dc8652012-11-27 22:43:45 +00001300// Each LexicalScope has first instruction and last instruction to mark
1301// beginning and end of a scope respectively. Create an inverse map that list
1302// scopes starts (and ends) with an instruction. One instruction may start (or
1303// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001304void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001305 SmallVector<LexicalScope *, 4> WorkList;
1306 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001307 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001308 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001309
Devang Patelbf47fdb2011-08-10 20:55:27 +00001310 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001311 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001312 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001313 SE = Children.end(); SI != SE; ++SI)
1314 WorkList.push_back(*SI);
1315
Devang Patel53bb5c92009-11-10 23:06:00 +00001316 if (S->isAbstractScope())
1317 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001318
Devang Patelbf47fdb2011-08-10 20:55:27 +00001319 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001320 if (Ranges.empty())
1321 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001322 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001323 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001324 assert(RI->first && "InsnRange does not have first instruction!");
1325 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001326 requestLabelBeforeInsn(RI->first);
1327 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001328 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001329 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001330}
1331
Eric Christopherb6dc8652012-11-27 22:43:45 +00001332// Get MDNode for DebugLoc's scope.
Devang Patela3f48672011-05-09 22:14:49 +00001333static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1334 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1335 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1336 return DL.getScope(Ctx);
1337}
1338
Eric Christopherb6dc8652012-11-27 22:43:45 +00001339// Walk up the scope chain of given debug loc and find line number info
1340// for the function.
Devang Patel4243e672011-05-11 19:22:19 +00001341static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1342 const MDNode *Scope = getScopeNode(DL, Ctx);
1343 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher6126a1e2012-04-03 00:43:49 +00001344 if (SP.Verify()) {
1345 // Check for number of operands since the compatibility is
1346 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001347 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-04-03 00:43:49 +00001348 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1349 else
1350 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1351 }
1352
Devang Patel4243e672011-05-11 19:22:19 +00001353 return DebugLoc();
1354}
1355
Eric Christopherb6dc8652012-11-27 22:43:45 +00001356// Gather pre-function debug information. Assumes being called immediately
1357// after the function entry point has been emitted.
Chris Lattnereec791a2010-01-26 23:18:02 +00001358void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001359 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001360 LScopes.initialize(*MF);
1361 if (LScopes.empty()) return;
1362 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001363
Devang Pateleac9c072010-04-27 19:46:33 +00001364 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1365 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001366 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001367 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001368
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001369 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1370
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001371 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopherb6dc8652012-11-27 22:43:45 +00001372 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001373 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1374
Devang Patelb2b31a62010-05-26 19:37:24 +00001375 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001376 I != E; ++I) {
1377 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001378 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1379 II != IE; ++II) {
1380 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001381
Devang Patelb2b31a62010-05-26 19:37:24 +00001382 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001383 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001384
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001385 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001386 const MDNode *Var =
1387 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001388
1389 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001390 if (isDbgValueInDefinedReg(MI))
1391 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1392
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001393 // Check the history of this variable.
1394 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1395 if (History.empty()) {
1396 UserVariables.push_back(Var);
1397 // The first mention of a function argument gets the FunctionBeginSym
1398 // label, so arguments are visible when breaking at function entry.
1399 DIVariable DV(Var);
1400 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1401 DISubprogram(getDISubprogram(DV.getContext()))
1402 .describes(MF->getFunction()))
1403 LabelsBeforeInsn[MI] = FunctionBeginSym;
1404 } else {
1405 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1406 const MachineInstr *Prev = History.back();
1407 if (Prev->isDebugValue()) {
1408 // Coalesce identical entries at the end of History.
1409 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001410 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher02c1a642012-10-08 20:48:49 +00001411 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001412 << "\t" << *Prev
Devang Patel79862892011-07-07 00:14:27 +00001413 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001414 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001415 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001416
1417 // Terminate old register assignments that don't reach MI;
1418 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1419 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1420 isDbgValueInDefinedReg(Prev)) {
1421 // Previous register assignment needs to terminate at the end of
1422 // its basic block.
1423 MachineBasicBlock::const_iterator LastMI =
1424 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001425 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001426 // Drop DBG_VALUE for empty range.
Eric Christopher02c1a642012-10-08 20:48:49 +00001427 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Devang Patel79862892011-07-07 00:14:27 +00001428 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001429 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001430 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001431 else {
1432 // Terminate after LastMI.
1433 History.push_back(LastMI);
1434 }
1435 }
1436 }
1437 }
1438 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001439 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001440 // Not a DBG_VALUE instruction.
1441 if (!MI->isLabel())
1442 AtBlockEntry = false;
1443
Eric Christopher0313ced2012-10-04 20:46:14 +00001444 // First known non-DBG_VALUE and non-frame setup location marks
1445 // the beginning of the function body.
1446 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1447 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel4243e672011-05-11 19:22:19 +00001448 PrologEndLoc = MI->getDebugLoc();
1449
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001450 // Check if the instruction clobbers any registers with debug vars.
1451 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1452 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1453 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1454 continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001455 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1456 AI.isValid(); ++AI) {
1457 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001458 const MDNode *Var = LiveUserVar[Reg];
1459 if (!Var)
1460 continue;
1461 // Reg is now clobbered.
1462 LiveUserVar[Reg] = 0;
1463
1464 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001465 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1466 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001467 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001468 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1469 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001470 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001471 const MachineInstr *Prev = History.back();
1472 // Sanity-check: Register assignments are terminated at the end of
1473 // their block.
1474 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1475 continue;
1476 // Is the variable still in Reg?
1477 if (!isDbgValueInDefinedReg(Prev) ||
1478 Prev->getOperand(0).getReg() != Reg)
1479 continue;
1480 // Var is clobbered. Make sure the next instruction gets a label.
1481 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001482 }
1483 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001484 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001485 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001486 }
1487
1488 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1489 I != E; ++I) {
1490 SmallVectorImpl<const MachineInstr*> &History = I->second;
1491 if (History.empty())
1492 continue;
1493
1494 // Make sure the final register assignments are terminated.
1495 const MachineInstr *Prev = History.back();
1496 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1497 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001498 MachineBasicBlock::const_iterator LastMI =
Devang Patel5bc942c2011-08-10 23:58:09 +00001499 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001500 if (LastMI == PrevMBB->end())
1501 // Drop DBG_VALUE for empty range.
1502 History.pop_back();
1503 else {
1504 // Terminate after LastMI.
1505 History.push_back(LastMI);
1506 }
1507 }
1508 // Request labels for the full history.
1509 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1510 const MachineInstr *MI = History[i];
1511 if (MI->isDebugValue())
1512 requestLabelBeforeInsn(MI);
1513 else
1514 requestLabelAfterInsn(MI);
1515 }
1516 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001517
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001518 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001519 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001520
1521 // Record beginning of function.
1522 if (!PrologEndLoc.isUnknown()) {
1523 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1524 MF->getFunction()->getContext());
1525 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1526 FnStartDL.getScope(MF->getFunction()->getContext()),
David Blaikie836cfc42012-12-04 22:02:33 +00001527 // We'd like to list the prologue as "not statements" but GDB behaves
1528 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
David Blaikieb36c5312012-12-04 21:05:36 +00001529 DWARF2_FLAG_IS_STMT);
Devang Patel4243e672011-05-11 19:22:19 +00001530 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001531}
1532
Devang Patelbf47fdb2011-08-10 20:55:27 +00001533void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1534// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1535 ScopeVariables[LS].push_back(Var);
1536// Vars.push_back(Var);
1537}
1538
Eric Christopherb6dc8652012-11-27 22:43:45 +00001539// Gather and emit post-function debug information.
Chris Lattnereec791a2010-01-26 23:18:02 +00001540void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001541 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001542
Devang Patelbf47fdb2011-08-10 20:55:27 +00001543 // Define end label for subprogram.
1544 FunctionEndSym = Asm->GetTempSymbol("func_end",
1545 Asm->getFunctionNumber());
1546 // Assumes in correct section after the entry point.
1547 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001548
Devang Patelbf47fdb2011-08-10 20:55:27 +00001549 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1550 collectVariableInfo(MF, ProcessedVars);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001551
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001552 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001553 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001554 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001555
Devang Patelbf47fdb2011-08-10 20:55:27 +00001556 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001557 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1558 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1559 LexicalScope *AScope = AList[i];
1560 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001561 if (SP.Verify()) {
1562 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001563 DIArray Variables = SP.getVariables();
1564 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1565 DIVariable DV(Variables.getElement(i));
1566 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1567 continue;
Alexey Samsonovb67bd332012-07-06 08:45:08 +00001568 // Check that DbgVariable for DV wasn't created earlier, when
1569 // findAbstractVariable() was called for inlined instance of DV.
1570 LLVMContext &Ctx = DV->getContext();
1571 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1572 if (AbstractVariables.lookup(CleanDV))
1573 continue;
Devang Patel93d39be2011-08-19 23:28:12 +00001574 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1575 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001576 }
1577 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001578 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001579 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001580 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001581
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001582 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001583
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001584 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001585 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001586
Devang Patelbf47fdb2011-08-10 20:55:27 +00001587 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1588 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001589
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001590 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001591 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1592 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1593 DeleteContainerPointers(I->second);
1594 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001595 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001596 UserVariables.clear();
1597 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001598 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001599 LabelsBeforeInsn.clear();
1600 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001601 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001602}
1603
Eric Christopherb6dc8652012-11-27 22:43:45 +00001604// Register a source line with debug info. Returns the unique label that was
1605// emitted and which provides correspondence to the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001606void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1607 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001608 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001609 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001610 unsigned Src = 1;
1611 if (S) {
1612 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001613
Dan Gohman1cc0d622010-05-05 23:41:32 +00001614 if (Scope.isCompileUnit()) {
1615 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001616 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001617 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001618 } else if (Scope.isFile()) {
1619 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001620 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001621 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001622 } else if (Scope.isSubprogram()) {
1623 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001624 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001625 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001626 } else if (Scope.isLexicalBlockFile()) {
1627 DILexicalBlockFile DBF(S);
1628 Fn = DBF.getFilename();
1629 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001630 } else if (Scope.isLexicalBlock()) {
1631 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001632 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001633 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001634 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001635 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001636
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001637 Src = getOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001638 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001639 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001640}
1641
Bill Wendling829e67b2009-05-20 23:22:40 +00001642//===----------------------------------------------------------------------===//
1643// Emit Methods
1644//===----------------------------------------------------------------------===//
1645
Eric Christopherb6dc8652012-11-27 22:43:45 +00001646// Compute the size and offset of a DIE.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001647unsigned
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001648DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001649 // Get the children.
1650 const std::vector<DIE *> &Children = Die->getChildren();
1651
Bill Wendling94d04b82009-05-20 23:21:38 +00001652 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001653 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001654
1655 // Get the abbreviation for this DIE.
1656 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001657 const DIEAbbrev *Abbrev = Abbreviations->at(AbbrevNumber - 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001658
1659 // Set DIE offset
1660 Die->setOffset(Offset);
1661
1662 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001663 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001664
1665 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1666 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1667
1668 // Size the DIE attribute values.
1669 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1670 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001671 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001672
1673 // Size the DIE children if any.
1674 if (!Children.empty()) {
1675 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1676 "Children flag not set");
1677
1678 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopherfbd19752012-11-20 22:14:13 +00001679 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling94d04b82009-05-20 23:21:38 +00001680
1681 // End of children marker.
1682 Offset += sizeof(int8_t);
1683 }
1684
1685 Die->setSize(Offset - Die->getOffset());
1686 return Offset;
1687}
1688
Eric Christopherb6dc8652012-11-27 22:43:45 +00001689// Compute the size and offset of all the DIEs.
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001690void DwarfUnits::computeSizeAndOffsets() {
1691 for (SmallVector<CompileUnit *, 1>::iterator I = CUs.begin(),
1692 E = CUs.end(); I != E; ++I) {
Eric Christopher98e237f2012-11-30 23:59:06 +00001693 unsigned Offset =
1694 sizeof(int32_t) + // Length of Compilation Unit Info
1695 sizeof(int16_t) + // DWARF version number
1696 sizeof(int32_t) + // Offset Into Abbrev. Section
1697 sizeof(int8_t); // Pointer Size (in bytes)
1698
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001699 computeSizeAndOffset((*I)->getCUDie(), Offset);
Devang Patel163a9f72010-05-10 22:49:55 +00001700 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001701}
1702
Eric Christopherb6dc8652012-11-27 22:43:45 +00001703// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001704void DwarfDebug::emitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001705 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001706
Bill Wendling94d04b82009-05-20 23:21:38 +00001707 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001708 DwarfInfoSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001709 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001710 DwarfAbbrevSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001711 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher6eebe472012-12-19 22:02:53 +00001712 if (useSplitDwarf())
1713 DwarfAbbrevDWOSectionSym =
1714 emitSectionSym(Asm, TLOF.getDwarfAbbrevDWOSection(),
1715 "section_abbrev_dwo");
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001716 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001717
Chris Lattner9c69e285532010-04-04 22:59:04 +00001718 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001719 emitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001720
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001721 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
1722 emitSectionSym(Asm, TLOF.getDwarfLocSection());
1723 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001724 DwarfStrSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001725 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
1726 DwarfDebugRangeSectionSym = emitSectionSym(Asm, TLOF.getDwarfRangesSection(),
Devang Patelf2548ca2010-04-16 23:33:45 +00001727 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001728
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001729 DwarfDebugLocSectionSym = emitSectionSym(Asm, TLOF.getDwarfLocSection(),
Devang Patelc3f5f782010-05-25 23:40:22 +00001730 "section_debug_loc");
1731
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001732 TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
1733 emitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001734}
1735
Eric Christopherb6dc8652012-11-27 22:43:45 +00001736// Recursively emits a debug information entry.
Eric Christopher6eebe472012-12-19 22:02:53 +00001737void DwarfDebug::emitDIE(DIE *Die, std::vector<DIEAbbrev *> *Abbrevs) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001738 // Get the abbreviation for this DIE.
1739 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher6eebe472012-12-19 22:02:53 +00001740 const DIEAbbrev *Abbrev = Abbrevs->at(AbbrevNumber - 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001741
Bill Wendling94d04b82009-05-20 23:21:38 +00001742 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001743 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001744 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1745 Twine::utohexstr(Die->getOffset()) + ":0x" +
1746 Twine::utohexstr(Die->getSize()) + " " +
1747 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001748 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001749
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001750 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001751 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1752
1753 // Emit the DIE attribute values.
1754 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1755 unsigned Attr = AbbrevData[i].getAttribute();
1756 unsigned Form = AbbrevData[i].getForm();
1757 assert(Form && "Too many attributes for DIE (check abbreviation)");
1758
Chris Lattner3f53c832010-04-04 18:52:31 +00001759 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001760 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001761
Bill Wendling94d04b82009-05-20 23:21:38 +00001762 switch (Attr) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001763 case dwarf::DW_AT_abstract_origin: {
1764 DIEEntry *E = cast<DIEEntry>(Values[i]);
1765 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001766 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001767 Asm->EmitInt32(Addr);
1768 break;
1769 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001770 case dwarf::DW_AT_ranges: {
1771 // DW_AT_range Value encodes offset in debug_range section.
1772 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001773
Nick Lewyckyffccd922012-06-22 01:25:12 +00001774 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001775 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1776 V->getValue(),
1777 4);
1778 } else {
1779 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1780 V->getValue(),
1781 DwarfDebugRangeSectionSym,
1782 4);
1783 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001784 break;
1785 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001786 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-06-22 01:25:12 +00001787 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1788 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1789 Asm->EmitLabelReference(L->getValue(), 4);
1790 else
1791 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1792 } else {
Devang Patelc3f5f782010-05-25 23:40:22 +00001793 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00001794 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001795 break;
1796 }
Devang Patel2a361602010-09-29 19:08:08 +00001797 case dwarf::DW_AT_accessibility: {
1798 if (Asm->isVerbose()) {
1799 DIEInteger *V = cast<DIEInteger>(Values[i]);
1800 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1801 }
1802 Values[i]->EmitValue(Asm, Form);
1803 break;
1804 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001805 default:
1806 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001807 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001808 break;
1809 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001810 }
1811
1812 // Emit the DIE children if any.
1813 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1814 const std::vector<DIE *> &Children = Die->getChildren();
1815
1816 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher6eebe472012-12-19 22:02:53 +00001817 emitDIE(Children[j], Abbrevs);
Bill Wendling94d04b82009-05-20 23:21:38 +00001818
Chris Lattner3f53c832010-04-04 18:52:31 +00001819 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001820 Asm->OutStreamer.AddComment("End Of Children Mark");
1821 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001822 }
1823}
1824
Eric Christopherb1e66d02012-12-15 00:04:07 +00001825// Emit the various dwarf units to the unit section USection with
1826// the abbreviations going into ASection.
1827void DwarfUnits::emitUnits(DwarfDebug *DD,
1828 const MCSection *USection,
1829 const MCSection *ASection,
1830 const MCSymbol *ASectionSym) {
1831 Asm->OutStreamer.SwitchSection(USection);
1832 for (SmallVector<CompileUnit *, 1>::iterator I = CUs.begin(),
1833 E = CUs.end(); I != E; ++I) {
1834 CompileUnit *TheCU = *I;
Devang Patel163a9f72010-05-10 22:49:55 +00001835 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001836
Devang Patel163a9f72010-05-10 22:49:55 +00001837 // Emit the compile units header.
Eric Christopherb1e66d02012-12-15 00:04:07 +00001838 Asm->OutStreamer
1839 .EmitLabel(Asm->GetTempSymbol(USection->getLabelBeginName(),
1840 TheCU->getUniqueID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001841
Devang Patel163a9f72010-05-10 22:49:55 +00001842 // Emit size of content not including length itself
1843 unsigned ContentSize = Die->getSize() +
1844 sizeof(int16_t) + // DWARF version number
1845 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001846 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001847
Devang Patel163a9f72010-05-10 22:49:55 +00001848 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1849 Asm->EmitInt32(ContentSize);
1850 Asm->OutStreamer.AddComment("DWARF version number");
1851 Asm->EmitInt16(dwarf::DWARF_VERSION);
1852 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christopherb1e66d02012-12-15 00:04:07 +00001853 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
1854 ASectionSym);
Devang Patel163a9f72010-05-10 22:49:55 +00001855 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001856 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001857
Eric Christopher6eebe472012-12-19 22:02:53 +00001858 DD->emitDIE(Die, Abbreviations);
Eric Christopherb1e66d02012-12-15 00:04:07 +00001859 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(USection->getLabelEndName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00001860 TheCU->getUniqueID()));
Devang Patel163a9f72010-05-10 22:49:55 +00001861 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001862}
1863
Eric Christopher98e237f2012-11-30 23:59:06 +00001864// Emit the debug info section.
1865void DwarfDebug::emitDebugInfo() {
Eric Christopherb1e66d02012-12-15 00:04:07 +00001866 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1867
1868 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
1869 Asm->getObjFileLowering().getDwarfAbbrevSection(),
1870 DwarfAbbrevSectionSym);
Eric Christopher98e237f2012-11-30 23:59:06 +00001871}
1872
Eric Christopherb6dc8652012-11-27 22:43:45 +00001873// Emit the abbreviation section.
Eric Christopher7dc68db2012-11-20 23:30:11 +00001874void DwarfDebug::emitAbbreviations() {
Eric Christopher6eebe472012-12-19 22:02:53 +00001875 if (!useSplitDwarf())
1876 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
1877 &Abbreviations);
1878 else
1879 emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
1880}
Bill Wendling94d04b82009-05-20 23:21:38 +00001881
Eric Christopher6eebe472012-12-19 22:02:53 +00001882void DwarfDebug::emitAbbrevs(const MCSection *Section,
1883 std::vector<DIEAbbrev *> *Abbrevs) {
1884 // Check to see if it is worth the effort.
1885 if (!Abbrevs->empty()) {
1886 // Start the debug abbrev section.
1887 Asm->OutStreamer.SwitchSection(Section);
1888
1889 MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Eric Christopher44fedba2012-12-13 03:00:38 +00001890 Asm->OutStreamer.EmitLabel(Begin);
Bill Wendling94d04b82009-05-20 23:21:38 +00001891
1892 // For each abbrevation.
Eric Christopher6eebe472012-12-19 22:02:53 +00001893 for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001894 // Get abbreviation data
Eric Christopher6eebe472012-12-19 22:02:53 +00001895 const DIEAbbrev *Abbrev = Abbrevs->at(i);
Bill Wendling94d04b82009-05-20 23:21:38 +00001896
1897 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001898 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001899
1900 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001901 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001902 }
1903
1904 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001905 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001906
Eric Christopher6eebe472012-12-19 22:02:53 +00001907 MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Eric Christopher44fedba2012-12-13 03:00:38 +00001908 Asm->OutStreamer.EmitLabel(End);
Bill Wendling94d04b82009-05-20 23:21:38 +00001909 }
1910}
1911
Eric Christopherb6dc8652012-11-27 22:43:45 +00001912// Emit the last address of the section and the end of the line matrix.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001913void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001914 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001915 Asm->OutStreamer.AddComment("Extended Op");
1916 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001917
Chris Lattner233f52b2010-03-09 23:52:58 +00001918 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001919 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001920 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1921 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1922
1923 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001924
Chris Lattnerc0215722010-04-04 19:25:43 +00001925 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001926 Asm->getDataLayout().getPointerSize(),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001927 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001928
1929 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001930 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1931 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001932 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001933 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001934}
1935
Eric Christopherb6dc8652012-11-27 22:43:45 +00001936// Emit visible names into a hashed accelerator table section.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001937void DwarfDebug::emitAccelNames() {
1938 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1939 dwarf::DW_FORM_data4));
1940 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1941 E = CUMap.end(); I != E; ++I) {
1942 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001943 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1944 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001945 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1946 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001947 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001948 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1949 DE = Entities.end(); DI != DE; ++DI)
1950 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001951 }
1952 }
1953
1954 AT.FinalizeTable(Asm, "Names");
1955 Asm->OutStreamer.SwitchSection(
1956 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1957 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1958 Asm->OutStreamer.EmitLabel(SectionBegin);
1959
1960 // Emit the full data.
1961 AT.Emit(Asm, SectionBegin, this);
1962}
1963
Eric Christopherb6dc8652012-11-27 22:43:45 +00001964// Emit objective C classes and categories into a hashed accelerator table section.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001965void DwarfDebug::emitAccelObjC() {
1966 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1967 dwarf::DW_FORM_data4));
1968 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1969 E = CUMap.end(); I != E; ++I) {
1970 CompileUnit *TheCU = I->second;
1971 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1972 for (StringMap<std::vector<DIE*> >::const_iterator
1973 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1974 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001975 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-11-07 09:24:32 +00001976 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1977 DE = Entities.end(); DI != DE; ++DI)
1978 AT.AddName(Name, (*DI));
1979 }
1980 }
1981
1982 AT.FinalizeTable(Asm, "ObjC");
1983 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1984 .getDwarfAccelObjCSection());
1985 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1986 Asm->OutStreamer.EmitLabel(SectionBegin);
1987
1988 // Emit the full data.
1989 AT.Emit(Asm, SectionBegin, this);
1990}
1991
Eric Christopherb6dc8652012-11-27 22:43:45 +00001992// Emit namespace dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001993void DwarfDebug::emitAccelNamespaces() {
1994 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1995 dwarf::DW_FORM_data4));
1996 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1997 E = CUMap.end(); I != E; ++I) {
1998 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001999 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
2000 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00002001 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2002 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00002003 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00002004 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2005 DE = Entities.end(); DI != DE; ++DI)
2006 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00002007 }
2008 }
2009
2010 AT.FinalizeTable(Asm, "namespac");
2011 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2012 .getDwarfAccelNamespaceSection());
2013 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2014 Asm->OutStreamer.EmitLabel(SectionBegin);
2015
2016 // Emit the full data.
2017 AT.Emit(Asm, SectionBegin, this);
2018}
2019
Eric Christopherb6dc8652012-11-27 22:43:45 +00002020// Emit type dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00002021void DwarfDebug::emitAccelTypes() {
Eric Christopherc36145f2012-01-06 04:35:23 +00002022 std::vector<DwarfAccelTable::Atom> Atoms;
2023 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2024 dwarf::DW_FORM_data4));
2025 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
2026 dwarf::DW_FORM_data2));
2027 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
2028 dwarf::DW_FORM_data1));
2029 DwarfAccelTable AT(Atoms);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002030 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2031 E = CUMap.end(); I != E; ++I) {
2032 CompileUnit *TheCU = I->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00002033 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
2034 = TheCU->getAccelTypes();
2035 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00002036 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2037 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00002038 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00002039 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
2040 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
2041 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002042 }
2043 }
2044
2045 AT.FinalizeTable(Asm, "types");
2046 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2047 .getDwarfAccelTypesSection());
2048 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2049 Asm->OutStreamer.EmitLabel(SectionBegin);
2050
2051 // Emit the full data.
2052 AT.Emit(Asm, SectionBegin, this);
2053}
2054
Devang Patel193f7202009-11-24 01:14:22 +00002055void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00002056 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2057 E = CUMap.end(); I != E; ++I) {
2058 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00002059 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-05-10 22:49:55 +00002060 Asm->OutStreamer.SwitchSection(
2061 Asm->getObjFileLowering().getDwarfPubTypesSection());
2062 Asm->OutStreamer.AddComment("Length of Public Types Info");
2063 Asm->EmitLabelDifference(
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002064 Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()),
2065 Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002066
Devang Patel163a9f72010-05-10 22:49:55 +00002067 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002068 TheCU->getUniqueID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002069
Devang Patel163a9f72010-05-10 22:49:55 +00002070 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2071 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002072
Devang Patel163a9f72010-05-10 22:49:55 +00002073 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002074 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2075 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002076 TheCU->getUniqueID()),
Devang Patel163a9f72010-05-10 22:49:55 +00002077 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002078
Devang Patel163a9f72010-05-10 22:49:55 +00002079 Asm->OutStreamer.AddComment("Compilation Unit Length");
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002080 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002081 TheCU->getUniqueID()),
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002082 Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002083 TheCU->getUniqueID()),
Devang Patel163a9f72010-05-10 22:49:55 +00002084 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002085
Devang Patel163a9f72010-05-10 22:49:55 +00002086 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2087 for (StringMap<DIE*>::const_iterator
2088 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2089 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00002090 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002091
Devang Patel163a9f72010-05-10 22:49:55 +00002092 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2093 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002094
Devang Patel163a9f72010-05-10 22:49:55 +00002095 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00002096 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00002097 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
2098 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002099
Devang Patel163a9f72010-05-10 22:49:55 +00002100 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002101 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00002102 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002103 TheCU->getUniqueID()));
Devang Patel193f7202009-11-24 01:14:22 +00002104 }
Devang Patel193f7202009-11-24 01:14:22 +00002105}
2106
Eric Christopherb6dc8652012-11-27 22:43:45 +00002107// Emit visible names into a debug str section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002108void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002109 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002110 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002111
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002112 // Start the dwarf str section.
2113 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002114 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002115
Chris Lattnerbc733f52010-03-13 02:17:42 +00002116 // Get all of the string pool entries and put them in an array by their ID so
2117 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002118 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00002119 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002120
Chris Lattnerbc733f52010-03-13 02:17:42 +00002121 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2122 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2123 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002124
Chris Lattnerbc733f52010-03-13 02:17:42 +00002125 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002126
Chris Lattnerbc733f52010-03-13 02:17:42 +00002127 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002128 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002129 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002130
Benjamin Kramer983c4572011-11-09 18:16:11 +00002131 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00002132 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
2133 Entries[i].second->getKeyLength()+1),
2134 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002135 }
2136}
2137
Eric Christopherb6dc8652012-11-27 22:43:45 +00002138// Emit visible names into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002139void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002140 if (DotDebugLocEntries.empty())
2141 return;
2142
Devang Patel6c3ea902011-02-04 22:57:18 +00002143 for (SmallVector<DotDebugLocEntry, 4>::iterator
2144 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2145 I != E; ++I) {
2146 DotDebugLocEntry &Entry = *I;
2147 if (I + 1 != DotDebugLocEntries.end())
2148 Entry.Merge(I+1);
2149 }
2150
Daniel Dunbar83320a02011-03-16 22:16:39 +00002151 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002152 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002153 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002154 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002155 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2156 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002157 for (SmallVector<DotDebugLocEntry, 4>::iterator
2158 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002159 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002160 DotDebugLocEntry &Entry = *I;
2161 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002162 if (Entry.isEmpty()) {
2163 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2164 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002165 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002166 } else {
2167 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2168 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002169 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002170 Asm->OutStreamer.AddComment("Loc expr size");
2171 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2172 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2173 Asm->EmitLabelDifference(end, begin, 2);
2174 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002175 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002176 DIBasicType BTy(DV.getType());
2177 if (BTy.Verify() &&
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002178 (BTy.getEncoding() == dwarf::DW_ATE_signed
Devang Patelc4329072011-06-01 22:03:25 +00002179 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2180 Asm->OutStreamer.AddComment("DW_OP_consts");
2181 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002182 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002183 } else {
2184 Asm->OutStreamer.AddComment("DW_OP_constu");
2185 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002186 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002187 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002188 } else if (Entry.isLocation()) {
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002189 if (!DV.hasComplexAddress())
Devang Patel80efd4e2011-07-08 16:49:43 +00002190 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002191 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002192 else {
2193 // Complex address entry.
2194 unsigned N = DV.getNumAddrElements();
2195 unsigned i = 0;
2196 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2197 if (Entry.Loc.getOffset()) {
2198 i = 2;
2199 Asm->EmitDwarfRegOp(Entry.Loc);
2200 Asm->OutStreamer.AddComment("DW_OP_deref");
2201 Asm->EmitInt8(dwarf::DW_OP_deref);
2202 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2203 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2204 Asm->EmitSLEB128(DV.getAddrElement(1));
2205 } else {
2206 // If first address element is OpPlus then emit
2207 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2208 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2209 Asm->EmitDwarfRegOp(Loc);
2210 i = 2;
2211 }
2212 } else {
2213 Asm->EmitDwarfRegOp(Entry.Loc);
2214 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002215
Devang Patel80efd4e2011-07-08 16:49:43 +00002216 // Emit remaining complex address elements.
2217 for (; i < N; ++i) {
2218 uint64_t Element = DV.getAddrElement(i);
2219 if (Element == DIBuilder::OpPlus) {
2220 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2221 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher50120762012-05-08 18:56:00 +00002222 } else if (Element == DIBuilder::OpDeref) {
Eric Christophera80f2d12012-05-08 21:24:39 +00002223 if (!Entry.Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002224 Asm->EmitInt8(dwarf::DW_OP_deref);
2225 } else
2226 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002227 }
Devang Patelc26f5442011-04-28 02:22:40 +00002228 }
Devang Patelc26f5442011-04-28 02:22:40 +00002229 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002230 // else ... ignore constant fp. There is not any good way to
2231 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002232 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002233 }
2234 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002235}
2236
Eric Christopherb6dc8652012-11-27 22:43:45 +00002237// Emit visible names into a debug aranges section.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00002238void DwarfDebug::emitDebugARanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002239 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002240 Asm->OutStreamer.SwitchSection(
2241 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002242}
2243
Eric Christopherb6dc8652012-11-27 22:43:45 +00002244// Emit visible names into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002245void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002246 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002247 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002248 Asm->getObjFileLowering().getDwarfRangesSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002249 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Pateleac9c072010-04-27 19:46:33 +00002250 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002251 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002252 I != E; ++I) {
2253 if (*I)
2254 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002255 else
Devang Pateleac9c072010-04-27 19:46:33 +00002256 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002257 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002258}
2259
Eric Christopherb6dc8652012-11-27 22:43:45 +00002260// Emit visible names into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002261void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002262 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002263 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002264 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002265 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002266 }
2267}
2268
Eric Christopherb6dc8652012-11-27 22:43:45 +00002269// Emit inline info using following format.
2270// Section Header:
2271// 1. length of section
2272// 2. Dwarf version number
2273// 3. address size.
2274//
2275// Entries (one "entry" for each function that was inlined):
2276//
2277// 1. offset into __debug_str section for MIPS linkage name, if exists;
2278// otherwise offset into __debug_str for regular function name.
2279// 2. offset into __debug_str section for regular function name.
2280// 3. an unsigned LEB128 number indicating the number of distinct inlining
2281// instances for the function.
2282//
2283// The rest of the entry consists of a {die_offset, low_pc} pair for each
2284// inlined instance; the die_offset points to the inlined_subroutine die in the
2285// __debug_info section, and the low_pc is the starting address for the
2286// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002287void DwarfDebug::emitDebugInlineInfo() {
Eric Christopherb83e2bb2012-03-02 02:11:47 +00002288 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002289 return;
2290
Devang Patel163a9f72010-05-10 22:49:55 +00002291 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002292 return;
2293
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002294 Asm->OutStreamer.SwitchSection(
2295 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002296
Chris Lattner233f52b2010-03-09 23:52:58 +00002297 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002298 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2299 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002300
Chris Lattnerc0215722010-04-04 19:25:43 +00002301 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002302
Chris Lattner233f52b2010-03-09 23:52:58 +00002303 Asm->OutStreamer.AddComment("Dwarf Version");
2304 Asm->EmitInt16(dwarf::DWARF_VERSION);
2305 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002306 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002307
Devang Patele9f8f5e2010-05-07 20:54:48 +00002308 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002309 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002310
Devang Patele9f8f5e2010-05-07 20:54:48 +00002311 const MDNode *Node = *I;
2312 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002313 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002314 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002315 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002316 StringRef LName = SP.getLinkageName();
2317 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002318
Chris Lattner233f52b2010-03-09 23:52:58 +00002319 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher50e26612012-03-02 01:57:52 +00002320 if (LName.empty())
2321 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2322 else
Chris Lattner6189ed12010-04-04 23:25:33 +00002323 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2324 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002325
Chris Lattner233f52b2010-03-09 23:52:58 +00002326 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002327 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002328 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002329
Devang Patel53bb5c92009-11-10 23:06:00 +00002330 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002331 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002332 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002333 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002334
Chris Lattner3f53c832010-04-04 18:52:31 +00002335 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002336 Asm->OutStreamer.EmitSymbolValue(LI->first,
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002337 Asm->getDataLayout().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002338 }
2339 }
2340
Chris Lattnerc0215722010-04-04 19:25:43 +00002341 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002342}
Eric Christopher98e237f2012-11-30 23:59:06 +00002343
Eric Christopher0b944ee2012-12-11 19:42:09 +00002344// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher98e237f2012-11-30 23:59:06 +00002345
2346// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2347// DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
2348// DW_AT_ranges_base, DW_AT_addr_base. If DW_AT_ranges is present,
2349// DW_AT_low_pc and DW_AT_high_pc are not used, and vice versa.
Eric Christopher4daaed12012-12-10 19:51:21 +00002350CompileUnit *DwarfDebug::constructSkeletonCU(const MDNode *N) {
Eric Christopher98e237f2012-11-30 23:59:06 +00002351 DICompileUnit DIUnit(N);
2352 StringRef FN = DIUnit.getFilename();
2353 CompilationDir = DIUnit.getDirectory();
Eric Christopher98e237f2012-11-30 23:59:06 +00002354
2355 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002356 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
2357 DIUnit.getLanguage(), Die, Asm, this);
Eric Christopher98e237f2012-11-30 23:59:06 +00002358 // FIXME: This should be the .dwo file.
2359 NewCU->addString(Die, dwarf::DW_AT_GNU_dwo_name, FN);
2360
2361 // FIXME: We also need DW_AT_addr_base and DW_AT_dwo_id.
2362
2363 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
2364 // into an entity.
2365 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
2366 // DW_AT_stmt_list is a offset of line number information for this
2367 // compile unit in debug_line section.
2368 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2369 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
2370 Asm->GetTempSymbol("section_line"));
2371 else
2372 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
2373
2374 if (!CompilationDir.empty())
2375 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
2376
Eric Christopher0e3e9b72012-12-10 23:34:43 +00002377 SkeletonHolder.addUnit(NewCU);
2378
Eric Christopher98e237f2012-11-30 23:59:06 +00002379 return NewCU;
2380}
2381
Eric Christopher4daaed12012-12-10 19:51:21 +00002382void DwarfDebug::emitSkeletonCU(const MCSection *Section) {
Eric Christopher98e237f2012-11-30 23:59:06 +00002383 Asm->OutStreamer.SwitchSection(Section);
Eric Christopher4daaed12012-12-10 19:51:21 +00002384 DIE *Die = SkeletonCU->getCUDie();
Eric Christopher98e237f2012-11-30 23:59:06 +00002385
2386 // Emit the compile units header.
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002387 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(Section->getLabelBeginName(),
Eric Christopher4daaed12012-12-10 19:51:21 +00002388 SkeletonCU->getUniqueID()));
Eric Christopher98e237f2012-11-30 23:59:06 +00002389
2390 // Emit size of content not including length itself
2391 unsigned ContentSize = Die->getSize() +
2392 sizeof(int16_t) + // DWARF version number
2393 sizeof(int32_t) + // Offset Into Abbrev. Section
2394 sizeof(int8_t); // Pointer Size (in bytes)
2395
2396 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2397 Asm->EmitInt32(ContentSize);
2398 Asm->OutStreamer.AddComment("DWARF version number");
2399 Asm->EmitInt16(dwarf::DWARF_VERSION);
2400 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christopher6eebe472012-12-19 22:02:53 +00002401
2402 const MCSection *ASec = Asm->getObjFileLowering().getDwarfAbbrevSection();
2403 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASec->getLabelBeginName()),
Eric Christopher98e237f2012-11-30 23:59:06 +00002404 DwarfAbbrevSectionSym);
2405 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2406 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2407
Eric Christopher6eebe472012-12-19 22:02:53 +00002408 emitDIE(Die, &SkeletonAbbrevs);
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002409 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(Section->getLabelEndName(),
Eric Christopher4daaed12012-12-10 19:51:21 +00002410 SkeletonCU->getUniqueID()));
Eric Christopher6eebe472012-12-19 22:02:53 +00002411}
Eric Christopher98e237f2012-11-30 23:59:06 +00002412
Eric Christopher6eebe472012-12-19 22:02:53 +00002413void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
2414 assert(useSplitDwarf() && "No split dwarf debug info?");
2415 emitAbbrevs(Section, &SkeletonAbbrevs);
Eric Christopher98e237f2012-11-30 23:59:06 +00002416}
2417
Eric Christopher0b944ee2012-12-11 19:42:09 +00002418// Emit the .debug_info.dwo section for separated dwarf. This contains the
2419// compile units that would normally be in debug_info.
Eric Christopher98e237f2012-11-30 23:59:06 +00002420void DwarfDebug::emitDebugInfoDWO() {
Eric Christopher4daaed12012-12-10 19:51:21 +00002421 assert(useSplitDwarf() && "No split dwarf debug info?");
Eric Christopherb1e66d02012-12-15 00:04:07 +00002422 InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Eric Christopher6eebe472012-12-19 22:02:53 +00002423 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2424 DwarfAbbrevDWOSectionSym);
2425}
2426
2427// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2428// abbreviations for the .debug_info.dwo section.
2429void DwarfDebug::emitDebugAbbrevDWO() {
2430 assert(useSplitDwarf() && "No split dwarf?");
2431 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(), &Abbreviations);
Eric Christopher98e237f2012-11-30 23:59:06 +00002432}