blob: 5c8ef241492d023cb019ba3368195750637aabe4 [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/DIBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000027#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/IR/Module.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000031#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000032#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000033#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000034#include "llvm/MC/MCSymbol.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 Christopher64f824c2012-12-27 02:14:01 +0000160 InfoHolder(A, &AbbreviationsSet, &Abbreviations,
161 &InfoStringPool, "info_string"),
Eric Christopher6eebe472012-12-19 22:02:53 +0000162 SkeletonCU(0),
163 SkeletonAbbrevSet(InitAbbreviationsSetSize),
Eric Christopher64f824c2012-12-27 02:14:01 +0000164 SkeletonStringPool(DIEValueAllocator),
165 SkeletonHolder(A, &SkeletonAbbrevSet, &SkeletonAbbrevs,
166 &SkeletonStringPool, "skel_string") {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000167
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000168 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000169 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000170 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Eric Christopher64f824c2012-12-27 02:14:01 +0000171 DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000172 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopher60777d82012-04-02 17:58:52 +0000173
Eric Christopher10cb7442012-08-23 07:10:46 +0000174 // Turn on accelerator tables and older gdb compatibility
175 // for Darwin.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000176 bool IsDarwin = Triple(M->getTargetTriple()).isOSDarwin();
Eric Christopher20f47ab2012-08-23 22:36:40 +0000177 if (DarwinGDBCompat == Default) {
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000178 if (IsDarwin)
179 IsDarwinGDBCompat = true;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000180 else
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000181 IsDarwinGDBCompat = false;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000182 } else
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000183 IsDarwinGDBCompat = DarwinGDBCompat == Enable ? true : false;
Eric Christopherc1610fa2012-08-23 22:36:36 +0000184
Eric Christopher20f47ab2012-08-23 22:36:40 +0000185 if (DwarfAccelTables == Default) {
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000186 if (IsDarwin)
187 HasDwarfAccelTables = true;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000188 else
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000189 HasDwarfAccelTables = false;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000190 } else
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000191 HasDwarfAccelTables = DwarfAccelTables == Enable ? true : false;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000192
Eric Christopher4daaed12012-12-10 19:51:21 +0000193 if (SplitDwarf == Default)
194 HasSplitDwarf = false;
Eric Christopherf5b6dcd2012-11-12 22:22:20 +0000195 else
Eric Christopher4daaed12012-12-10 19:51:21 +0000196 HasSplitDwarf = SplitDwarf == Enable ? true : false;
Eric Christopherf5b6dcd2012-11-12 22:22:20 +0000197
Dan Gohman03c3dc72010-06-18 15:56:31 +0000198 {
199 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopherc4639d62012-11-19 22:42:15 +0000200 beginModule();
Torok Edwin9c421072010-04-07 10:44:46 +0000201 }
Bill Wendling0310d762009-05-15 09:23:25 +0000202}
203DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000204}
205
Eric Christopherb6dc8652012-11-27 22:43:45 +0000206// Switch to the specified MCSection and emit an assembler
207// temporary label to it if SymbolStem is specified.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000208static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christopherd8a87522011-11-07 09:18:38 +0000209 const char *SymbolStem = 0) {
210 Asm->OutStreamer.SwitchSection(Section);
211 if (!SymbolStem) return 0;
212
213 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
214 Asm->OutStreamer.EmitLabel(TmpSym);
215 return TmpSym;
216}
217
Eric Christopher2e5d8702012-12-20 21:58:36 +0000218MCSymbol *DwarfUnits::getStringPoolSym() {
Eric Christopher64f824c2012-12-27 02:14:01 +0000219 return Asm->GetTempSymbol(StringPref);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000220}
221
Eric Christopher2e5d8702012-12-20 21:58:36 +0000222MCSymbol *DwarfUnits::getStringPoolEntry(StringRef Str) {
223 std::pair<MCSymbol*, unsigned> &Entry =
224 StringPool->GetOrCreateValue(Str).getValue();
Chris Lattnerbc733f52010-03-13 02:17:42 +0000225 if (Entry.first) return Entry.first;
226
227 Entry.second = NextStringPoolNumber++;
Eric Christopher64f824c2012-12-27 02:14:01 +0000228 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000229}
230
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000231unsigned DwarfUnits::getStringPoolIndex(StringRef Str) {
232 std::pair<MCSymbol*, unsigned> &Entry =
233 StringPool->GetOrCreateValue(Str).getValue();
234 if (Entry.first) return Entry.second;
235
236 Entry.second = NextStringPoolNumber++;
237 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
238 return Entry.second;
239}
240
Eric Christopherb6dc8652012-11-27 22:43:45 +0000241// Define a unique number for the abbreviation.
242//
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000243void DwarfUnits::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000244 // Profile the node so that we can make it unique.
245 FoldingSetNodeID ID;
246 Abbrev.Profile(ID);
247
248 // Check the set for priors.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000249 DIEAbbrev *InSet = AbbreviationsSet->GetOrInsertNode(&Abbrev);
Bill Wendling0310d762009-05-15 09:23:25 +0000250
251 // If it's newly added.
252 if (InSet == &Abbrev) {
253 // Add to abbreviation list.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000254 Abbreviations->push_back(&Abbrev);
Bill Wendling0310d762009-05-15 09:23:25 +0000255
256 // Assign the vector position + 1 as its number.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000257 Abbrev.setNumber(Abbreviations->size());
Bill Wendling0310d762009-05-15 09:23:25 +0000258 } else {
259 // Assign existing abbreviation number.
260 Abbrev.setNumber(InSet->getNumber());
261 }
262}
263
Eric Christopherb6dc8652012-11-27 22:43:45 +0000264// If special LLVM prefix that is used to inform the asm
265// printer to not emit usual symbol prefix before the symbol name is used then
266// return linkage name after skipping this special LLVM prefix.
Devang Patel351ca332010-01-05 01:46:14 +0000267static StringRef getRealLinkageName(StringRef LinkageName) {
268 char One = '\1';
269 if (LinkageName.startswith(StringRef(&One, 1)))
270 return LinkageName.substr(1);
271 return LinkageName;
272}
273
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000274static bool isObjCClass(StringRef Name) {
275 return Name.startswith("+") || Name.startswith("-");
276}
277
278static bool hasObjCCategory(StringRef Name) {
279 if (!isObjCClass(Name)) return false;
280
281 size_t pos = Name.find(')');
282 if (pos != std::string::npos) {
283 if (Name[pos+1] != ' ') return false;
284 return true;
285 }
286 return false;
287}
288
289static void getObjCClassCategory(StringRef In, StringRef &Class,
290 StringRef &Category) {
291 if (!hasObjCCategory(In)) {
292 Class = In.slice(In.find('[') + 1, In.find(' '));
293 Category = "";
294 return;
295 }
296
297 Class = In.slice(In.find('[') + 1, In.find('('));
298 Category = In.slice(In.find('[') + 1, In.find(' '));
299 return;
300}
301
302static StringRef getObjCMethodName(StringRef In) {
303 return In.slice(In.find(' ') + 1, In.find(']'));
304}
305
306// Add the various names to the Dwarf accelerator table names.
307static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
308 DIE* Die) {
309 if (!SP.isDefinition()) return;
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000310
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000311 TheCU->addAccelName(SP.getName(), Die);
312
313 // If the linkage name is different than the name, go ahead and output
314 // that as well into the name table.
315 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
316 TheCU->addAccelName(SP.getLinkageName(), Die);
317
318 // If this is an Objective-C selector name add it to the ObjC accelerator
319 // too.
320 if (isObjCClass(SP.getName())) {
321 StringRef Class, Category;
322 getObjCClassCategory(SP.getName(), Class, Category);
323 TheCU->addAccelObjC(Class, Die);
324 if (Category != "")
325 TheCU->addAccelObjC(Category, Die);
326 // Also add the base method name to the name table.
327 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
328 }
329}
330
Eric Christopherb6dc8652012-11-27 22:43:45 +0000331// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
332// and DW_AT_high_pc attributes. If there are global variables in this
333// scope then create and insert DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000334DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
335 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000336 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000337
Chris Lattnerd38fee82010-04-05 00:13:49 +0000338 assert(SPDie && "Unable to find subprogram DIE!");
339 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000340
Bill Wendling168c1902012-11-07 05:19:04 +0000341 // If we're updating an abstract DIE, then we will be adding the children and
342 // object pointer later on. But what we don't want to do is process the
343 // concrete DIE twice.
Devang Patel8aa61472010-07-07 22:20:57 +0000344 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
Bill Wendling168c1902012-11-07 05:19:04 +0000345 // Pick up abstract subprogram DIE.
Devang Patel8aa61472010-07-07 22:20:57 +0000346 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000347 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
348 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000349 SPCU->addDie(SPDie);
Bill Wendlinga4c76932012-11-07 04:42:18 +0000350 } else {
351 DISubprogram SPDecl = SP.getFunctionDeclaration();
352 if (!SPDecl.isSubprogram()) {
353 // There is not any need to generate specification DIE for a function
354 // defined at compile unit level. If a function is defined inside another
355 // function then gdb prefers the definition at top level and but does not
356 // expect specification DIE in parent function. So avoid creating
357 // specification DIE for a function defined inside a function.
358 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
359 !SP.getContext().isFile() &&
360 !isSubprogramContext(SP.getContext())) {
361 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
362
363 // Add arguments.
364 DICompositeType SPTy = SP.getType();
365 DIArray Args = SPTy.getTypeArray();
366 unsigned SPTag = SPTy.getTag();
367 if (SPTag == dwarf::DW_TAG_subroutine_type)
368 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
369 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
370 DIType ATy = DIType(Args.getElement(i));
371 SPCU->addType(Arg, ATy);
372 if (ATy.isArtificial())
373 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
374 if (ATy.isObjectPointer())
375 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer,
376 dwarf::DW_FORM_ref4, Arg);
377 SPDie->addChild(Arg);
378 }
379 DIE *SPDeclDie = SPDie;
380 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000381 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification,
382 dwarf::DW_FORM_ref4, SPDeclDie);
Bill Wendlinga4c76932012-11-07 04:42:18 +0000383 SPCU->addDie(SPDie);
384 }
385 }
Devang Patel8aa61472010-07-07 22:20:57 +0000386 }
387
Devang Patel3cbee302011-04-12 22:53:02 +0000388 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
389 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
390 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
391 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000392 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
393 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000394 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000395
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000396 // Add name to the name table, we do this here because we're guaranteed
397 // to have concrete versions of our DW_TAG_subprogram nodes.
398 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000399
Chris Lattnerd38fee82010-04-05 00:13:49 +0000400 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000401}
402
Eric Christopherb6dc8652012-11-27 22:43:45 +0000403// Construct new DW_TAG_lexical_block for this scope and attach
404// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000405DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
Devang Pateld3024342011-08-15 22:24:32 +0000406 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000407 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
408 if (Scope->isAbstractScope())
409 return ScopeDIE;
410
Devang Patelbf47fdb2011-08-10 20:55:27 +0000411 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000412 if (Ranges.empty())
413 return 0;
414
Devang Patelbf47fdb2011-08-10 20:55:27 +0000415 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000416 if (Ranges.size() > 1) {
417 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000418 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000419 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000420 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000421 DebugRangeSymbols.size()
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000422 * Asm->getDataLayout().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000423 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000424 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000425 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
426 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000427 }
428 DebugRangeSymbols.push_back(NULL);
429 DebugRangeSymbols.push_back(NULL);
430 return ScopeDIE;
431 }
432
Devang Patelc3f5f782010-05-25 23:40:22 +0000433 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
434 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000435
Devang Patelc3f5f782010-05-25 23:40:22 +0000436 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000437
Chris Lattnerb7db7332010-03-09 01:58:53 +0000438 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
439 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000440
Devang Patel3cbee302011-04-12 22:53:02 +0000441 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
442 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000443
444 return ScopeDIE;
445}
446
Eric Christopherb6dc8652012-11-27 22:43:45 +0000447// This scope represents inlined body of a function. Construct DIE to
448// represent this concrete inlined copy of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000449DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
450 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000451 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000452 assert(Ranges.empty() == false &&
453 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000454
Devang Patel26a92002011-07-27 00:34:13 +0000455 if (!Scope->getScopeNode())
456 return NULL;
457 DIScope DS(Scope->getScopeNode());
458 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000459 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
460 if (!OriginDIE) {
Bill Wendlinge0aae5b2012-10-30 17:51:02 +0000461 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patel26a92002011-07-27 00:34:13 +0000462 return NULL;
463 }
464
Devang Patelbf47fdb2011-08-10 20:55:27 +0000465 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000466 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
467 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000468
Devang Patel0afbf232010-07-08 22:39:20 +0000469 if (StartLabel == 0 || EndLabel == 0) {
Bill Wendlinge0aae5b2012-10-30 17:51:02 +0000470 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000471 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000472 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000473 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000474 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000475 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000476
Devang Pateld96efb82011-05-05 17:54:26 +0000477 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000478 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
479 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000480
Devang Patel26a92002011-07-27 00:34:13 +0000481 if (Ranges.size() > 1) {
482 // .debug_range section has not been laid out yet. Emit offset in
483 // .debug_range as a uint, size 4, for now. emitDIE will handle
484 // DW_AT_ranges appropriately.
485 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000486 DebugRangeSymbols.size()
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000487 * Asm->getDataLayout().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000488 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000489 RE = Ranges.end(); RI != RE; ++RI) {
490 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
491 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
492 }
493 DebugRangeSymbols.push_back(NULL);
494 DebugRangeSymbols.push_back(NULL);
495 } else {
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000496 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel5bc942c2011-08-10 23:58:09 +0000497 StartLabel);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000498 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel5bc942c2011-08-10 23:58:09 +0000499 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000500 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000501
502 InlinedSubprogramDIEs.insert(OriginDIE);
503
504 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000505 //.debug_inlined section specification does not clearly state how
506 // to emit inlined scope that is split into multiple instruction ranges.
507 // For now, use first instruction range and emit low_pc/high_pc pair and
508 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000509 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000510 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000511
512 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000513 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000514 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000515 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000516 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000517
Devang Patel53bb5c92009-11-10 23:06:00 +0000518 DILocation DL(Scope->getInlinedAt());
Eric Christopher08212002012-03-26 21:38:38 +0000519 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000520 getOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patel3cbee302011-04-12 22:53:02 +0000521 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000522
Eric Christopher309bedd2011-12-04 06:02:38 +0000523 // Add name to the name table, we do this here because we're guaranteed
524 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
525 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000526
Devang Patel53bb5c92009-11-10 23:06:00 +0000527 return ScopeDIE;
528}
529
Eric Christopherb6dc8652012-11-27 22:43:45 +0000530// Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000531DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000532 if (!Scope || !Scope->getScopeNode())
533 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000534
Nick Lewycky746cb672011-10-26 22:55:33 +0000535 SmallVector<DIE *, 8> Children;
Eric Christophere5212782012-09-12 23:36:19 +0000536 DIE *ObjectPointer = NULL;
Devang Patel0478c152011-03-01 22:58:55 +0000537
538 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000539 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000540 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
541 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000542 if (DIE *Arg =
Eric Christophere5212782012-09-12 23:36:19 +0000543 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope())) {
Devang Patel0478c152011-03-01 22:58:55 +0000544 Children.push_back(Arg);
Eric Christophere5212782012-09-12 23:36:19 +0000545 if (ArgDV->isObjectPointer()) ObjectPointer = Arg;
546 }
Devang Patel0478c152011-03-01 22:58:55 +0000547
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000548 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000549 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000550 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000551 if (DIE *Variable =
Eric Christopher7b451cf2012-09-21 22:18:52 +0000552 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope())) {
Devang Patel5bc9fec2011-02-19 01:31:27 +0000553 Children.push_back(Variable);
Eric Christopher7b451cf2012-09-21 22:18:52 +0000554 if (Variables[i]->isObjectPointer()) ObjectPointer = Variable;
555 }
Devang Patelbf47fdb2011-08-10 20:55:27 +0000556 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000557 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000558 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000559 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000560 DIScope DS(Scope->getScopeNode());
561 DIE *ScopeDIE = NULL;
562 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000563 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000564 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000565 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000566 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000567 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000568 // Note down abstract DIE.
569 if (ScopeDIE)
570 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
571 }
Devang Patel3c91b052010-03-08 20:52:55 +0000572 else
Devang Pateld3024342011-08-15 22:24:32 +0000573 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000574 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000575 else {
576 // There is no need to emit empty lexical block DIE.
577 if (Children.empty())
578 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000579 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000580 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000581
Devang Patelaead63c2010-03-29 22:59:58 +0000582 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000583
Devang Patel5bc9fec2011-02-19 01:31:27 +0000584 // Add children
585 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
586 E = Children.end(); I != E; ++I)
587 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000588
Eric Christophere5212782012-09-12 23:36:19 +0000589 if (DS.isSubprogram() && ObjectPointer != NULL)
590 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer,
591 dwarf::DW_FORM_ref4, ObjectPointer);
592
Jim Grosbach1e20b962010-07-21 21:21:52 +0000593 if (DS.isSubprogram())
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000594 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000595
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000596 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000597}
598
Eric Christopherb6dc8652012-11-27 22:43:45 +0000599// Look up the source id with the given directory and source file names.
600// If none currently exists, create a new id and insert it in the
601// SourceIds map. This can update DirectoryNames and SourceFileNames maps
602// as well.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000603unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName,
Devang Patel23670e52011-03-24 20:30:50 +0000604 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000605 // If FE did not provide a file name, then assume stdin.
606 if (FileName.empty())
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000607 return getOrCreateSourceID("<stdin>", StringRef());
Devang Patel23670e52011-03-24 20:30:50 +0000608
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000609 // TODO: this might not belong here. See if we can factor this better.
610 if (DirName == CompilationDir)
611 DirName = "";
612
Nick Lewycky44d798d2011-10-17 23:05:28 +0000613 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel1905a182010-09-16 20:57:49 +0000614
Benjamin Kramer74612c22012-03-11 14:56:26 +0000615 // We look up the file/dir pair by concatenating them with a zero byte.
616 SmallString<128> NamePair;
617 NamePair += DirName;
618 NamePair += '\0'; // Zero bytes are not allowed in paths.
619 NamePair += FileName;
620
621 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
622 if (Ent.getValue() != SrcId)
623 return Ent.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000624
Rafael Espindola5c055632010-11-18 02:04:25 +0000625 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer74612c22012-03-11 14:56:26 +0000626 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000627
628 return SrcId;
629}
630
Eric Christopher72c16552012-12-20 21:58:40 +0000631// Create new CompileUnit for the given metadata node with tag
632// DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000633CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000634 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000635 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000636 CompilationDir = DIUnit.getDirectory();
Eli Benderskyd4a05e02012-12-03 18:45:45 +0000637 // Call this to emit a .file directive if it wasn't emitted for the source
638 // file this CU comes from yet.
639 getOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000640
641 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyd4a05e02012-12-03 18:45:45 +0000642 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
Eric Christopher2e5d8702012-12-20 21:58:36 +0000643 DIUnit.getLanguage(), Die, Asm,
644 this, &InfoHolder);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000645 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000646 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
647 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000648 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher6635cad2012-08-01 18:19:01 +0000649 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
650 // into an entity.
651 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000652 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000653 // compile unit in debug_line section.
Rafael Espindola2241e512012-06-22 13:24:07 +0000654 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindola597a7662011-05-04 17:44:06 +0000655 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000656 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000657 else
Devang Patel3cbee302011-04-12 22:53:02 +0000658 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000659
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000660 if (!CompilationDir.empty())
661 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000662 if (DIUnit.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000663 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000664
Devang Patel65dbc902009-11-25 17:36:49 +0000665 StringRef Flags = DIUnit.getFlags();
666 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000667 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000668
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000669 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000670 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000671 dwarf::DW_FORM_data1, RVer);
672
Devang Patel163a9f72010-05-10 22:49:55 +0000673 if (!FirstCU)
674 FirstCU = NewCU;
Eric Christopher4daaed12012-12-10 19:51:21 +0000675 if (useSplitDwarf() && !SkeletonCU)
676 SkeletonCU = constructSkeletonCU(N);
Eric Christopher98e237f2012-11-30 23:59:06 +0000677
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000678 InfoHolder.addUnit(NewCU);
679
Devang Patel163a9f72010-05-10 22:49:55 +0000680 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000681 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000682}
683
Eric Christopherb6dc8652012-11-27 22:43:45 +0000684// Construct subprogram DIE.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000685void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
Devang Patel3655a212011-08-15 23:36:40 +0000686 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000687 CompileUnit *&CURef = SPMap[N];
688 if (CURef)
689 return;
690 CURef = TheCU;
691
Devang Patele4b27562009-08-28 23:24:31 +0000692 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000693 if (!SP.isDefinition())
694 // This is a method declaration which will be handled while constructing
695 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000696 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000697
Devang Pateldbc64af2011-08-15 17:24:54 +0000698 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000699
700 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000701 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000702
703 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000704 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000705
Devang Patel13e16b62009-06-26 01:49:18 +0000706 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000707}
708
Eric Christopherb6dc8652012-11-27 22:43:45 +0000709// Collect debug info from named mdnodes such as llvm.dbg.enum and llvm.dbg.ty.
Eric Christopherc4639d62012-11-19 22:42:15 +0000710void DwarfDebug::collectInfoFromNamedMDNodes(const Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000711 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
712 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
713 const MDNode *N = NMD->getOperand(i);
714 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
715 constructSubprogramDIE(CU, N);
716 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000717
Devang Patel94c7ddb2011-08-16 22:09:43 +0000718 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
719 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
720 const MDNode *N = NMD->getOperand(i);
721 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000722 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000723 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000724
Devang Patel02e603f2011-08-15 23:47:24 +0000725 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
726 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
727 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000728 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
729 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000730 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000731
Devang Patel02e603f2011-08-15 23:47:24 +0000732 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
733 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
734 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000735 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
736 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000737 }
738}
739
Eric Christopherb6dc8652012-11-27 22:43:45 +0000740// Collect debug info using DebugInfoFinder.
741// FIXME - Remove this when dragonegg switches to DIBuilder.
Eric Christopherc4639d62012-11-19 22:42:15 +0000742bool DwarfDebug::collectLegacyDebugInfo(const Module *M) {
Devang Patel02e603f2011-08-15 23:47:24 +0000743 DebugInfoFinder DbgFinder;
744 DbgFinder.processModule(*M);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000745
Devang Patel02e603f2011-08-15 23:47:24 +0000746 bool HasDebugInfo = false;
747 // Scan all the compile-units to see if there are any marked as the main
748 // unit. If not, we do not generate debug info.
749 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
750 E = DbgFinder.compile_unit_end(); I != E; ++I) {
751 if (DICompileUnit(*I).isMain()) {
752 HasDebugInfo = true;
753 break;
754 }
755 }
756 if (!HasDebugInfo) return false;
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000757
Devang Patel02e603f2011-08-15 23:47:24 +0000758 // Create all the compile unit DIEs.
759 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
760 E = DbgFinder.compile_unit_end(); I != E; ++I)
761 constructCompileUnit(*I);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000762
Devang Patel02e603f2011-08-15 23:47:24 +0000763 // Create DIEs for each global variable.
764 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
765 E = DbgFinder.global_variable_end(); I != E; ++I) {
766 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000767 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000768 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000769 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000770
Devang Patel02e603f2011-08-15 23:47:24 +0000771 // Create DIEs for each subprogram.
772 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
773 E = DbgFinder.subprogram_end(); I != E; ++I) {
774 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000775 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
776 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000777 }
778
779 return HasDebugInfo;
780}
781
Eric Christopherb6dc8652012-11-27 22:43:45 +0000782// Emit all Dwarf sections that should come prior to the content. Create
783// global DIEs and emit initial debug info sections. This is invoked by
784// the target AsmPrinter.
Eric Christopherc4639d62012-11-19 22:42:15 +0000785void DwarfDebug::beginModule() {
Devang Pateleac9c072010-04-27 19:46:33 +0000786 if (DisableDebugInfoPrinting)
787 return;
788
Eric Christopherc4639d62012-11-19 22:42:15 +0000789 const Module *M = MMI->getModule();
790
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000791 // If module has named metadata anchors then use them, otherwise scan the
792 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000793 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
794 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000795 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
796 DICompileUnit CUNode(CU_Nodes->getOperand(i));
797 CompileUnit *CU = constructCompileUnit(CUNode);
798 DIArray GVs = CUNode.getGlobalVariables();
799 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000800 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000801 DIArray SPs = CUNode.getSubprograms();
802 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
803 constructSubprogramDIE(CU, SPs.getElement(i));
804 DIArray EnumTypes = CUNode.getEnumTypes();
805 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
806 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
807 DIArray RetainedTypes = CUNode.getRetainedTypes();
808 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
809 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
810 }
Devang Patel02e603f2011-08-15 23:47:24 +0000811 } else if (!collectLegacyDebugInfo(M))
812 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000813
Devang Patel02e603f2011-08-15 23:47:24 +0000814 collectInfoFromNamedMDNodes(M);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000815
Chris Lattnerd850ac72010-04-05 02:19:28 +0000816 // Tell MMI that we have debug info.
817 MMI->setDebugInfoAvailability(true);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000818
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000819 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000820 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000821}
822
Eric Christopher4117bec2012-11-22 00:59:49 +0000823// Attach DW_AT_inline attribute with inlined subprogram DIEs.
824void DwarfDebug::computeInlinedDIEs() {
Devang Patel53bb5c92009-11-10 23:06:00 +0000825 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
826 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000827 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
Devang Patel53bb5c92009-11-10 23:06:00 +0000828 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000829 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000830 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000831 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000832 AE = AbstractSPDies.end(); AI != AE; ++AI) {
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000833 DIE *ISP = AI->second;
834 if (InlinedSubprogramDIEs.count(ISP))
835 continue;
836 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
837 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000838}
839
840// Collect info for variables that were optimized out.
841void DwarfDebug::collectDeadVariables() {
842 const Module *M = MMI->getModule();
843 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
844
845 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
846 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
847 DICompileUnit TheCU(CU_Nodes->getOperand(i));
848 DIArray Subprograms = TheCU.getSubprograms();
849 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopherbdab8002012-11-27 00:13:51 +0000850 DISubprogram SP(Subprograms.getElement(i));
851 if (ProcessedSPNodes.count(SP) != 0) continue;
852 if (!SP.Verify()) continue;
853 if (!SP.isDefinition()) continue;
854 DIArray Variables = SP.getVariables();
855 if (Variables.getNumElements() == 0) continue;
Eric Christopher4117bec2012-11-22 00:59:49 +0000856
Eric Christopherbdab8002012-11-27 00:13:51 +0000857 LexicalScope *Scope =
858 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
859 DeadFnScopeMap[SP] = Scope;
Eric Christopher4117bec2012-11-22 00:59:49 +0000860
Eric Christopherbdab8002012-11-27 00:13:51 +0000861 // Construct subprogram DIE and add variables DIEs.
862 CompileUnit *SPCU = CUMap.lookup(TheCU);
863 assert(SPCU && "Unable to find Compile Unit!");
864 constructSubprogramDIE(SPCU, SP);
865 DIE *ScopeDIE = SPCU->getDIE(SP);
866 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
867 DIVariable DV(Variables.getElement(vi));
868 if (!DV.Verify()) continue;
869 DbgVariable *NewVar = new DbgVariable(DV, NULL);
870 if (DIE *VariableDIE =
871 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
872 ScopeDIE->addChild(VariableDIE);
873 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000874 }
875 }
876 }
877 DeleteContainerSeconds(DeadFnScopeMap);
878}
879
880void DwarfDebug::finalizeModuleInfo() {
881 // Collect info for variables that were optimized out.
882 collectDeadVariables();
883
884 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
885 computeInlinedDIEs();
Devang Patel53bb5c92009-11-10 23:06:00 +0000886
Eric Christopher6635cad2012-08-01 18:19:01 +0000887 // Emit DW_AT_containing_type attribute to connect types with their
888 // vtable holding type.
Devang Pateldbc64af2011-08-15 17:24:54 +0000889 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000890 CUE = CUMap.end(); CUI != CUE; ++CUI) {
Devang Pateldbc64af2011-08-15 17:24:54 +0000891 CompileUnit *TheCU = CUI->second;
892 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000893 }
894
Eric Christopher4117bec2012-11-22 00:59:49 +0000895 // Compute DIE offsets and sizes.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000896 InfoHolder.computeSizeAndOffsets();
897 if (useSplitDwarf())
898 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher4117bec2012-11-22 00:59:49 +0000899}
900
901void DwarfDebug::endSections() {
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000902 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000903 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000904 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000905 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000906 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000907
908 // End text sections.
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +0000909 for (unsigned I = 0, E = SectionMap.size(); I != E; ++I) {
910 Asm->OutStreamer.SwitchSection(SectionMap[I]);
911 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", I+1));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000912 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000913}
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000914
Eric Christopherb6dc8652012-11-27 22:43:45 +0000915// Emit all Dwarf sections that should come after the content.
Eric Christopher4117bec2012-11-22 00:59:49 +0000916void DwarfDebug::endModule() {
917
918 if (!FirstCU) return;
919
920 // End any existing sections.
921 // TODO: Does this need to happen?
922 endSections();
923
924 // Finalize the debug info for the module.
925 finalizeModuleInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000926
Eric Christopher97c34722012-11-19 19:43:59 +0000927 // Emit initial sections.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000928 emitSectionLabels();
Eric Christopher97c34722012-11-19 19:43:59 +0000929
Eric Christopher4daaed12012-12-10 19:51:21 +0000930 if (!useSplitDwarf()) {
Eric Christopher42885022012-11-27 22:43:42 +0000931 // Emit all the DIEs into a debug info section.
932 emitDebugInfo();
Eric Christopher74802f72012-11-27 00:41:54 +0000933
Eric Christopher42885022012-11-27 22:43:42 +0000934 // Corresponding abbreviations into a abbrev section.
935 emitAbbreviations();
936
937 // Emit info into a debug loc section.
938 emitDebugLoc();
939
940 // Emit info into a debug aranges section.
941 emitDebugARanges();
942
943 // Emit info into a debug ranges section.
944 emitDebugRanges();
945
946 // Emit info into a debug macinfo section.
947 emitDebugMacInfo();
948
949 // Emit inline info.
950 // TODO: When we don't need the option anymore we
951 // can remove all of the code that this section
952 // depends upon.
953 if (useDarwinGDBCompat())
954 emitDebugInlineInfo();
955 } else {
Eric Christopher0b944ee2012-12-11 19:42:09 +0000956 // TODO: Fill this in for separated debug sections and separate
Eric Christopher42885022012-11-27 22:43:42 +0000957 // out information into new sections.
958
Eric Christopher98e237f2012-11-30 23:59:06 +0000959 // Emit the debug info section and compile units.
Eric Christopher42885022012-11-27 22:43:42 +0000960 emitDebugInfo();
Eric Christopher98e237f2012-11-30 23:59:06 +0000961 emitDebugInfoDWO();
Eric Christopher42885022012-11-27 22:43:42 +0000962
963 // Corresponding abbreviations into a abbrev section.
964 emitAbbreviations();
Eric Christopher6eebe472012-12-19 22:02:53 +0000965 emitDebugAbbrevDWO();
Eric Christopher42885022012-11-27 22:43:42 +0000966
967 // Emit info into a debug loc section.
968 emitDebugLoc();
969
970 // Emit info into a debug aranges section.
971 emitDebugARanges();
972
973 // Emit info into a debug ranges section.
974 emitDebugRanges();
975
976 // Emit info into a debug macinfo section.
977 emitDebugMacInfo();
978
979 // Emit inline info.
980 // TODO: When we don't need the option anymore we
981 // can remove all of the code that this section
982 // depends upon.
983 if (useDarwinGDBCompat())
984 emitDebugInlineInfo();
985 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000986
Eric Christopher9d9f5a52012-08-23 07:32:06 +0000987 // Emit info into the dwarf accelerator table sections.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000988 if (useDwarfAccelTables()) {
Eric Christopher09ac3d82011-11-07 09:24:32 +0000989 emitAccelNames();
990 emitAccelObjC();
991 emitAccelNamespaces();
992 emitAccelTypes();
993 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000994
Devang Patel193f7202009-11-24 01:14:22 +0000995 // Emit info into a debug pubtypes section.
Eric Christopher360f0062012-08-23 07:10:56 +0000996 // TODO: When we don't need the option anymore we can
997 // remove all of the code that adds to the table.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000998 if (useDarwinGDBCompat())
Eric Christopher360f0062012-08-23 07:10:56 +0000999 emitDebugPubTypes();
Devang Patel193f7202009-11-24 01:14:22 +00001000
Eric Christopher42885022012-11-27 22:43:42 +00001001 // Finally emit string information into a string table.
Eric Christopherfcdbecb2012-11-27 06:49:23 +00001002 emitDebugStr();
Eric Christopher64f824c2012-12-27 02:14:01 +00001003 if (useSplitDwarf())
1004 emitDebugStrDWO();
Eric Christopherfcdbecb2012-11-27 06:49:23 +00001005
Devang Patele9a1cca2010-08-02 17:32:15 +00001006 // clean up.
Devang Patel94c7ddb2011-08-16 22:09:43 +00001007 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +00001008 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1009 E = CUMap.end(); I != E; ++I)
1010 delete I->second;
Eric Christopher9ec87b32012-12-10 19:51:18 +00001011
Eric Christopher4daaed12012-12-10 19:51:21 +00001012 delete SkeletonCU;
Eric Christopher9ec87b32012-12-10 19:51:18 +00001013
Eric Christopher98e237f2012-11-30 23:59:06 +00001014 // Reset these for the next Module if we have one.
1015 FirstCU = NULL;
Eric Christopher4daaed12012-12-10 19:51:21 +00001016 SkeletonCU = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001017}
1018
Eric Christopherb6dc8652012-11-27 22:43:45 +00001019// Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +00001020DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +00001021 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +00001022 LLVMContext &Ctx = DV->getContext();
1023 // More then one inlined variable corresponds to one abstract variable.
1024 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +00001025 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001026 if (AbsDbgVariable)
1027 return AbsDbgVariable;
1028
Devang Patelbf47fdb2011-08-10 20:55:27 +00001029 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00001030 if (!Scope)
1031 return NULL;
1032
Devang Patel5a1a67c2011-08-15 19:01:20 +00001033 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001034 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00001035 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00001036 return AbsDbgVariable;
1037}
1038
Eric Christopherb6dc8652012-11-27 22:43:45 +00001039// If Var is a current function argument then add it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +00001040bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +00001041 DbgVariable *Var, LexicalScope *Scope) {
1042 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +00001043 return false;
1044 DIVariable DV = Var->getVariable();
1045 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1046 return false;
1047 unsigned ArgNo = DV.getArgNumber();
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001048 if (ArgNo == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001049 return false;
1050
Devang Patelcb3a6572011-03-03 20:02:02 +00001051 size_t Size = CurrentFnArguments.size();
1052 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001053 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +00001054 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +00001055 // arguments does the function have at source level.
1056 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +00001057 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +00001058 CurrentFnArguments[ArgNo - 1] = Var;
1059 return true;
1060}
1061
Eric Christopherb6dc8652012-11-27 22:43:45 +00001062// Collect variable information from side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001063void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001064DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +00001065 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +00001066 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1067 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1068 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00001069 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00001070 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00001071 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00001072 DIVariable DV(Var);
1073 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001074
Devang Patelbf47fdb2011-08-10 20:55:27 +00001075 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001076
Devang Patelfb0ee432009-11-10 23:20:04 +00001077 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00001078 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00001079 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001080
Devang Patel26c1e562010-05-20 16:36:41 +00001081 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001082 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +00001083 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +00001084 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001085 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001086 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001087 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +00001088 }
Devang Patelee432862010-05-20 19:57:06 +00001089}
Devang Patel90a48ad2010-03-15 18:33:46 +00001090
Eric Christopherb6dc8652012-11-27 22:43:45 +00001091// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1092// defined reg.
Devang Patelc3f5f782010-05-25 23:40:22 +00001093static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001094 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001095 return MI->getNumOperands() == 3 &&
1096 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
1097 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +00001098}
1099
Eric Christopherb6dc8652012-11-27 22:43:45 +00001100// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001101static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1102 const MCSymbol *FLabel,
Devang Patel90b40412011-07-08 17:09:57 +00001103 const MCSymbol *SLabel,
1104 const MachineInstr *MI) {
1105 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1106
1107 if (MI->getNumOperands() != 3) {
1108 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1109 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1110 }
1111 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1112 MachineLocation MLoc;
1113 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1114 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1115 }
1116 if (MI->getOperand(0).isImm())
1117 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1118 if (MI->getOperand(0).isFPImm())
1119 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1120 if (MI->getOperand(0).isCImm())
1121 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1122
Craig Topper5e25ee82012-02-05 08:31:47 +00001123 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +00001124}
1125
Eric Christopherb6dc8652012-11-27 22:43:45 +00001126// Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001127void
Devang Patel78e127d2010-06-25 22:07:34 +00001128DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1129 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001130
Eric Christopherb6dc8652012-11-27 22:43:45 +00001131 // collection info from MMI table.
Devang Patelee432862010-05-20 19:57:06 +00001132 collectVariableInfoFromMMITable(MF, Processed);
1133
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001134 for (SmallVectorImpl<const MDNode*>::const_iterator
1135 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1136 ++UVI) {
1137 const MDNode *Var = *UVI;
1138 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001139 continue;
1140
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001141 // History contains relevant DBG_VALUE instructions for Var and instructions
1142 // clobbering it.
1143 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1144 if (History.empty())
1145 continue;
1146 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001147
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001148 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001149 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001150 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1151 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001152 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001153 else {
1154 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001155 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001156 else {
1157 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001158 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001159 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001160 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001161 }
1162 }
Devang Patelee432862010-05-20 19:57:06 +00001163 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001164 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001165 continue;
1166
1167 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001168 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001169 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1170 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001171 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001172 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001173 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001174 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001175
Eric Christopherc56e3f02012-10-08 20:48:54 +00001176 // Simplify ranges that are fully coalesced.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001177 if (History.size() <= 1 || (History.size() == 2 &&
1178 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001179 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001180 continue;
1181 }
1182
1183 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001184 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001185
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001186 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1187 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1188 const MachineInstr *Begin = *HI;
1189 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001190
Devang Patel4ada1d72011-06-01 23:00:17 +00001191 // Check if DBG_VALUE is truncating a range.
1192 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1193 && !Begin->getOperand(0).getReg())
1194 continue;
1195
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001196 // Compute the range for a register location.
1197 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1198 const MCSymbol *SLabel = 0;
1199
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001200 if (HI + 1 == HE)
1201 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001202 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001203 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001204 else {
1205 const MachineInstr *End = HI[1];
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001206 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Devang Patel476df5f2011-07-07 21:44:42 +00001207 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001208 if (End->isDebugValue())
1209 SLabel = getLabelBeforeInsn(End);
1210 else {
1211 // End is a normal instruction clobbering the range.
1212 SLabel = getLabelAfterInsn(End);
1213 assert(SLabel && "Forgot label after clobber instruction");
1214 ++HI;
1215 }
1216 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001217
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001218 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001219 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1220 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001221 }
1222 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001223 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001224
1225 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001226 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1227 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1228 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1229 DIVariable DV(Variables.getElement(i));
1230 if (!DV || !DV.Verify() || !Processed.insert(DV))
1231 continue;
1232 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1233 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001234 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001235}
Devang Patel98e1cac2010-05-14 21:01:35 +00001236
Eric Christopherb6dc8652012-11-27 22:43:45 +00001237// Return Label preceding the instruction.
Devang Patelc3f5f782010-05-25 23:40:22 +00001238const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001239 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1240 assert(Label && "Didn't insert label before instruction");
1241 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001242}
1243
Eric Christopherb6dc8652012-11-27 22:43:45 +00001244// Return Label immediately following the instruction.
Devang Patelc3f5f782010-05-25 23:40:22 +00001245const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001246 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001247}
1248
Eric Christopherb6dc8652012-11-27 22:43:45 +00001249// Process beginning of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +00001250void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001251 // Check if source location changes, but ignore DBG_VALUE locations.
1252 if (!MI->isDebugValue()) {
1253 DebugLoc DL = MI->getDebugLoc();
1254 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001255 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001256 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001257 if (DL == PrologEndLoc) {
1258 Flags |= DWARF2_FLAG_PROLOGUE_END;
1259 PrologEndLoc = DebugLoc();
1260 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001261 if (PrologEndLoc.isUnknown())
1262 Flags |= DWARF2_FLAG_IS_STMT;
1263
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001264 if (!DL.isUnknown()) {
1265 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001266 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001267 } else
Devang Patel4243e672011-05-11 19:22:19 +00001268 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001269 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001270 }
Devang Patelaead63c2010-03-29 22:59:58 +00001271
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001272 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001273 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1274 LabelsBeforeInsn.find(MI);
1275
1276 // No label needed.
1277 if (I == LabelsBeforeInsn.end())
1278 return;
1279
1280 // Label already assigned.
1281 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001282 return;
Devang Patel553881b2010-03-29 17:20:31 +00001283
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001284 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001285 PrevLabel = MMI->getContext().CreateTempSymbol();
1286 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001287 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001288 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001289}
1290
Eric Christopherb6dc8652012-11-27 22:43:45 +00001291// Process end of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +00001292void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001293 // Don't create a new label after DBG_VALUE instructions.
1294 // They don't generate code.
1295 if (!MI->isDebugValue())
1296 PrevLabel = 0;
1297
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001298 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1299 LabelsAfterInsn.find(MI);
1300
1301 // No label needed.
1302 if (I == LabelsAfterInsn.end())
1303 return;
1304
1305 // Label already assigned.
1306 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001307 return;
1308
1309 // We need a label after this instruction.
1310 if (!PrevLabel) {
1311 PrevLabel = MMI->getContext().CreateTempSymbol();
1312 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001313 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001314 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001315}
1316
Eric Christopherb6dc8652012-11-27 22:43:45 +00001317// Each LexicalScope has first instruction and last instruction to mark
1318// beginning and end of a scope respectively. Create an inverse map that list
1319// scopes starts (and ends) with an instruction. One instruction may start (or
1320// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001321void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001322 SmallVector<LexicalScope *, 4> WorkList;
1323 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001324 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001325 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001326
Devang Patelbf47fdb2011-08-10 20:55:27 +00001327 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001328 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001329 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001330 SE = Children.end(); SI != SE; ++SI)
1331 WorkList.push_back(*SI);
1332
Devang Patel53bb5c92009-11-10 23:06:00 +00001333 if (S->isAbstractScope())
1334 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001335
Devang Patelbf47fdb2011-08-10 20:55:27 +00001336 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001337 if (Ranges.empty())
1338 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001339 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001340 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001341 assert(RI->first && "InsnRange does not have first instruction!");
1342 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001343 requestLabelBeforeInsn(RI->first);
1344 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001345 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001346 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001347}
1348
Eric Christopherb6dc8652012-11-27 22:43:45 +00001349// Get MDNode for DebugLoc's scope.
Devang Patela3f48672011-05-09 22:14:49 +00001350static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1351 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1352 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1353 return DL.getScope(Ctx);
1354}
1355
Eric Christopherb6dc8652012-11-27 22:43:45 +00001356// Walk up the scope chain of given debug loc and find line number info
1357// for the function.
Devang Patel4243e672011-05-11 19:22:19 +00001358static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1359 const MDNode *Scope = getScopeNode(DL, Ctx);
1360 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher6126a1e2012-04-03 00:43:49 +00001361 if (SP.Verify()) {
1362 // Check for number of operands since the compatibility is
1363 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001364 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-04-03 00:43:49 +00001365 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1366 else
1367 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1368 }
1369
Devang Patel4243e672011-05-11 19:22:19 +00001370 return DebugLoc();
1371}
1372
Eric Christopherb6dc8652012-11-27 22:43:45 +00001373// Gather pre-function debug information. Assumes being called immediately
1374// after the function entry point has been emitted.
Chris Lattnereec791a2010-01-26 23:18:02 +00001375void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001376 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001377 LScopes.initialize(*MF);
1378 if (LScopes.empty()) return;
1379 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001380
Devang Pateleac9c072010-04-27 19:46:33 +00001381 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1382 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001383 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001384 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001385
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001386 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1387
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001388 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopherb6dc8652012-11-27 22:43:45 +00001389 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001390 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1391
Devang Patelb2b31a62010-05-26 19:37:24 +00001392 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001393 I != E; ++I) {
1394 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001395 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1396 II != IE; ++II) {
1397 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001398
Devang Patelb2b31a62010-05-26 19:37:24 +00001399 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001400 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001401
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001402 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001403 const MDNode *Var =
1404 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001405
1406 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001407 if (isDbgValueInDefinedReg(MI))
1408 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1409
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001410 // Check the history of this variable.
1411 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1412 if (History.empty()) {
1413 UserVariables.push_back(Var);
1414 // The first mention of a function argument gets the FunctionBeginSym
1415 // label, so arguments are visible when breaking at function entry.
1416 DIVariable DV(Var);
1417 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1418 DISubprogram(getDISubprogram(DV.getContext()))
1419 .describes(MF->getFunction()))
1420 LabelsBeforeInsn[MI] = FunctionBeginSym;
1421 } else {
1422 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1423 const MachineInstr *Prev = History.back();
1424 if (Prev->isDebugValue()) {
1425 // Coalesce identical entries at the end of History.
1426 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001427 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher02c1a642012-10-08 20:48:49 +00001428 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001429 << "\t" << *Prev
Devang Patel79862892011-07-07 00:14:27 +00001430 << "\t" << *History[History.size() - 2] << "\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
1434 // Terminate old register assignments that don't reach MI;
1435 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1436 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1437 isDbgValueInDefinedReg(Prev)) {
1438 // Previous register assignment needs to terminate at the end of
1439 // its basic block.
1440 MachineBasicBlock::const_iterator LastMI =
1441 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001442 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001443 // Drop DBG_VALUE for empty range.
Eric Christopher02c1a642012-10-08 20:48:49 +00001444 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Devang Patel79862892011-07-07 00:14:27 +00001445 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001446 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001447 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001448 else {
1449 // Terminate after LastMI.
1450 History.push_back(LastMI);
1451 }
1452 }
1453 }
1454 }
1455 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001456 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001457 // Not a DBG_VALUE instruction.
1458 if (!MI->isLabel())
1459 AtBlockEntry = false;
1460
Eric Christopher0313ced2012-10-04 20:46:14 +00001461 // First known non-DBG_VALUE and non-frame setup location marks
1462 // the beginning of the function body.
1463 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1464 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel4243e672011-05-11 19:22:19 +00001465 PrologEndLoc = MI->getDebugLoc();
1466
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001467 // Check if the instruction clobbers any registers with debug vars.
1468 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1469 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1470 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1471 continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001472 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1473 AI.isValid(); ++AI) {
1474 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001475 const MDNode *Var = LiveUserVar[Reg];
1476 if (!Var)
1477 continue;
1478 // Reg is now clobbered.
1479 LiveUserVar[Reg] = 0;
1480
1481 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001482 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1483 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001484 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001485 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1486 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001487 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001488 const MachineInstr *Prev = History.back();
1489 // Sanity-check: Register assignments are terminated at the end of
1490 // their block.
1491 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1492 continue;
1493 // Is the variable still in Reg?
1494 if (!isDbgValueInDefinedReg(Prev) ||
1495 Prev->getOperand(0).getReg() != Reg)
1496 continue;
1497 // Var is clobbered. Make sure the next instruction gets a label.
1498 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001499 }
1500 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001501 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001502 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001503 }
1504
1505 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1506 I != E; ++I) {
1507 SmallVectorImpl<const MachineInstr*> &History = I->second;
1508 if (History.empty())
1509 continue;
1510
1511 // Make sure the final register assignments are terminated.
1512 const MachineInstr *Prev = History.back();
1513 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1514 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001515 MachineBasicBlock::const_iterator LastMI =
Devang Patel5bc942c2011-08-10 23:58:09 +00001516 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001517 if (LastMI == PrevMBB->end())
1518 // Drop DBG_VALUE for empty range.
1519 History.pop_back();
1520 else {
1521 // Terminate after LastMI.
1522 History.push_back(LastMI);
1523 }
1524 }
1525 // Request labels for the full history.
1526 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1527 const MachineInstr *MI = History[i];
1528 if (MI->isDebugValue())
1529 requestLabelBeforeInsn(MI);
1530 else
1531 requestLabelAfterInsn(MI);
1532 }
1533 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001534
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001535 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001536 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001537
1538 // Record beginning of function.
1539 if (!PrologEndLoc.isUnknown()) {
1540 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1541 MF->getFunction()->getContext());
1542 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1543 FnStartDL.getScope(MF->getFunction()->getContext()),
David Blaikie836cfc42012-12-04 22:02:33 +00001544 // We'd like to list the prologue as "not statements" but GDB behaves
1545 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
David Blaikieb36c5312012-12-04 21:05:36 +00001546 DWARF2_FLAG_IS_STMT);
Devang Patel4243e672011-05-11 19:22:19 +00001547 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001548}
1549
Devang Patelbf47fdb2011-08-10 20:55:27 +00001550void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1551// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1552 ScopeVariables[LS].push_back(Var);
1553// Vars.push_back(Var);
1554}
1555
Eric Christopherb6dc8652012-11-27 22:43:45 +00001556// Gather and emit post-function debug information.
Chris Lattnereec791a2010-01-26 23:18:02 +00001557void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001558 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001559
Devang Patelbf47fdb2011-08-10 20:55:27 +00001560 // Define end label for subprogram.
1561 FunctionEndSym = Asm->GetTempSymbol("func_end",
1562 Asm->getFunctionNumber());
1563 // Assumes in correct section after the entry point.
1564 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001565
Devang Patelbf47fdb2011-08-10 20:55:27 +00001566 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1567 collectVariableInfo(MF, ProcessedVars);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001568
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001569 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001570 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001571 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001572
Devang Patelbf47fdb2011-08-10 20:55:27 +00001573 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001574 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1575 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1576 LexicalScope *AScope = AList[i];
1577 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001578 if (SP.Verify()) {
1579 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001580 DIArray Variables = SP.getVariables();
1581 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1582 DIVariable DV(Variables.getElement(i));
1583 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1584 continue;
Alexey Samsonovb67bd332012-07-06 08:45:08 +00001585 // Check that DbgVariable for DV wasn't created earlier, when
1586 // findAbstractVariable() was called for inlined instance of DV.
1587 LLVMContext &Ctx = DV->getContext();
1588 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1589 if (AbstractVariables.lookup(CleanDV))
1590 continue;
Devang Patel93d39be2011-08-19 23:28:12 +00001591 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1592 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001593 }
1594 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001595 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001596 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001597 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001598
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001599 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001600
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001601 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001602 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001603
Devang Patelbf47fdb2011-08-10 20:55:27 +00001604 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1605 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001606
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001607 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001608 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1609 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1610 DeleteContainerPointers(I->second);
1611 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001612 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001613 UserVariables.clear();
1614 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001615 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001616 LabelsBeforeInsn.clear();
1617 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001618 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001619}
1620
Eric Christopherb6dc8652012-11-27 22:43:45 +00001621// Register a source line with debug info. Returns the unique label that was
1622// emitted and which provides correspondence to the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001623void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1624 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001625 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001626 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001627 unsigned Src = 1;
1628 if (S) {
1629 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001630
Dan Gohman1cc0d622010-05-05 23:41:32 +00001631 if (Scope.isCompileUnit()) {
1632 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001633 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001634 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001635 } else if (Scope.isFile()) {
1636 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001637 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001638 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001639 } else if (Scope.isSubprogram()) {
1640 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001641 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001642 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001643 } else if (Scope.isLexicalBlockFile()) {
1644 DILexicalBlockFile DBF(S);
1645 Fn = DBF.getFilename();
1646 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001647 } else if (Scope.isLexicalBlock()) {
1648 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001649 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001650 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001651 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001652 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001653
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001654 Src = getOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001655 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001656 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001657}
1658
Bill Wendling829e67b2009-05-20 23:22:40 +00001659//===----------------------------------------------------------------------===//
1660// Emit Methods
1661//===----------------------------------------------------------------------===//
1662
Eric Christopherb6dc8652012-11-27 22:43:45 +00001663// Compute the size and offset of a DIE.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001664unsigned
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001665DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001666 // Get the children.
1667 const std::vector<DIE *> &Children = Die->getChildren();
1668
Bill Wendling94d04b82009-05-20 23:21:38 +00001669 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001670 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001671
1672 // Get the abbreviation for this DIE.
1673 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001674 const DIEAbbrev *Abbrev = Abbreviations->at(AbbrevNumber - 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001675
1676 // Set DIE offset
1677 Die->setOffset(Offset);
1678
1679 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001680 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001681
1682 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1683 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1684
1685 // Size the DIE attribute values.
1686 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1687 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001688 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001689
1690 // Size the DIE children if any.
1691 if (!Children.empty()) {
1692 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1693 "Children flag not set");
1694
1695 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopherfbd19752012-11-20 22:14:13 +00001696 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling94d04b82009-05-20 23:21:38 +00001697
1698 // End of children marker.
1699 Offset += sizeof(int8_t);
1700 }
1701
1702 Die->setSize(Offset - Die->getOffset());
1703 return Offset;
1704}
1705
Eric Christopherb6dc8652012-11-27 22:43:45 +00001706// Compute the size and offset of all the DIEs.
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001707void DwarfUnits::computeSizeAndOffsets() {
1708 for (SmallVector<CompileUnit *, 1>::iterator I = CUs.begin(),
1709 E = CUs.end(); I != E; ++I) {
Eric Christopher98e237f2012-11-30 23:59:06 +00001710 unsigned Offset =
1711 sizeof(int32_t) + // Length of Compilation Unit Info
1712 sizeof(int16_t) + // DWARF version number
1713 sizeof(int32_t) + // Offset Into Abbrev. Section
1714 sizeof(int8_t); // Pointer Size (in bytes)
1715
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001716 computeSizeAndOffset((*I)->getCUDie(), Offset);
Devang Patel163a9f72010-05-10 22:49:55 +00001717 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001718}
1719
Eric Christopherb6dc8652012-11-27 22:43:45 +00001720// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001721void DwarfDebug::emitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001722 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001723
Bill Wendling94d04b82009-05-20 23:21:38 +00001724 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001725 DwarfInfoSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001726 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001727 DwarfAbbrevSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001728 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher6eebe472012-12-19 22:02:53 +00001729 if (useSplitDwarf())
1730 DwarfAbbrevDWOSectionSym =
1731 emitSectionSym(Asm, TLOF.getDwarfAbbrevDWOSection(),
1732 "section_abbrev_dwo");
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001733 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001734
Chris Lattner9c69e285532010-04-04 22:59:04 +00001735 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001736 emitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001737
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001738 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
1739 emitSectionSym(Asm, TLOF.getDwarfLocSection());
1740 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001741 DwarfStrSectionSym =
Eric Christopher64f824c2012-12-27 02:14:01 +00001742 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
1743 if (useSplitDwarf())
1744 DwarfStrDWOSectionSym =
1745 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001746 DwarfDebugRangeSectionSym = emitSectionSym(Asm, TLOF.getDwarfRangesSection(),
Devang Patelf2548ca2010-04-16 23:33:45 +00001747 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001748
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001749 DwarfDebugLocSectionSym = emitSectionSym(Asm, TLOF.getDwarfLocSection(),
Devang Patelc3f5f782010-05-25 23:40:22 +00001750 "section_debug_loc");
1751
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001752 TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
1753 emitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001754}
1755
Eric Christopherb6dc8652012-11-27 22:43:45 +00001756// Recursively emits a debug information entry.
Eric Christopher6eebe472012-12-19 22:02:53 +00001757void DwarfDebug::emitDIE(DIE *Die, std::vector<DIEAbbrev *> *Abbrevs) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001758 // Get the abbreviation for this DIE.
1759 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher6eebe472012-12-19 22:02:53 +00001760 const DIEAbbrev *Abbrev = Abbrevs->at(AbbrevNumber - 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001761
Bill Wendling94d04b82009-05-20 23:21:38 +00001762 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001763 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001764 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1765 Twine::utohexstr(Die->getOffset()) + ":0x" +
1766 Twine::utohexstr(Die->getSize()) + " " +
1767 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001768 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001769
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001770 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001771 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1772
1773 // Emit the DIE attribute values.
1774 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1775 unsigned Attr = AbbrevData[i].getAttribute();
1776 unsigned Form = AbbrevData[i].getForm();
1777 assert(Form && "Too many attributes for DIE (check abbreviation)");
1778
Chris Lattner3f53c832010-04-04 18:52:31 +00001779 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001780 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001781
Bill Wendling94d04b82009-05-20 23:21:38 +00001782 switch (Attr) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001783 case dwarf::DW_AT_abstract_origin: {
1784 DIEEntry *E = cast<DIEEntry>(Values[i]);
1785 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001786 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001787 Asm->EmitInt32(Addr);
1788 break;
1789 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001790 case dwarf::DW_AT_ranges: {
1791 // DW_AT_range Value encodes offset in debug_range section.
1792 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001793
Nick Lewyckyffccd922012-06-22 01:25:12 +00001794 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001795 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1796 V->getValue(),
1797 4);
1798 } else {
1799 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1800 V->getValue(),
1801 DwarfDebugRangeSectionSym,
1802 4);
1803 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001804 break;
1805 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001806 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-06-22 01:25:12 +00001807 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1808 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1809 Asm->EmitLabelReference(L->getValue(), 4);
1810 else
1811 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1812 } else {
Devang Patelc3f5f782010-05-25 23:40:22 +00001813 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00001814 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001815 break;
1816 }
Devang Patel2a361602010-09-29 19:08:08 +00001817 case dwarf::DW_AT_accessibility: {
1818 if (Asm->isVerbose()) {
1819 DIEInteger *V = cast<DIEInteger>(Values[i]);
1820 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1821 }
1822 Values[i]->EmitValue(Asm, Form);
1823 break;
1824 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001825 default:
1826 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001827 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001828 break;
1829 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001830 }
1831
1832 // Emit the DIE children if any.
1833 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1834 const std::vector<DIE *> &Children = Die->getChildren();
1835
1836 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher6eebe472012-12-19 22:02:53 +00001837 emitDIE(Children[j], Abbrevs);
Bill Wendling94d04b82009-05-20 23:21:38 +00001838
Chris Lattner3f53c832010-04-04 18:52:31 +00001839 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001840 Asm->OutStreamer.AddComment("End Of Children Mark");
1841 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001842 }
1843}
1844
Eric Christopherb1e66d02012-12-15 00:04:07 +00001845// Emit the various dwarf units to the unit section USection with
1846// the abbreviations going into ASection.
1847void DwarfUnits::emitUnits(DwarfDebug *DD,
1848 const MCSection *USection,
1849 const MCSection *ASection,
1850 const MCSymbol *ASectionSym) {
1851 Asm->OutStreamer.SwitchSection(USection);
1852 for (SmallVector<CompileUnit *, 1>::iterator I = CUs.begin(),
1853 E = CUs.end(); I != E; ++I) {
1854 CompileUnit *TheCU = *I;
Devang Patel163a9f72010-05-10 22:49:55 +00001855 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001856
Devang Patel163a9f72010-05-10 22:49:55 +00001857 // Emit the compile units header.
Eric Christopherb1e66d02012-12-15 00:04:07 +00001858 Asm->OutStreamer
1859 .EmitLabel(Asm->GetTempSymbol(USection->getLabelBeginName(),
1860 TheCU->getUniqueID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001861
Devang Patel163a9f72010-05-10 22:49:55 +00001862 // Emit size of content not including length itself
1863 unsigned ContentSize = Die->getSize() +
1864 sizeof(int16_t) + // DWARF version number
1865 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001866 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001867
Devang Patel163a9f72010-05-10 22:49:55 +00001868 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1869 Asm->EmitInt32(ContentSize);
1870 Asm->OutStreamer.AddComment("DWARF version number");
1871 Asm->EmitInt16(dwarf::DWARF_VERSION);
1872 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christopherb1e66d02012-12-15 00:04:07 +00001873 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
1874 ASectionSym);
Devang Patel163a9f72010-05-10 22:49:55 +00001875 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001876 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001877
Eric Christopher6eebe472012-12-19 22:02:53 +00001878 DD->emitDIE(Die, Abbreviations);
Eric Christopherb1e66d02012-12-15 00:04:07 +00001879 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(USection->getLabelEndName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00001880 TheCU->getUniqueID()));
Devang Patel163a9f72010-05-10 22:49:55 +00001881 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001882}
1883
Eric Christopher98e237f2012-11-30 23:59:06 +00001884// Emit the debug info section.
1885void DwarfDebug::emitDebugInfo() {
Eric Christopherb1e66d02012-12-15 00:04:07 +00001886 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1887
1888 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
1889 Asm->getObjFileLowering().getDwarfAbbrevSection(),
1890 DwarfAbbrevSectionSym);
Eric Christopher98e237f2012-11-30 23:59:06 +00001891}
1892
Eric Christopherb6dc8652012-11-27 22:43:45 +00001893// Emit the abbreviation section.
Eric Christopher7dc68db2012-11-20 23:30:11 +00001894void DwarfDebug::emitAbbreviations() {
Eric Christopher6eebe472012-12-19 22:02:53 +00001895 if (!useSplitDwarf())
1896 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
1897 &Abbreviations);
1898 else
1899 emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
1900}
Bill Wendling94d04b82009-05-20 23:21:38 +00001901
Eric Christopher6eebe472012-12-19 22:02:53 +00001902void DwarfDebug::emitAbbrevs(const MCSection *Section,
1903 std::vector<DIEAbbrev *> *Abbrevs) {
1904 // Check to see if it is worth the effort.
1905 if (!Abbrevs->empty()) {
1906 // Start the debug abbrev section.
1907 Asm->OutStreamer.SwitchSection(Section);
1908
1909 MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Eric Christopher44fedba2012-12-13 03:00:38 +00001910 Asm->OutStreamer.EmitLabel(Begin);
Bill Wendling94d04b82009-05-20 23:21:38 +00001911
1912 // For each abbrevation.
Eric Christopher6eebe472012-12-19 22:02:53 +00001913 for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001914 // Get abbreviation data
Eric Christopher6eebe472012-12-19 22:02:53 +00001915 const DIEAbbrev *Abbrev = Abbrevs->at(i);
Bill Wendling94d04b82009-05-20 23:21:38 +00001916
1917 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001918 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001919
1920 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001921 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001922 }
1923
1924 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001925 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001926
Eric Christopher6eebe472012-12-19 22:02:53 +00001927 MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Eric Christopher44fedba2012-12-13 03:00:38 +00001928 Asm->OutStreamer.EmitLabel(End);
Bill Wendling94d04b82009-05-20 23:21:38 +00001929 }
1930}
1931
Eric Christopherb6dc8652012-11-27 22:43:45 +00001932// Emit the last address of the section and the end of the line matrix.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001933void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001934 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001935 Asm->OutStreamer.AddComment("Extended Op");
1936 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001937
Chris Lattner233f52b2010-03-09 23:52:58 +00001938 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001939 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001940 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1941 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1942
1943 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001944
Chris Lattnerc0215722010-04-04 19:25:43 +00001945 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001946 Asm->getDataLayout().getPointerSize(),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001947 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001948
1949 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001950 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1951 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001952 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001953 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001954}
1955
Eric Christopherb6dc8652012-11-27 22:43:45 +00001956// Emit visible names into a hashed accelerator table section.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001957void DwarfDebug::emitAccelNames() {
1958 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1959 dwarf::DW_FORM_data4));
1960 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1961 E = CUMap.end(); I != E; ++I) {
1962 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001963 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1964 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001965 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1966 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001967 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001968 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1969 DE = Entities.end(); DI != DE; ++DI)
1970 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001971 }
1972 }
1973
1974 AT.FinalizeTable(Asm, "Names");
1975 Asm->OutStreamer.SwitchSection(
1976 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1977 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1978 Asm->OutStreamer.EmitLabel(SectionBegin);
1979
1980 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00001981 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001982}
1983
Eric Christopher72c16552012-12-20 21:58:40 +00001984// Emit objective C classes and categories into a hashed accelerator table
1985// section.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001986void DwarfDebug::emitAccelObjC() {
1987 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1988 dwarf::DW_FORM_data4));
1989 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1990 E = CUMap.end(); I != E; ++I) {
1991 CompileUnit *TheCU = I->second;
1992 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1993 for (StringMap<std::vector<DIE*> >::const_iterator
1994 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1995 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001996 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-11-07 09:24:32 +00001997 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1998 DE = Entities.end(); DI != DE; ++DI)
1999 AT.AddName(Name, (*DI));
2000 }
2001 }
2002
2003 AT.FinalizeTable(Asm, "ObjC");
2004 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2005 .getDwarfAccelObjCSection());
2006 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2007 Asm->OutStreamer.EmitLabel(SectionBegin);
2008
2009 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002010 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002011}
2012
Eric Christopherb6dc8652012-11-27 22:43:45 +00002013// Emit namespace dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00002014void DwarfDebug::emitAccelNamespaces() {
2015 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2016 dwarf::DW_FORM_data4));
2017 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2018 E = CUMap.end(); I != E; ++I) {
2019 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00002020 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
2021 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00002022 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2023 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00002024 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00002025 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2026 DE = Entities.end(); DI != DE; ++DI)
2027 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00002028 }
2029 }
2030
2031 AT.FinalizeTable(Asm, "namespac");
2032 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2033 .getDwarfAccelNamespaceSection());
2034 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2035 Asm->OutStreamer.EmitLabel(SectionBegin);
2036
2037 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002038 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002039}
2040
Eric Christopherb6dc8652012-11-27 22:43:45 +00002041// Emit type dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00002042void DwarfDebug::emitAccelTypes() {
Eric Christopherc36145f2012-01-06 04:35:23 +00002043 std::vector<DwarfAccelTable::Atom> Atoms;
2044 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2045 dwarf::DW_FORM_data4));
2046 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
2047 dwarf::DW_FORM_data2));
2048 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
2049 dwarf::DW_FORM_data1));
2050 DwarfAccelTable AT(Atoms);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002051 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2052 E = CUMap.end(); I != E; ++I) {
2053 CompileUnit *TheCU = I->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00002054 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
2055 = TheCU->getAccelTypes();
2056 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00002057 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2058 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00002059 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00002060 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
2061 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
2062 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002063 }
2064 }
2065
2066 AT.FinalizeTable(Asm, "types");
2067 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2068 .getDwarfAccelTypesSection());
2069 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2070 Asm->OutStreamer.EmitLabel(SectionBegin);
2071
2072 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002073 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002074}
2075
Devang Patel193f7202009-11-24 01:14:22 +00002076void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00002077 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2078 E = CUMap.end(); I != E; ++I) {
2079 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00002080 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-05-10 22:49:55 +00002081 Asm->OutStreamer.SwitchSection(
2082 Asm->getObjFileLowering().getDwarfPubTypesSection());
2083 Asm->OutStreamer.AddComment("Length of Public Types Info");
2084 Asm->EmitLabelDifference(
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002085 Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()),
2086 Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002087
Devang Patel163a9f72010-05-10 22:49:55 +00002088 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002089 TheCU->getUniqueID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002090
Devang Patel163a9f72010-05-10 22:49:55 +00002091 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2092 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002093
Devang Patel163a9f72010-05-10 22:49:55 +00002094 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002095 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2096 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002097 TheCU->getUniqueID()),
Devang Patel163a9f72010-05-10 22:49:55 +00002098 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002099
Devang Patel163a9f72010-05-10 22:49:55 +00002100 Asm->OutStreamer.AddComment("Compilation Unit Length");
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002101 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002102 TheCU->getUniqueID()),
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002103 Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002104 TheCU->getUniqueID()),
Devang Patel163a9f72010-05-10 22:49:55 +00002105 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002106
Devang Patel163a9f72010-05-10 22:49:55 +00002107 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2108 for (StringMap<DIE*>::const_iterator
2109 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2110 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00002111 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002112
Devang Patel163a9f72010-05-10 22:49:55 +00002113 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2114 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002115
Devang Patel163a9f72010-05-10 22:49:55 +00002116 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00002117 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00002118 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
2119 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002120
Devang Patel163a9f72010-05-10 22:49:55 +00002121 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002122 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00002123 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002124 TheCU->getUniqueID()));
Devang Patel193f7202009-11-24 01:14:22 +00002125 }
Devang Patel193f7202009-11-24 01:14:22 +00002126}
2127
Eric Christopher64f824c2012-12-27 02:14:01 +00002128// Emit strings into a string section.
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002129void DwarfUnits::emitStrings(const MCSection *StrSection,
2130 const MCSection *OffsetSection = NULL,
2131 const MCSymbol *StrSecSym = NULL) {
Eric Christopher64f824c2012-12-27 02:14:01 +00002132
2133 if (StringPool->empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002134
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002135 // Start the dwarf str section.
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002136 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling94d04b82009-05-20 23:21:38 +00002137
Chris Lattnerbc733f52010-03-13 02:17:42 +00002138 // Get all of the string pool entries and put them in an array by their ID so
2139 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002140 SmallVector<std::pair<unsigned,
Eric Christopherff348452013-01-07 22:40:45 +00002141 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002142
Chris Lattnerbc733f52010-03-13 02:17:42 +00002143 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
Eric Christopher64f824c2012-12-27 02:14:01 +00002144 I = StringPool->begin(), E = StringPool->end();
Eric Christopher72c16552012-12-20 21:58:40 +00002145 I != E; ++I)
Chris Lattnerbc733f52010-03-13 02:17:42 +00002146 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002147
Chris Lattnerbc733f52010-03-13 02:17:42 +00002148 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002149
Chris Lattnerbc733f52010-03-13 02:17:42 +00002150 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002151 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002152 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002153
Benjamin Kramer983c4572011-11-09 18:16:11 +00002154 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00002155 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
2156 Entries[i].second->getKeyLength()+1),
2157 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002158 }
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002159
2160 // If we've got an offset section go ahead and emit that now as well.
2161 if (OffsetSection) {
2162 Asm->OutStreamer.SwitchSection(OffsetSection);
2163 unsigned offset = 0;
2164 unsigned size = 4;
2165 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
2166 Asm->OutStreamer.EmitIntValue(offset, size, 0);
2167 offset += Entries[i].second->getKeyLength() + 1;
2168 }
2169 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002170}
2171
Eric Christopher64f824c2012-12-27 02:14:01 +00002172// Emit visible names into a debug str section.
2173void DwarfDebug::emitDebugStr() {
2174 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2175 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2176}
2177
Eric Christopherb6dc8652012-11-27 22:43:45 +00002178// Emit visible names into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002179void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002180 if (DotDebugLocEntries.empty())
2181 return;
2182
Devang Patel6c3ea902011-02-04 22:57:18 +00002183 for (SmallVector<DotDebugLocEntry, 4>::iterator
2184 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2185 I != E; ++I) {
2186 DotDebugLocEntry &Entry = *I;
2187 if (I + 1 != DotDebugLocEntries.end())
2188 Entry.Merge(I+1);
2189 }
2190
Daniel Dunbar83320a02011-03-16 22:16:39 +00002191 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002192 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002193 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002194 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002195 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2196 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002197 for (SmallVector<DotDebugLocEntry, 4>::iterator
2198 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002199 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002200 DotDebugLocEntry &Entry = *I;
2201 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002202 if (Entry.isEmpty()) {
2203 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2204 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002205 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002206 } else {
2207 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2208 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002209 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002210 Asm->OutStreamer.AddComment("Loc expr size");
2211 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2212 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2213 Asm->EmitLabelDifference(end, begin, 2);
2214 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002215 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002216 DIBasicType BTy(DV.getType());
2217 if (BTy.Verify() &&
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002218 (BTy.getEncoding() == dwarf::DW_ATE_signed
Devang Patelc4329072011-06-01 22:03:25 +00002219 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2220 Asm->OutStreamer.AddComment("DW_OP_consts");
2221 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002222 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002223 } else {
2224 Asm->OutStreamer.AddComment("DW_OP_constu");
2225 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002226 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002227 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002228 } else if (Entry.isLocation()) {
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002229 if (!DV.hasComplexAddress())
Devang Patel80efd4e2011-07-08 16:49:43 +00002230 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002231 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002232 else {
2233 // Complex address entry.
2234 unsigned N = DV.getNumAddrElements();
2235 unsigned i = 0;
2236 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2237 if (Entry.Loc.getOffset()) {
2238 i = 2;
2239 Asm->EmitDwarfRegOp(Entry.Loc);
2240 Asm->OutStreamer.AddComment("DW_OP_deref");
2241 Asm->EmitInt8(dwarf::DW_OP_deref);
2242 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2243 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2244 Asm->EmitSLEB128(DV.getAddrElement(1));
2245 } else {
2246 // If first address element is OpPlus then emit
2247 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2248 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2249 Asm->EmitDwarfRegOp(Loc);
2250 i = 2;
2251 }
2252 } else {
2253 Asm->EmitDwarfRegOp(Entry.Loc);
2254 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002255
Devang Patel80efd4e2011-07-08 16:49:43 +00002256 // Emit remaining complex address elements.
2257 for (; i < N; ++i) {
2258 uint64_t Element = DV.getAddrElement(i);
2259 if (Element == DIBuilder::OpPlus) {
2260 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2261 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher50120762012-05-08 18:56:00 +00002262 } else if (Element == DIBuilder::OpDeref) {
Eric Christophera80f2d12012-05-08 21:24:39 +00002263 if (!Entry.Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002264 Asm->EmitInt8(dwarf::DW_OP_deref);
2265 } else
2266 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002267 }
Devang Patelc26f5442011-04-28 02:22:40 +00002268 }
Devang Patelc26f5442011-04-28 02:22:40 +00002269 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002270 // else ... ignore constant fp. There is not any good way to
2271 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002272 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002273 }
2274 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002275}
2276
Eric Christopherb6dc8652012-11-27 22:43:45 +00002277// Emit visible names into a debug aranges section.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00002278void DwarfDebug::emitDebugARanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002279 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002280 Asm->OutStreamer.SwitchSection(
2281 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002282}
2283
Eric Christopherb6dc8652012-11-27 22:43:45 +00002284// Emit visible names into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002285void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002286 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002287 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002288 Asm->getObjFileLowering().getDwarfRangesSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002289 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Pateleac9c072010-04-27 19:46:33 +00002290 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002291 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002292 I != E; ++I) {
2293 if (*I)
2294 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002295 else
Devang Pateleac9c072010-04-27 19:46:33 +00002296 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002297 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002298}
2299
Eric Christopherb6dc8652012-11-27 22:43:45 +00002300// Emit visible names into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002301void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002302 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002303 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002304 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002305 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002306 }
2307}
2308
Eric Christopherb6dc8652012-11-27 22:43:45 +00002309// Emit inline info using following format.
2310// Section Header:
2311// 1. length of section
2312// 2. Dwarf version number
2313// 3. address size.
2314//
2315// Entries (one "entry" for each function that was inlined):
2316//
2317// 1. offset into __debug_str section for MIPS linkage name, if exists;
2318// otherwise offset into __debug_str for regular function name.
2319// 2. offset into __debug_str section for regular function name.
2320// 3. an unsigned LEB128 number indicating the number of distinct inlining
2321// instances for the function.
2322//
2323// The rest of the entry consists of a {die_offset, low_pc} pair for each
2324// inlined instance; the die_offset points to the inlined_subroutine die in the
2325// __debug_info section, and the low_pc is the starting address for the
2326// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002327void DwarfDebug::emitDebugInlineInfo() {
Eric Christopherb83e2bb2012-03-02 02:11:47 +00002328 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002329 return;
2330
Devang Patel163a9f72010-05-10 22:49:55 +00002331 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002332 return;
2333
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002334 Asm->OutStreamer.SwitchSection(
2335 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002336
Chris Lattner233f52b2010-03-09 23:52:58 +00002337 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002338 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2339 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002340
Chris Lattnerc0215722010-04-04 19:25:43 +00002341 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002342
Chris Lattner233f52b2010-03-09 23:52:58 +00002343 Asm->OutStreamer.AddComment("Dwarf Version");
2344 Asm->EmitInt16(dwarf::DWARF_VERSION);
2345 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002346 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002347
Devang Patele9f8f5e2010-05-07 20:54:48 +00002348 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002349 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002350
Devang Patele9f8f5e2010-05-07 20:54:48 +00002351 const MDNode *Node = *I;
2352 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002353 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002354 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002355 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002356 StringRef LName = SP.getLinkageName();
2357 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002358
Chris Lattner233f52b2010-03-09 23:52:58 +00002359 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher50e26612012-03-02 01:57:52 +00002360 if (LName.empty())
Eric Christopher2e5d8702012-12-20 21:58:36 +00002361 Asm->EmitSectionOffset(InfoHolder.getStringPoolEntry(Name),
2362 DwarfStrSectionSym);
Eric Christopher50e26612012-03-02 01:57:52 +00002363 else
Eric Christopher2e5d8702012-12-20 21:58:36 +00002364 Asm->EmitSectionOffset(InfoHolder
2365 .getStringPoolEntry(getRealLinkageName(LName)),
Chris Lattner6189ed12010-04-04 23:25:33 +00002366 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002367
Chris Lattner233f52b2010-03-09 23:52:58 +00002368 Asm->OutStreamer.AddComment("Function name");
Eric Christopher2e5d8702012-12-20 21:58:36 +00002369 Asm->EmitSectionOffset(InfoHolder.getStringPoolEntry(Name),
2370 DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002371 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002372
Devang Patel53bb5c92009-11-10 23:06:00 +00002373 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002374 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002375 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002376 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002377
Chris Lattner3f53c832010-04-04 18:52:31 +00002378 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002379 Asm->OutStreamer.EmitSymbolValue(LI->first,
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002380 Asm->getDataLayout().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002381 }
2382 }
2383
Chris Lattnerc0215722010-04-04 19:25:43 +00002384 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002385}
Eric Christopher98e237f2012-11-30 23:59:06 +00002386
Eric Christopher0b944ee2012-12-11 19:42:09 +00002387// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher98e237f2012-11-30 23:59:06 +00002388
2389// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2390// DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
2391// DW_AT_ranges_base, DW_AT_addr_base. If DW_AT_ranges is present,
2392// DW_AT_low_pc and DW_AT_high_pc are not used, and vice versa.
Eric Christopher4daaed12012-12-10 19:51:21 +00002393CompileUnit *DwarfDebug::constructSkeletonCU(const MDNode *N) {
Eric Christopher98e237f2012-11-30 23:59:06 +00002394 DICompileUnit DIUnit(N);
2395 StringRef FN = DIUnit.getFilename();
2396 CompilationDir = DIUnit.getDirectory();
Eric Christopher98e237f2012-11-30 23:59:06 +00002397
2398 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002399 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
Eric Christopher2e5d8702012-12-20 21:58:36 +00002400 DIUnit.getLanguage(), Die, Asm,
Eric Christopher64f824c2012-12-27 02:14:01 +00002401 this, &SkeletonHolder);
Eric Christopher98e237f2012-11-30 23:59:06 +00002402 // FIXME: This should be the .dwo file.
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002403 NewCU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name, FN);
Eric Christopher98e237f2012-11-30 23:59:06 +00002404
2405 // FIXME: We also need DW_AT_addr_base and DW_AT_dwo_id.
2406
2407 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
2408 // into an entity.
2409 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
2410 // DW_AT_stmt_list is a offset of line number information for this
2411 // compile unit in debug_line section.
2412 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2413 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
2414 Asm->GetTempSymbol("section_line"));
2415 else
2416 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
2417
2418 if (!CompilationDir.empty())
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002419 NewCU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christopher98e237f2012-11-30 23:59:06 +00002420
Eric Christopher0e3e9b72012-12-10 23:34:43 +00002421 SkeletonHolder.addUnit(NewCU);
2422
Eric Christopher98e237f2012-11-30 23:59:06 +00002423 return NewCU;
2424}
2425
Eric Christopher4daaed12012-12-10 19:51:21 +00002426void DwarfDebug::emitSkeletonCU(const MCSection *Section) {
Eric Christopher98e237f2012-11-30 23:59:06 +00002427 Asm->OutStreamer.SwitchSection(Section);
Eric Christopher4daaed12012-12-10 19:51:21 +00002428 DIE *Die = SkeletonCU->getCUDie();
Eric Christopher98e237f2012-11-30 23:59:06 +00002429
2430 // Emit the compile units header.
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002431 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(Section->getLabelBeginName(),
Eric Christopher4daaed12012-12-10 19:51:21 +00002432 SkeletonCU->getUniqueID()));
Eric Christopher98e237f2012-11-30 23:59:06 +00002433
2434 // Emit size of content not including length itself
2435 unsigned ContentSize = Die->getSize() +
2436 sizeof(int16_t) + // DWARF version number
2437 sizeof(int32_t) + // Offset Into Abbrev. Section
2438 sizeof(int8_t); // Pointer Size (in bytes)
2439
2440 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2441 Asm->EmitInt32(ContentSize);
2442 Asm->OutStreamer.AddComment("DWARF version number");
2443 Asm->EmitInt16(dwarf::DWARF_VERSION);
2444 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christopher6eebe472012-12-19 22:02:53 +00002445
2446 const MCSection *ASec = Asm->getObjFileLowering().getDwarfAbbrevSection();
2447 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASec->getLabelBeginName()),
Eric Christopher98e237f2012-11-30 23:59:06 +00002448 DwarfAbbrevSectionSym);
2449 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2450 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2451
Eric Christopher6eebe472012-12-19 22:02:53 +00002452 emitDIE(Die, &SkeletonAbbrevs);
Eric Christophercf6b8ad2012-12-15 00:04:04 +00002453 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(Section->getLabelEndName(),
Eric Christopher4daaed12012-12-10 19:51:21 +00002454 SkeletonCU->getUniqueID()));
Eric Christopher6eebe472012-12-19 22:02:53 +00002455}
Eric Christopher98e237f2012-11-30 23:59:06 +00002456
Eric Christopher6eebe472012-12-19 22:02:53 +00002457void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
2458 assert(useSplitDwarf() && "No split dwarf debug info?");
2459 emitAbbrevs(Section, &SkeletonAbbrevs);
Eric Christopher98e237f2012-11-30 23:59:06 +00002460}
2461
Eric Christopher0b944ee2012-12-11 19:42:09 +00002462// Emit the .debug_info.dwo section for separated dwarf. This contains the
2463// compile units that would normally be in debug_info.
Eric Christopher98e237f2012-11-30 23:59:06 +00002464void DwarfDebug::emitDebugInfoDWO() {
Eric Christopher4daaed12012-12-10 19:51:21 +00002465 assert(useSplitDwarf() && "No split dwarf debug info?");
Eric Christopherb1e66d02012-12-15 00:04:07 +00002466 InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Eric Christopher6eebe472012-12-19 22:02:53 +00002467 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2468 DwarfAbbrevDWOSectionSym);
2469}
2470
2471// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2472// abbreviations for the .debug_info.dwo section.
2473void DwarfDebug::emitDebugAbbrevDWO() {
2474 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopher72c16552012-12-20 21:58:40 +00002475 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2476 &Abbreviations);
Eric Christopher98e237f2012-11-30 23:59:06 +00002477}
Eric Christopher64f824c2012-12-27 02:14:01 +00002478
2479// Emit the .debug_str.dwo section for separated dwarf. This contains the
2480// string section and is identical in format to traditional .debug_str
2481// sections.
2482void DwarfDebug::emitDebugStrDWO() {
2483 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherff348452013-01-07 22:40:45 +00002484 const MCSection *OffSec = Asm->getObjFileLowering()
2485 .getDwarfStrOffDWOSection();
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002486 const MCSymbol *StrSym = DwarfStrSectionSym;
2487 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
2488 OffSec, StrSym);
Eric Christopher64f824c2012-12-27 02:14:01 +00002489}