blob: 4867bb817711740d11a6ffc91a72096d13b0a0bc [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 Christopher72c16552012-12-20 21:58:40 +0000618// Create new CompileUnit for the given metadata node with tag
619// DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000620CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000621 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000622 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000623 CompilationDir = DIUnit.getDirectory();
Eli Benderskyd4a05e02012-12-03 18:45:45 +0000624 // Call this to emit a .file directive if it wasn't emitted for the source
625 // file this CU comes from yet.
626 getOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000627
628 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyd4a05e02012-12-03 18:45:45 +0000629 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
Eric Christopher2e5d8702012-12-20 21:58:36 +0000630 DIUnit.getLanguage(), Die, Asm,
631 this, &InfoHolder);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000632 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000633 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
634 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000635 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher6635cad2012-08-01 18:19:01 +0000636 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
637 // into an entity.
638 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000639 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000640 // compile unit in debug_line section.
Rafael Espindola2241e512012-06-22 13:24:07 +0000641 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindola597a7662011-05-04 17:44:06 +0000642 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000643 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000644 else
Devang Patel3cbee302011-04-12 22:53:02 +0000645 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000646
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000647 if (!CompilationDir.empty())
648 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000649 if (DIUnit.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000650 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000651
Devang Patel65dbc902009-11-25 17:36:49 +0000652 StringRef Flags = DIUnit.getFlags();
653 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000654 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000655
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000656 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000657 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000658 dwarf::DW_FORM_data1, RVer);
659
Devang Patel163a9f72010-05-10 22:49:55 +0000660 if (!FirstCU)
661 FirstCU = NewCU;
Eric Christopher4daaed12012-12-10 19:51:21 +0000662 if (useSplitDwarf() && !SkeletonCU)
663 SkeletonCU = constructSkeletonCU(N);
Eric Christopher98e237f2012-11-30 23:59:06 +0000664
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000665 InfoHolder.addUnit(NewCU);
666
Devang Patel163a9f72010-05-10 22:49:55 +0000667 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000668 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000669}
670
Eric Christopherb6dc8652012-11-27 22:43:45 +0000671// Construct subprogram DIE.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000672void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
Devang Patel3655a212011-08-15 23:36:40 +0000673 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000674 CompileUnit *&CURef = SPMap[N];
675 if (CURef)
676 return;
677 CURef = TheCU;
678
Devang Patele4b27562009-08-28 23:24:31 +0000679 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000680 if (!SP.isDefinition())
681 // This is a method declaration which will be handled while constructing
682 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000683 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000684
Devang Pateldbc64af2011-08-15 17:24:54 +0000685 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000686
687 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000688 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000689
690 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000691 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000692
Devang Patel13e16b62009-06-26 01:49:18 +0000693 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000694}
695
Eric Christopherb6dc8652012-11-27 22:43:45 +0000696// Collect debug info from named mdnodes such as llvm.dbg.enum and llvm.dbg.ty.
Eric Christopherc4639d62012-11-19 22:42:15 +0000697void DwarfDebug::collectInfoFromNamedMDNodes(const Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000698 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
699 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
700 const MDNode *N = NMD->getOperand(i);
701 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
702 constructSubprogramDIE(CU, N);
703 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000704
Devang Patel94c7ddb2011-08-16 22:09:43 +0000705 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
706 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
707 const MDNode *N = NMD->getOperand(i);
708 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000709 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000710 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000711
Devang Patel02e603f2011-08-15 23:47:24 +0000712 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
713 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
714 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000715 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
716 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000717 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000718
Devang Patel02e603f2011-08-15 23:47:24 +0000719 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
720 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
721 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000722 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
723 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000724 }
725}
726
Eric Christopherb6dc8652012-11-27 22:43:45 +0000727// Collect debug info using DebugInfoFinder.
728// FIXME - Remove this when dragonegg switches to DIBuilder.
Eric Christopherc4639d62012-11-19 22:42:15 +0000729bool DwarfDebug::collectLegacyDebugInfo(const Module *M) {
Devang Patel02e603f2011-08-15 23:47:24 +0000730 DebugInfoFinder DbgFinder;
731 DbgFinder.processModule(*M);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000732
Devang Patel02e603f2011-08-15 23:47:24 +0000733 bool HasDebugInfo = false;
734 // Scan all the compile-units to see if there are any marked as the main
735 // unit. If not, we do not generate debug info.
736 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
737 E = DbgFinder.compile_unit_end(); I != E; ++I) {
738 if (DICompileUnit(*I).isMain()) {
739 HasDebugInfo = true;
740 break;
741 }
742 }
743 if (!HasDebugInfo) return false;
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000744
Devang Patel02e603f2011-08-15 23:47:24 +0000745 // Create all the compile unit DIEs.
746 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
747 E = DbgFinder.compile_unit_end(); I != E; ++I)
748 constructCompileUnit(*I);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000749
Devang Patel02e603f2011-08-15 23:47:24 +0000750 // Create DIEs for each global variable.
751 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
752 E = DbgFinder.global_variable_end(); I != E; ++I) {
753 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000754 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000755 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000756 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000757
Devang Patel02e603f2011-08-15 23:47:24 +0000758 // Create DIEs for each subprogram.
759 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
760 E = DbgFinder.subprogram_end(); I != E; ++I) {
761 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000762 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
763 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000764 }
765
766 return HasDebugInfo;
767}
768
Eric Christopherb6dc8652012-11-27 22:43:45 +0000769// Emit all Dwarf sections that should come prior to the content. Create
770// global DIEs and emit initial debug info sections. This is invoked by
771// the target AsmPrinter.
Eric Christopherc4639d62012-11-19 22:42:15 +0000772void DwarfDebug::beginModule() {
Devang Pateleac9c072010-04-27 19:46:33 +0000773 if (DisableDebugInfoPrinting)
774 return;
775
Eric Christopherc4639d62012-11-19 22:42:15 +0000776 const Module *M = MMI->getModule();
777
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000778 // If module has named metadata anchors then use them, otherwise scan the
779 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000780 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
781 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000782 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
783 DICompileUnit CUNode(CU_Nodes->getOperand(i));
784 CompileUnit *CU = constructCompileUnit(CUNode);
785 DIArray GVs = CUNode.getGlobalVariables();
786 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000787 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000788 DIArray SPs = CUNode.getSubprograms();
789 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
790 constructSubprogramDIE(CU, SPs.getElement(i));
791 DIArray EnumTypes = CUNode.getEnumTypes();
792 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
793 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
794 DIArray RetainedTypes = CUNode.getRetainedTypes();
795 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
796 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
797 }
Devang Patel02e603f2011-08-15 23:47:24 +0000798 } else if (!collectLegacyDebugInfo(M))
799 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000800
Devang Patel02e603f2011-08-15 23:47:24 +0000801 collectInfoFromNamedMDNodes(M);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000802
Chris Lattnerd850ac72010-04-05 02:19:28 +0000803 // Tell MMI that we have debug info.
804 MMI->setDebugInfoAvailability(true);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000805
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000806 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000807 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000808}
809
Eric Christopher4117bec2012-11-22 00:59:49 +0000810// Attach DW_AT_inline attribute with inlined subprogram DIEs.
811void DwarfDebug::computeInlinedDIEs() {
Devang Patel53bb5c92009-11-10 23:06:00 +0000812 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
813 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000814 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
Devang Patel53bb5c92009-11-10 23:06:00 +0000815 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000816 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000817 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000818 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000819 AE = AbstractSPDies.end(); AI != AE; ++AI) {
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000820 DIE *ISP = AI->second;
821 if (InlinedSubprogramDIEs.count(ISP))
822 continue;
823 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
824 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000825}
826
827// Collect info for variables that were optimized out.
828void DwarfDebug::collectDeadVariables() {
829 const Module *M = MMI->getModule();
830 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
831
832 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
833 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
834 DICompileUnit TheCU(CU_Nodes->getOperand(i));
835 DIArray Subprograms = TheCU.getSubprograms();
836 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopherbdab8002012-11-27 00:13:51 +0000837 DISubprogram SP(Subprograms.getElement(i));
838 if (ProcessedSPNodes.count(SP) != 0) continue;
839 if (!SP.Verify()) continue;
840 if (!SP.isDefinition()) continue;
841 DIArray Variables = SP.getVariables();
842 if (Variables.getNumElements() == 0) continue;
Eric Christopher4117bec2012-11-22 00:59:49 +0000843
Eric Christopherbdab8002012-11-27 00:13:51 +0000844 LexicalScope *Scope =
845 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
846 DeadFnScopeMap[SP] = Scope;
Eric Christopher4117bec2012-11-22 00:59:49 +0000847
Eric Christopherbdab8002012-11-27 00:13:51 +0000848 // Construct subprogram DIE and add variables DIEs.
849 CompileUnit *SPCU = CUMap.lookup(TheCU);
850 assert(SPCU && "Unable to find Compile Unit!");
851 constructSubprogramDIE(SPCU, SP);
852 DIE *ScopeDIE = SPCU->getDIE(SP);
853 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
854 DIVariable DV(Variables.getElement(vi));
855 if (!DV.Verify()) continue;
856 DbgVariable *NewVar = new DbgVariable(DV, NULL);
857 if (DIE *VariableDIE =
858 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
859 ScopeDIE->addChild(VariableDIE);
860 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000861 }
862 }
863 }
864 DeleteContainerSeconds(DeadFnScopeMap);
865}
866
867void DwarfDebug::finalizeModuleInfo() {
868 // Collect info for variables that were optimized out.
869 collectDeadVariables();
870
871 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
872 computeInlinedDIEs();
Devang Patel53bb5c92009-11-10 23:06:00 +0000873
Eric Christopher6635cad2012-08-01 18:19:01 +0000874 // Emit DW_AT_containing_type attribute to connect types with their
875 // vtable holding type.
Devang Pateldbc64af2011-08-15 17:24:54 +0000876 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000877 CUE = CUMap.end(); CUI != CUE; ++CUI) {
Devang Pateldbc64af2011-08-15 17:24:54 +0000878 CompileUnit *TheCU = CUI->second;
879 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000880 }
881
Eric Christopher4117bec2012-11-22 00:59:49 +0000882 // Compute DIE offsets and sizes.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000883 InfoHolder.computeSizeAndOffsets();
884 if (useSplitDwarf())
885 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher4117bec2012-11-22 00:59:49 +0000886}
887
888void DwarfDebug::endSections() {
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000889 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000890 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000891 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000892 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000893 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000894
895 // End text sections.
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +0000896 for (unsigned I = 0, E = SectionMap.size(); I != E; ++I) {
897 Asm->OutStreamer.SwitchSection(SectionMap[I]);
898 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", I+1));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000899 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000900}
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000901
Eric Christopherb6dc8652012-11-27 22:43:45 +0000902// Emit all Dwarf sections that should come after the content.
Eric Christopher4117bec2012-11-22 00:59:49 +0000903void DwarfDebug::endModule() {
904
905 if (!FirstCU) return;
906
907 // End any existing sections.
908 // TODO: Does this need to happen?
909 endSections();
910
911 // Finalize the debug info for the module.
912 finalizeModuleInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000913
Eric Christopher97c34722012-11-19 19:43:59 +0000914 // Emit initial sections.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000915 emitSectionLabels();
Eric Christopher97c34722012-11-19 19:43:59 +0000916
Eric Christopher4daaed12012-12-10 19:51:21 +0000917 if (!useSplitDwarf()) {
Eric Christopher42885022012-11-27 22:43:42 +0000918 // Emit all the DIEs into a debug info section.
919 emitDebugInfo();
Eric Christopher74802f72012-11-27 00:41:54 +0000920
Eric Christopher42885022012-11-27 22:43:42 +0000921 // Corresponding abbreviations into a abbrev section.
922 emitAbbreviations();
923
924 // Emit info into a debug loc section.
925 emitDebugLoc();
926
927 // Emit info into a debug aranges section.
928 emitDebugARanges();
929
930 // Emit info into a debug ranges section.
931 emitDebugRanges();
932
933 // Emit info into a debug macinfo section.
934 emitDebugMacInfo();
935
936 // Emit inline info.
937 // TODO: When we don't need the option anymore we
938 // can remove all of the code that this section
939 // depends upon.
940 if (useDarwinGDBCompat())
941 emitDebugInlineInfo();
942 } else {
Eric Christopher0b944ee2012-12-11 19:42:09 +0000943 // TODO: Fill this in for separated debug sections and separate
Eric Christopher42885022012-11-27 22:43:42 +0000944 // out information into new sections.
945
Eric Christopher98e237f2012-11-30 23:59:06 +0000946 // Emit the debug info section and compile units.
Eric Christopher42885022012-11-27 22:43:42 +0000947 emitDebugInfo();
Eric Christopher98e237f2012-11-30 23:59:06 +0000948 emitDebugInfoDWO();
Eric Christopher42885022012-11-27 22:43:42 +0000949
950 // Corresponding abbreviations into a abbrev section.
951 emitAbbreviations();
Eric Christopher6eebe472012-12-19 22:02:53 +0000952 emitDebugAbbrevDWO();
Eric Christopher42885022012-11-27 22:43:42 +0000953
954 // Emit info into a debug loc section.
955 emitDebugLoc();
956
957 // Emit info into a debug aranges section.
958 emitDebugARanges();
959
960 // Emit info into a debug ranges section.
961 emitDebugRanges();
962
963 // Emit info into a debug macinfo section.
964 emitDebugMacInfo();
965
966 // Emit inline info.
967 // TODO: When we don't need the option anymore we
968 // can remove all of the code that this section
969 // depends upon.
970 if (useDarwinGDBCompat())
971 emitDebugInlineInfo();
972 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000973
Eric Christopher9d9f5a52012-08-23 07:32:06 +0000974 // Emit info into the dwarf accelerator table sections.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000975 if (useDwarfAccelTables()) {
Eric Christopher09ac3d82011-11-07 09:24:32 +0000976 emitAccelNames();
977 emitAccelObjC();
978 emitAccelNamespaces();
979 emitAccelTypes();
980 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000981
Devang Patel193f7202009-11-24 01:14:22 +0000982 // Emit info into a debug pubtypes section.
Eric Christopher360f0062012-08-23 07:10:56 +0000983 // TODO: When we don't need the option anymore we can
984 // remove all of the code that adds to the table.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000985 if (useDarwinGDBCompat())
Eric Christopher360f0062012-08-23 07:10:56 +0000986 emitDebugPubTypes();
Devang Patel193f7202009-11-24 01:14:22 +0000987
Eric Christopher42885022012-11-27 22:43:42 +0000988 // Finally emit string information into a string table.
Eric Christopherfcdbecb2012-11-27 06:49:23 +0000989 emitDebugStr();
990
Devang Patele9a1cca2010-08-02 17:32:15 +0000991 // clean up.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000992 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000993 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
994 E = CUMap.end(); I != E; ++I)
995 delete I->second;
Eric Christopher9ec87b32012-12-10 19:51:18 +0000996
Eric Christopher4daaed12012-12-10 19:51:21 +0000997 delete SkeletonCU;
Eric Christopher9ec87b32012-12-10 19:51:18 +0000998
Eric Christopher98e237f2012-11-30 23:59:06 +0000999 // Reset these for the next Module if we have one.
1000 FirstCU = NULL;
Eric Christopher4daaed12012-12-10 19:51:21 +00001001 SkeletonCU = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001002}
1003
Eric Christopherb6dc8652012-11-27 22:43:45 +00001004// Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +00001005DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +00001006 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +00001007 LLVMContext &Ctx = DV->getContext();
1008 // More then one inlined variable corresponds to one abstract variable.
1009 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +00001010 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001011 if (AbsDbgVariable)
1012 return AbsDbgVariable;
1013
Devang Patelbf47fdb2011-08-10 20:55:27 +00001014 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00001015 if (!Scope)
1016 return NULL;
1017
Devang Patel5a1a67c2011-08-15 19:01:20 +00001018 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001019 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00001020 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00001021 return AbsDbgVariable;
1022}
1023
Eric Christopherb6dc8652012-11-27 22:43:45 +00001024// If Var is a current function argument then add it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +00001025bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +00001026 DbgVariable *Var, LexicalScope *Scope) {
1027 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +00001028 return false;
1029 DIVariable DV = Var->getVariable();
1030 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1031 return false;
1032 unsigned ArgNo = DV.getArgNumber();
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001033 if (ArgNo == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001034 return false;
1035
Devang Patelcb3a6572011-03-03 20:02:02 +00001036 size_t Size = CurrentFnArguments.size();
1037 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001038 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +00001039 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +00001040 // arguments does the function have at source level.
1041 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +00001042 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +00001043 CurrentFnArguments[ArgNo - 1] = Var;
1044 return true;
1045}
1046
Eric Christopherb6dc8652012-11-27 22:43:45 +00001047// Collect variable information from side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001048void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001049DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +00001050 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +00001051 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1052 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1053 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00001054 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00001055 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00001056 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00001057 DIVariable DV(Var);
1058 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001059
Devang Patelbf47fdb2011-08-10 20:55:27 +00001060 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001061
Devang Patelfb0ee432009-11-10 23:20:04 +00001062 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00001063 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00001064 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001065
Devang Patel26c1e562010-05-20 16:36:41 +00001066 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001067 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +00001068 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +00001069 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001070 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001071 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001072 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +00001073 }
Devang Patelee432862010-05-20 19:57:06 +00001074}
Devang Patel90a48ad2010-03-15 18:33:46 +00001075
Eric Christopherb6dc8652012-11-27 22:43:45 +00001076// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1077// defined reg.
Devang Patelc3f5f782010-05-25 23:40:22 +00001078static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001079 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001080 return MI->getNumOperands() == 3 &&
1081 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
1082 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +00001083}
1084
Eric Christopherb6dc8652012-11-27 22:43:45 +00001085// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001086static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1087 const MCSymbol *FLabel,
Devang Patel90b40412011-07-08 17:09:57 +00001088 const MCSymbol *SLabel,
1089 const MachineInstr *MI) {
1090 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1091
1092 if (MI->getNumOperands() != 3) {
1093 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1094 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1095 }
1096 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1097 MachineLocation MLoc;
1098 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1099 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1100 }
1101 if (MI->getOperand(0).isImm())
1102 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1103 if (MI->getOperand(0).isFPImm())
1104 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1105 if (MI->getOperand(0).isCImm())
1106 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1107
Craig Topper5e25ee82012-02-05 08:31:47 +00001108 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +00001109}
1110
Eric Christopherb6dc8652012-11-27 22:43:45 +00001111// Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001112void
Devang Patel78e127d2010-06-25 22:07:34 +00001113DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1114 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001115
Eric Christopherb6dc8652012-11-27 22:43:45 +00001116 // collection info from MMI table.
Devang Patelee432862010-05-20 19:57:06 +00001117 collectVariableInfoFromMMITable(MF, Processed);
1118
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001119 for (SmallVectorImpl<const MDNode*>::const_iterator
1120 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1121 ++UVI) {
1122 const MDNode *Var = *UVI;
1123 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001124 continue;
1125
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001126 // History contains relevant DBG_VALUE instructions for Var and instructions
1127 // clobbering it.
1128 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1129 if (History.empty())
1130 continue;
1131 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001132
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001133 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001134 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001135 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1136 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001137 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001138 else {
1139 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001140 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001141 else {
1142 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001143 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001144 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001145 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001146 }
1147 }
Devang Patelee432862010-05-20 19:57:06 +00001148 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001149 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001150 continue;
1151
1152 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001153 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001154 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1155 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001156 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001157 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001158 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001159 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001160
Eric Christopherc56e3f02012-10-08 20:48:54 +00001161 // Simplify ranges that are fully coalesced.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001162 if (History.size() <= 1 || (History.size() == 2 &&
1163 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001164 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001165 continue;
1166 }
1167
1168 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001169 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001170
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001171 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1172 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1173 const MachineInstr *Begin = *HI;
1174 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001175
Devang Patel4ada1d72011-06-01 23:00:17 +00001176 // Check if DBG_VALUE is truncating a range.
1177 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1178 && !Begin->getOperand(0).getReg())
1179 continue;
1180
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001181 // Compute the range for a register location.
1182 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1183 const MCSymbol *SLabel = 0;
1184
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001185 if (HI + 1 == HE)
1186 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001187 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001188 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001189 else {
1190 const MachineInstr *End = HI[1];
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001191 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Devang Patel476df5f2011-07-07 21:44:42 +00001192 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001193 if (End->isDebugValue())
1194 SLabel = getLabelBeforeInsn(End);
1195 else {
1196 // End is a normal instruction clobbering the range.
1197 SLabel = getLabelAfterInsn(End);
1198 assert(SLabel && "Forgot label after clobber instruction");
1199 ++HI;
1200 }
1201 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001202
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001203 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001204 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1205 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001206 }
1207 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001208 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001209
1210 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001211 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1212 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1213 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1214 DIVariable DV(Variables.getElement(i));
1215 if (!DV || !DV.Verify() || !Processed.insert(DV))
1216 continue;
1217 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1218 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001219 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001220}
Devang Patel98e1cac2010-05-14 21:01:35 +00001221
Eric Christopherb6dc8652012-11-27 22:43:45 +00001222// Return Label preceding the instruction.
Devang Patelc3f5f782010-05-25 23:40:22 +00001223const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001224 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1225 assert(Label && "Didn't insert label before instruction");
1226 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001227}
1228
Eric Christopherb6dc8652012-11-27 22:43:45 +00001229// Return Label immediately following the instruction.
Devang Patelc3f5f782010-05-25 23:40:22 +00001230const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001231 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001232}
1233
Eric Christopherb6dc8652012-11-27 22:43:45 +00001234// Process beginning of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +00001235void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001236 // Check if source location changes, but ignore DBG_VALUE locations.
1237 if (!MI->isDebugValue()) {
1238 DebugLoc DL = MI->getDebugLoc();
1239 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001240 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001241 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001242 if (DL == PrologEndLoc) {
1243 Flags |= DWARF2_FLAG_PROLOGUE_END;
1244 PrologEndLoc = DebugLoc();
1245 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001246 if (PrologEndLoc.isUnknown())
1247 Flags |= DWARF2_FLAG_IS_STMT;
1248
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001249 if (!DL.isUnknown()) {
1250 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001251 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001252 } else
Devang Patel4243e672011-05-11 19:22:19 +00001253 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001254 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001255 }
Devang Patelaead63c2010-03-29 22:59:58 +00001256
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001257 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001258 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1259 LabelsBeforeInsn.find(MI);
1260
1261 // No label needed.
1262 if (I == LabelsBeforeInsn.end())
1263 return;
1264
1265 // Label already assigned.
1266 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001267 return;
Devang Patel553881b2010-03-29 17:20:31 +00001268
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001269 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001270 PrevLabel = MMI->getContext().CreateTempSymbol();
1271 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001272 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001273 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001274}
1275
Eric Christopherb6dc8652012-11-27 22:43:45 +00001276// Process end of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +00001277void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001278 // Don't create a new label after DBG_VALUE instructions.
1279 // They don't generate code.
1280 if (!MI->isDebugValue())
1281 PrevLabel = 0;
1282
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001283 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1284 LabelsAfterInsn.find(MI);
1285
1286 // No label needed.
1287 if (I == LabelsAfterInsn.end())
1288 return;
1289
1290 // Label already assigned.
1291 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001292 return;
1293
1294 // We need a label after this instruction.
1295 if (!PrevLabel) {
1296 PrevLabel = MMI->getContext().CreateTempSymbol();
1297 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001298 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001299 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001300}
1301
Eric Christopherb6dc8652012-11-27 22:43:45 +00001302// Each LexicalScope has first instruction and last instruction to mark
1303// beginning and end of a scope respectively. Create an inverse map that list
1304// scopes starts (and ends) with an instruction. One instruction may start (or
1305// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001306void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001307 SmallVector<LexicalScope *, 4> WorkList;
1308 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001309 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001310 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001311
Devang Patelbf47fdb2011-08-10 20:55:27 +00001312 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001313 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001314 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001315 SE = Children.end(); SI != SE; ++SI)
1316 WorkList.push_back(*SI);
1317
Devang Patel53bb5c92009-11-10 23:06:00 +00001318 if (S->isAbstractScope())
1319 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001320
Devang Patelbf47fdb2011-08-10 20:55:27 +00001321 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001322 if (Ranges.empty())
1323 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001324 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001325 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001326 assert(RI->first && "InsnRange does not have first instruction!");
1327 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001328 requestLabelBeforeInsn(RI->first);
1329 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001330 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001331 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001332}
1333
Eric Christopherb6dc8652012-11-27 22:43:45 +00001334// Get MDNode for DebugLoc's scope.
Devang Patela3f48672011-05-09 22:14:49 +00001335static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1336 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1337 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1338 return DL.getScope(Ctx);
1339}
1340
Eric Christopherb6dc8652012-11-27 22:43:45 +00001341// Walk up the scope chain of given debug loc and find line number info
1342// for the function.
Devang Patel4243e672011-05-11 19:22:19 +00001343static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1344 const MDNode *Scope = getScopeNode(DL, Ctx);
1345 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher6126a1e2012-04-03 00:43:49 +00001346 if (SP.Verify()) {
1347 // Check for number of operands since the compatibility is
1348 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001349 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-04-03 00:43:49 +00001350 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1351 else
1352 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1353 }
1354
Devang Patel4243e672011-05-11 19:22:19 +00001355 return DebugLoc();
1356}
1357
Eric Christopherb6dc8652012-11-27 22:43:45 +00001358// Gather pre-function debug information. Assumes being called immediately
1359// after the function entry point has been emitted.
Chris Lattnereec791a2010-01-26 23:18:02 +00001360void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001361 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001362 LScopes.initialize(*MF);
1363 if (LScopes.empty()) return;
1364 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001365
Devang Pateleac9c072010-04-27 19:46:33 +00001366 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1367 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001368 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001369 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001370
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001371 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1372
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001373 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopherb6dc8652012-11-27 22:43:45 +00001374 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001375 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1376
Devang Patelb2b31a62010-05-26 19:37:24 +00001377 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001378 I != E; ++I) {
1379 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001380 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1381 II != IE; ++II) {
1382 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001383
Devang Patelb2b31a62010-05-26 19:37:24 +00001384 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001385 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001386
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001387 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001388 const MDNode *Var =
1389 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001390
1391 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001392 if (isDbgValueInDefinedReg(MI))
1393 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1394
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001395 // Check the history of this variable.
1396 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1397 if (History.empty()) {
1398 UserVariables.push_back(Var);
1399 // The first mention of a function argument gets the FunctionBeginSym
1400 // label, so arguments are visible when breaking at function entry.
1401 DIVariable DV(Var);
1402 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1403 DISubprogram(getDISubprogram(DV.getContext()))
1404 .describes(MF->getFunction()))
1405 LabelsBeforeInsn[MI] = FunctionBeginSym;
1406 } else {
1407 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1408 const MachineInstr *Prev = History.back();
1409 if (Prev->isDebugValue()) {
1410 // Coalesce identical entries at the end of History.
1411 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001412 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher02c1a642012-10-08 20:48:49 +00001413 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001414 << "\t" << *Prev
Devang Patel79862892011-07-07 00:14:27 +00001415 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001416 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001417 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001418
1419 // Terminate old register assignments that don't reach MI;
1420 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1421 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1422 isDbgValueInDefinedReg(Prev)) {
1423 // Previous register assignment needs to terminate at the end of
1424 // its basic block.
1425 MachineBasicBlock::const_iterator LastMI =
1426 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001427 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001428 // Drop DBG_VALUE for empty range.
Eric Christopher02c1a642012-10-08 20:48:49 +00001429 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Devang Patel79862892011-07-07 00:14:27 +00001430 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001431 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001432 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001433 else {
1434 // Terminate after LastMI.
1435 History.push_back(LastMI);
1436 }
1437 }
1438 }
1439 }
1440 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001441 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001442 // Not a DBG_VALUE instruction.
1443 if (!MI->isLabel())
1444 AtBlockEntry = false;
1445
Eric Christopher0313ced2012-10-04 20:46:14 +00001446 // First known non-DBG_VALUE and non-frame setup location marks
1447 // the beginning of the function body.
1448 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1449 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel4243e672011-05-11 19:22:19 +00001450 PrologEndLoc = MI->getDebugLoc();
1451
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001452 // Check if the instruction clobbers any registers with debug vars.
1453 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1454 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1455 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1456 continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001457 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1458 AI.isValid(); ++AI) {
1459 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001460 const MDNode *Var = LiveUserVar[Reg];
1461 if (!Var)
1462 continue;
1463 // Reg is now clobbered.
1464 LiveUserVar[Reg] = 0;
1465
1466 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001467 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1468 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001469 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001470 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1471 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001472 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001473 const MachineInstr *Prev = History.back();
1474 // Sanity-check: Register assignments are terminated at the end of
1475 // their block.
1476 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1477 continue;
1478 // Is the variable still in Reg?
1479 if (!isDbgValueInDefinedReg(Prev) ||
1480 Prev->getOperand(0).getReg() != Reg)
1481 continue;
1482 // Var is clobbered. Make sure the next instruction gets a label.
1483 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001484 }
1485 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001486 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001487 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001488 }
1489
1490 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1491 I != E; ++I) {
1492 SmallVectorImpl<const MachineInstr*> &History = I->second;
1493 if (History.empty())
1494 continue;
1495
1496 // Make sure the final register assignments are terminated.
1497 const MachineInstr *Prev = History.back();
1498 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1499 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001500 MachineBasicBlock::const_iterator LastMI =
Devang Patel5bc942c2011-08-10 23:58:09 +00001501 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001502 if (LastMI == PrevMBB->end())
1503 // Drop DBG_VALUE for empty range.
1504 History.pop_back();
1505 else {
1506 // Terminate after LastMI.
1507 History.push_back(LastMI);
1508 }
1509 }
1510 // Request labels for the full history.
1511 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1512 const MachineInstr *MI = History[i];
1513 if (MI->isDebugValue())
1514 requestLabelBeforeInsn(MI);
1515 else
1516 requestLabelAfterInsn(MI);
1517 }
1518 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001519
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001520 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001521 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001522
1523 // Record beginning of function.
1524 if (!PrologEndLoc.isUnknown()) {
1525 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1526 MF->getFunction()->getContext());
1527 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1528 FnStartDL.getScope(MF->getFunction()->getContext()),
David Blaikie836cfc42012-12-04 22:02:33 +00001529 // We'd like to list the prologue as "not statements" but GDB behaves
1530 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
David Blaikieb36c5312012-12-04 21:05:36 +00001531 DWARF2_FLAG_IS_STMT);
Devang Patel4243e672011-05-11 19:22:19 +00001532 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001533}
1534
Devang Patelbf47fdb2011-08-10 20:55:27 +00001535void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1536// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1537 ScopeVariables[LS].push_back(Var);
1538// Vars.push_back(Var);
1539}
1540
Eric Christopherb6dc8652012-11-27 22:43:45 +00001541// Gather and emit post-function debug information.
Chris Lattnereec791a2010-01-26 23:18:02 +00001542void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001543 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001544
Devang Patelbf47fdb2011-08-10 20:55:27 +00001545 // Define end label for subprogram.
1546 FunctionEndSym = Asm->GetTempSymbol("func_end",
1547 Asm->getFunctionNumber());
1548 // Assumes in correct section after the entry point.
1549 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001550
Devang Patelbf47fdb2011-08-10 20:55:27 +00001551 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1552 collectVariableInfo(MF, ProcessedVars);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001553
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001554 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001555 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001556 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001557
Devang Patelbf47fdb2011-08-10 20:55:27 +00001558 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001559 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1560 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1561 LexicalScope *AScope = AList[i];
1562 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001563 if (SP.Verify()) {
1564 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001565 DIArray Variables = SP.getVariables();
1566 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1567 DIVariable DV(Variables.getElement(i));
1568 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1569 continue;
Alexey Samsonovb67bd332012-07-06 08:45:08 +00001570 // Check that DbgVariable for DV wasn't created earlier, when
1571 // findAbstractVariable() was called for inlined instance of DV.
1572 LLVMContext &Ctx = DV->getContext();
1573 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1574 if (AbstractVariables.lookup(CleanDV))
1575 continue;
Devang Patel93d39be2011-08-19 23:28:12 +00001576 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1577 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001578 }
1579 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001580 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001581 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001582 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001583
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001584 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001585
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001586 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001587 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001588
Devang Patelbf47fdb2011-08-10 20:55:27 +00001589 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1590 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001591
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001592 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001593 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1594 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1595 DeleteContainerPointers(I->second);
1596 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001597 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001598 UserVariables.clear();
1599 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001600 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001601 LabelsBeforeInsn.clear();
1602 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001603 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001604}
1605
Eric Christopherb6dc8652012-11-27 22:43:45 +00001606// Register a source line with debug info. Returns the unique label that was
1607// emitted and which provides correspondence to the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001608void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1609 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001610 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001611 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001612 unsigned Src = 1;
1613 if (S) {
1614 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001615
Dan Gohman1cc0d622010-05-05 23:41:32 +00001616 if (Scope.isCompileUnit()) {
1617 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001618 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001619 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001620 } else if (Scope.isFile()) {
1621 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001622 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001623 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001624 } else if (Scope.isSubprogram()) {
1625 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001626 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001627 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001628 } else if (Scope.isLexicalBlockFile()) {
1629 DILexicalBlockFile DBF(S);
1630 Fn = DBF.getFilename();
1631 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001632 } else if (Scope.isLexicalBlock()) {
1633 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001634 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001635 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001636 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001637 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001638
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001639 Src = getOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001640 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001641 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001642}
1643
Bill Wendling829e67b2009-05-20 23:22:40 +00001644//===----------------------------------------------------------------------===//
1645// Emit Methods
1646//===----------------------------------------------------------------------===//
1647
Eric Christopherb6dc8652012-11-27 22:43:45 +00001648// Compute the size and offset of a DIE.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001649unsigned
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001650DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001651 // Get the children.
1652 const std::vector<DIE *> &Children = Die->getChildren();
1653
Bill Wendling94d04b82009-05-20 23:21:38 +00001654 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001655 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001656
1657 // Get the abbreviation for this DIE.
1658 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001659 const DIEAbbrev *Abbrev = Abbreviations->at(AbbrevNumber - 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001660
1661 // Set DIE offset
1662 Die->setOffset(Offset);
1663
1664 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001665 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001666
1667 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1668 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1669
1670 // Size the DIE attribute values.
1671 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1672 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001673 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001674
1675 // Size the DIE children if any.
1676 if (!Children.empty()) {
1677 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1678 "Children flag not set");
1679
1680 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopherfbd19752012-11-20 22:14:13 +00001681 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling94d04b82009-05-20 23:21:38 +00001682
1683 // End of children marker.
1684 Offset += sizeof(int8_t);
1685 }
1686
1687 Die->setSize(Offset - Die->getOffset());
1688 return Offset;
1689}
1690
Eric Christopherb6dc8652012-11-27 22:43:45 +00001691// Compute the size and offset of all the DIEs.
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001692void DwarfUnits::computeSizeAndOffsets() {
1693 for (SmallVector<CompileUnit *, 1>::iterator I = CUs.begin(),
1694 E = CUs.end(); I != E; ++I) {
Eric Christopher98e237f2012-11-30 23:59:06 +00001695 unsigned Offset =
1696 sizeof(int32_t) + // Length of Compilation Unit Info
1697 sizeof(int16_t) + // DWARF version number
1698 sizeof(int32_t) + // Offset Into Abbrev. Section
1699 sizeof(int8_t); // Pointer Size (in bytes)
1700
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001701 computeSizeAndOffset((*I)->getCUDie(), Offset);
Devang Patel163a9f72010-05-10 22:49:55 +00001702 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001703}
1704
Eric Christopherb6dc8652012-11-27 22:43:45 +00001705// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001706void DwarfDebug::emitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001707 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001708
Bill Wendling94d04b82009-05-20 23:21:38 +00001709 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001710 DwarfInfoSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001711 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001712 DwarfAbbrevSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001713 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher6eebe472012-12-19 22:02:53 +00001714 if (useSplitDwarf())
1715 DwarfAbbrevDWOSectionSym =
1716 emitSectionSym(Asm, TLOF.getDwarfAbbrevDWOSection(),
1717 "section_abbrev_dwo");
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001718 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001719
Chris Lattner9c69e285532010-04-04 22:59:04 +00001720 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001721 emitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001722
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001723 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
1724 emitSectionSym(Asm, TLOF.getDwarfLocSection());
1725 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001726 DwarfStrSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001727 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
1728 DwarfDebugRangeSectionSym = emitSectionSym(Asm, TLOF.getDwarfRangesSection(),
Devang Patelf2548ca2010-04-16 23:33:45 +00001729 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001730
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001731 DwarfDebugLocSectionSym = emitSectionSym(Asm, TLOF.getDwarfLocSection(),
Devang Patelc3f5f782010-05-25 23:40:22 +00001732 "section_debug_loc");
1733
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001734 TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
1735 emitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001736}
1737
Eric Christopherb6dc8652012-11-27 22:43:45 +00001738// Recursively emits a debug information entry.
Eric Christopher6eebe472012-12-19 22:02:53 +00001739void DwarfDebug::emitDIE(DIE *Die, std::vector<DIEAbbrev *> *Abbrevs) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001740 // Get the abbreviation for this DIE.
1741 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher6eebe472012-12-19 22:02:53 +00001742 const DIEAbbrev *Abbrev = Abbrevs->at(AbbrevNumber - 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001743
Bill Wendling94d04b82009-05-20 23:21:38 +00001744 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001745 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001746 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1747 Twine::utohexstr(Die->getOffset()) + ":0x" +
1748 Twine::utohexstr(Die->getSize()) + " " +
1749 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001750 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001751
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001752 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001753 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1754
1755 // Emit the DIE attribute values.
1756 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1757 unsigned Attr = AbbrevData[i].getAttribute();
1758 unsigned Form = AbbrevData[i].getForm();
1759 assert(Form && "Too many attributes for DIE (check abbreviation)");
1760
Chris Lattner3f53c832010-04-04 18:52:31 +00001761 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001762 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001763
Bill Wendling94d04b82009-05-20 23:21:38 +00001764 switch (Attr) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001765 case dwarf::DW_AT_abstract_origin: {
1766 DIEEntry *E = cast<DIEEntry>(Values[i]);
1767 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001768 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001769 Asm->EmitInt32(Addr);
1770 break;
1771 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001772 case dwarf::DW_AT_ranges: {
1773 // DW_AT_range Value encodes offset in debug_range section.
1774 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001775
Nick Lewyckyffccd922012-06-22 01:25:12 +00001776 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001777 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1778 V->getValue(),
1779 4);
1780 } else {
1781 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1782 V->getValue(),
1783 DwarfDebugRangeSectionSym,
1784 4);
1785 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001786 break;
1787 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001788 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-06-22 01:25:12 +00001789 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1790 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1791 Asm->EmitLabelReference(L->getValue(), 4);
1792 else
1793 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1794 } else {
Devang Patelc3f5f782010-05-25 23:40:22 +00001795 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00001796 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001797 break;
1798 }
Devang Patel2a361602010-09-29 19:08:08 +00001799 case dwarf::DW_AT_accessibility: {
1800 if (Asm->isVerbose()) {
1801 DIEInteger *V = cast<DIEInteger>(Values[i]);
1802 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1803 }
1804 Values[i]->EmitValue(Asm, Form);
1805 break;
1806 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001807 default:
1808 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001809 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001810 break;
1811 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001812 }
1813
1814 // Emit the DIE children if any.
1815 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1816 const std::vector<DIE *> &Children = Die->getChildren();
1817
1818 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher6eebe472012-12-19 22:02:53 +00001819 emitDIE(Children[j], Abbrevs);
Bill Wendling94d04b82009-05-20 23:21:38 +00001820
Chris Lattner3f53c832010-04-04 18:52:31 +00001821 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001822 Asm->OutStreamer.AddComment("End Of Children Mark");
1823 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001824 }
1825}
1826
Eric Christopherb1e66d02012-12-15 00:04:07 +00001827// Emit the various dwarf units to the unit section USection with
1828// the abbreviations going into ASection.
1829void DwarfUnits::emitUnits(DwarfDebug *DD,
1830 const MCSection *USection,
1831 const MCSection *ASection,
1832 const MCSymbol *ASectionSym) {
1833 Asm->OutStreamer.SwitchSection(USection);
1834 for (SmallVector<CompileUnit *, 1>::iterator I = CUs.begin(),
1835 E = CUs.end(); I != E; ++I) {
1836 CompileUnit *TheCU = *I;
Devang Patel163a9f72010-05-10 22:49:55 +00001837 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001838
Devang Patel163a9f72010-05-10 22:49:55 +00001839 // Emit the compile units header.
Eric Christopherb1e66d02012-12-15 00:04:07 +00001840 Asm->OutStreamer
1841 .EmitLabel(Asm->GetTempSymbol(USection->getLabelBeginName(),
1842 TheCU->getUniqueID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001843
Devang Patel163a9f72010-05-10 22:49:55 +00001844 // Emit size of content not including length itself
1845 unsigned ContentSize = Die->getSize() +
1846 sizeof(int16_t) + // DWARF version number
1847 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001848 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001849
Devang Patel163a9f72010-05-10 22:49:55 +00001850 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1851 Asm->EmitInt32(ContentSize);
1852 Asm->OutStreamer.AddComment("DWARF version number");
1853 Asm->EmitInt16(dwarf::DWARF_VERSION);
1854 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christopherb1e66d02012-12-15 00:04:07 +00001855 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
1856 ASectionSym);
Devang Patel163a9f72010-05-10 22:49:55 +00001857 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001858 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001859
Eric Christopher6eebe472012-12-19 22:02:53 +00001860 DD->emitDIE(Die, Abbreviations);
Eric Christopherb1e66d02012-12-15 00:04:07 +00001861 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(USection->getLabelEndName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00001862 TheCU->getUniqueID()));
Devang Patel163a9f72010-05-10 22:49:55 +00001863 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001864}
1865
Eric Christopher98e237f2012-11-30 23:59:06 +00001866// Emit the debug info section.
1867void DwarfDebug::emitDebugInfo() {
Eric Christopherb1e66d02012-12-15 00:04:07 +00001868 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1869
1870 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
1871 Asm->getObjFileLowering().getDwarfAbbrevSection(),
1872 DwarfAbbrevSectionSym);
Eric Christopher98e237f2012-11-30 23:59:06 +00001873}
1874
Eric Christopherb6dc8652012-11-27 22:43:45 +00001875// Emit the abbreviation section.
Eric Christopher7dc68db2012-11-20 23:30:11 +00001876void DwarfDebug::emitAbbreviations() {
Eric Christopher6eebe472012-12-19 22:02:53 +00001877 if (!useSplitDwarf())
1878 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
1879 &Abbreviations);
1880 else
1881 emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
1882}
Bill Wendling94d04b82009-05-20 23:21:38 +00001883
Eric Christopher6eebe472012-12-19 22:02:53 +00001884void DwarfDebug::emitAbbrevs(const MCSection *Section,
1885 std::vector<DIEAbbrev *> *Abbrevs) {
1886 // Check to see if it is worth the effort.
1887 if (!Abbrevs->empty()) {
1888 // Start the debug abbrev section.
1889 Asm->OutStreamer.SwitchSection(Section);
1890
1891 MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Eric Christopher44fedba2012-12-13 03:00:38 +00001892 Asm->OutStreamer.EmitLabel(Begin);
Bill Wendling94d04b82009-05-20 23:21:38 +00001893
1894 // For each abbrevation.
Eric Christopher6eebe472012-12-19 22:02:53 +00001895 for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001896 // Get abbreviation data
Eric Christopher6eebe472012-12-19 22:02:53 +00001897 const DIEAbbrev *Abbrev = Abbrevs->at(i);
Bill Wendling94d04b82009-05-20 23:21:38 +00001898
1899 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001900 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001901
1902 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001903 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001904 }
1905
1906 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001907 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001908
Eric Christopher6eebe472012-12-19 22:02:53 +00001909 MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Eric Christopher44fedba2012-12-13 03:00:38 +00001910 Asm->OutStreamer.EmitLabel(End);
Bill Wendling94d04b82009-05-20 23:21:38 +00001911 }
1912}
1913
Eric Christopherb6dc8652012-11-27 22:43:45 +00001914// Emit the last address of the section and the end of the line matrix.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001915void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001916 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001917 Asm->OutStreamer.AddComment("Extended Op");
1918 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001919
Chris Lattner233f52b2010-03-09 23:52:58 +00001920 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001921 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001922 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1923 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1924
1925 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001926
Chris Lattnerc0215722010-04-04 19:25:43 +00001927 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001928 Asm->getDataLayout().getPointerSize(),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001929 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001930
1931 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001932 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1933 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001934 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001935 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001936}
1937
Eric Christopherb6dc8652012-11-27 22:43:45 +00001938// Emit visible names into a hashed accelerator table section.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001939void DwarfDebug::emitAccelNames() {
1940 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1941 dwarf::DW_FORM_data4));
1942 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1943 E = CUMap.end(); I != E; ++I) {
1944 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001945 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1946 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001947 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1948 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001949 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001950 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1951 DE = Entities.end(); DI != DE; ++DI)
1952 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001953 }
1954 }
1955
1956 AT.FinalizeTable(Asm, "Names");
1957 Asm->OutStreamer.SwitchSection(
1958 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1959 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1960 Asm->OutStreamer.EmitLabel(SectionBegin);
1961
1962 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00001963 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001964}
1965
Eric Christopher72c16552012-12-20 21:58:40 +00001966// Emit objective C classes and categories into a hashed accelerator table
1967// section.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001968void DwarfDebug::emitAccelObjC() {
1969 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1970 dwarf::DW_FORM_data4));
1971 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1972 E = CUMap.end(); I != E; ++I) {
1973 CompileUnit *TheCU = I->second;
1974 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1975 for (StringMap<std::vector<DIE*> >::const_iterator
1976 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1977 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001978 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-11-07 09:24:32 +00001979 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1980 DE = Entities.end(); DI != DE; ++DI)
1981 AT.AddName(Name, (*DI));
1982 }
1983 }
1984
1985 AT.FinalizeTable(Asm, "ObjC");
1986 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1987 .getDwarfAccelObjCSection());
1988 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1989 Asm->OutStreamer.EmitLabel(SectionBegin);
1990
1991 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00001992 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001993}
1994
Eric Christopherb6dc8652012-11-27 22:43:45 +00001995// Emit namespace dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001996void DwarfDebug::emitAccelNamespaces() {
1997 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1998 dwarf::DW_FORM_data4));
1999 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2000 E = CUMap.end(); I != E; ++I) {
2001 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00002002 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
2003 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00002004 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2005 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00002006 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00002007 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2008 DE = Entities.end(); DI != DE; ++DI)
2009 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00002010 }
2011 }
2012
2013 AT.FinalizeTable(Asm, "namespac");
2014 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2015 .getDwarfAccelNamespaceSection());
2016 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2017 Asm->OutStreamer.EmitLabel(SectionBegin);
2018
2019 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002020 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002021}
2022
Eric Christopherb6dc8652012-11-27 22:43:45 +00002023// Emit type dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00002024void DwarfDebug::emitAccelTypes() {
Eric Christopherc36145f2012-01-06 04:35:23 +00002025 std::vector<DwarfAccelTable::Atom> Atoms;
2026 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2027 dwarf::DW_FORM_data4));
2028 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
2029 dwarf::DW_FORM_data2));
2030 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
2031 dwarf::DW_FORM_data1));
2032 DwarfAccelTable AT(Atoms);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002033 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2034 E = CUMap.end(); I != E; ++I) {
2035 CompileUnit *TheCU = I->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00002036 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
2037 = TheCU->getAccelTypes();
2038 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00002039 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2040 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00002041 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00002042 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
2043 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
2044 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002045 }
2046 }
2047
2048 AT.FinalizeTable(Asm, "types");
2049 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2050 .getDwarfAccelTypesSection());
2051 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2052 Asm->OutStreamer.EmitLabel(SectionBegin);
2053
2054 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002055 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002056}
2057
Devang Patel193f7202009-11-24 01:14:22 +00002058void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00002059 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2060 E = CUMap.end(); I != E; ++I) {
2061 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00002062 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-05-10 22:49:55 +00002063 Asm->OutStreamer.SwitchSection(
2064 Asm->getObjFileLowering().getDwarfPubTypesSection());
2065 Asm->OutStreamer.AddComment("Length of Public Types Info");
2066 Asm->EmitLabelDifference(
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002067 Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()),
2068 Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002069
Devang Patel163a9f72010-05-10 22:49:55 +00002070 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002071 TheCU->getUniqueID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002072
Devang Patel163a9f72010-05-10 22:49:55 +00002073 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2074 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002075
Devang Patel163a9f72010-05-10 22:49:55 +00002076 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002077 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2078 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002079 TheCU->getUniqueID()),
Devang Patel163a9f72010-05-10 22:49:55 +00002080 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002081
Devang Patel163a9f72010-05-10 22:49:55 +00002082 Asm->OutStreamer.AddComment("Compilation Unit Length");
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002083 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002084 TheCU->getUniqueID()),
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002085 Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002086 TheCU->getUniqueID()),
Devang Patel163a9f72010-05-10 22:49:55 +00002087 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002088
Devang Patel163a9f72010-05-10 22:49:55 +00002089 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2090 for (StringMap<DIE*>::const_iterator
2091 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2092 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00002093 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002094
Devang Patel163a9f72010-05-10 22:49:55 +00002095 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2096 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002097
Devang Patel163a9f72010-05-10 22:49:55 +00002098 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00002099 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00002100 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
2101 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002102
Devang Patel163a9f72010-05-10 22:49:55 +00002103 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002104 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00002105 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002106 TheCU->getUniqueID()));
Devang Patel193f7202009-11-24 01:14:22 +00002107 }
Devang Patel193f7202009-11-24 01:14:22 +00002108}
2109
Eric Christopherb6dc8652012-11-27 22:43:45 +00002110// Emit visible names into a debug str section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002111void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002112 // Check to see if it is worth the effort.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002113 if (InfoHolder.getStringPool()->empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002114
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002115 // Start the dwarf str section.
2116 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002117 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002118
Chris Lattnerbc733f52010-03-13 02:17:42 +00002119 // Get all of the string pool entries and put them in an array by their ID so
2120 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002121 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00002122 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002123
Chris Lattnerbc733f52010-03-13 02:17:42 +00002124 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
Eric Christopher72c16552012-12-20 21:58:40 +00002125 I = InfoHolder.getStringPool()->begin(),
2126 E = InfoHolder.getStringPool()->end();
2127 I != E; ++I)
Chris Lattnerbc733f52010-03-13 02:17:42 +00002128 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002129
Chris Lattnerbc733f52010-03-13 02:17:42 +00002130 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002131
Chris Lattnerbc733f52010-03-13 02:17:42 +00002132 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002133 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002134 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002135
Benjamin Kramer983c4572011-11-09 18:16:11 +00002136 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00002137 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
2138 Entries[i].second->getKeyLength()+1),
2139 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002140 }
2141}
2142
Eric Christopherb6dc8652012-11-27 22:43:45 +00002143// Emit visible names into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002144void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002145 if (DotDebugLocEntries.empty())
2146 return;
2147
Devang Patel6c3ea902011-02-04 22:57:18 +00002148 for (SmallVector<DotDebugLocEntry, 4>::iterator
2149 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2150 I != E; ++I) {
2151 DotDebugLocEntry &Entry = *I;
2152 if (I + 1 != DotDebugLocEntries.end())
2153 Entry.Merge(I+1);
2154 }
2155
Daniel Dunbar83320a02011-03-16 22:16:39 +00002156 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002157 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002158 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002159 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002160 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2161 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002162 for (SmallVector<DotDebugLocEntry, 4>::iterator
2163 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002164 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002165 DotDebugLocEntry &Entry = *I;
2166 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002167 if (Entry.isEmpty()) {
2168 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2169 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002170 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002171 } else {
2172 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2173 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002174 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002175 Asm->OutStreamer.AddComment("Loc expr size");
2176 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2177 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2178 Asm->EmitLabelDifference(end, begin, 2);
2179 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002180 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002181 DIBasicType BTy(DV.getType());
2182 if (BTy.Verify() &&
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002183 (BTy.getEncoding() == dwarf::DW_ATE_signed
Devang Patelc4329072011-06-01 22:03:25 +00002184 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2185 Asm->OutStreamer.AddComment("DW_OP_consts");
2186 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002187 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002188 } else {
2189 Asm->OutStreamer.AddComment("DW_OP_constu");
2190 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002191 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002192 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002193 } else if (Entry.isLocation()) {
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002194 if (!DV.hasComplexAddress())
Devang Patel80efd4e2011-07-08 16:49:43 +00002195 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002196 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002197 else {
2198 // Complex address entry.
2199 unsigned N = DV.getNumAddrElements();
2200 unsigned i = 0;
2201 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2202 if (Entry.Loc.getOffset()) {
2203 i = 2;
2204 Asm->EmitDwarfRegOp(Entry.Loc);
2205 Asm->OutStreamer.AddComment("DW_OP_deref");
2206 Asm->EmitInt8(dwarf::DW_OP_deref);
2207 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2208 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2209 Asm->EmitSLEB128(DV.getAddrElement(1));
2210 } else {
2211 // If first address element is OpPlus then emit
2212 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2213 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2214 Asm->EmitDwarfRegOp(Loc);
2215 i = 2;
2216 }
2217 } else {
2218 Asm->EmitDwarfRegOp(Entry.Loc);
2219 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002220
Devang Patel80efd4e2011-07-08 16:49:43 +00002221 // Emit remaining complex address elements.
2222 for (; i < N; ++i) {
2223 uint64_t Element = DV.getAddrElement(i);
2224 if (Element == DIBuilder::OpPlus) {
2225 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2226 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher50120762012-05-08 18:56:00 +00002227 } else if (Element == DIBuilder::OpDeref) {
Eric Christophera80f2d12012-05-08 21:24:39 +00002228 if (!Entry.Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002229 Asm->EmitInt8(dwarf::DW_OP_deref);
2230 } else
2231 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002232 }
Devang Patelc26f5442011-04-28 02:22:40 +00002233 }
Devang Patelc26f5442011-04-28 02:22:40 +00002234 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002235 // else ... ignore constant fp. There is not any good way to
2236 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002237 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002238 }
2239 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002240}
2241
Eric Christopherb6dc8652012-11-27 22:43:45 +00002242// Emit visible names into a debug aranges section.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00002243void DwarfDebug::emitDebugARanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002244 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002245 Asm->OutStreamer.SwitchSection(
2246 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002247}
2248
Eric Christopherb6dc8652012-11-27 22:43:45 +00002249// Emit visible names into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002250void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002251 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002252 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002253 Asm->getObjFileLowering().getDwarfRangesSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002254 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Pateleac9c072010-04-27 19:46:33 +00002255 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002256 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002257 I != E; ++I) {
2258 if (*I)
2259 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002260 else
Devang Pateleac9c072010-04-27 19:46:33 +00002261 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002262 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002263}
2264
Eric Christopherb6dc8652012-11-27 22:43:45 +00002265// Emit visible names into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002266void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002267 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002268 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002269 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002270 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002271 }
2272}
2273
Eric Christopherb6dc8652012-11-27 22:43:45 +00002274// Emit inline info using following format.
2275// Section Header:
2276// 1. length of section
2277// 2. Dwarf version number
2278// 3. address size.
2279//
2280// Entries (one "entry" for each function that was inlined):
2281//
2282// 1. offset into __debug_str section for MIPS linkage name, if exists;
2283// otherwise offset into __debug_str for regular function name.
2284// 2. offset into __debug_str section for regular function name.
2285// 3. an unsigned LEB128 number indicating the number of distinct inlining
2286// instances for the function.
2287//
2288// The rest of the entry consists of a {die_offset, low_pc} pair for each
2289// inlined instance; the die_offset points to the inlined_subroutine die in the
2290// __debug_info section, and the low_pc is the starting address for the
2291// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002292void DwarfDebug::emitDebugInlineInfo() {
Eric Christopherb83e2bb2012-03-02 02:11:47 +00002293 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002294 return;
2295
Devang Patel163a9f72010-05-10 22:49:55 +00002296 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002297 return;
2298
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002299 Asm->OutStreamer.SwitchSection(
2300 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002301
Chris Lattner233f52b2010-03-09 23:52:58 +00002302 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002303 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2304 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002305
Chris Lattnerc0215722010-04-04 19:25:43 +00002306 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002307
Chris Lattner233f52b2010-03-09 23:52:58 +00002308 Asm->OutStreamer.AddComment("Dwarf Version");
2309 Asm->EmitInt16(dwarf::DWARF_VERSION);
2310 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002311 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002312
Devang Patele9f8f5e2010-05-07 20:54:48 +00002313 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002314 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002315
Devang Patele9f8f5e2010-05-07 20:54:48 +00002316 const MDNode *Node = *I;
2317 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002318 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002319 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002320 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002321 StringRef LName = SP.getLinkageName();
2322 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002323
Chris Lattner233f52b2010-03-09 23:52:58 +00002324 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher50e26612012-03-02 01:57:52 +00002325 if (LName.empty())
Eric Christopher2e5d8702012-12-20 21:58:36 +00002326 Asm->EmitSectionOffset(InfoHolder.getStringPoolEntry(Name),
2327 DwarfStrSectionSym);
Eric Christopher50e26612012-03-02 01:57:52 +00002328 else
Eric Christopher2e5d8702012-12-20 21:58:36 +00002329 Asm->EmitSectionOffset(InfoHolder
2330 .getStringPoolEntry(getRealLinkageName(LName)),
Chris Lattner6189ed12010-04-04 23:25:33 +00002331 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002332
Chris Lattner233f52b2010-03-09 23:52:58 +00002333 Asm->OutStreamer.AddComment("Function name");
Eric Christopher2e5d8702012-12-20 21:58:36 +00002334 Asm->EmitSectionOffset(InfoHolder.getStringPoolEntry(Name),
2335 DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002336 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002337
Devang Patel53bb5c92009-11-10 23:06:00 +00002338 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002339 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002340 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002341 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002342
Chris Lattner3f53c832010-04-04 18:52:31 +00002343 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002344 Asm->OutStreamer.EmitSymbolValue(LI->first,
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002345 Asm->getDataLayout().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002346 }
2347 }
2348
Chris Lattnerc0215722010-04-04 19:25:43 +00002349 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002350}
Eric Christopher98e237f2012-11-30 23:59:06 +00002351
Eric Christopher0b944ee2012-12-11 19:42:09 +00002352// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher98e237f2012-11-30 23:59:06 +00002353
2354// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2355// DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
2356// DW_AT_ranges_base, DW_AT_addr_base. If DW_AT_ranges is present,
2357// DW_AT_low_pc and DW_AT_high_pc are not used, and vice versa.
Eric Christopher4daaed12012-12-10 19:51:21 +00002358CompileUnit *DwarfDebug::constructSkeletonCU(const MDNode *N) {
Eric Christopher98e237f2012-11-30 23:59:06 +00002359 DICompileUnit DIUnit(N);
2360 StringRef FN = DIUnit.getFilename();
2361 CompilationDir = DIUnit.getDirectory();
Eric Christopher98e237f2012-11-30 23:59:06 +00002362
2363 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002364 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
Eric Christopher2e5d8702012-12-20 21:58:36 +00002365 DIUnit.getLanguage(), Die, Asm,
2366 this, &InfoHolder);
Eric Christopher98e237f2012-11-30 23:59:06 +00002367 // FIXME: This should be the .dwo file.
2368 NewCU->addString(Die, dwarf::DW_AT_GNU_dwo_name, FN);
2369
2370 // FIXME: We also need DW_AT_addr_base and DW_AT_dwo_id.
2371
2372 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
2373 // into an entity.
2374 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
2375 // DW_AT_stmt_list is a offset of line number information for this
2376 // compile unit in debug_line section.
2377 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2378 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
2379 Asm->GetTempSymbol("section_line"));
2380 else
2381 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
2382
2383 if (!CompilationDir.empty())
2384 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
2385
Eric Christopher0e3e9b72012-12-10 23:34:43 +00002386 SkeletonHolder.addUnit(NewCU);
2387
Eric Christopher98e237f2012-11-30 23:59:06 +00002388 return NewCU;
2389}
2390
Eric Christopher4daaed12012-12-10 19:51:21 +00002391void DwarfDebug::emitSkeletonCU(const MCSection *Section) {
Eric Christopher98e237f2012-11-30 23:59:06 +00002392 Asm->OutStreamer.SwitchSection(Section);
Eric Christopher4daaed12012-12-10 19:51:21 +00002393 DIE *Die = SkeletonCU->getCUDie();
Eric Christopher98e237f2012-11-30 23:59:06 +00002394
2395 // Emit the compile units header.
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002396 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(Section->getLabelBeginName(),
Eric Christopher4daaed12012-12-10 19:51:21 +00002397 SkeletonCU->getUniqueID()));
Eric Christopher98e237f2012-11-30 23:59:06 +00002398
2399 // Emit size of content not including length itself
2400 unsigned ContentSize = Die->getSize() +
2401 sizeof(int16_t) + // DWARF version number
2402 sizeof(int32_t) + // Offset Into Abbrev. Section
2403 sizeof(int8_t); // Pointer Size (in bytes)
2404
2405 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2406 Asm->EmitInt32(ContentSize);
2407 Asm->OutStreamer.AddComment("DWARF version number");
2408 Asm->EmitInt16(dwarf::DWARF_VERSION);
2409 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christopher6eebe472012-12-19 22:02:53 +00002410
2411 const MCSection *ASec = Asm->getObjFileLowering().getDwarfAbbrevSection();
2412 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASec->getLabelBeginName()),
Eric Christopher98e237f2012-11-30 23:59:06 +00002413 DwarfAbbrevSectionSym);
2414 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2415 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2416
Eric Christopher6eebe472012-12-19 22:02:53 +00002417 emitDIE(Die, &SkeletonAbbrevs);
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002418 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(Section->getLabelEndName(),
Eric Christopher4daaed12012-12-10 19:51:21 +00002419 SkeletonCU->getUniqueID()));
Eric Christopher6eebe472012-12-19 22:02:53 +00002420}
Eric Christopher98e237f2012-11-30 23:59:06 +00002421
Eric Christopher6eebe472012-12-19 22:02:53 +00002422void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
2423 assert(useSplitDwarf() && "No split dwarf debug info?");
2424 emitAbbrevs(Section, &SkeletonAbbrevs);
Eric Christopher98e237f2012-11-30 23:59:06 +00002425}
2426
Eric Christopher0b944ee2012-12-11 19:42:09 +00002427// Emit the .debug_info.dwo section for separated dwarf. This contains the
2428// compile units that would normally be in debug_info.
Eric Christopher98e237f2012-11-30 23:59:06 +00002429void DwarfDebug::emitDebugInfoDWO() {
Eric Christopher4daaed12012-12-10 19:51:21 +00002430 assert(useSplitDwarf() && "No split dwarf debug info?");
Eric Christopherb1e66d02012-12-15 00:04:07 +00002431 InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Eric Christopher6eebe472012-12-19 22:02:53 +00002432 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2433 DwarfAbbrevDWOSectionSym);
2434}
2435
2436// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2437// abbreviations for the .debug_info.dwo section.
2438void DwarfDebug::emitDebugAbbrevDWO() {
2439 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopher72c16552012-12-20 21:58:40 +00002440 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2441 &Abbreviations);
Eric Christopher98e237f2012-11-30 23:59:06 +00002442}