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