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