blob: 2a9c2ff232884905c3bce3e18de07ac140a6b039 [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),
Eric Christopher2e5d8702012-12-20 21:58:36 +0000158 SourceIdMap(DIEValueAllocator), InfoStringPool(DIEValueAllocator),
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000159 PrevLabel(NULL), GlobalCUIndexCount(0),
Eric Christopher2e5d8702012-12-20 21:58:36 +0000160 InfoHolder(A, &AbbreviationsSet, &Abbreviations, &InfoStringPool),
Eric Christopher6eebe472012-12-19 22:02:53 +0000161 SkeletonCU(0),
162 SkeletonAbbrevSet(InitAbbreviationsSetSize),
Eric Christopher2e5d8702012-12-20 21:58:36 +0000163 SkeletonHolder(A, &SkeletonAbbrevSet, &SkeletonAbbrevs, &InfoStringPool) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000164
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000165 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000166 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000167 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Eric Christopher6eebe472012-12-19 22:02:53 +0000168 DwarfAbbrevDWOSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000169 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopher60777d82012-04-02 17:58:52 +0000170
Eric Christopher10cb7442012-08-23 07:10:46 +0000171 // Turn on accelerator tables and older gdb compatibility
172 // for Darwin.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000173 bool IsDarwin = Triple(M->getTargetTriple()).isOSDarwin();
Eric Christopher20f47ab2012-08-23 22:36:40 +0000174 if (DarwinGDBCompat == Default) {
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000175 if (IsDarwin)
176 IsDarwinGDBCompat = true;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000177 else
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000178 IsDarwinGDBCompat = false;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000179 } else
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000180 IsDarwinGDBCompat = DarwinGDBCompat == Enable ? true : false;
Eric Christopherc1610fa2012-08-23 22:36:36 +0000181
Eric Christopher20f47ab2012-08-23 22:36:40 +0000182 if (DwarfAccelTables == Default) {
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000183 if (IsDarwin)
184 HasDwarfAccelTables = true;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000185 else
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000186 HasDwarfAccelTables = false;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000187 } else
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000188 HasDwarfAccelTables = DwarfAccelTables == Enable ? true : false;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000189
Eric Christopher4daaed12012-12-10 19:51:21 +0000190 if (SplitDwarf == Default)
191 HasSplitDwarf = false;
Eric Christopherf5b6dcd2012-11-12 22:22:20 +0000192 else
Eric Christopher4daaed12012-12-10 19:51:21 +0000193 HasSplitDwarf = SplitDwarf == Enable ? true : false;
Eric Christopherf5b6dcd2012-11-12 22:22:20 +0000194
Dan Gohman03c3dc72010-06-18 15:56:31 +0000195 {
196 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopherc4639d62012-11-19 22:42:15 +0000197 beginModule();
Torok Edwin9c421072010-04-07 10:44:46 +0000198 }
Bill Wendling0310d762009-05-15 09:23:25 +0000199}
200DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000201}
202
Eric Christopherb6dc8652012-11-27 22:43:45 +0000203// Switch to the specified MCSection and emit an assembler
204// temporary label to it if SymbolStem is specified.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000205static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christopherd8a87522011-11-07 09:18:38 +0000206 const char *SymbolStem = 0) {
207 Asm->OutStreamer.SwitchSection(Section);
208 if (!SymbolStem) return 0;
209
210 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
211 Asm->OutStreamer.EmitLabel(TmpSym);
212 return TmpSym;
213}
214
Eric Christopher2e5d8702012-12-20 21:58:36 +0000215MCSymbol *DwarfUnits::getStringPoolSym() {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000216 return Asm->GetTempSymbol("section_str");
217}
218
Eric Christopher2e5d8702012-12-20 21:58:36 +0000219MCSymbol *DwarfUnits::getStringPoolEntry(StringRef Str) {
220 std::pair<MCSymbol*, unsigned> &Entry =
221 StringPool->GetOrCreateValue(Str).getValue();
Chris Lattnerbc733f52010-03-13 02:17:42 +0000222 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++,
Eric Christopher2e5d8702012-12-20 21:58:36 +0000629 DIUnit.getLanguage(), Die, Asm,
630 this, &InfoHolder);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000631 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000632 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
633 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000634 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher6635cad2012-08-01 18:19:01 +0000635 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
636 // into an entity.
637 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000638 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000639 // compile unit in debug_line section.
Rafael Espindola2241e512012-06-22 13:24:07 +0000640 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindola597a7662011-05-04 17:44:06 +0000641 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000642 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000643 else
Devang Patel3cbee302011-04-12 22:53:02 +0000644 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000645
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000646 if (!CompilationDir.empty())
647 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000648 if (DIUnit.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000649 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000650
Devang Patel65dbc902009-11-25 17:36:49 +0000651 StringRef Flags = DIUnit.getFlags();
652 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000653 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000654
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000655 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000656 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000657 dwarf::DW_FORM_data1, RVer);
658
Devang Patel163a9f72010-05-10 22:49:55 +0000659 if (!FirstCU)
660 FirstCU = NewCU;
Eric Christopher4daaed12012-12-10 19:51:21 +0000661 if (useSplitDwarf() && !SkeletonCU)
662 SkeletonCU = constructSkeletonCU(N);
Eric Christopher98e237f2012-11-30 23:59:06 +0000663
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000664 InfoHolder.addUnit(NewCU);
665
Devang Patel163a9f72010-05-10 22:49:55 +0000666 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000667 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000668}
669
Eric Christopherb6dc8652012-11-27 22:43:45 +0000670// Construct subprogram DIE.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000671void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
Devang Patel3655a212011-08-15 23:36:40 +0000672 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000673 CompileUnit *&CURef = SPMap[N];
674 if (CURef)
675 return;
676 CURef = TheCU;
677
Devang Patele4b27562009-08-28 23:24:31 +0000678 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000679 if (!SP.isDefinition())
680 // This is a method declaration which will be handled while constructing
681 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000682 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000683
Devang Pateldbc64af2011-08-15 17:24:54 +0000684 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000685
686 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000687 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000688
689 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000690 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000691
Devang Patel13e16b62009-06-26 01:49:18 +0000692 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000693}
694
Eric Christopherb6dc8652012-11-27 22:43:45 +0000695// Collect debug info from named mdnodes such as llvm.dbg.enum and llvm.dbg.ty.
Eric Christopherc4639d62012-11-19 22:42:15 +0000696void DwarfDebug::collectInfoFromNamedMDNodes(const Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000697 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
698 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
699 const MDNode *N = NMD->getOperand(i);
700 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
701 constructSubprogramDIE(CU, N);
702 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000703
Devang Patel94c7ddb2011-08-16 22:09:43 +0000704 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
705 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
706 const MDNode *N = NMD->getOperand(i);
707 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000708 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000709 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000710
Devang Patel02e603f2011-08-15 23:47:24 +0000711 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
712 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
713 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000714 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
715 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000716 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000717
Devang Patel02e603f2011-08-15 23:47:24 +0000718 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
719 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
720 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000721 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
722 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000723 }
724}
725
Eric Christopherb6dc8652012-11-27 22:43:45 +0000726// Collect debug info using DebugInfoFinder.
727// FIXME - Remove this when dragonegg switches to DIBuilder.
Eric Christopherc4639d62012-11-19 22:42:15 +0000728bool DwarfDebug::collectLegacyDebugInfo(const Module *M) {
Devang Patel02e603f2011-08-15 23:47:24 +0000729 DebugInfoFinder DbgFinder;
730 DbgFinder.processModule(*M);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000731
Devang Patel02e603f2011-08-15 23:47:24 +0000732 bool HasDebugInfo = false;
733 // Scan all the compile-units to see if there are any marked as the main
734 // unit. If not, we do not generate debug info.
735 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
736 E = DbgFinder.compile_unit_end(); I != E; ++I) {
737 if (DICompileUnit(*I).isMain()) {
738 HasDebugInfo = true;
739 break;
740 }
741 }
742 if (!HasDebugInfo) return false;
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000743
Devang Patel02e603f2011-08-15 23:47:24 +0000744 // Create all the compile unit DIEs.
745 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
746 E = DbgFinder.compile_unit_end(); I != E; ++I)
747 constructCompileUnit(*I);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000748
Devang Patel02e603f2011-08-15 23:47:24 +0000749 // Create DIEs for each global variable.
750 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
751 E = DbgFinder.global_variable_end(); I != E; ++I) {
752 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000753 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000754 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000755 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000756
Devang Patel02e603f2011-08-15 23:47:24 +0000757 // Create DIEs for each subprogram.
758 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
759 E = DbgFinder.subprogram_end(); I != E; ++I) {
760 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000761 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
762 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000763 }
764
765 return HasDebugInfo;
766}
767
Eric Christopherb6dc8652012-11-27 22:43:45 +0000768// Emit all Dwarf sections that should come prior to the content. Create
769// global DIEs and emit initial debug info sections. This is invoked by
770// the target AsmPrinter.
Eric Christopherc4639d62012-11-19 22:42:15 +0000771void DwarfDebug::beginModule() {
Devang Pateleac9c072010-04-27 19:46:33 +0000772 if (DisableDebugInfoPrinting)
773 return;
774
Eric Christopherc4639d62012-11-19 22:42:15 +0000775 const Module *M = MMI->getModule();
776
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000777 // If module has named metadata anchors then use them, otherwise scan the
778 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000779 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
780 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000781 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
782 DICompileUnit CUNode(CU_Nodes->getOperand(i));
783 CompileUnit *CU = constructCompileUnit(CUNode);
784 DIArray GVs = CUNode.getGlobalVariables();
785 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000786 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000787 DIArray SPs = CUNode.getSubprograms();
788 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
789 constructSubprogramDIE(CU, SPs.getElement(i));
790 DIArray EnumTypes = CUNode.getEnumTypes();
791 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
792 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
793 DIArray RetainedTypes = CUNode.getRetainedTypes();
794 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
795 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
796 }
Devang Patel02e603f2011-08-15 23:47:24 +0000797 } else if (!collectLegacyDebugInfo(M))
798 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000799
Devang Patel02e603f2011-08-15 23:47:24 +0000800 collectInfoFromNamedMDNodes(M);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000801
Chris Lattnerd850ac72010-04-05 02:19:28 +0000802 // Tell MMI that we have debug info.
803 MMI->setDebugInfoAvailability(true);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000804
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000805 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000806 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000807}
808
Eric Christopher4117bec2012-11-22 00:59:49 +0000809// Attach DW_AT_inline attribute with inlined subprogram DIEs.
810void DwarfDebug::computeInlinedDIEs() {
Devang Patel53bb5c92009-11-10 23:06:00 +0000811 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
812 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000813 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
Devang Patel53bb5c92009-11-10 23:06:00 +0000814 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000815 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000816 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000817 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000818 AE = AbstractSPDies.end(); AI != AE; ++AI) {
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000819 DIE *ISP = AI->second;
820 if (InlinedSubprogramDIEs.count(ISP))
821 continue;
822 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
823 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000824}
825
826// Collect info for variables that were optimized out.
827void DwarfDebug::collectDeadVariables() {
828 const Module *M = MMI->getModule();
829 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
830
831 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
832 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
833 DICompileUnit TheCU(CU_Nodes->getOperand(i));
834 DIArray Subprograms = TheCU.getSubprograms();
835 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopherbdab8002012-11-27 00:13:51 +0000836 DISubprogram SP(Subprograms.getElement(i));
837 if (ProcessedSPNodes.count(SP) != 0) continue;
838 if (!SP.Verify()) continue;
839 if (!SP.isDefinition()) continue;
840 DIArray Variables = SP.getVariables();
841 if (Variables.getNumElements() == 0) continue;
Eric Christopher4117bec2012-11-22 00:59:49 +0000842
Eric Christopherbdab8002012-11-27 00:13:51 +0000843 LexicalScope *Scope =
844 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
845 DeadFnScopeMap[SP] = Scope;
Eric Christopher4117bec2012-11-22 00:59:49 +0000846
Eric Christopherbdab8002012-11-27 00:13:51 +0000847 // Construct subprogram DIE and add variables DIEs.
848 CompileUnit *SPCU = CUMap.lookup(TheCU);
849 assert(SPCU && "Unable to find Compile Unit!");
850 constructSubprogramDIE(SPCU, SP);
851 DIE *ScopeDIE = SPCU->getDIE(SP);
852 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
853 DIVariable DV(Variables.getElement(vi));
854 if (!DV.Verify()) continue;
855 DbgVariable *NewVar = new DbgVariable(DV, NULL);
856 if (DIE *VariableDIE =
857 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
858 ScopeDIE->addChild(VariableDIE);
859 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000860 }
861 }
862 }
863 DeleteContainerSeconds(DeadFnScopeMap);
864}
865
866void DwarfDebug::finalizeModuleInfo() {
867 // Collect info for variables that were optimized out.
868 collectDeadVariables();
869
870 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
871 computeInlinedDIEs();
Devang Patel53bb5c92009-11-10 23:06:00 +0000872
Eric Christopher6635cad2012-08-01 18:19:01 +0000873 // Emit DW_AT_containing_type attribute to connect types with their
874 // vtable holding type.
Devang Pateldbc64af2011-08-15 17:24:54 +0000875 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000876 CUE = CUMap.end(); CUI != CUE; ++CUI) {
Devang Pateldbc64af2011-08-15 17:24:54 +0000877 CompileUnit *TheCU = CUI->second;
878 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000879 }
880
Eric Christopher4117bec2012-11-22 00:59:49 +0000881 // Compute DIE offsets and sizes.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000882 InfoHolder.computeSizeAndOffsets();
883 if (useSplitDwarf())
884 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher4117bec2012-11-22 00:59:49 +0000885}
886
887void DwarfDebug::endSections() {
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000888 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000889 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000890 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000891 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000892 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000893
894 // End text sections.
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +0000895 for (unsigned I = 0, E = SectionMap.size(); I != E; ++I) {
896 Asm->OutStreamer.SwitchSection(SectionMap[I]);
897 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", I+1));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000898 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000899}
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000900
Eric Christopherb6dc8652012-11-27 22:43:45 +0000901// Emit all Dwarf sections that should come after the content.
Eric Christopher4117bec2012-11-22 00:59:49 +0000902void DwarfDebug::endModule() {
903
904 if (!FirstCU) return;
905
906 // End any existing sections.
907 // TODO: Does this need to happen?
908 endSections();
909
910 // Finalize the debug info for the module.
911 finalizeModuleInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000912
Eric Christopher97c34722012-11-19 19:43:59 +0000913 // Emit initial sections.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000914 emitSectionLabels();
Eric Christopher97c34722012-11-19 19:43:59 +0000915
Eric Christopher4daaed12012-12-10 19:51:21 +0000916 if (!useSplitDwarf()) {
Eric Christopher42885022012-11-27 22:43:42 +0000917 // Emit all the DIEs into a debug info section.
918 emitDebugInfo();
Eric Christopher74802f72012-11-27 00:41:54 +0000919
Eric Christopher42885022012-11-27 22:43:42 +0000920 // Corresponding abbreviations into a abbrev section.
921 emitAbbreviations();
922
923 // Emit info into a debug loc section.
924 emitDebugLoc();
925
926 // Emit info into a debug aranges section.
927 emitDebugARanges();
928
929 // Emit info into a debug ranges section.
930 emitDebugRanges();
931
932 // Emit info into a debug macinfo section.
933 emitDebugMacInfo();
934
935 // Emit inline info.
936 // TODO: When we don't need the option anymore we
937 // can remove all of the code that this section
938 // depends upon.
939 if (useDarwinGDBCompat())
940 emitDebugInlineInfo();
941 } else {
Eric Christopher0b944ee2012-12-11 19:42:09 +0000942 // TODO: Fill this in for separated debug sections and separate
Eric Christopher42885022012-11-27 22:43:42 +0000943 // out information into new sections.
944
Eric Christopher98e237f2012-11-30 23:59:06 +0000945 // Emit the debug info section and compile units.
Eric Christopher42885022012-11-27 22:43:42 +0000946 emitDebugInfo();
Eric Christopher98e237f2012-11-30 23:59:06 +0000947 emitDebugInfoDWO();
Eric Christopher42885022012-11-27 22:43:42 +0000948
949 // Corresponding abbreviations into a abbrev section.
950 emitAbbreviations();
Eric Christopher6eebe472012-12-19 22:02:53 +0000951 emitDebugAbbrevDWO();
Eric Christopher42885022012-11-27 22:43:42 +0000952
953 // Emit info into a debug loc section.
954 emitDebugLoc();
955
956 // Emit info into a debug aranges section.
957 emitDebugARanges();
958
959 // Emit info into a debug ranges section.
960 emitDebugRanges();
961
962 // Emit info into a debug macinfo section.
963 emitDebugMacInfo();
964
965 // Emit inline info.
966 // TODO: When we don't need the option anymore we
967 // can remove all of the code that this section
968 // depends upon.
969 if (useDarwinGDBCompat())
970 emitDebugInlineInfo();
971 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000972
Eric Christopher9d9f5a52012-08-23 07:32:06 +0000973 // Emit info into the dwarf accelerator table sections.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000974 if (useDwarfAccelTables()) {
Eric Christopher09ac3d82011-11-07 09:24:32 +0000975 emitAccelNames();
976 emitAccelObjC();
977 emitAccelNamespaces();
978 emitAccelTypes();
979 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000980
Devang Patel193f7202009-11-24 01:14:22 +0000981 // Emit info into a debug pubtypes section.
Eric Christopher360f0062012-08-23 07:10:56 +0000982 // TODO: When we don't need the option anymore we can
983 // remove all of the code that adds to the table.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000984 if (useDarwinGDBCompat())
Eric Christopher360f0062012-08-23 07:10:56 +0000985 emitDebugPubTypes();
Devang Patel193f7202009-11-24 01:14:22 +0000986
Eric Christopher42885022012-11-27 22:43:42 +0000987 // Finally emit string information into a string table.
Eric Christopherfcdbecb2012-11-27 06:49:23 +0000988 emitDebugStr();
989
Devang Patele9a1cca2010-08-02 17:32:15 +0000990 // clean up.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000991 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000992 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
993 E = CUMap.end(); I != E; ++I)
994 delete I->second;
Eric Christopher9ec87b32012-12-10 19:51:18 +0000995
Eric Christopher4daaed12012-12-10 19:51:21 +0000996 delete SkeletonCU;
Eric Christopher9ec87b32012-12-10 19:51:18 +0000997
Eric Christopher98e237f2012-11-30 23:59:06 +0000998 // Reset these for the next Module if we have one.
999 FirstCU = NULL;
Eric Christopher4daaed12012-12-10 19:51:21 +00001000 SkeletonCU = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001001}
1002
Eric Christopherb6dc8652012-11-27 22:43:45 +00001003// Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +00001004DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +00001005 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +00001006 LLVMContext &Ctx = DV->getContext();
1007 // More then one inlined variable corresponds to one abstract variable.
1008 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +00001009 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001010 if (AbsDbgVariable)
1011 return AbsDbgVariable;
1012
Devang Patelbf47fdb2011-08-10 20:55:27 +00001013 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00001014 if (!Scope)
1015 return NULL;
1016
Devang Patel5a1a67c2011-08-15 19:01:20 +00001017 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001018 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00001019 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00001020 return AbsDbgVariable;
1021}
1022
Eric Christopherb6dc8652012-11-27 22:43:45 +00001023// If Var is a current function argument then add it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +00001024bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +00001025 DbgVariable *Var, LexicalScope *Scope) {
1026 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +00001027 return false;
1028 DIVariable DV = Var->getVariable();
1029 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1030 return false;
1031 unsigned ArgNo = DV.getArgNumber();
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001032 if (ArgNo == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001033 return false;
1034
Devang Patelcb3a6572011-03-03 20:02:02 +00001035 size_t Size = CurrentFnArguments.size();
1036 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001037 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +00001038 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +00001039 // arguments does the function have at source level.
1040 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +00001041 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +00001042 CurrentFnArguments[ArgNo - 1] = Var;
1043 return true;
1044}
1045
Eric Christopherb6dc8652012-11-27 22:43:45 +00001046// Collect variable information from side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001047void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001048DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +00001049 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +00001050 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1051 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1052 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00001053 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00001054 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00001055 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00001056 DIVariable DV(Var);
1057 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001058
Devang Patelbf47fdb2011-08-10 20:55:27 +00001059 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001060
Devang Patelfb0ee432009-11-10 23:20:04 +00001061 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00001062 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00001063 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001064
Devang Patel26c1e562010-05-20 16:36:41 +00001065 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001066 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +00001067 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +00001068 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001069 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001070 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001071 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +00001072 }
Devang Patelee432862010-05-20 19:57:06 +00001073}
Devang Patel90a48ad2010-03-15 18:33:46 +00001074
Eric Christopherb6dc8652012-11-27 22:43:45 +00001075// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1076// defined reg.
Devang Patelc3f5f782010-05-25 23:40:22 +00001077static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001078 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001079 return MI->getNumOperands() == 3 &&
1080 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
1081 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +00001082}
1083
Eric Christopherb6dc8652012-11-27 22:43:45 +00001084// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001085static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1086 const MCSymbol *FLabel,
Devang Patel90b40412011-07-08 17:09:57 +00001087 const MCSymbol *SLabel,
1088 const MachineInstr *MI) {
1089 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1090
1091 if (MI->getNumOperands() != 3) {
1092 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1093 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1094 }
1095 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1096 MachineLocation MLoc;
1097 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1098 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1099 }
1100 if (MI->getOperand(0).isImm())
1101 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1102 if (MI->getOperand(0).isFPImm())
1103 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1104 if (MI->getOperand(0).isCImm())
1105 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1106
Craig Topper5e25ee82012-02-05 08:31:47 +00001107 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +00001108}
1109
Eric Christopherb6dc8652012-11-27 22:43:45 +00001110// Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001111void
Devang Patel78e127d2010-06-25 22:07:34 +00001112DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1113 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001114
Eric Christopherb6dc8652012-11-27 22:43:45 +00001115 // collection info from MMI table.
Devang Patelee432862010-05-20 19:57:06 +00001116 collectVariableInfoFromMMITable(MF, Processed);
1117
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001118 for (SmallVectorImpl<const MDNode*>::const_iterator
1119 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1120 ++UVI) {
1121 const MDNode *Var = *UVI;
1122 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001123 continue;
1124
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001125 // History contains relevant DBG_VALUE instructions for Var and instructions
1126 // clobbering it.
1127 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1128 if (History.empty())
1129 continue;
1130 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001131
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001132 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001133 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001134 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1135 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001136 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001137 else {
1138 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001139 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001140 else {
1141 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001142 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001143 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001144 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001145 }
1146 }
Devang Patelee432862010-05-20 19:57:06 +00001147 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001148 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001149 continue;
1150
1151 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001152 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001153 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1154 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001155 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001156 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001157 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001158 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001159
Eric Christopherc56e3f02012-10-08 20:48:54 +00001160 // Simplify ranges that are fully coalesced.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001161 if (History.size() <= 1 || (History.size() == 2 &&
1162 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001163 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001164 continue;
1165 }
1166
1167 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001168 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001169
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001170 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1171 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1172 const MachineInstr *Begin = *HI;
1173 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001174
Devang Patel4ada1d72011-06-01 23:00:17 +00001175 // Check if DBG_VALUE is truncating a range.
1176 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1177 && !Begin->getOperand(0).getReg())
1178 continue;
1179
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001180 // Compute the range for a register location.
1181 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1182 const MCSymbol *SLabel = 0;
1183
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001184 if (HI + 1 == HE)
1185 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001186 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001187 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001188 else {
1189 const MachineInstr *End = HI[1];
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001190 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Devang Patel476df5f2011-07-07 21:44:42 +00001191 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001192 if (End->isDebugValue())
1193 SLabel = getLabelBeforeInsn(End);
1194 else {
1195 // End is a normal instruction clobbering the range.
1196 SLabel = getLabelAfterInsn(End);
1197 assert(SLabel && "Forgot label after clobber instruction");
1198 ++HI;
1199 }
1200 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001201
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001202 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001203 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1204 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001205 }
1206 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001207 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001208
1209 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001210 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1211 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1212 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1213 DIVariable DV(Variables.getElement(i));
1214 if (!DV || !DV.Verify() || !Processed.insert(DV))
1215 continue;
1216 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1217 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001218 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001219}
Devang Patel98e1cac2010-05-14 21:01:35 +00001220
Eric Christopherb6dc8652012-11-27 22:43:45 +00001221// Return Label preceding the instruction.
Devang Patelc3f5f782010-05-25 23:40:22 +00001222const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001223 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1224 assert(Label && "Didn't insert label before instruction");
1225 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001226}
1227
Eric Christopherb6dc8652012-11-27 22:43:45 +00001228// Return Label immediately following the instruction.
Devang Patelc3f5f782010-05-25 23:40:22 +00001229const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001230 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001231}
1232
Eric Christopherb6dc8652012-11-27 22:43:45 +00001233// Process beginning of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +00001234void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001235 // Check if source location changes, but ignore DBG_VALUE locations.
1236 if (!MI->isDebugValue()) {
1237 DebugLoc DL = MI->getDebugLoc();
1238 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001239 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001240 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001241 if (DL == PrologEndLoc) {
1242 Flags |= DWARF2_FLAG_PROLOGUE_END;
1243 PrologEndLoc = DebugLoc();
1244 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001245 if (PrologEndLoc.isUnknown())
1246 Flags |= DWARF2_FLAG_IS_STMT;
1247
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001248 if (!DL.isUnknown()) {
1249 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001250 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001251 } else
Devang Patel4243e672011-05-11 19:22:19 +00001252 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001253 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001254 }
Devang Patelaead63c2010-03-29 22:59:58 +00001255
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001256 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001257 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1258 LabelsBeforeInsn.find(MI);
1259
1260 // No label needed.
1261 if (I == LabelsBeforeInsn.end())
1262 return;
1263
1264 // Label already assigned.
1265 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001266 return;
Devang Patel553881b2010-03-29 17:20:31 +00001267
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001268 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001269 PrevLabel = MMI->getContext().CreateTempSymbol();
1270 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001271 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001272 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001273}
1274
Eric Christopherb6dc8652012-11-27 22:43:45 +00001275// Process end of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +00001276void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001277 // Don't create a new label after DBG_VALUE instructions.
1278 // They don't generate code.
1279 if (!MI->isDebugValue())
1280 PrevLabel = 0;
1281
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001282 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1283 LabelsAfterInsn.find(MI);
1284
1285 // No label needed.
1286 if (I == LabelsAfterInsn.end())
1287 return;
1288
1289 // Label already assigned.
1290 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001291 return;
1292
1293 // We need a label after this instruction.
1294 if (!PrevLabel) {
1295 PrevLabel = MMI->getContext().CreateTempSymbol();
1296 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001297 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001298 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001299}
1300
Eric Christopherb6dc8652012-11-27 22:43:45 +00001301// Each LexicalScope has first instruction and last instruction to mark
1302// beginning and end of a scope respectively. Create an inverse map that list
1303// scopes starts (and ends) with an instruction. One instruction may start (or
1304// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001305void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001306 SmallVector<LexicalScope *, 4> WorkList;
1307 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001308 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001309 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001310
Devang Patelbf47fdb2011-08-10 20:55:27 +00001311 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001312 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001313 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001314 SE = Children.end(); SI != SE; ++SI)
1315 WorkList.push_back(*SI);
1316
Devang Patel53bb5c92009-11-10 23:06:00 +00001317 if (S->isAbstractScope())
1318 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001319
Devang Patelbf47fdb2011-08-10 20:55:27 +00001320 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001321 if (Ranges.empty())
1322 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001323 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001324 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001325 assert(RI->first && "InsnRange does not have first instruction!");
1326 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001327 requestLabelBeforeInsn(RI->first);
1328 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001329 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001330 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001331}
1332
Eric Christopherb6dc8652012-11-27 22:43:45 +00001333// Get MDNode for DebugLoc's scope.
Devang Patela3f48672011-05-09 22:14:49 +00001334static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1335 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1336 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1337 return DL.getScope(Ctx);
1338}
1339
Eric Christopherb6dc8652012-11-27 22:43:45 +00001340// Walk up the scope chain of given debug loc and find line number info
1341// for the function.
Devang Patel4243e672011-05-11 19:22:19 +00001342static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1343 const MDNode *Scope = getScopeNode(DL, Ctx);
1344 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher6126a1e2012-04-03 00:43:49 +00001345 if (SP.Verify()) {
1346 // Check for number of operands since the compatibility is
1347 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001348 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-04-03 00:43:49 +00001349 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1350 else
1351 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1352 }
1353
Devang Patel4243e672011-05-11 19:22:19 +00001354 return DebugLoc();
1355}
1356
Eric Christopherb6dc8652012-11-27 22:43:45 +00001357// Gather pre-function debug information. Assumes being called immediately
1358// after the function entry point has been emitted.
Chris Lattnereec791a2010-01-26 23:18:02 +00001359void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001360 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001361 LScopes.initialize(*MF);
1362 if (LScopes.empty()) return;
1363 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001364
Devang Pateleac9c072010-04-27 19:46:33 +00001365 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1366 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001367 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001368 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001369
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001370 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1371
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001372 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopherb6dc8652012-11-27 22:43:45 +00001373 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001374 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1375
Devang Patelb2b31a62010-05-26 19:37:24 +00001376 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001377 I != E; ++I) {
1378 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001379 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1380 II != IE; ++II) {
1381 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001382
Devang Patelb2b31a62010-05-26 19:37:24 +00001383 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001384 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001385
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001386 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001387 const MDNode *Var =
1388 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001389
1390 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001391 if (isDbgValueInDefinedReg(MI))
1392 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1393
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001394 // Check the history of this variable.
1395 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1396 if (History.empty()) {
1397 UserVariables.push_back(Var);
1398 // The first mention of a function argument gets the FunctionBeginSym
1399 // label, so arguments are visible when breaking at function entry.
1400 DIVariable DV(Var);
1401 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1402 DISubprogram(getDISubprogram(DV.getContext()))
1403 .describes(MF->getFunction()))
1404 LabelsBeforeInsn[MI] = FunctionBeginSym;
1405 } else {
1406 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1407 const MachineInstr *Prev = History.back();
1408 if (Prev->isDebugValue()) {
1409 // Coalesce identical entries at the end of History.
1410 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001411 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher02c1a642012-10-08 20:48:49 +00001412 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001413 << "\t" << *Prev
Devang Patel79862892011-07-07 00:14:27 +00001414 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001415 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001416 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001417
1418 // Terminate old register assignments that don't reach MI;
1419 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1420 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1421 isDbgValueInDefinedReg(Prev)) {
1422 // Previous register assignment needs to terminate at the end of
1423 // its basic block.
1424 MachineBasicBlock::const_iterator LastMI =
1425 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001426 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001427 // Drop DBG_VALUE for empty range.
Eric Christopher02c1a642012-10-08 20:48:49 +00001428 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Devang Patel79862892011-07-07 00:14:27 +00001429 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001430 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001431 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001432 else {
1433 // Terminate after LastMI.
1434 History.push_back(LastMI);
1435 }
1436 }
1437 }
1438 }
1439 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001440 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001441 // Not a DBG_VALUE instruction.
1442 if (!MI->isLabel())
1443 AtBlockEntry = false;
1444
Eric Christopher0313ced2012-10-04 20:46:14 +00001445 // First known non-DBG_VALUE and non-frame setup location marks
1446 // the beginning of the function body.
1447 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1448 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel4243e672011-05-11 19:22:19 +00001449 PrologEndLoc = MI->getDebugLoc();
1450
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001451 // Check if the instruction clobbers any registers with debug vars.
1452 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1453 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1454 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1455 continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001456 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1457 AI.isValid(); ++AI) {
1458 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001459 const MDNode *Var = LiveUserVar[Reg];
1460 if (!Var)
1461 continue;
1462 // Reg is now clobbered.
1463 LiveUserVar[Reg] = 0;
1464
1465 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001466 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1467 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001468 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001469 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1470 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001471 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001472 const MachineInstr *Prev = History.back();
1473 // Sanity-check: Register assignments are terminated at the end of
1474 // their block.
1475 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1476 continue;
1477 // Is the variable still in Reg?
1478 if (!isDbgValueInDefinedReg(Prev) ||
1479 Prev->getOperand(0).getReg() != Reg)
1480 continue;
1481 // Var is clobbered. Make sure the next instruction gets a label.
1482 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001483 }
1484 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001485 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001486 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001487 }
1488
1489 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1490 I != E; ++I) {
1491 SmallVectorImpl<const MachineInstr*> &History = I->second;
1492 if (History.empty())
1493 continue;
1494
1495 // Make sure the final register assignments are terminated.
1496 const MachineInstr *Prev = History.back();
1497 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1498 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001499 MachineBasicBlock::const_iterator LastMI =
Devang Patel5bc942c2011-08-10 23:58:09 +00001500 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001501 if (LastMI == PrevMBB->end())
1502 // Drop DBG_VALUE for empty range.
1503 History.pop_back();
1504 else {
1505 // Terminate after LastMI.
1506 History.push_back(LastMI);
1507 }
1508 }
1509 // Request labels for the full history.
1510 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1511 const MachineInstr *MI = History[i];
1512 if (MI->isDebugValue())
1513 requestLabelBeforeInsn(MI);
1514 else
1515 requestLabelAfterInsn(MI);
1516 }
1517 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001518
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001519 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001520 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001521
1522 // Record beginning of function.
1523 if (!PrologEndLoc.isUnknown()) {
1524 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1525 MF->getFunction()->getContext());
1526 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1527 FnStartDL.getScope(MF->getFunction()->getContext()),
David Blaikie836cfc42012-12-04 22:02:33 +00001528 // We'd like to list the prologue as "not statements" but GDB behaves
1529 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
David Blaikieb36c5312012-12-04 21:05:36 +00001530 DWARF2_FLAG_IS_STMT);
Devang Patel4243e672011-05-11 19:22:19 +00001531 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001532}
1533
Devang Patelbf47fdb2011-08-10 20:55:27 +00001534void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1535// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1536 ScopeVariables[LS].push_back(Var);
1537// Vars.push_back(Var);
1538}
1539
Eric Christopherb6dc8652012-11-27 22:43:45 +00001540// Gather and emit post-function debug information.
Chris Lattnereec791a2010-01-26 23:18:02 +00001541void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001542 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001543
Devang Patelbf47fdb2011-08-10 20:55:27 +00001544 // Define end label for subprogram.
1545 FunctionEndSym = Asm->GetTempSymbol("func_end",
1546 Asm->getFunctionNumber());
1547 // Assumes in correct section after the entry point.
1548 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001549
Devang Patelbf47fdb2011-08-10 20:55:27 +00001550 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1551 collectVariableInfo(MF, ProcessedVars);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001552
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001553 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001554 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001555 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001556
Devang Patelbf47fdb2011-08-10 20:55:27 +00001557 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001558 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1559 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1560 LexicalScope *AScope = AList[i];
1561 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001562 if (SP.Verify()) {
1563 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001564 DIArray Variables = SP.getVariables();
1565 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1566 DIVariable DV(Variables.getElement(i));
1567 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1568 continue;
Alexey Samsonovb67bd332012-07-06 08:45:08 +00001569 // Check that DbgVariable for DV wasn't created earlier, when
1570 // findAbstractVariable() was called for inlined instance of DV.
1571 LLVMContext &Ctx = DV->getContext();
1572 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1573 if (AbstractVariables.lookup(CleanDV))
1574 continue;
Devang Patel93d39be2011-08-19 23:28:12 +00001575 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1576 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001577 }
1578 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001579 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001580 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001581 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001582
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001583 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001584
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001585 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001586 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001587
Devang Patelbf47fdb2011-08-10 20:55:27 +00001588 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1589 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001590
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001591 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001592 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1593 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1594 DeleteContainerPointers(I->second);
1595 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001596 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001597 UserVariables.clear();
1598 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001599 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001600 LabelsBeforeInsn.clear();
1601 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001602 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001603}
1604
Eric Christopherb6dc8652012-11-27 22:43:45 +00001605// Register a source line with debug info. Returns the unique label that was
1606// emitted and which provides correspondence to the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001607void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1608 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001609 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001610 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001611 unsigned Src = 1;
1612 if (S) {
1613 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001614
Dan Gohman1cc0d622010-05-05 23:41:32 +00001615 if (Scope.isCompileUnit()) {
1616 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001617 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001618 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001619 } else if (Scope.isFile()) {
1620 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001621 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001622 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001623 } else if (Scope.isSubprogram()) {
1624 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001625 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001626 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001627 } else if (Scope.isLexicalBlockFile()) {
1628 DILexicalBlockFile DBF(S);
1629 Fn = DBF.getFilename();
1630 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001631 } else if (Scope.isLexicalBlock()) {
1632 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001633 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001634 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001635 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001636 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001637
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001638 Src = getOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001639 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001640 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001641}
1642
Bill Wendling829e67b2009-05-20 23:22:40 +00001643//===----------------------------------------------------------------------===//
1644// Emit Methods
1645//===----------------------------------------------------------------------===//
1646
Eric Christopherb6dc8652012-11-27 22:43:45 +00001647// Compute the size and offset of a DIE.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001648unsigned
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001649DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001650 // Get the children.
1651 const std::vector<DIE *> &Children = Die->getChildren();
1652
Bill Wendling94d04b82009-05-20 23:21:38 +00001653 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001654 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001655
1656 // Get the abbreviation for this DIE.
1657 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001658 const DIEAbbrev *Abbrev = Abbreviations->at(AbbrevNumber - 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001659
1660 // Set DIE offset
1661 Die->setOffset(Offset);
1662
1663 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001664 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001665
1666 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1667 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1668
1669 // Size the DIE attribute values.
1670 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1671 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001672 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001673
1674 // Size the DIE children if any.
1675 if (!Children.empty()) {
1676 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1677 "Children flag not set");
1678
1679 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopherfbd19752012-11-20 22:14:13 +00001680 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling94d04b82009-05-20 23:21:38 +00001681
1682 // End of children marker.
1683 Offset += sizeof(int8_t);
1684 }
1685
1686 Die->setSize(Offset - Die->getOffset());
1687 return Offset;
1688}
1689
Eric Christopherb6dc8652012-11-27 22:43:45 +00001690// Compute the size and offset of all the DIEs.
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001691void DwarfUnits::computeSizeAndOffsets() {
1692 for (SmallVector<CompileUnit *, 1>::iterator I = CUs.begin(),
1693 E = CUs.end(); I != E; ++I) {
Eric Christopher98e237f2012-11-30 23:59:06 +00001694 unsigned Offset =
1695 sizeof(int32_t) + // Length of Compilation Unit Info
1696 sizeof(int16_t) + // DWARF version number
1697 sizeof(int32_t) + // Offset Into Abbrev. Section
1698 sizeof(int8_t); // Pointer Size (in bytes)
1699
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001700 computeSizeAndOffset((*I)->getCUDie(), Offset);
Devang Patel163a9f72010-05-10 22:49:55 +00001701 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001702}
1703
Eric Christopherb6dc8652012-11-27 22:43:45 +00001704// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001705void DwarfDebug::emitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001706 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001707
Bill Wendling94d04b82009-05-20 23:21:38 +00001708 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001709 DwarfInfoSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001710 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001711 DwarfAbbrevSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001712 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher6eebe472012-12-19 22:02:53 +00001713 if (useSplitDwarf())
1714 DwarfAbbrevDWOSectionSym =
1715 emitSectionSym(Asm, TLOF.getDwarfAbbrevDWOSection(),
1716 "section_abbrev_dwo");
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001717 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001718
Chris Lattner9c69e285532010-04-04 22:59:04 +00001719 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001720 emitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001721
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001722 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
1723 emitSectionSym(Asm, TLOF.getDwarfLocSection());
1724 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001725 DwarfStrSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001726 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
1727 DwarfDebugRangeSectionSym = emitSectionSym(Asm, TLOF.getDwarfRangesSection(),
Devang Patelf2548ca2010-04-16 23:33:45 +00001728 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001729
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001730 DwarfDebugLocSectionSym = emitSectionSym(Asm, TLOF.getDwarfLocSection(),
Devang Patelc3f5f782010-05-25 23:40:22 +00001731 "section_debug_loc");
1732
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001733 TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
1734 emitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001735}
1736
Eric Christopherb6dc8652012-11-27 22:43:45 +00001737// Recursively emits a debug information entry.
Eric Christopher6eebe472012-12-19 22:02:53 +00001738void DwarfDebug::emitDIE(DIE *Die, std::vector<DIEAbbrev *> *Abbrevs) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001739 // Get the abbreviation for this DIE.
1740 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher6eebe472012-12-19 22:02:53 +00001741 const DIEAbbrev *Abbrev = Abbrevs->at(AbbrevNumber - 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001742
Bill Wendling94d04b82009-05-20 23:21:38 +00001743 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001744 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001745 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1746 Twine::utohexstr(Die->getOffset()) + ":0x" +
1747 Twine::utohexstr(Die->getSize()) + " " +
1748 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001749 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001750
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001751 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001752 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1753
1754 // Emit the DIE attribute values.
1755 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1756 unsigned Attr = AbbrevData[i].getAttribute();
1757 unsigned Form = AbbrevData[i].getForm();
1758 assert(Form && "Too many attributes for DIE (check abbreviation)");
1759
Chris Lattner3f53c832010-04-04 18:52:31 +00001760 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001761 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001762
Bill Wendling94d04b82009-05-20 23:21:38 +00001763 switch (Attr) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001764 case dwarf::DW_AT_abstract_origin: {
1765 DIEEntry *E = cast<DIEEntry>(Values[i]);
1766 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001767 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001768 Asm->EmitInt32(Addr);
1769 break;
1770 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001771 case dwarf::DW_AT_ranges: {
1772 // DW_AT_range Value encodes offset in debug_range section.
1773 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001774
Nick Lewyckyffccd922012-06-22 01:25:12 +00001775 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001776 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1777 V->getValue(),
1778 4);
1779 } else {
1780 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1781 V->getValue(),
1782 DwarfDebugRangeSectionSym,
1783 4);
1784 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001785 break;
1786 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001787 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-06-22 01:25:12 +00001788 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1789 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1790 Asm->EmitLabelReference(L->getValue(), 4);
1791 else
1792 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1793 } else {
Devang Patelc3f5f782010-05-25 23:40:22 +00001794 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00001795 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001796 break;
1797 }
Devang Patel2a361602010-09-29 19:08:08 +00001798 case dwarf::DW_AT_accessibility: {
1799 if (Asm->isVerbose()) {
1800 DIEInteger *V = cast<DIEInteger>(Values[i]);
1801 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1802 }
1803 Values[i]->EmitValue(Asm, Form);
1804 break;
1805 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001806 default:
1807 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001808 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001809 break;
1810 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001811 }
1812
1813 // Emit the DIE children if any.
1814 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1815 const std::vector<DIE *> &Children = Die->getChildren();
1816
1817 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher6eebe472012-12-19 22:02:53 +00001818 emitDIE(Children[j], Abbrevs);
Bill Wendling94d04b82009-05-20 23:21:38 +00001819
Chris Lattner3f53c832010-04-04 18:52:31 +00001820 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001821 Asm->OutStreamer.AddComment("End Of Children Mark");
1822 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001823 }
1824}
1825
Eric Christopherb1e66d02012-12-15 00:04:07 +00001826// Emit the various dwarf units to the unit section USection with
1827// the abbreviations going into ASection.
1828void DwarfUnits::emitUnits(DwarfDebug *DD,
1829 const MCSection *USection,
1830 const MCSection *ASection,
1831 const MCSymbol *ASectionSym) {
1832 Asm->OutStreamer.SwitchSection(USection);
1833 for (SmallVector<CompileUnit *, 1>::iterator I = CUs.begin(),
1834 E = CUs.end(); I != E; ++I) {
1835 CompileUnit *TheCU = *I;
Devang Patel163a9f72010-05-10 22:49:55 +00001836 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001837
Devang Patel163a9f72010-05-10 22:49:55 +00001838 // Emit the compile units header.
Eric Christopherb1e66d02012-12-15 00:04:07 +00001839 Asm->OutStreamer
1840 .EmitLabel(Asm->GetTempSymbol(USection->getLabelBeginName(),
1841 TheCU->getUniqueID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001842
Devang Patel163a9f72010-05-10 22:49:55 +00001843 // Emit size of content not including length itself
1844 unsigned ContentSize = Die->getSize() +
1845 sizeof(int16_t) + // DWARF version number
1846 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001847 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001848
Devang Patel163a9f72010-05-10 22:49:55 +00001849 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1850 Asm->EmitInt32(ContentSize);
1851 Asm->OutStreamer.AddComment("DWARF version number");
1852 Asm->EmitInt16(dwarf::DWARF_VERSION);
1853 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christopherb1e66d02012-12-15 00:04:07 +00001854 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
1855 ASectionSym);
Devang Patel163a9f72010-05-10 22:49:55 +00001856 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001857 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001858
Eric Christopher6eebe472012-12-19 22:02:53 +00001859 DD->emitDIE(Die, Abbreviations);
Eric Christopherb1e66d02012-12-15 00:04:07 +00001860 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(USection->getLabelEndName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00001861 TheCU->getUniqueID()));
Devang Patel163a9f72010-05-10 22:49:55 +00001862 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001863}
1864
Eric Christopher98e237f2012-11-30 23:59:06 +00001865// Emit the debug info section.
1866void DwarfDebug::emitDebugInfo() {
Eric Christopherb1e66d02012-12-15 00:04:07 +00001867 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1868
1869 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
1870 Asm->getObjFileLowering().getDwarfAbbrevSection(),
1871 DwarfAbbrevSectionSym);
Eric Christopher98e237f2012-11-30 23:59:06 +00001872}
1873
Eric Christopherb6dc8652012-11-27 22:43:45 +00001874// Emit the abbreviation section.
Eric Christopher7dc68db2012-11-20 23:30:11 +00001875void DwarfDebug::emitAbbreviations() {
Eric Christopher6eebe472012-12-19 22:02:53 +00001876 if (!useSplitDwarf())
1877 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
1878 &Abbreviations);
1879 else
1880 emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
1881}
Bill Wendling94d04b82009-05-20 23:21:38 +00001882
Eric Christopher6eebe472012-12-19 22:02:53 +00001883void DwarfDebug::emitAbbrevs(const MCSection *Section,
1884 std::vector<DIEAbbrev *> *Abbrevs) {
1885 // Check to see if it is worth the effort.
1886 if (!Abbrevs->empty()) {
1887 // Start the debug abbrev section.
1888 Asm->OutStreamer.SwitchSection(Section);
1889
1890 MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Eric Christopher44fedba2012-12-13 03:00:38 +00001891 Asm->OutStreamer.EmitLabel(Begin);
Bill Wendling94d04b82009-05-20 23:21:38 +00001892
1893 // For each abbrevation.
Eric Christopher6eebe472012-12-19 22:02:53 +00001894 for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001895 // Get abbreviation data
Eric Christopher6eebe472012-12-19 22:02:53 +00001896 const DIEAbbrev *Abbrev = Abbrevs->at(i);
Bill Wendling94d04b82009-05-20 23:21:38 +00001897
1898 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001899 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001900
1901 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001902 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001903 }
1904
1905 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001906 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001907
Eric Christopher6eebe472012-12-19 22:02:53 +00001908 MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Eric Christopher44fedba2012-12-13 03:00:38 +00001909 Asm->OutStreamer.EmitLabel(End);
Bill Wendling94d04b82009-05-20 23:21:38 +00001910 }
1911}
1912
Eric Christopherb6dc8652012-11-27 22:43:45 +00001913// Emit the last address of the section and the end of the line matrix.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001914void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001915 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001916 Asm->OutStreamer.AddComment("Extended Op");
1917 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001918
Chris Lattner233f52b2010-03-09 23:52:58 +00001919 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001920 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001921 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1922 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1923
1924 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001925
Chris Lattnerc0215722010-04-04 19:25:43 +00001926 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001927 Asm->getDataLayout().getPointerSize(),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001928 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001929
1930 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001931 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1932 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001933 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001934 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001935}
1936
Eric Christopherb6dc8652012-11-27 22:43:45 +00001937// Emit visible names into a hashed accelerator table section.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001938void DwarfDebug::emitAccelNames() {
1939 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1940 dwarf::DW_FORM_data4));
1941 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1942 E = CUMap.end(); I != E; ++I) {
1943 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001944 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1945 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001946 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1947 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001948 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001949 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1950 DE = Entities.end(); DI != DE; ++DI)
1951 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001952 }
1953 }
1954
1955 AT.FinalizeTable(Asm, "Names");
1956 Asm->OutStreamer.SwitchSection(
1957 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1958 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1959 Asm->OutStreamer.EmitLabel(SectionBegin);
1960
1961 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00001962 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001963}
1964
Eric Christopherb6dc8652012-11-27 22:43:45 +00001965// Emit objective C classes and categories into a hashed accelerator table section.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001966void DwarfDebug::emitAccelObjC() {
1967 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1968 dwarf::DW_FORM_data4));
1969 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1970 E = CUMap.end(); I != E; ++I) {
1971 CompileUnit *TheCU = I->second;
1972 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1973 for (StringMap<std::vector<DIE*> >::const_iterator
1974 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1975 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001976 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-11-07 09:24:32 +00001977 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1978 DE = Entities.end(); DI != DE; ++DI)
1979 AT.AddName(Name, (*DI));
1980 }
1981 }
1982
1983 AT.FinalizeTable(Asm, "ObjC");
1984 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1985 .getDwarfAccelObjCSection());
1986 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1987 Asm->OutStreamer.EmitLabel(SectionBegin);
1988
1989 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00001990 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001991}
1992
Eric Christopherb6dc8652012-11-27 22:43:45 +00001993// Emit namespace dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001994void DwarfDebug::emitAccelNamespaces() {
1995 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1996 dwarf::DW_FORM_data4));
1997 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1998 E = CUMap.end(); I != E; ++I) {
1999 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00002000 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
2001 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00002002 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2003 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00002004 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00002005 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2006 DE = Entities.end(); DI != DE; ++DI)
2007 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00002008 }
2009 }
2010
2011 AT.FinalizeTable(Asm, "namespac");
2012 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2013 .getDwarfAccelNamespaceSection());
2014 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2015 Asm->OutStreamer.EmitLabel(SectionBegin);
2016
2017 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002018 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002019}
2020
Eric Christopherb6dc8652012-11-27 22:43:45 +00002021// Emit type dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00002022void DwarfDebug::emitAccelTypes() {
Eric Christopherc36145f2012-01-06 04:35:23 +00002023 std::vector<DwarfAccelTable::Atom> Atoms;
2024 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2025 dwarf::DW_FORM_data4));
2026 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
2027 dwarf::DW_FORM_data2));
2028 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
2029 dwarf::DW_FORM_data1));
2030 DwarfAccelTable AT(Atoms);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002031 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2032 E = CUMap.end(); I != E; ++I) {
2033 CompileUnit *TheCU = I->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00002034 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
2035 = TheCU->getAccelTypes();
2036 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00002037 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2038 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00002039 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00002040 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
2041 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
2042 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002043 }
2044 }
2045
2046 AT.FinalizeTable(Asm, "types");
2047 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2048 .getDwarfAccelTypesSection());
2049 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2050 Asm->OutStreamer.EmitLabel(SectionBegin);
2051
2052 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002053 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002054}
2055
Devang Patel193f7202009-11-24 01:14:22 +00002056void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00002057 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2058 E = CUMap.end(); I != E; ++I) {
2059 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00002060 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-05-10 22:49:55 +00002061 Asm->OutStreamer.SwitchSection(
2062 Asm->getObjFileLowering().getDwarfPubTypesSection());
2063 Asm->OutStreamer.AddComment("Length of Public Types Info");
2064 Asm->EmitLabelDifference(
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002065 Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()),
2066 Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002067
Devang Patel163a9f72010-05-10 22:49:55 +00002068 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002069 TheCU->getUniqueID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002070
Devang Patel163a9f72010-05-10 22:49:55 +00002071 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2072 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002073
Devang Patel163a9f72010-05-10 22:49:55 +00002074 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002075 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2076 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002077 TheCU->getUniqueID()),
Devang Patel163a9f72010-05-10 22:49:55 +00002078 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002079
Devang Patel163a9f72010-05-10 22:49:55 +00002080 Asm->OutStreamer.AddComment("Compilation Unit Length");
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002081 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002082 TheCU->getUniqueID()),
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002083 Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002084 TheCU->getUniqueID()),
Devang Patel163a9f72010-05-10 22:49:55 +00002085 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002086
Devang Patel163a9f72010-05-10 22:49:55 +00002087 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2088 for (StringMap<DIE*>::const_iterator
2089 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2090 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00002091 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002092
Devang Patel163a9f72010-05-10 22:49:55 +00002093 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2094 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002095
Devang Patel163a9f72010-05-10 22:49:55 +00002096 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00002097 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00002098 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
2099 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002100
Devang Patel163a9f72010-05-10 22:49:55 +00002101 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002102 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00002103 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002104 TheCU->getUniqueID()));
Devang Patel193f7202009-11-24 01:14:22 +00002105 }
Devang Patel193f7202009-11-24 01:14:22 +00002106}
2107
Eric Christopherb6dc8652012-11-27 22:43:45 +00002108// Emit visible names into a debug str section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002109void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002110 // Check to see if it is worth the effort.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002111 if (InfoHolder.getStringPool()->empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002112
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002113 // Start the dwarf str section.
2114 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002115 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002116
Chris Lattnerbc733f52010-03-13 02:17:42 +00002117 // Get all of the string pool entries and put them in an array by their ID so
2118 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002119 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00002120 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002121
Chris Lattnerbc733f52010-03-13 02:17:42 +00002122 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
Eric Christopher2e5d8702012-12-20 21:58:36 +00002123 I = InfoHolder.getStringPool()->begin(), E = InfoHolder.getStringPool()->end(); I != E; ++I)
Chris Lattnerbc733f52010-03-13 02:17:42 +00002124 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002125
Chris Lattnerbc733f52010-03-13 02:17:42 +00002126 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002127
Chris Lattnerbc733f52010-03-13 02:17:42 +00002128 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002129 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002130 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002131
Benjamin Kramer983c4572011-11-09 18:16:11 +00002132 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00002133 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
2134 Entries[i].second->getKeyLength()+1),
2135 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002136 }
2137}
2138
Eric Christopherb6dc8652012-11-27 22:43:45 +00002139// Emit visible names into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002140void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002141 if (DotDebugLocEntries.empty())
2142 return;
2143
Devang Patel6c3ea902011-02-04 22:57:18 +00002144 for (SmallVector<DotDebugLocEntry, 4>::iterator
2145 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2146 I != E; ++I) {
2147 DotDebugLocEntry &Entry = *I;
2148 if (I + 1 != DotDebugLocEntries.end())
2149 Entry.Merge(I+1);
2150 }
2151
Daniel Dunbar83320a02011-03-16 22:16:39 +00002152 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002153 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002154 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002155 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002156 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2157 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002158 for (SmallVector<DotDebugLocEntry, 4>::iterator
2159 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002160 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002161 DotDebugLocEntry &Entry = *I;
2162 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002163 if (Entry.isEmpty()) {
2164 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2165 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002166 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002167 } else {
2168 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2169 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002170 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002171 Asm->OutStreamer.AddComment("Loc expr size");
2172 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2173 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2174 Asm->EmitLabelDifference(end, begin, 2);
2175 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002176 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002177 DIBasicType BTy(DV.getType());
2178 if (BTy.Verify() &&
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002179 (BTy.getEncoding() == dwarf::DW_ATE_signed
Devang Patelc4329072011-06-01 22:03:25 +00002180 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2181 Asm->OutStreamer.AddComment("DW_OP_consts");
2182 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002183 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002184 } else {
2185 Asm->OutStreamer.AddComment("DW_OP_constu");
2186 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002187 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002188 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002189 } else if (Entry.isLocation()) {
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002190 if (!DV.hasComplexAddress())
Devang Patel80efd4e2011-07-08 16:49:43 +00002191 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002192 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002193 else {
2194 // Complex address entry.
2195 unsigned N = DV.getNumAddrElements();
2196 unsigned i = 0;
2197 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2198 if (Entry.Loc.getOffset()) {
2199 i = 2;
2200 Asm->EmitDwarfRegOp(Entry.Loc);
2201 Asm->OutStreamer.AddComment("DW_OP_deref");
2202 Asm->EmitInt8(dwarf::DW_OP_deref);
2203 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2204 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2205 Asm->EmitSLEB128(DV.getAddrElement(1));
2206 } else {
2207 // If first address element is OpPlus then emit
2208 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2209 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2210 Asm->EmitDwarfRegOp(Loc);
2211 i = 2;
2212 }
2213 } else {
2214 Asm->EmitDwarfRegOp(Entry.Loc);
2215 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002216
Devang Patel80efd4e2011-07-08 16:49:43 +00002217 // Emit remaining complex address elements.
2218 for (; i < N; ++i) {
2219 uint64_t Element = DV.getAddrElement(i);
2220 if (Element == DIBuilder::OpPlus) {
2221 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2222 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher50120762012-05-08 18:56:00 +00002223 } else if (Element == DIBuilder::OpDeref) {
Eric Christophera80f2d12012-05-08 21:24:39 +00002224 if (!Entry.Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002225 Asm->EmitInt8(dwarf::DW_OP_deref);
2226 } else
2227 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002228 }
Devang Patelc26f5442011-04-28 02:22:40 +00002229 }
Devang Patelc26f5442011-04-28 02:22:40 +00002230 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002231 // else ... ignore constant fp. There is not any good way to
2232 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002233 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002234 }
2235 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002236}
2237
Eric Christopherb6dc8652012-11-27 22:43:45 +00002238// Emit visible names into a debug aranges section.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00002239void DwarfDebug::emitDebugARanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002240 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002241 Asm->OutStreamer.SwitchSection(
2242 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002243}
2244
Eric Christopherb6dc8652012-11-27 22:43:45 +00002245// Emit visible names into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002246void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002247 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002248 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002249 Asm->getObjFileLowering().getDwarfRangesSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002250 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Pateleac9c072010-04-27 19:46:33 +00002251 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002252 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002253 I != E; ++I) {
2254 if (*I)
2255 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002256 else
Devang Pateleac9c072010-04-27 19:46:33 +00002257 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002258 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002259}
2260
Eric Christopherb6dc8652012-11-27 22:43:45 +00002261// Emit visible names into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002262void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002263 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002264 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002265 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002266 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002267 }
2268}
2269
Eric Christopherb6dc8652012-11-27 22:43:45 +00002270// Emit inline info using following format.
2271// Section Header:
2272// 1. length of section
2273// 2. Dwarf version number
2274// 3. address size.
2275//
2276// Entries (one "entry" for each function that was inlined):
2277//
2278// 1. offset into __debug_str section for MIPS linkage name, if exists;
2279// otherwise offset into __debug_str for regular function name.
2280// 2. offset into __debug_str section for regular function name.
2281// 3. an unsigned LEB128 number indicating the number of distinct inlining
2282// instances for the function.
2283//
2284// The rest of the entry consists of a {die_offset, low_pc} pair for each
2285// inlined instance; the die_offset points to the inlined_subroutine die in the
2286// __debug_info section, and the low_pc is the starting address for the
2287// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002288void DwarfDebug::emitDebugInlineInfo() {
Eric Christopherb83e2bb2012-03-02 02:11:47 +00002289 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002290 return;
2291
Devang Patel163a9f72010-05-10 22:49:55 +00002292 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002293 return;
2294
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002295 Asm->OutStreamer.SwitchSection(
2296 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002297
Chris Lattner233f52b2010-03-09 23:52:58 +00002298 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002299 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2300 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002301
Chris Lattnerc0215722010-04-04 19:25:43 +00002302 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002303
Chris Lattner233f52b2010-03-09 23:52:58 +00002304 Asm->OutStreamer.AddComment("Dwarf Version");
2305 Asm->EmitInt16(dwarf::DWARF_VERSION);
2306 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002307 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002308
Devang Patele9f8f5e2010-05-07 20:54:48 +00002309 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002310 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002311
Devang Patele9f8f5e2010-05-07 20:54:48 +00002312 const MDNode *Node = *I;
2313 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002314 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002315 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002316 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002317 StringRef LName = SP.getLinkageName();
2318 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002319
Chris Lattner233f52b2010-03-09 23:52:58 +00002320 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher50e26612012-03-02 01:57:52 +00002321 if (LName.empty())
Eric Christopher2e5d8702012-12-20 21:58:36 +00002322 Asm->EmitSectionOffset(InfoHolder.getStringPoolEntry(Name),
2323 DwarfStrSectionSym);
Eric Christopher50e26612012-03-02 01:57:52 +00002324 else
Eric Christopher2e5d8702012-12-20 21:58:36 +00002325 Asm->EmitSectionOffset(InfoHolder
2326 .getStringPoolEntry(getRealLinkageName(LName)),
Chris Lattner6189ed12010-04-04 23:25:33 +00002327 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002328
Chris Lattner233f52b2010-03-09 23:52:58 +00002329 Asm->OutStreamer.AddComment("Function name");
Eric Christopher2e5d8702012-12-20 21:58:36 +00002330 Asm->EmitSectionOffset(InfoHolder.getStringPoolEntry(Name),
2331 DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002332 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002333
Devang Patel53bb5c92009-11-10 23:06:00 +00002334 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002335 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002336 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002337 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002338
Chris Lattner3f53c832010-04-04 18:52:31 +00002339 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002340 Asm->OutStreamer.EmitSymbolValue(LI->first,
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002341 Asm->getDataLayout().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002342 }
2343 }
2344
Chris Lattnerc0215722010-04-04 19:25:43 +00002345 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002346}
Eric Christopher98e237f2012-11-30 23:59:06 +00002347
Eric Christopher0b944ee2012-12-11 19:42:09 +00002348// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher98e237f2012-11-30 23:59:06 +00002349
2350// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2351// DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
2352// DW_AT_ranges_base, DW_AT_addr_base. If DW_AT_ranges is present,
2353// DW_AT_low_pc and DW_AT_high_pc are not used, and vice versa.
Eric Christopher4daaed12012-12-10 19:51:21 +00002354CompileUnit *DwarfDebug::constructSkeletonCU(const MDNode *N) {
Eric Christopher98e237f2012-11-30 23:59:06 +00002355 DICompileUnit DIUnit(N);
2356 StringRef FN = DIUnit.getFilename();
2357 CompilationDir = DIUnit.getDirectory();
Eric Christopher98e237f2012-11-30 23:59:06 +00002358
2359 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002360 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
Eric Christopher2e5d8702012-12-20 21:58:36 +00002361 DIUnit.getLanguage(), Die, Asm,
2362 this, &InfoHolder);
Eric Christopher98e237f2012-11-30 23:59:06 +00002363 // FIXME: This should be the .dwo file.
2364 NewCU->addString(Die, dwarf::DW_AT_GNU_dwo_name, FN);
2365
2366 // FIXME: We also need DW_AT_addr_base and DW_AT_dwo_id.
2367
2368 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
2369 // into an entity.
2370 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
2371 // DW_AT_stmt_list is a offset of line number information for this
2372 // compile unit in debug_line section.
2373 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2374 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
2375 Asm->GetTempSymbol("section_line"));
2376 else
2377 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
2378
2379 if (!CompilationDir.empty())
2380 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
2381
Eric Christopher0e3e9b72012-12-10 23:34:43 +00002382 SkeletonHolder.addUnit(NewCU);
2383
Eric Christopher98e237f2012-11-30 23:59:06 +00002384 return NewCU;
2385}
2386
Eric Christopher4daaed12012-12-10 19:51:21 +00002387void DwarfDebug::emitSkeletonCU(const MCSection *Section) {
Eric Christopher98e237f2012-11-30 23:59:06 +00002388 Asm->OutStreamer.SwitchSection(Section);
Eric Christopher4daaed12012-12-10 19:51:21 +00002389 DIE *Die = SkeletonCU->getCUDie();
Eric Christopher98e237f2012-11-30 23:59:06 +00002390
2391 // Emit the compile units header.
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002392 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(Section->getLabelBeginName(),
Eric Christopher4daaed12012-12-10 19:51:21 +00002393 SkeletonCU->getUniqueID()));
Eric Christopher98e237f2012-11-30 23:59:06 +00002394
2395 // Emit size of content not including length itself
2396 unsigned ContentSize = Die->getSize() +
2397 sizeof(int16_t) + // DWARF version number
2398 sizeof(int32_t) + // Offset Into Abbrev. Section
2399 sizeof(int8_t); // Pointer Size (in bytes)
2400
2401 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2402 Asm->EmitInt32(ContentSize);
2403 Asm->OutStreamer.AddComment("DWARF version number");
2404 Asm->EmitInt16(dwarf::DWARF_VERSION);
2405 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christopher6eebe472012-12-19 22:02:53 +00002406
2407 const MCSection *ASec = Asm->getObjFileLowering().getDwarfAbbrevSection();
2408 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASec->getLabelBeginName()),
Eric Christopher98e237f2012-11-30 23:59:06 +00002409 DwarfAbbrevSectionSym);
2410 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2411 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2412
Eric Christopher6eebe472012-12-19 22:02:53 +00002413 emitDIE(Die, &SkeletonAbbrevs);
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002414 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(Section->getLabelEndName(),
Eric Christopher4daaed12012-12-10 19:51:21 +00002415 SkeletonCU->getUniqueID()));
Eric Christopher6eebe472012-12-19 22:02:53 +00002416}
Eric Christopher98e237f2012-11-30 23:59:06 +00002417
Eric Christopher6eebe472012-12-19 22:02:53 +00002418void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
2419 assert(useSplitDwarf() && "No split dwarf debug info?");
2420 emitAbbrevs(Section, &SkeletonAbbrevs);
Eric Christopher98e237f2012-11-30 23:59:06 +00002421}
2422
Eric Christopher0b944ee2012-12-11 19:42:09 +00002423// Emit the .debug_info.dwo section for separated dwarf. This contains the
2424// compile units that would normally be in debug_info.
Eric Christopher98e237f2012-11-30 23:59:06 +00002425void DwarfDebug::emitDebugInfoDWO() {
Eric Christopher4daaed12012-12-10 19:51:21 +00002426 assert(useSplitDwarf() && "No split dwarf debug info?");
Eric Christopherb1e66d02012-12-15 00:04:07 +00002427 InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Eric Christopher6eebe472012-12-19 22:02:53 +00002428 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2429 DwarfAbbrevDWOSectionSym);
2430}
2431
2432// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2433// abbreviations for the .debug_info.dwo section.
2434void DwarfDebug::emitDebugAbbrevDWO() {
2435 assert(useSplitDwarf() && "No split dwarf?");
2436 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(), &Abbreviations);
Eric Christopher98e237f2012-11-30 23:59:06 +00002437}