blob: afe6901f1da5194c7e982e685c7d6edfdaba7eef [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 Christopher27614582013-01-08 22:22:06 +0000158 SourceIdMap(DIEValueAllocator),
Eric Christopherc8a310e2012-12-10 23:34:43 +0000159 PrevLabel(NULL), GlobalCUIndexCount(0),
Eric Christopher27614582013-01-08 22:22:06 +0000160 InfoHolder(A, &AbbreviationsSet, &Abbreviations, "info_string",
161 DIEValueAllocator),
Eric Christopher3c5a1912012-12-19 22:02:53 +0000162 SkeletonCU(0),
163 SkeletonAbbrevSet(InitAbbreviationsSetSize),
Eric Christopher27614582013-01-08 22:22:06 +0000164 SkeletonHolder(A, &SkeletonAbbrevSet, &SkeletonAbbrevs, "skel_string",
165 DIEValueAllocator) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000166
Rafael Espindolaa7160962011-05-06 14:56:22 +0000167 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattnere58b5472010-04-04 23:10:38 +0000168 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000169 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000170 DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000171 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopherad9fe892012-04-02 17:58:52 +0000172
Eric Christopher978fbff2012-08-23 07:10:46 +0000173 // Turn on accelerator tables and older gdb compatibility
174 // for Darwin.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000175 bool IsDarwin = Triple(M->getTargetTriple()).isOSDarwin();
Eric Christopher20b76a72012-08-23 22:36:40 +0000176 if (DarwinGDBCompat == Default) {
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000177 if (IsDarwin)
178 IsDarwinGDBCompat = true;
Eric Christopher20b76a72012-08-23 22:36:40 +0000179 else
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000180 IsDarwinGDBCompat = false;
Eric Christopher20b76a72012-08-23 22:36:40 +0000181 } else
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000182 IsDarwinGDBCompat = DarwinGDBCompat == Enable ? true : false;
Eric Christopher4977f212012-08-23 22:36:36 +0000183
Eric Christopher20b76a72012-08-23 22:36:40 +0000184 if (DwarfAccelTables == Default) {
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000185 if (IsDarwin)
186 HasDwarfAccelTables = true;
Eric Christopher20b76a72012-08-23 22:36:40 +0000187 else
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000188 HasDwarfAccelTables = false;
Eric Christopher20b76a72012-08-23 22:36:40 +0000189 } else
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000190 HasDwarfAccelTables = DwarfAccelTables == Enable ? true : false;
Eric Christopher20b76a72012-08-23 22:36:40 +0000191
Eric Christophercdf218d2012-12-10 19:51:21 +0000192 if (SplitDwarf == Default)
193 HasSplitDwarf = false;
Eric Christopher29424312012-11-12 22:22:20 +0000194 else
Eric Christophercdf218d2012-12-10 19:51:21 +0000195 HasSplitDwarf = SplitDwarf == Enable ? true : false;
Eric Christopher29424312012-11-12 22:22:20 +0000196
Dan Gohman6e681a52010-06-18 15:56:31 +0000197 {
198 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopher58f41952012-11-19 22:42:15 +0000199 beginModule();
Torok Edwinf8dba242010-04-07 10:44:46 +0000200 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000201}
202DwarfDebug::~DwarfDebug() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000203}
204
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000205// Switch to the specified MCSection and emit an assembler
206// temporary label to it if SymbolStem is specified.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000207static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christophera7b61892011-11-07 09:18:38 +0000208 const char *SymbolStem = 0) {
209 Asm->OutStreamer.SwitchSection(Section);
210 if (!SymbolStem) return 0;
211
212 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
213 Asm->OutStreamer.EmitLabel(TmpSym);
214 return TmpSym;
215}
216
Eric Christophere698f532012-12-20 21:58:36 +0000217MCSymbol *DwarfUnits::getStringPoolSym() {
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000218 return Asm->GetTempSymbol(StringPref);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000219}
220
Eric Christophere698f532012-12-20 21:58:36 +0000221MCSymbol *DwarfUnits::getStringPoolEntry(StringRef Str) {
222 std::pair<MCSymbol*, unsigned> &Entry =
Eric Christopher27614582013-01-08 22:22:06 +0000223 StringPool.GetOrCreateValue(Str).getValue();
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000224 if (Entry.first) return Entry.first;
225
226 Entry.second = NextStringPoolNumber++;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000227 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000228}
229
Eric Christopher2cbd5762013-01-07 19:32:41 +0000230unsigned DwarfUnits::getStringPoolIndex(StringRef Str) {
231 std::pair<MCSymbol*, unsigned> &Entry =
Eric Christopher27614582013-01-08 22:22:06 +0000232 StringPool.GetOrCreateValue(Str).getValue();
Eric Christopher2cbd5762013-01-07 19:32:41 +0000233 if (Entry.first) return Entry.second;
234
235 Entry.second = NextStringPoolNumber++;
236 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
237 return Entry.second;
238}
239
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000240// Define a unique number for the abbreviation.
241//
Eric Christopherc8a310e2012-12-10 23:34:43 +0000242void DwarfUnits::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000243 // Profile the node so that we can make it unique.
244 FoldingSetNodeID ID;
245 Abbrev.Profile(ID);
246
247 // Check the set for priors.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000248 DIEAbbrev *InSet = AbbreviationsSet->GetOrInsertNode(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000249
250 // If it's newly added.
251 if (InSet == &Abbrev) {
252 // Add to abbreviation list.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000253 Abbreviations->push_back(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000254
255 // Assign the vector position + 1 as its number.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000256 Abbrev.setNumber(Abbreviations->size());
Bill Wendling2f921f82009-05-15 09:23:25 +0000257 } else {
258 // Assign existing abbreviation number.
259 Abbrev.setNumber(InSet->getNumber());
260 }
261}
262
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000263// If special LLVM prefix that is used to inform the asm
264// printer to not emit usual symbol prefix before the symbol name is used then
265// return linkage name after skipping this special LLVM prefix.
Devang Patel43ef34d2010-01-05 01:46:14 +0000266static StringRef getRealLinkageName(StringRef LinkageName) {
267 char One = '\1';
268 if (LinkageName.startswith(StringRef(&One, 1)))
269 return LinkageName.substr(1);
270 return LinkageName;
271}
272
Eric Christopherd9843b32011-11-10 19:25:34 +0000273static bool isObjCClass(StringRef Name) {
274 return Name.startswith("+") || Name.startswith("-");
275}
276
277static bool hasObjCCategory(StringRef Name) {
278 if (!isObjCClass(Name)) return false;
279
280 size_t pos = Name.find(')');
281 if (pos != std::string::npos) {
282 if (Name[pos+1] != ' ') return false;
283 return true;
284 }
285 return false;
286}
287
288static void getObjCClassCategory(StringRef In, StringRef &Class,
289 StringRef &Category) {
290 if (!hasObjCCategory(In)) {
291 Class = In.slice(In.find('[') + 1, In.find(' '));
292 Category = "";
293 return;
294 }
295
296 Class = In.slice(In.find('[') + 1, In.find('('));
297 Category = In.slice(In.find('[') + 1, In.find(' '));
298 return;
299}
300
301static StringRef getObjCMethodName(StringRef In) {
302 return In.slice(In.find(' ') + 1, In.find(']'));
303}
304
305// Add the various names to the Dwarf accelerator table names.
306static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
307 DIE* Die) {
308 if (!SP.isDefinition()) return;
Eric Christopher6a841382012-11-19 22:42:10 +0000309
Eric Christopherd9843b32011-11-10 19:25:34 +0000310 TheCU->addAccelName(SP.getName(), Die);
311
312 // If the linkage name is different than the name, go ahead and output
313 // that as well into the name table.
314 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
315 TheCU->addAccelName(SP.getLinkageName(), Die);
316
317 // If this is an Objective-C selector name add it to the ObjC accelerator
318 // too.
319 if (isObjCClass(SP.getName())) {
320 StringRef Class, Category;
321 getObjCClassCategory(SP.getName(), Class, Category);
322 TheCU->addAccelObjC(Class, Die);
323 if (Category != "")
324 TheCU->addAccelObjC(Category, Die);
325 // Also add the base method name to the name table.
326 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
327 }
328}
329
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000330// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
331// and DW_AT_high_pc attributes. If there are global variables in this
332// scope then create and insert DIEs for these variables.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000333DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
334 const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000335 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +0000336
Chris Lattner3a383cb2010-04-05 00:13:49 +0000337 assert(SPDie && "Unable to find subprogram DIE!");
338 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000339
Bill Wendlingf720bf62012-11-07 05:19:04 +0000340 // If we're updating an abstract DIE, then we will be adding the children and
341 // object pointer later on. But what we don't want to do is process the
342 // concrete DIE twice.
Devang Patela37a95e2010-07-07 22:20:57 +0000343 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
Bill Wendlingf720bf62012-11-07 05:19:04 +0000344 // Pick up abstract subprogram DIE.
Devang Patela37a95e2010-07-07 22:20:57 +0000345 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf20c4f72011-04-12 22:53:02 +0000346 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
347 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patela37a95e2010-07-07 22:20:57 +0000348 SPCU->addDie(SPDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000349 } else {
350 DISubprogram SPDecl = SP.getFunctionDeclaration();
351 if (!SPDecl.isSubprogram()) {
352 // There is not any need to generate specification DIE for a function
353 // defined at compile unit level. If a function is defined inside another
354 // function then gdb prefers the definition at top level and but does not
355 // expect specification DIE in parent function. So avoid creating
356 // specification DIE for a function defined inside a function.
357 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
358 !SP.getContext().isFile() &&
359 !isSubprogramContext(SP.getContext())) {
360 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
361
362 // Add arguments.
363 DICompositeType SPTy = SP.getType();
364 DIArray Args = SPTy.getTypeArray();
365 unsigned SPTag = SPTy.getTag();
366 if (SPTag == dwarf::DW_TAG_subroutine_type)
367 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
368 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
369 DIType ATy = DIType(Args.getElement(i));
370 SPCU->addType(Arg, ATy);
371 if (ATy.isArtificial())
372 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
373 if (ATy.isObjectPointer())
374 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer,
375 dwarf::DW_FORM_ref4, Arg);
376 SPDie->addChild(Arg);
377 }
378 DIE *SPDeclDie = SPDie;
379 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Eric Christopher6a841382012-11-19 22:42:10 +0000380 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification,
381 dwarf::DW_FORM_ref4, SPDeclDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000382 SPCU->addDie(SPDie);
383 }
384 }
Devang Patela37a95e2010-07-07 22:20:57 +0000385 }
386
Devang Patelf20c4f72011-04-12 22:53:02 +0000387 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
388 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
389 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
390 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattner3a383cb2010-04-05 00:13:49 +0000391 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
392 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000393 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000394
Eric Christopherd9843b32011-11-10 19:25:34 +0000395 // Add name to the name table, we do this here because we're guaranteed
396 // to have concrete versions of our DW_TAG_subprogram nodes.
397 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher6a841382012-11-19 22:42:10 +0000398
Chris Lattner3a383cb2010-04-05 00:13:49 +0000399 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000400}
401
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000402// Construct new DW_TAG_lexical_block for this scope and attach
403// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher6a841382012-11-19 22:42:10 +0000404DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000405 LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000406 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
407 if (Scope->isAbstractScope())
408 return ScopeDIE;
409
Devang Patel7e623022011-08-10 20:55:27 +0000410 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +0000411 if (Ranges.empty())
412 return 0;
413
Devang Patel7e623022011-08-10 20:55:27 +0000414 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel6c74a872010-04-27 19:46:33 +0000415 if (Ranges.size() > 1) {
416 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000417 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000418 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000419 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Eric Christopher6a841382012-11-19 22:42:10 +0000420 DebugRangeSymbols.size()
Chandler Carruth5da3f052012-11-01 09:14:31 +0000421 * Asm->getDataLayout().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000422 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +0000423 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000424 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
425 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000426 }
427 DebugRangeSymbols.push_back(NULL);
428 DebugRangeSymbols.push_back(NULL);
429 return ScopeDIE;
430 }
431
Devang Patel9fc11702010-05-25 23:40:22 +0000432 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
433 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000434
Devang Patel9fc11702010-05-25 23:40:22 +0000435 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000436
Chris Lattnere13c3722010-03-09 01:58:53 +0000437 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
438 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000439
Devang Patelf20c4f72011-04-12 22:53:02 +0000440 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
441 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000442
443 return ScopeDIE;
444}
445
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000446// This scope represents inlined body of a function. Construct DIE to
447// represent this concrete inlined copy of the function.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000448DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
449 LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +0000450 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000451 assert(Ranges.empty() == false &&
452 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000453
Devang Patelf098ce22011-07-27 00:34:13 +0000454 if (!Scope->getScopeNode())
455 return NULL;
456 DIScope DS(Scope->getScopeNode());
457 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patelf098ce22011-07-27 00:34:13 +0000458 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
459 if (!OriginDIE) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000460 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patelf098ce22011-07-27 00:34:13 +0000461 return NULL;
462 }
463
Devang Patel7e623022011-08-10 20:55:27 +0000464 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +0000465 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
466 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000467
Devang Patel4c6bd662010-07-08 22:39:20 +0000468 if (StartLabel == 0 || EndLabel == 0) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000469 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000470 }
Chris Lattnere13c3722010-03-09 01:58:53 +0000471 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000472 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +0000473 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000474 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000475
Devang Patel73bc1722011-05-05 17:54:26 +0000476 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patelf20c4f72011-04-12 22:53:02 +0000477 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
478 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000479
Devang Patelf098ce22011-07-27 00:34:13 +0000480 if (Ranges.size() > 1) {
481 // .debug_range section has not been laid out yet. Emit offset in
482 // .debug_range as a uint, size 4, for now. emitDIE will handle
483 // DW_AT_ranges appropriately.
484 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Eric Christopher6a841382012-11-19 22:42:10 +0000485 DebugRangeSymbols.size()
Chandler Carruth5da3f052012-11-01 09:14:31 +0000486 * Asm->getDataLayout().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000487 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patelf098ce22011-07-27 00:34:13 +0000488 RE = Ranges.end(); RI != RE; ++RI) {
489 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
490 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
491 }
492 DebugRangeSymbols.push_back(NULL);
493 DebugRangeSymbols.push_back(NULL);
494 } else {
Eric Christopher6a841382012-11-19 22:42:10 +0000495 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel784077e2011-08-10 23:58:09 +0000496 StartLabel);
Eric Christopher6a841382012-11-19 22:42:10 +0000497 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel784077e2011-08-10 23:58:09 +0000498 EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000499 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000500
501 InlinedSubprogramDIEs.insert(OriginDIE);
502
503 // Track the start label for this inlined function.
Devang Patelf098ce22011-07-27 00:34:13 +0000504 //.debug_inlined section specification does not clearly state how
505 // to emit inlined scope that is split into multiple instruction ranges.
506 // For now, use first instruction range and emit low_pc/high_pc pair and
507 // corresponding .debug_inlined section entry for this pair.
Devang Patel32cc43c2010-05-07 20:54:48 +0000508 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000509 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000510
511 if (I == InlineInfo.end()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000512 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000513 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000514 } else
Chris Lattner085b6522010-03-09 00:31:02 +0000515 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000516
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000517 DILocation DL(Scope->getInlinedAt());
Eric Christopher0925c622012-03-26 21:38:38 +0000518 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000519 getOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patelf20c4f72011-04-12 22:53:02 +0000520 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000521
Eric Christopher8dda5d02011-12-04 06:02:38 +0000522 // Add name to the name table, we do this here because we're guaranteed
523 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
524 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher6a841382012-11-19 22:42:10 +0000525
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000526 return ScopeDIE;
527}
528
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000529// Construct a DIE for this scope.
Devang Patel3acc70e2011-08-15 22:04:40 +0000530DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000531 if (!Scope || !Scope->getScopeNode())
532 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000533
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000534 SmallVector<DIE *, 8> Children;
Eric Christophere3417762012-09-12 23:36:19 +0000535 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000536
537 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000538 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000539 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
540 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Eric Christopher6a841382012-11-19 22:42:10 +0000541 if (DIE *Arg =
Eric Christophere3417762012-09-12 23:36:19 +0000542 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000543 Children.push_back(Arg);
Eric Christophere3417762012-09-12 23:36:19 +0000544 if (ArgDV->isObjectPointer()) ObjectPointer = Arg;
545 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000546
Eric Christopherf84354b2011-10-03 15:49:16 +0000547 // Collect lexical scope children first.
Devang Patel7e623022011-08-10 20:55:27 +0000548 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000549 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Eric Christopher6a841382012-11-19 22:42:10 +0000550 if (DIE *Variable =
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000551 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope())) {
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000552 Children.push_back(Variable);
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000553 if (Variables[i]->isObjectPointer()) ObjectPointer = Variable;
554 }
Devang Patel7e623022011-08-10 20:55:27 +0000555 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000556 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000557 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000558 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000559 DIScope DS(Scope->getScopeNode());
560 DIE *ScopeDIE = NULL;
561 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000562 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000563 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000564 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000565 if (Scope->isAbstractScope()) {
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000566 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000567 // Note down abstract DIE.
568 if (ScopeDIE)
569 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
570 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000571 else
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000572 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000573 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000574 else {
575 // There is no need to emit empty lexical block DIE.
576 if (Children.empty())
577 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000578 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000579 }
Eric Christopher6a841382012-11-19 22:42:10 +0000580
Devang Patel23b2ae62010-03-29 22:59:58 +0000581 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000582
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000583 // Add children
584 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
585 E = Children.end(); I != E; ++I)
586 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000587
Eric Christophere3417762012-09-12 23:36:19 +0000588 if (DS.isSubprogram() && ObjectPointer != NULL)
589 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer,
590 dwarf::DW_FORM_ref4, ObjectPointer);
591
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000592 if (DS.isSubprogram())
Eric Christopherd9843b32011-11-10 19:25:34 +0000593 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000594
Eric Christopherd9843b32011-11-10 19:25:34 +0000595 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000596}
597
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000598// Look up the source id with the given directory and source file names.
599// If none currently exists, create a new id and insert it in the
600// SourceIds map. This can update DirectoryNames and SourceFileNames maps
601// as well.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000602unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName,
Devang Patele01b75c2011-03-24 20:30:50 +0000603 StringRef DirName) {
Devang Patel871d0b12010-09-16 20:57:49 +0000604 // If FE did not provide a file name, then assume stdin.
605 if (FileName.empty())
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000606 return getOrCreateSourceID("<stdin>", StringRef());
Devang Patele01b75c2011-03-24 20:30:50 +0000607
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000608 // TODO: this might not belong here. See if we can factor this better.
609 if (DirName == CompilationDir)
610 DirName = "";
611
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000612 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel871d0b12010-09-16 20:57:49 +0000613
Benjamin Kramer71b19732012-03-11 14:56:26 +0000614 // We look up the file/dir pair by concatenating them with a zero byte.
615 SmallString<128> NamePair;
616 NamePair += DirName;
617 NamePair += '\0'; // Zero bytes are not allowed in paths.
618 NamePair += FileName;
619
620 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
621 if (Ent.getValue() != SrcId)
622 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000623
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000624 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000625 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendling2b128d72009-05-20 23:19:06 +0000626
627 return SrcId;
628}
629
Eric Christopher48fef592012-12-20 21:58:40 +0000630// Create new CompileUnit for the given metadata node with tag
631// DW_TAG_compile_unit.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000632CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000633 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +0000634 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000635 CompilationDir = DIUnit.getDirectory();
Eli Benderskyb42d1462012-12-03 18:45:45 +0000636 // Call this to emit a .file directive if it wasn't emitted for the source
637 // file this CU comes from yet.
638 getOrCreateSourceID(FN, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000639
640 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyb42d1462012-12-03 18:45:45 +0000641 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
Eric Christophere698f532012-12-20 21:58:36 +0000642 DIUnit.getLanguage(), Die, Asm,
643 this, &InfoHolder);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000644 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000645 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
646 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000647 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopherb1b94512012-08-01 18:19:01 +0000648 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
649 // into an entity.
650 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +0000651 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +0000652 // compile unit in debug_line section.
Rafael Espindolad7bdaf52012-06-22 13:24:07 +0000653 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindolaa7558912011-05-04 17:44:06 +0000654 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patelf20c4f72011-04-12 22:53:02 +0000655 Asm->GetTempSymbol("section_line"));
Devang Patelea636392010-08-31 23:50:19 +0000656 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000657 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +0000658
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000659 if (!CompilationDir.empty())
660 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000661 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000662 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000663
Devang Patel2d9caf92009-11-25 17:36:49 +0000664 StringRef Flags = DIUnit.getFlags();
665 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000666 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000667
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000668 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000669 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000670 dwarf::DW_FORM_data1, RVer);
671
Devang Patel1a0df9a2010-05-10 22:49:55 +0000672 if (!FirstCU)
673 FirstCU = NewCU;
Eric Christophercdf218d2012-12-10 19:51:21 +0000674 if (useSplitDwarf() && !SkeletonCU)
675 SkeletonCU = constructSkeletonCU(N);
Eric Christopher9c2ecd92012-11-30 23:59:06 +0000676
Eric Christopherc8a310e2012-12-10 23:34:43 +0000677 InfoHolder.addUnit(NewCU);
678
Devang Patel1a0df9a2010-05-10 22:49:55 +0000679 CUMap.insert(std::make_pair(N, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000680 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000681}
682
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000683// Construct subprogram DIE.
Eric Christopher6a841382012-11-19 22:42:10 +0000684void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
Devang Patel1f4f98d2011-08-15 23:36:40 +0000685 const MDNode *N) {
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000686 CompileUnit *&CURef = SPMap[N];
687 if (CURef)
688 return;
689 CURef = TheCU;
690
Devang Patel80ae3492009-08-28 23:24:31 +0000691 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000692 if (!SP.isDefinition())
693 // This is a method declaration which will be handled while constructing
694 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000695 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000696
Devang Patel89543712011-08-15 17:24:54 +0000697 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000698
699 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000700 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000701
702 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +0000703 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +0000704
Devang Patel0751a282009-06-26 01:49:18 +0000705 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000706}
707
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000708// Collect debug info from named mdnodes such as llvm.dbg.enum and llvm.dbg.ty.
Eric Christopher58f41952012-11-19 22:42:15 +0000709void DwarfDebug::collectInfoFromNamedMDNodes(const Module *M) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000710 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
711 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
712 const MDNode *N = NMD->getOperand(i);
713 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
714 constructSubprogramDIE(CU, N);
715 }
Eric Christopher6a841382012-11-19 22:42:10 +0000716
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000717 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
718 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
719 const MDNode *N = NMD->getOperand(i);
720 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000721 CU->createGlobalVariableDIE(N);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000722 }
Eric Christopher6a841382012-11-19 22:42:10 +0000723
Devang Patel07bb9ee2011-08-15 23:47:24 +0000724 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
725 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
726 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000727 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
728 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000729 }
Eric Christopher6a841382012-11-19 22:42:10 +0000730
Devang Patel07bb9ee2011-08-15 23:47:24 +0000731 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
732 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
733 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000734 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
735 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000736 }
737}
738
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000739// Collect debug info using DebugInfoFinder.
740// FIXME - Remove this when dragonegg switches to DIBuilder.
Eric Christopher58f41952012-11-19 22:42:15 +0000741bool DwarfDebug::collectLegacyDebugInfo(const Module *M) {
Devang Patel07bb9ee2011-08-15 23:47:24 +0000742 DebugInfoFinder DbgFinder;
743 DbgFinder.processModule(*M);
Eric Christopher6a841382012-11-19 22:42:10 +0000744
Devang Patel07bb9ee2011-08-15 23:47:24 +0000745 bool HasDebugInfo = false;
746 // Scan all the compile-units to see if there are any marked as the main
747 // unit. If not, we do not generate debug info.
748 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
749 E = DbgFinder.compile_unit_end(); I != E; ++I) {
750 if (DICompileUnit(*I).isMain()) {
751 HasDebugInfo = true;
752 break;
753 }
754 }
755 if (!HasDebugInfo) return false;
Eric Christopher6a841382012-11-19 22:42:10 +0000756
Devang Patel07bb9ee2011-08-15 23:47:24 +0000757 // Create all the compile unit DIEs.
758 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
759 E = DbgFinder.compile_unit_end(); I != E; ++I)
760 constructCompileUnit(*I);
Eric Christopher6a841382012-11-19 22:42:10 +0000761
Devang Patel07bb9ee2011-08-15 23:47:24 +0000762 // Create DIEs for each global variable.
763 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
764 E = DbgFinder.global_variable_end(); I != E; ++I) {
765 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000766 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000767 CU->createGlobalVariableDIE(N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000768 }
Eric Christopher6a841382012-11-19 22:42:10 +0000769
Devang Patel07bb9ee2011-08-15 23:47:24 +0000770 // Create DIEs for each subprogram.
771 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
772 E = DbgFinder.subprogram_end(); I != E; ++I) {
773 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000774 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
775 constructSubprogramDIE(CU, N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000776 }
777
778 return HasDebugInfo;
779}
780
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000781// Emit all Dwarf sections that should come prior to the content. Create
782// global DIEs and emit initial debug info sections. This is invoked by
783// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000784void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000785 if (DisableDebugInfoPrinting)
786 return;
787
Eric Christopher58f41952012-11-19 22:42:15 +0000788 const Module *M = MMI->getModule();
789
Nick Lewycky019d2552011-07-29 03:49:23 +0000790 // If module has named metadata anchors then use them, otherwise scan the
791 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000792 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
793 if (CU_Nodes) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000794 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
795 DICompileUnit CUNode(CU_Nodes->getOperand(i));
796 CompileUnit *CU = constructCompileUnit(CUNode);
797 DIArray GVs = CUNode.getGlobalVariables();
798 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000799 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000800 DIArray SPs = CUNode.getSubprograms();
801 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
802 constructSubprogramDIE(CU, SPs.getElement(i));
803 DIArray EnumTypes = CUNode.getEnumTypes();
804 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
805 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
806 DIArray RetainedTypes = CUNode.getRetainedTypes();
807 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
808 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
809 }
Devang Patel07bb9ee2011-08-15 23:47:24 +0000810 } else if (!collectLegacyDebugInfo(M))
811 return;
Devang Patele02e5852011-05-03 16:45:22 +0000812
Devang Patel07bb9ee2011-08-15 23:47:24 +0000813 collectInfoFromNamedMDNodes(M);
Eric Christopher6a841382012-11-19 22:42:10 +0000814
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000815 // Tell MMI that we have debug info.
816 MMI->setDebugInfoAvailability(true);
Eric Christopher6a841382012-11-19 22:42:10 +0000817
Bill Wendling2b128d72009-05-20 23:19:06 +0000818 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +0000819 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +0000820}
821
Eric Christopher960ac372012-11-22 00:59:49 +0000822// Attach DW_AT_inline attribute with inlined subprogram DIEs.
823void DwarfDebug::computeInlinedDIEs() {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000824 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
825 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopher735401c2012-11-27 00:13:51 +0000826 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000827 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +0000828 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000829 }
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000830 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopher735401c2012-11-27 00:13:51 +0000831 AE = AbstractSPDies.end(); AI != AE; ++AI) {
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000832 DIE *ISP = AI->second;
833 if (InlinedSubprogramDIEs.count(ISP))
834 continue;
835 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
836 }
Eric Christopher960ac372012-11-22 00:59:49 +0000837}
838
839// Collect info for variables that were optimized out.
840void DwarfDebug::collectDeadVariables() {
841 const Module *M = MMI->getModule();
842 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
843
844 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
845 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
846 DICompileUnit TheCU(CU_Nodes->getOperand(i));
847 DIArray Subprograms = TheCU.getSubprograms();
848 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopher735401c2012-11-27 00:13:51 +0000849 DISubprogram SP(Subprograms.getElement(i));
850 if (ProcessedSPNodes.count(SP) != 0) continue;
851 if (!SP.Verify()) continue;
852 if (!SP.isDefinition()) continue;
853 DIArray Variables = SP.getVariables();
854 if (Variables.getNumElements() == 0) continue;
Eric Christopher960ac372012-11-22 00:59:49 +0000855
Eric Christopher735401c2012-11-27 00:13:51 +0000856 LexicalScope *Scope =
857 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
858 DeadFnScopeMap[SP] = Scope;
Eric Christopher960ac372012-11-22 00:59:49 +0000859
Eric Christopher735401c2012-11-27 00:13:51 +0000860 // Construct subprogram DIE and add variables DIEs.
861 CompileUnit *SPCU = CUMap.lookup(TheCU);
862 assert(SPCU && "Unable to find Compile Unit!");
863 constructSubprogramDIE(SPCU, SP);
864 DIE *ScopeDIE = SPCU->getDIE(SP);
865 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
866 DIVariable DV(Variables.getElement(vi));
867 if (!DV.Verify()) continue;
868 DbgVariable *NewVar = new DbgVariable(DV, NULL);
869 if (DIE *VariableDIE =
870 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
871 ScopeDIE->addChild(VariableDIE);
872 }
Eric Christopher960ac372012-11-22 00:59:49 +0000873 }
874 }
875 }
876 DeleteContainerSeconds(DeadFnScopeMap);
877}
878
879void DwarfDebug::finalizeModuleInfo() {
880 // Collect info for variables that were optimized out.
881 collectDeadVariables();
882
883 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
884 computeInlinedDIEs();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000885
Eric Christopherb1b94512012-08-01 18:19:01 +0000886 // Emit DW_AT_containing_type attribute to connect types with their
887 // vtable holding type.
Devang Patel89543712011-08-15 17:24:54 +0000888 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
Eric Christopher735401c2012-11-27 00:13:51 +0000889 CUE = CUMap.end(); CUI != CUE; ++CUI) {
Devang Patel89543712011-08-15 17:24:54 +0000890 CompileUnit *TheCU = CUI->second;
891 TheCU->constructContainingTypeDIEs();
Devang Pateleb57c592009-12-03 19:11:07 +0000892 }
893
Eric Christopher960ac372012-11-22 00:59:49 +0000894 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000895 InfoHolder.computeSizeAndOffsets();
896 if (useSplitDwarf())
897 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +0000898}
899
900void DwarfDebug::endSections() {
Bill Wendling2b128d72009-05-20 23:19:06 +0000901 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000902 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000903 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000904 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000905 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +0000906
907 // End text sections.
Benjamin Kramer15591272012-10-31 13:45:49 +0000908 for (unsigned I = 0, E = SectionMap.size(); I != E; ++I) {
909 Asm->OutStreamer.SwitchSection(SectionMap[I]);
910 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", I+1));
Bill Wendling2b128d72009-05-20 23:19:06 +0000911 }
Eric Christopher960ac372012-11-22 00:59:49 +0000912}
Bill Wendling2b128d72009-05-20 23:19:06 +0000913
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000914// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +0000915void DwarfDebug::endModule() {
916
917 if (!FirstCU) return;
918
919 // End any existing sections.
920 // TODO: Does this need to happen?
921 endSections();
922
923 // Finalize the debug info for the module.
924 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000925
Eric Christophercebb0ec2012-11-19 19:43:59 +0000926 // Emit initial sections.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000927 emitSectionLabels();
Eric Christophercebb0ec2012-11-19 19:43:59 +0000928
Eric Christophercdf218d2012-12-10 19:51:21 +0000929 if (!useSplitDwarf()) {
Eric Christopher95198f502012-11-27 22:43:42 +0000930 // Emit all the DIEs into a debug info section.
931 emitDebugInfo();
Eric Christopher4c9b1192012-11-27 00:41:54 +0000932
Eric Christopher95198f502012-11-27 22:43:42 +0000933 // Corresponding abbreviations into a abbrev section.
934 emitAbbreviations();
935
936 // Emit info into a debug loc section.
937 emitDebugLoc();
938
939 // Emit info into a debug aranges section.
940 emitDebugARanges();
941
942 // Emit info into a debug ranges section.
943 emitDebugRanges();
944
945 // Emit info into a debug macinfo section.
946 emitDebugMacInfo();
947
948 // Emit inline info.
949 // TODO: When we don't need the option anymore we
950 // can remove all of the code that this section
951 // depends upon.
952 if (useDarwinGDBCompat())
953 emitDebugInlineInfo();
954 } else {
Eric Christopherd692c1d2012-12-11 19:42:09 +0000955 // TODO: Fill this in for separated debug sections and separate
Eric Christopher95198f502012-11-27 22:43:42 +0000956 // out information into new sections.
957
Eric Christopher9c2ecd92012-11-30 23:59:06 +0000958 // Emit the debug info section and compile units.
Eric Christopher95198f502012-11-27 22:43:42 +0000959 emitDebugInfo();
Eric Christopher9c2ecd92012-11-30 23:59:06 +0000960 emitDebugInfoDWO();
Eric Christopher95198f502012-11-27 22:43:42 +0000961
962 // Corresponding abbreviations into a abbrev section.
963 emitAbbreviations();
Eric Christopher3c5a1912012-12-19 22:02:53 +0000964 emitDebugAbbrevDWO();
Eric Christopher95198f502012-11-27 22:43:42 +0000965
966 // Emit info into a debug loc section.
967 emitDebugLoc();
968
969 // Emit info into a debug aranges section.
970 emitDebugARanges();
971
972 // Emit info into a debug ranges section.
973 emitDebugRanges();
974
975 // Emit info into a debug macinfo section.
976 emitDebugMacInfo();
977
978 // Emit inline info.
979 // TODO: When we don't need the option anymore we
980 // can remove all of the code that this section
981 // depends upon.
982 if (useDarwinGDBCompat())
983 emitDebugInlineInfo();
984 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000985
Eric Christophera876b822012-08-23 07:32:06 +0000986 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +0000987 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +0000988 emitAccelNames();
989 emitAccelObjC();
990 emitAccelNamespaces();
991 emitAccelTypes();
992 }
Eric Christopher6a841382012-11-19 22:42:10 +0000993
Devang Patel04d2f2d2009-11-24 01:14:22 +0000994 // Emit info into a debug pubtypes section.
Eric Christopher77826182012-08-23 07:10:56 +0000995 // TODO: When we don't need the option anymore we can
996 // remove all of the code that adds to the table.
Eric Christopher20b76a72012-08-23 22:36:40 +0000997 if (useDarwinGDBCompat())
Eric Christopher77826182012-08-23 07:10:56 +0000998 emitDebugPubTypes();
Devang Patel04d2f2d2009-11-24 01:14:22 +0000999
Eric Christopher95198f502012-11-27 22:43:42 +00001000 // Finally emit string information into a string table.
Eric Christopher6e20a162012-11-27 06:49:23 +00001001 emitDebugStr();
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001002 if (useSplitDwarf())
1003 emitDebugStrDWO();
Eric Christopher6e20a162012-11-27 06:49:23 +00001004
Devang Pateld0701282010-08-02 17:32:15 +00001005 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001006 SPMap.clear();
Devang Patel1a0df9a2010-05-10 22:49:55 +00001007 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1008 E = CUMap.end(); I != E; ++I)
1009 delete I->second;
Eric Christopher8afd7b62012-12-10 19:51:18 +00001010
Eric Christophercdf218d2012-12-10 19:51:21 +00001011 delete SkeletonCU;
Eric Christopher8afd7b62012-12-10 19:51:18 +00001012
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001013 // Reset these for the next Module if we have one.
1014 FirstCU = NULL;
Eric Christophercdf218d2012-12-10 19:51:21 +00001015 SkeletonCU = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001016}
1017
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001018// Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001019DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001020 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001021 LLVMContext &Ctx = DV->getContext();
1022 // More then one inlined variable corresponds to one abstract variable.
1023 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001024 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001025 if (AbsDbgVariable)
1026 return AbsDbgVariable;
1027
Devang Patel7e623022011-08-10 20:55:27 +00001028 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001029 if (!Scope)
1030 return NULL;
1031
Devang Patel99819b52011-08-15 19:01:20 +00001032 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patel7e623022011-08-10 20:55:27 +00001033 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001034 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001035 return AbsDbgVariable;
1036}
1037
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001038// If Var is a current function argument then add it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +00001039bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +00001040 DbgVariable *Var, LexicalScope *Scope) {
1041 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001042 return false;
1043 DIVariable DV = Var->getVariable();
1044 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1045 return false;
1046 unsigned ArgNo = DV.getArgNumber();
Eric Christopher6a841382012-11-19 22:42:10 +00001047 if (ArgNo == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001048 return false;
1049
Devang Patel4ab660b2011-03-03 20:02:02 +00001050 size_t Size = CurrentFnArguments.size();
1051 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001052 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001053 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001054 // arguments does the function have at source level.
1055 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001056 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001057 CurrentFnArguments[ArgNo - 1] = Var;
1058 return true;
1059}
1060
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001061// Collect variable information from side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001062void
Nick Lewycky019d2552011-07-29 03:49:23 +00001063DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patel490c8ab2010-05-20 19:57:06 +00001064 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +00001065 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1066 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1067 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00001068 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001069 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001070 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001071 DIVariable DV(Var);
1072 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001073
Devang Patel7e623022011-08-10 20:55:27 +00001074 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001075
Devang Patelcdb7d442009-11-10 23:20:04 +00001076 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001077 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001078 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001079
Devang Patele1c53f22010-05-20 16:36:41 +00001080 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel99819b52011-08-15 19:01:20 +00001081 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patel3e4a9652011-08-15 21:24:36 +00001082 RegVar->setFrameIndex(VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +00001083 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001084 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001085 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +00001086 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +00001087 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001088}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001089
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001090// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1091// defined reg.
Devang Patel9fc11702010-05-25 23:40:22 +00001092static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001093 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001094 return MI->getNumOperands() == 3 &&
1095 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
1096 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +00001097}
1098
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001099// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher6a841382012-11-19 22:42:10 +00001100static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1101 const MCSymbol *FLabel,
Devang Patel2442a892011-07-08 17:09:57 +00001102 const MCSymbol *SLabel,
1103 const MachineInstr *MI) {
1104 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1105
1106 if (MI->getNumOperands() != 3) {
1107 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1108 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1109 }
1110 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1111 MachineLocation MLoc;
1112 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1113 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1114 }
1115 if (MI->getOperand(0).isImm())
1116 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1117 if (MI->getOperand(0).isFPImm())
1118 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1119 if (MI->getOperand(0).isCImm())
1120 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1121
Craig Topperee4dab52012-02-05 08:31:47 +00001122 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001123}
1124
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001125// Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001126void
Devang Patel5c0f85c2010-06-25 22:07:34 +00001127DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1128 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001129
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001130 // collection info from MMI table.
Devang Patel490c8ab2010-05-20 19:57:06 +00001131 collectVariableInfoFromMMITable(MF, Processed);
1132
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001133 for (SmallVectorImpl<const MDNode*>::const_iterator
1134 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1135 ++UVI) {
1136 const MDNode *Var = *UVI;
1137 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001138 continue;
1139
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001140 // History contains relevant DBG_VALUE instructions for Var and instructions
1141 // clobbering it.
1142 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1143 if (History.empty())
1144 continue;
1145 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001146
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001147 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001148 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001149 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1150 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001151 Scope = LScopes.getCurrentFunctionScope();
Devang Patel8fb9fd62011-07-20 22:18:50 +00001152 else {
1153 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patel7e623022011-08-10 20:55:27 +00001154 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel8fb9fd62011-07-20 22:18:50 +00001155 else {
1156 if (MDNode *IA = DV.getInlinedAt())
Devang Patel7e623022011-08-10 20:55:27 +00001157 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001158 else
Devang Patel7e623022011-08-10 20:55:27 +00001159 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001160 }
1161 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001162 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001163 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001164 continue;
1165
1166 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001167 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001168 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1169 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel6c622ef2011-03-01 22:58:55 +00001170 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001171 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001172 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001173 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001174
Eric Christophercc10d202012-10-08 20:48:54 +00001175 // Simplify ranges that are fully coalesced.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001176 if (History.size() <= 1 || (History.size() == 2 &&
1177 MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001178 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001179 continue;
1180 }
1181
1182 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001183 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001184
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001185 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1186 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1187 const MachineInstr *Begin = *HI;
1188 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001189
Devang Patele7181b52011-06-01 23:00:17 +00001190 // Check if DBG_VALUE is truncating a range.
1191 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1192 && !Begin->getOperand(0).getReg())
1193 continue;
1194
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001195 // Compute the range for a register location.
1196 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1197 const MCSymbol *SLabel = 0;
1198
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001199 if (HI + 1 == HE)
1200 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001201 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001202 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001203 else {
1204 const MachineInstr *End = HI[1];
Eric Christopher6a841382012-11-19 22:42:10 +00001205 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Devang Patel53b050a2011-07-07 21:44:42 +00001206 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001207 if (End->isDebugValue())
1208 SLabel = getLabelBeforeInsn(End);
1209 else {
1210 // End is a normal instruction clobbering the range.
1211 SLabel = getLabelAfterInsn(End);
1212 assert(SLabel && "Forgot label after clobber instruction");
1213 ++HI;
1214 }
1215 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001216
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001217 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopher365d0832011-12-16 23:42:31 +00001218 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1219 Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001220 }
1221 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001222 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001223
1224 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001225 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1226 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1227 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1228 DIVariable DV(Variables.getElement(i));
1229 if (!DV || !DV.Verify() || !Processed.insert(DV))
1230 continue;
1231 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1232 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patele0a94bf2010-05-14 21:01:35 +00001233 }
Devang Patel9fc11702010-05-25 23:40:22 +00001234}
Devang Patele0a94bf2010-05-14 21:01:35 +00001235
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001236// Return Label preceding the instruction.
Devang Patel9fc11702010-05-25 23:40:22 +00001237const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001238 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1239 assert(Label && "Didn't insert label before instruction");
1240 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001241}
1242
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001243// Return Label immediately following the instruction.
Devang Patel9fc11702010-05-25 23:40:22 +00001244const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001245 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001246}
1247
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001248// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001249void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001250 // Check if source location changes, but ignore DBG_VALUE locations.
1251 if (!MI->isDebugValue()) {
1252 DebugLoc DL = MI->getDebugLoc();
1253 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001254 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001255 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001256 if (DL == PrologEndLoc) {
1257 Flags |= DWARF2_FLAG_PROLOGUE_END;
1258 PrologEndLoc = DebugLoc();
1259 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001260 if (PrologEndLoc.isUnknown())
1261 Flags |= DWARF2_FLAG_IS_STMT;
1262
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001263 if (!DL.isUnknown()) {
1264 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001265 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001266 } else
Devang Patel34a66202011-05-11 19:22:19 +00001267 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001268 }
Devang Patel9fc11702010-05-25 23:40:22 +00001269 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001270
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001271 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001272 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1273 LabelsBeforeInsn.find(MI);
1274
1275 // No label needed.
1276 if (I == LabelsBeforeInsn.end())
1277 return;
1278
1279 // Label already assigned.
1280 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001281 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001282
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001283 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001284 PrevLabel = MMI->getContext().CreateTempSymbol();
1285 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001286 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001287 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001288}
1289
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001290// Process end of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001291void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001292 // Don't create a new label after DBG_VALUE instructions.
1293 // They don't generate code.
1294 if (!MI->isDebugValue())
1295 PrevLabel = 0;
1296
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001297 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1298 LabelsAfterInsn.find(MI);
1299
1300 // No label needed.
1301 if (I == LabelsAfterInsn.end())
1302 return;
1303
1304 // Label already assigned.
1305 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001306 return;
1307
1308 // We need a label after this instruction.
1309 if (!PrevLabel) {
1310 PrevLabel = MMI->getContext().CreateTempSymbol();
1311 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001312 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001313 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001314}
1315
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001316// Each LexicalScope has first instruction and last instruction to mark
1317// beginning and end of a scope respectively. Create an inverse map that list
1318// scopes starts (and ends) with an instruction. One instruction may start (or
1319// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001320void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001321 SmallVector<LexicalScope *, 4> WorkList;
1322 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001323 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001324 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001325
Devang Patel7e623022011-08-10 20:55:27 +00001326 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001327 if (!Children.empty())
Devang Patel7e623022011-08-10 20:55:27 +00001328 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001329 SE = Children.end(); SI != SE; ++SI)
1330 WorkList.push_back(*SI);
1331
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001332 if (S->isAbstractScope())
1333 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001334
Devang Patel7e623022011-08-10 20:55:27 +00001335 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001336 if (Ranges.empty())
1337 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001338 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +00001339 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001340 assert(RI->first && "InsnRange does not have first instruction!");
1341 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001342 requestLabelBeforeInsn(RI->first);
1343 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001344 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001345 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001346}
1347
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001348// Get MDNode for DebugLoc's scope.
Devang Patel589845d2011-05-09 22:14:49 +00001349static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1350 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1351 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1352 return DL.getScope(Ctx);
1353}
1354
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001355// Walk up the scope chain of given debug loc and find line number info
1356// for the function.
Devang Patel34a66202011-05-11 19:22:19 +00001357static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1358 const MDNode *Scope = getScopeNode(DL, Ctx);
1359 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher34164192012-04-03 00:43:49 +00001360 if (SP.Verify()) {
1361 // Check for number of operands since the compatibility is
1362 // cheap here.
Eric Christopherb81e2b42012-04-03 17:55:42 +00001363 if (SP->getNumOperands() > 19)
Eric Christopher34164192012-04-03 00:43:49 +00001364 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1365 else
1366 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1367 }
1368
Devang Patel34a66202011-05-11 19:22:19 +00001369 return DebugLoc();
1370}
1371
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001372// Gather pre-function debug information. Assumes being called immediately
1373// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001374void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001375 if (!MMI->hasDebugInfo()) return;
Devang Patel7e623022011-08-10 20:55:27 +00001376 LScopes.initialize(*MF);
1377 if (LScopes.empty()) return;
1378 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001379
Devang Patel6c74a872010-04-27 19:46:33 +00001380 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1381 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001382 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001383 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001384
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001385 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1386
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001387 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001388 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001389 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1390
Devang Patel002d54d2010-05-26 19:37:24 +00001391 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001392 I != E; ++I) {
1393 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001394 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1395 II != IE; ++II) {
1396 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001397
Devang Patel002d54d2010-05-26 19:37:24 +00001398 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001399 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001400
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001401 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001402 const MDNode *Var =
1403 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001404
1405 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001406 if (isDbgValueInDefinedReg(MI))
1407 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1408
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001409 // Check the history of this variable.
1410 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1411 if (History.empty()) {
1412 UserVariables.push_back(Var);
1413 // The first mention of a function argument gets the FunctionBeginSym
1414 // label, so arguments are visible when breaking at function entry.
1415 DIVariable DV(Var);
1416 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1417 DISubprogram(getDISubprogram(DV.getContext()))
1418 .describes(MF->getFunction()))
1419 LabelsBeforeInsn[MI] = FunctionBeginSym;
1420 } else {
1421 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1422 const MachineInstr *Prev = History.back();
1423 if (Prev->isDebugValue()) {
1424 // Coalesce identical entries at the end of History.
1425 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001426 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher85a495e2012-10-08 20:48:49 +00001427 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopher6a841382012-11-19 22:42:10 +00001428 << "\t" << *Prev
Devang Patelb7a328e2011-07-07 00:14:27 +00001429 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001430 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001431 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001432
1433 // Terminate old register assignments that don't reach MI;
1434 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1435 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1436 isDbgValueInDefinedReg(Prev)) {
1437 // Previous register assignment needs to terminate at the end of
1438 // its basic block.
1439 MachineBasicBlock::const_iterator LastMI =
1440 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001441 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001442 // Drop DBG_VALUE for empty range.
Eric Christopher85a495e2012-10-08 20:48:49 +00001443 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Devang Patelb7a328e2011-07-07 00:14:27 +00001444 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001445 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001446 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001447 else {
1448 // Terminate after LastMI.
1449 History.push_back(LastMI);
1450 }
1451 }
1452 }
1453 }
1454 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001455 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001456 // Not a DBG_VALUE instruction.
1457 if (!MI->isLabel())
1458 AtBlockEntry = false;
1459
Eric Christopher133195782012-10-04 20:46:14 +00001460 // First known non-DBG_VALUE and non-frame setup location marks
1461 // the beginning of the function body.
1462 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1463 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel34a66202011-05-11 19:22:19 +00001464 PrologEndLoc = MI->getDebugLoc();
1465
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001466 // Check if the instruction clobbers any registers with debug vars.
1467 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1468 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1469 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1470 continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001471 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1472 AI.isValid(); ++AI) {
1473 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001474 const MDNode *Var = LiveUserVar[Reg];
1475 if (!Var)
1476 continue;
1477 // Reg is now clobbered.
1478 LiveUserVar[Reg] = 0;
1479
1480 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001481 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1482 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001483 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001484 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1485 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001486 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001487 const MachineInstr *Prev = History.back();
1488 // Sanity-check: Register assignments are terminated at the end of
1489 // their block.
1490 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1491 continue;
1492 // Is the variable still in Reg?
1493 if (!isDbgValueInDefinedReg(Prev) ||
1494 Prev->getOperand(0).getReg() != Reg)
1495 continue;
1496 // Var is clobbered. Make sure the next instruction gets a label.
1497 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001498 }
1499 }
Devang Patel002d54d2010-05-26 19:37:24 +00001500 }
Devang Patel002d54d2010-05-26 19:37:24 +00001501 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001502 }
1503
1504 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1505 I != E; ++I) {
1506 SmallVectorImpl<const MachineInstr*> &History = I->second;
1507 if (History.empty())
1508 continue;
1509
1510 // Make sure the final register assignments are terminated.
1511 const MachineInstr *Prev = History.back();
1512 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1513 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher6a841382012-11-19 22:42:10 +00001514 MachineBasicBlock::const_iterator LastMI =
Devang Patel784077e2011-08-10 23:58:09 +00001515 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001516 if (LastMI == PrevMBB->end())
1517 // Drop DBG_VALUE for empty range.
1518 History.pop_back();
1519 else {
1520 // Terminate after LastMI.
1521 History.push_back(LastMI);
1522 }
1523 }
1524 // Request labels for the full history.
1525 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1526 const MachineInstr *MI = History[i];
1527 if (MI->isDebugValue())
1528 requestLabelBeforeInsn(MI);
1529 else
1530 requestLabelAfterInsn(MI);
1531 }
1532 }
Devang Patel002d54d2010-05-26 19:37:24 +00001533
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001534 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001535 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001536
1537 // Record beginning of function.
1538 if (!PrologEndLoc.isUnknown()) {
1539 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1540 MF->getFunction()->getContext());
1541 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1542 FnStartDL.getScope(MF->getFunction()->getContext()),
David Blaikie67cb31e2012-12-04 22:02:33 +00001543 // We'd like to list the prologue as "not statements" but GDB behaves
1544 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
David Blaikie5a773bb2012-12-04 21:05:36 +00001545 DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001546 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001547}
1548
Devang Patel7e623022011-08-10 20:55:27 +00001549void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1550// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1551 ScopeVariables[LS].push_back(Var);
1552// Vars.push_back(Var);
1553}
1554
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001555// Gather and emit post-function debug information.
Chris Lattner76555b52010-01-26 23:18:02 +00001556void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patel7e623022011-08-10 20:55:27 +00001557 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00001558
Devang Patel7e623022011-08-10 20:55:27 +00001559 // Define end label for subprogram.
1560 FunctionEndSym = Asm->GetTempSymbol("func_end",
1561 Asm->getFunctionNumber());
1562 // Assumes in correct section after the entry point.
1563 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Eric Christopher6a841382012-11-19 22:42:10 +00001564
Devang Patel7e623022011-08-10 20:55:27 +00001565 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1566 collectVariableInfo(MF, ProcessedVars);
Eric Christopher6a841382012-11-19 22:42:10 +00001567
Devang Patel3acc70e2011-08-15 22:04:40 +00001568 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001569 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001570 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001571
Devang Patel7e623022011-08-10 20:55:27 +00001572 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001573 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1574 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1575 LexicalScope *AScope = AList[i];
1576 DISubprogram SP(AScope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +00001577 if (SP.Verify()) {
1578 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001579 DIArray Variables = SP.getVariables();
1580 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1581 DIVariable DV(Variables.getElement(i));
1582 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1583 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001584 // Check that DbgVariable for DV wasn't created earlier, when
1585 // findAbstractVariable() was called for inlined instance of DV.
1586 LLVMContext &Ctx = DV->getContext();
1587 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1588 if (AbstractVariables.lookup(CleanDV))
1589 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001590 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1591 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001592 }
1593 }
Devang Patel44403472011-08-12 18:10:19 +00001594 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Patel3acc70e2011-08-15 22:04:40 +00001595 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001596 }
Eric Christopher6a841382012-11-19 22:42:10 +00001597
Devang Patel3acc70e2011-08-15 22:04:40 +00001598 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Eric Christopher6a841382012-11-19 22:42:10 +00001599
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001600 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopherbb69a272012-08-24 01:14:27 +00001601 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001602
Devang Patel7e623022011-08-10 20:55:27 +00001603 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1604 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00001605
Bill Wendling2b128d72009-05-20 23:19:06 +00001606 // Clear debug info
Devang Patel7e623022011-08-10 20:55:27 +00001607 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1608 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1609 DeleteContainerPointers(I->second);
1610 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001611 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001612 UserVariables.clear();
1613 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001614 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001615 LabelsBeforeInsn.clear();
1616 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001617 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001618}
1619
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001620// Register a source line with debug info. Returns the unique label that was
1621// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001622void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1623 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001624 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001625 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001626 unsigned Src = 1;
1627 if (S) {
1628 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001629
Dan Gohman50849c62010-05-05 23:41:32 +00001630 if (Scope.isCompileUnit()) {
1631 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001632 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001633 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001634 } else if (Scope.isFile()) {
1635 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001636 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001637 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001638 } else if (Scope.isSubprogram()) {
1639 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001640 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001641 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001642 } else if (Scope.isLexicalBlockFile()) {
1643 DILexicalBlockFile DBF(S);
1644 Fn = DBF.getFilename();
1645 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001646 } else if (Scope.isLexicalBlock()) {
1647 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001648 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001649 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001650 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001651 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001652
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001653 Src = getOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00001654 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001655 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001656}
1657
Bill Wendling806535f2009-05-20 23:22:40 +00001658//===----------------------------------------------------------------------===//
1659// Emit Methods
1660//===----------------------------------------------------------------------===//
1661
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001662// Compute the size and offset of a DIE.
Jim Grosbach00e9c612009-11-22 19:20:36 +00001663unsigned
Eric Christopherc8a310e2012-12-10 23:34:43 +00001664DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling480ff322009-05-20 23:21:38 +00001665 // Get the children.
1666 const std::vector<DIE *> &Children = Die->getChildren();
1667
Bill Wendling480ff322009-05-20 23:21:38 +00001668 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001669 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001670
1671 // Get the abbreviation for this DIE.
1672 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopherc8a310e2012-12-10 23:34:43 +00001673 const DIEAbbrev *Abbrev = Abbreviations->at(AbbrevNumber - 1);
Bill Wendling480ff322009-05-20 23:21:38 +00001674
1675 // Set DIE offset
1676 Die->setOffset(Offset);
1677
1678 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001679 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001680
1681 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1682 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1683
1684 // Size the DIE attribute values.
1685 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1686 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001687 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001688
1689 // Size the DIE children if any.
1690 if (!Children.empty()) {
1691 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1692 "Children flag not set");
1693
1694 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher1f0cbb82012-11-20 22:14:13 +00001695 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling480ff322009-05-20 23:21:38 +00001696
1697 // End of children marker.
1698 Offset += sizeof(int8_t);
1699 }
1700
1701 Die->setSize(Offset - Die->getOffset());
1702 return Offset;
1703}
1704
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001705// Compute the size and offset of all the DIEs.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001706void DwarfUnits::computeSizeAndOffsets() {
1707 for (SmallVector<CompileUnit *, 1>::iterator I = CUs.begin(),
1708 E = CUs.end(); I != E; ++I) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001709 unsigned Offset =
1710 sizeof(int32_t) + // Length of Compilation Unit Info
1711 sizeof(int16_t) + // DWARF version number
1712 sizeof(int32_t) + // Offset Into Abbrev. Section
1713 sizeof(int8_t); // Pointer Size (in bytes)
1714
Eric Christopherc8a310e2012-12-10 23:34:43 +00001715 computeSizeAndOffset((*I)->getCUDie(), Offset);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001716 }
Bill Wendling480ff322009-05-20 23:21:38 +00001717}
1718
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001719// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001720void DwarfDebug::emitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001721 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001722
Bill Wendling480ff322009-05-20 23:21:38 +00001723 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001724 DwarfInfoSectionSym =
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001725 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001726 DwarfAbbrevSectionSym =
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001727 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher3c5a1912012-12-19 22:02:53 +00001728 if (useSplitDwarf())
1729 DwarfAbbrevDWOSectionSym =
1730 emitSectionSym(Asm, TLOF.getDwarfAbbrevDWOSection(),
1731 "section_abbrev_dwo");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001732 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001733
Chris Lattner6629ca92010-04-04 22:59:04 +00001734 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001735 emitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001736
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001737 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
1738 emitSectionSym(Asm, TLOF.getDwarfLocSection());
1739 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001740 DwarfStrSectionSym =
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001741 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
1742 if (useSplitDwarf())
1743 DwarfStrDWOSectionSym =
1744 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001745 DwarfDebugRangeSectionSym = emitSectionSym(Asm, TLOF.getDwarfRangesSection(),
Devang Patel12563b32010-04-16 23:33:45 +00001746 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001747
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001748 DwarfDebugLocSectionSym = emitSectionSym(Asm, TLOF.getDwarfLocSection(),
Devang Patel9fc11702010-05-25 23:40:22 +00001749 "section_debug_loc");
1750
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001751 TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
1752 emitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001753}
1754
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001755// Recursively emits a debug information entry.
Eric Christopher3c5a1912012-12-19 22:02:53 +00001756void DwarfDebug::emitDIE(DIE *Die, std::vector<DIEAbbrev *> *Abbrevs) {
Bill Wendling480ff322009-05-20 23:21:38 +00001757 // Get the abbreviation for this DIE.
1758 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001759 const DIEAbbrev *Abbrev = Abbrevs->at(AbbrevNumber - 1);
Bill Wendling480ff322009-05-20 23:21:38 +00001760
Bill Wendling480ff322009-05-20 23:21:38 +00001761 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001762 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00001763 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1764 Twine::utohexstr(Die->getOffset()) + ":0x" +
1765 Twine::utohexstr(Die->getSize()) + " " +
1766 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00001767 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001768
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001769 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00001770 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1771
1772 // Emit the DIE attribute values.
1773 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1774 unsigned Attr = AbbrevData[i].getAttribute();
1775 unsigned Form = AbbrevData[i].getForm();
1776 assert(Form && "Too many attributes for DIE (check abbreviation)");
1777
Chris Lattner7bde8c02010-04-04 18:52:31 +00001778 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001779 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001780
Bill Wendling480ff322009-05-20 23:21:38 +00001781 switch (Attr) {
Bill Wendling480ff322009-05-20 23:21:38 +00001782 case dwarf::DW_AT_abstract_origin: {
1783 DIEEntry *E = cast<DIEEntry>(Values[i]);
1784 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001785 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00001786 Asm->EmitInt32(Addr);
1787 break;
1788 }
Devang Patel12563b32010-04-16 23:33:45 +00001789 case dwarf::DW_AT_ranges: {
1790 // DW_AT_range Value encodes offset in debug_range section.
1791 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00001792
Nick Lewycky33da3362012-06-22 01:25:12 +00001793 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelda3ef852010-09-02 16:43:44 +00001794 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1795 V->getValue(),
1796 4);
1797 } else {
1798 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1799 V->getValue(),
1800 DwarfDebugRangeSectionSym,
1801 4);
1802 }
Devang Patel12563b32010-04-16 23:33:45 +00001803 break;
1804 }
Devang Patel9fc11702010-05-25 23:40:22 +00001805 case dwarf::DW_AT_location: {
Nick Lewycky33da3362012-06-22 01:25:12 +00001806 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1807 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1808 Asm->EmitLabelReference(L->getValue(), 4);
1809 else
1810 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1811 } else {
Devang Patel9fc11702010-05-25 23:40:22 +00001812 Values[i]->EmitValue(Asm, Form);
Nick Lewycky33da3362012-06-22 01:25:12 +00001813 }
Devang Patel9fc11702010-05-25 23:40:22 +00001814 break;
1815 }
Devang Patela1bd5a12010-09-29 19:08:08 +00001816 case dwarf::DW_AT_accessibility: {
1817 if (Asm->isVerbose()) {
1818 DIEInteger *V = cast<DIEInteger>(Values[i]);
1819 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1820 }
1821 Values[i]->EmitValue(Asm, Form);
1822 break;
1823 }
Bill Wendling480ff322009-05-20 23:21:38 +00001824 default:
1825 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001826 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001827 break;
1828 }
Bill Wendling480ff322009-05-20 23:21:38 +00001829 }
1830
1831 // Emit the DIE children if any.
1832 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1833 const std::vector<DIE *> &Children = Die->getChildren();
1834
1835 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher3c5a1912012-12-19 22:02:53 +00001836 emitDIE(Children[j], Abbrevs);
Bill Wendling480ff322009-05-20 23:21:38 +00001837
Chris Lattner7bde8c02010-04-04 18:52:31 +00001838 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00001839 Asm->OutStreamer.AddComment("End Of Children Mark");
1840 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001841 }
1842}
1843
Eric Christophera2de8262012-12-15 00:04:07 +00001844// Emit the various dwarf units to the unit section USection with
1845// the abbreviations going into ASection.
1846void DwarfUnits::emitUnits(DwarfDebug *DD,
1847 const MCSection *USection,
1848 const MCSection *ASection,
1849 const MCSymbol *ASectionSym) {
1850 Asm->OutStreamer.SwitchSection(USection);
1851 for (SmallVector<CompileUnit *, 1>::iterator I = CUs.begin(),
1852 E = CUs.end(); I != E; ++I) {
1853 CompileUnit *TheCU = *I;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001854 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001855
Devang Patel1a0df9a2010-05-10 22:49:55 +00001856 // Emit the compile units header.
Eric Christophera2de8262012-12-15 00:04:07 +00001857 Asm->OutStreamer
1858 .EmitLabel(Asm->GetTempSymbol(USection->getLabelBeginName(),
1859 TheCU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001860
Devang Patel1a0df9a2010-05-10 22:49:55 +00001861 // Emit size of content not including length itself
1862 unsigned ContentSize = Die->getSize() +
1863 sizeof(int16_t) + // DWARF version number
1864 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00001865 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001866
Devang Patel1a0df9a2010-05-10 22:49:55 +00001867 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1868 Asm->EmitInt32(ContentSize);
1869 Asm->OutStreamer.AddComment("DWARF version number");
1870 Asm->EmitInt16(dwarf::DWARF_VERSION);
1871 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christophera2de8262012-12-15 00:04:07 +00001872 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
1873 ASectionSym);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001874 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth5da3f052012-11-01 09:14:31 +00001875 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001876
Eric Christopher3c5a1912012-12-19 22:02:53 +00001877 DD->emitDIE(Die, Abbreviations);
Eric Christophera2de8262012-12-15 00:04:07 +00001878 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(USection->getLabelEndName(),
Eli Benderskyb42d1462012-12-03 18:45:45 +00001879 TheCU->getUniqueID()));
Devang Patel1a0df9a2010-05-10 22:49:55 +00001880 }
Bill Wendling480ff322009-05-20 23:21:38 +00001881}
1882
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001883// Emit the debug info section.
1884void DwarfDebug::emitDebugInfo() {
Eric Christophera2de8262012-12-15 00:04:07 +00001885 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1886
1887 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
1888 Asm->getObjFileLowering().getDwarfAbbrevSection(),
1889 DwarfAbbrevSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001890}
1891
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001892// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00001893void DwarfDebug::emitAbbreviations() {
Eric Christopher3c5a1912012-12-19 22:02:53 +00001894 if (!useSplitDwarf())
1895 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
1896 &Abbreviations);
1897 else
1898 emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
1899}
Bill Wendling480ff322009-05-20 23:21:38 +00001900
Eric Christopher3c5a1912012-12-19 22:02:53 +00001901void DwarfDebug::emitAbbrevs(const MCSection *Section,
1902 std::vector<DIEAbbrev *> *Abbrevs) {
1903 // Check to see if it is worth the effort.
1904 if (!Abbrevs->empty()) {
1905 // Start the debug abbrev section.
1906 Asm->OutStreamer.SwitchSection(Section);
1907
1908 MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Eric Christopher996b2b72012-12-13 03:00:38 +00001909 Asm->OutStreamer.EmitLabel(Begin);
Bill Wendling480ff322009-05-20 23:21:38 +00001910
1911 // For each abbrevation.
Eric Christopher3c5a1912012-12-19 22:02:53 +00001912 for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
Bill Wendling480ff322009-05-20 23:21:38 +00001913 // Get abbreviation data
Eric Christopher3c5a1912012-12-19 22:02:53 +00001914 const DIEAbbrev *Abbrev = Abbrevs->at(i);
Bill Wendling480ff322009-05-20 23:21:38 +00001915
1916 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00001917 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00001918
1919 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001920 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00001921 }
1922
1923 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00001924 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00001925
Eric Christopher3c5a1912012-12-19 22:02:53 +00001926 MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Eric Christopher996b2b72012-12-13 03:00:38 +00001927 Asm->OutStreamer.EmitLabel(End);
Bill Wendling480ff322009-05-20 23:21:38 +00001928 }
1929}
1930
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001931// Emit the last address of the section and the end of the line matrix.
Devang Patel930143b2009-11-21 02:48:08 +00001932void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00001933 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00001934 Asm->OutStreamer.AddComment("Extended Op");
1935 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001936
Chris Lattner566cae92010-03-09 23:52:58 +00001937 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth5da3f052012-11-01 09:14:31 +00001938 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00001939 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1940 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1941
1942 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00001943
Chris Lattnera179b522010-04-04 19:25:43 +00001944 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Eric Christopherce0cfce2013-01-09 01:35:34 +00001945 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00001946
1947 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00001948 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1949 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001950 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00001951 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00001952}
1953
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001954// Emit visible names into a hashed accelerator table section.
Eric Christopher4996c702011-11-07 09:24:32 +00001955void DwarfDebug::emitAccelNames() {
1956 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1957 dwarf::DW_FORM_data4));
1958 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1959 E = CUMap.end(); I != E; ++I) {
1960 CompileUnit *TheCU = I->second;
Eric Christopherd9843b32011-11-10 19:25:34 +00001961 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1962 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00001963 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1964 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001965 const std::vector<DIE *> &Entities = GI->second;
Eric Christopherd9843b32011-11-10 19:25:34 +00001966 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1967 DE = Entities.end(); DI != DE; ++DI)
1968 AT.AddName(Name, (*DI));
Eric Christopher4996c702011-11-07 09:24:32 +00001969 }
1970 }
1971
1972 AT.FinalizeTable(Asm, "Names");
1973 Asm->OutStreamer.SwitchSection(
1974 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1975 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1976 Asm->OutStreamer.EmitLabel(SectionBegin);
1977
1978 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00001979 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00001980}
1981
Eric Christopher48fef592012-12-20 21:58:40 +00001982// Emit objective C classes and categories into a hashed accelerator table
1983// section.
Eric Christopher4996c702011-11-07 09:24:32 +00001984void DwarfDebug::emitAccelObjC() {
1985 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1986 dwarf::DW_FORM_data4));
1987 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1988 E = CUMap.end(); I != E; ++I) {
1989 CompileUnit *TheCU = I->second;
1990 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1991 for (StringMap<std::vector<DIE*> >::const_iterator
1992 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1993 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001994 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher4996c702011-11-07 09:24:32 +00001995 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1996 DE = Entities.end(); DI != DE; ++DI)
1997 AT.AddName(Name, (*DI));
1998 }
1999 }
2000
2001 AT.FinalizeTable(Asm, "ObjC");
2002 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2003 .getDwarfAccelObjCSection());
2004 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2005 Asm->OutStreamer.EmitLabel(SectionBegin);
2006
2007 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002008 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002009}
2010
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002011// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002012void DwarfDebug::emitAccelNamespaces() {
2013 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2014 dwarf::DW_FORM_data4));
2015 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2016 E = CUMap.end(); I != E; ++I) {
2017 CompileUnit *TheCU = I->second;
Eric Christopher66b37db2011-11-10 21:47:55 +00002018 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
2019 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00002020 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2021 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00002022 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher66b37db2011-11-10 21:47:55 +00002023 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2024 DE = Entities.end(); DI != DE; ++DI)
2025 AT.AddName(Name, (*DI));
Eric Christopher4996c702011-11-07 09:24:32 +00002026 }
2027 }
2028
2029 AT.FinalizeTable(Asm, "namespac");
2030 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2031 .getDwarfAccelNamespaceSection());
2032 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2033 Asm->OutStreamer.EmitLabel(SectionBegin);
2034
2035 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002036 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002037}
2038
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002039// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002040void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00002041 std::vector<DwarfAccelTable::Atom> Atoms;
2042 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2043 dwarf::DW_FORM_data4));
2044 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
2045 dwarf::DW_FORM_data2));
2046 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
2047 dwarf::DW_FORM_data1));
2048 DwarfAccelTable AT(Atoms);
Eric Christopher4996c702011-11-07 09:24:32 +00002049 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2050 E = CUMap.end(); I != E; ++I) {
2051 CompileUnit *TheCU = I->second;
Eric Christopher21bde872012-01-06 04:35:23 +00002052 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
2053 = TheCU->getAccelTypes();
2054 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00002055 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2056 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00002057 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopher21bde872012-01-06 04:35:23 +00002058 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
2059 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
2060 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher4996c702011-11-07 09:24:32 +00002061 }
2062 }
2063
2064 AT.FinalizeTable(Asm, "types");
2065 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2066 .getDwarfAccelTypesSection());
2067 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2068 Asm->OutStreamer.EmitLabel(SectionBegin);
2069
2070 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002071 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002072}
2073
Devang Patel04d2f2d2009-11-24 01:14:22 +00002074void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002075 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2076 E = CUMap.end(); I != E; ++I) {
2077 CompileUnit *TheCU = I->second;
Eric Christopher6abc9c52011-11-07 09:18:35 +00002078 // Start the dwarf pubtypes section.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002079 Asm->OutStreamer.SwitchSection(
2080 Asm->getObjFileLowering().getDwarfPubTypesSection());
2081 Asm->OutStreamer.AddComment("Length of Public Types Info");
2082 Asm->EmitLabelDifference(
Eli Benderskyb42d1462012-12-03 18:45:45 +00002083 Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()),
2084 Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002085
Devang Patel1a0df9a2010-05-10 22:49:55 +00002086 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
Eli Benderskyb42d1462012-12-03 18:45:45 +00002087 TheCU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002088
Devang Patel1a0df9a2010-05-10 22:49:55 +00002089 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2090 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002091
Devang Patel1a0df9a2010-05-10 22:49:55 +00002092 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Eric Christopher16485a52012-12-15 00:04:04 +00002093 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2094 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyb42d1462012-12-03 18:45:45 +00002095 TheCU->getUniqueID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00002096 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002097
Devang Patel1a0df9a2010-05-10 22:49:55 +00002098 Asm->OutStreamer.AddComment("Compilation Unit Length");
Eric Christopher16485a52012-12-15 00:04:04 +00002099 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(),
Eli Benderskyb42d1462012-12-03 18:45:45 +00002100 TheCU->getUniqueID()),
Eric Christopher16485a52012-12-15 00:04:04 +00002101 Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyb42d1462012-12-03 18:45:45 +00002102 TheCU->getUniqueID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00002103 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002104
Devang Patel1a0df9a2010-05-10 22:49:55 +00002105 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2106 for (StringMap<DIE*>::const_iterator
2107 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2108 const char *Name = GI->getKeyData();
Nick Lewycky019d2552011-07-29 03:49:23 +00002109 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002110
Devang Patel1a0df9a2010-05-10 22:49:55 +00002111 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2112 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002113
Devang Patel1a0df9a2010-05-10 22:49:55 +00002114 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002115 // Emit the name with a terminating null byte.
Eric Christophere3ab3d02013-01-09 01:57:54 +00002116 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002117 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002118
Devang Patel1a0df9a2010-05-10 22:49:55 +00002119 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002120 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00002121 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
Eli Benderskyb42d1462012-12-03 18:45:45 +00002122 TheCU->getUniqueID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00002123 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002124}
2125
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002126// Emit strings into a string section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002127void DwarfUnits::emitStrings(const MCSection *StrSection,
2128 const MCSection *OffsetSection = NULL,
2129 const MCSymbol *StrSecSym = NULL) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002130
Eric Christopher27614582013-01-08 22:22:06 +00002131 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002132
Chris Lattner3d72a672010-03-09 23:38:23 +00002133 // Start the dwarf str section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002134 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling480ff322009-05-20 23:21:38 +00002135
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002136 // Get all of the string pool entries and put them in an array by their ID so
2137 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002138 SmallVector<std::pair<unsigned,
Eric Christopherb800ff72013-01-07 22:40:45 +00002139 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002140
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002141 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
Eric Christopher27614582013-01-08 22:22:06 +00002142 I = StringPool.begin(), E = StringPool.end();
Eric Christopher48fef592012-12-20 21:58:40 +00002143 I != E; ++I)
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002144 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002145
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002146 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002147
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002148 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002149 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002150 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002151
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002152 // Emit the string itself with a terminating null byte.
Benjamin Kramer148db362011-11-09 12:12:04 +00002153 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
Eric Christopherce0cfce2013-01-09 01:35:34 +00002154 Entries[i].second->getKeyLength()+1));
Bill Wendling480ff322009-05-20 23:21:38 +00002155 }
Eric Christopher2cbd5762013-01-07 19:32:41 +00002156
2157 // If we've got an offset section go ahead and emit that now as well.
2158 if (OffsetSection) {
2159 Asm->OutStreamer.SwitchSection(OffsetSection);
2160 unsigned offset = 0;
2161 unsigned size = 4;
2162 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
2163 Asm->OutStreamer.EmitIntValue(offset, size, 0);
2164 offset += Entries[i].second->getKeyLength() + 1;
2165 }
2166 }
Bill Wendling480ff322009-05-20 23:21:38 +00002167}
2168
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002169// Emit visible names into a debug str section.
2170void DwarfDebug::emitDebugStr() {
2171 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2172 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2173}
2174
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002175// Emit visible names into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002176void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002177 if (DotDebugLocEntries.empty())
2178 return;
2179
Devang Patel116a9d72011-02-04 22:57:18 +00002180 for (SmallVector<DotDebugLocEntry, 4>::iterator
2181 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2182 I != E; ++I) {
2183 DotDebugLocEntry &Entry = *I;
2184 if (I + 1 != DotDebugLocEntries.end())
2185 Entry.Merge(I+1);
2186 }
2187
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002188 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002189 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00002190 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002191 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002192 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2193 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002194 for (SmallVector<DotDebugLocEntry, 4>::iterator
2195 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002196 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002197 DotDebugLocEntry &Entry = *I;
2198 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002199 if (Entry.isEmpty()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +00002200 Asm->OutStreamer.EmitIntValue(0, Size);
2201 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002202 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002203 } else {
2204 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2205 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patel3e021532011-04-28 02:22:40 +00002206 DIVariable DV(Entry.Variable);
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002207 Asm->OutStreamer.AddComment("Loc expr size");
2208 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2209 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2210 Asm->EmitLabelDifference(end, begin, 2);
2211 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002212 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002213 DIBasicType BTy(DV.getType());
2214 if (BTy.Verify() &&
Eric Christopher6a841382012-11-19 22:42:10 +00002215 (BTy.getEncoding() == dwarf::DW_ATE_signed
Devang Patel324f8432011-06-01 22:03:25 +00002216 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2217 Asm->OutStreamer.AddComment("DW_OP_consts");
2218 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002219 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002220 } else {
2221 Asm->OutStreamer.AddComment("DW_OP_constu");
2222 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002223 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002224 }
Devang Pateled9fd452011-07-08 16:49:43 +00002225 } else if (Entry.isLocation()) {
Eric Christopher6a841382012-11-19 22:42:10 +00002226 if (!DV.hasComplexAddress())
Devang Pateled9fd452011-07-08 16:49:43 +00002227 // Regular entry.
Devang Patel3e021532011-04-28 02:22:40 +00002228 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Pateled9fd452011-07-08 16:49:43 +00002229 else {
2230 // Complex address entry.
2231 unsigned N = DV.getNumAddrElements();
2232 unsigned i = 0;
2233 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2234 if (Entry.Loc.getOffset()) {
2235 i = 2;
2236 Asm->EmitDwarfRegOp(Entry.Loc);
2237 Asm->OutStreamer.AddComment("DW_OP_deref");
2238 Asm->EmitInt8(dwarf::DW_OP_deref);
2239 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2240 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2241 Asm->EmitSLEB128(DV.getAddrElement(1));
2242 } else {
2243 // If first address element is OpPlus then emit
2244 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2245 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2246 Asm->EmitDwarfRegOp(Loc);
2247 i = 2;
2248 }
2249 } else {
2250 Asm->EmitDwarfRegOp(Entry.Loc);
2251 }
Eric Christopher6a841382012-11-19 22:42:10 +00002252
Devang Pateled9fd452011-07-08 16:49:43 +00002253 // Emit remaining complex address elements.
2254 for (; i < N; ++i) {
2255 uint64_t Element = DV.getAddrElement(i);
2256 if (Element == DIBuilder::OpPlus) {
2257 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2258 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002259 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher8d2a77d2012-05-08 21:24:39 +00002260 if (!Entry.Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002261 Asm->EmitInt8(dwarf::DW_OP_deref);
2262 } else
2263 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002264 }
Devang Patel3e021532011-04-28 02:22:40 +00002265 }
Devang Patel3e021532011-04-28 02:22:40 +00002266 }
Devang Pateled9fd452011-07-08 16:49:43 +00002267 // else ... ignore constant fp. There is not any good way to
2268 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002269 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002270 }
2271 }
Bill Wendling480ff322009-05-20 23:21:38 +00002272}
2273
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002274// Emit visible names into a debug aranges section.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002275void DwarfDebug::emitDebugARanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002276 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002277 Asm->OutStreamer.SwitchSection(
2278 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002279}
2280
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002281// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002282void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002283 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002284 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00002285 Asm->getObjFileLowering().getDwarfRangesSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002286 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6c74a872010-04-27 19:46:33 +00002287 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002288 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00002289 I != E; ++I) {
2290 if (*I)
2291 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00002292 else
Eric Christopherce0cfce2013-01-09 01:35:34 +00002293 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel12563b32010-04-16 23:33:45 +00002294 }
Bill Wendling480ff322009-05-20 23:21:38 +00002295}
2296
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002297// Emit visible names into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00002298void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002299 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002300 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002301 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002302 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002303 }
2304}
2305
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002306// Emit inline info using following format.
2307// Section Header:
2308// 1. length of section
2309// 2. Dwarf version number
2310// 3. address size.
2311//
2312// Entries (one "entry" for each function that was inlined):
2313//
2314// 1. offset into __debug_str section for MIPS linkage name, if exists;
2315// otherwise offset into __debug_str for regular function name.
2316// 2. offset into __debug_str section for regular function name.
2317// 3. an unsigned LEB128 number indicating the number of distinct inlining
2318// instances for the function.
2319//
2320// The rest of the entry consists of a {die_offset, low_pc} pair for each
2321// inlined instance; the die_offset points to the inlined_subroutine die in the
2322// __debug_info section, and the low_pc is the starting address for the
2323// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002324void DwarfDebug::emitDebugInlineInfo() {
Eric Christopher1df94bf2012-03-02 02:11:47 +00002325 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002326 return;
2327
Devang Patel1a0df9a2010-05-10 22:49:55 +00002328 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002329 return;
2330
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002331 Asm->OutStreamer.SwitchSection(
2332 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002333
Chris Lattner566cae92010-03-09 23:52:58 +00002334 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002335 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2336 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002337
Chris Lattnera179b522010-04-04 19:25:43 +00002338 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002339
Chris Lattner566cae92010-03-09 23:52:58 +00002340 Asm->OutStreamer.AddComment("Dwarf Version");
2341 Asm->EmitInt16(dwarf::DWARF_VERSION);
2342 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002343 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002344
Devang Patel32cc43c2010-05-07 20:54:48 +00002345 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002346 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002347
Devang Patel32cc43c2010-05-07 20:54:48 +00002348 const MDNode *Node = *I;
2349 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00002350 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002351 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002352 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002353 StringRef LName = SP.getLinkageName();
2354 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002355
Chris Lattner566cae92010-03-09 23:52:58 +00002356 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher77725312012-03-02 01:57:52 +00002357 if (LName.empty())
Eric Christophere698f532012-12-20 21:58:36 +00002358 Asm->EmitSectionOffset(InfoHolder.getStringPoolEntry(Name),
2359 DwarfStrSectionSym);
Eric Christopher77725312012-03-02 01:57:52 +00002360 else
Eric Christophere698f532012-12-20 21:58:36 +00002361 Asm->EmitSectionOffset(InfoHolder
2362 .getStringPoolEntry(getRealLinkageName(LName)),
Chris Lattner70a4fce2010-04-04 23:25:33 +00002363 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002364
Chris Lattner566cae92010-03-09 23:52:58 +00002365 Asm->OutStreamer.AddComment("Function name");
Eric Christophere698f532012-12-20 21:58:36 +00002366 Asm->EmitSectionOffset(InfoHolder.getStringPoolEntry(Name),
2367 DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00002368 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002369
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002370 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002371 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002372 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002373 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002374
Chris Lattner7bde8c02010-04-04 18:52:31 +00002375 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002376 Asm->OutStreamer.EmitSymbolValue(LI->first,
Chandler Carruth5da3f052012-11-01 09:14:31 +00002377 Asm->getDataLayout().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00002378 }
2379 }
2380
Chris Lattnera179b522010-04-04 19:25:43 +00002381 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002382}
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002383
Eric Christopherd692c1d2012-12-11 19:42:09 +00002384// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002385
2386// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2387// DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
2388// DW_AT_ranges_base, DW_AT_addr_base. If DW_AT_ranges is present,
2389// DW_AT_low_pc and DW_AT_high_pc are not used, and vice versa.
Eric Christophercdf218d2012-12-10 19:51:21 +00002390CompileUnit *DwarfDebug::constructSkeletonCU(const MDNode *N) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002391 DICompileUnit DIUnit(N);
2392 StringRef FN = DIUnit.getFilename();
2393 CompilationDir = DIUnit.getDirectory();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002394
2395 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyb42d1462012-12-03 18:45:45 +00002396 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
Eric Christophere698f532012-12-20 21:58:36 +00002397 DIUnit.getLanguage(), Die, Asm,
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002398 this, &SkeletonHolder);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002399 // FIXME: This should be the .dwo file.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002400 NewCU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name, FN);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002401
2402 // FIXME: We also need DW_AT_addr_base and DW_AT_dwo_id.
2403
2404 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
2405 // into an entity.
2406 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
2407 // DW_AT_stmt_list is a offset of line number information for this
2408 // compile unit in debug_line section.
2409 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2410 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
2411 Asm->GetTempSymbol("section_line"));
2412 else
2413 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
2414
2415 if (!CompilationDir.empty())
Eric Christopher2cbd5762013-01-07 19:32:41 +00002416 NewCU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002417
Eric Christopherc8a310e2012-12-10 23:34:43 +00002418 SkeletonHolder.addUnit(NewCU);
2419
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002420 return NewCU;
2421}
2422
Eric Christophercdf218d2012-12-10 19:51:21 +00002423void DwarfDebug::emitSkeletonCU(const MCSection *Section) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002424 Asm->OutStreamer.SwitchSection(Section);
Eric Christophercdf218d2012-12-10 19:51:21 +00002425 DIE *Die = SkeletonCU->getCUDie();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002426
2427 // Emit the compile units header.
Eric Christopher16485a52012-12-15 00:04:04 +00002428 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(Section->getLabelBeginName(),
Eric Christophercdf218d2012-12-10 19:51:21 +00002429 SkeletonCU->getUniqueID()));
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002430
2431 // Emit size of content not including length itself
2432 unsigned ContentSize = Die->getSize() +
2433 sizeof(int16_t) + // DWARF version number
2434 sizeof(int32_t) + // Offset Into Abbrev. Section
2435 sizeof(int8_t); // Pointer Size (in bytes)
2436
2437 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2438 Asm->EmitInt32(ContentSize);
2439 Asm->OutStreamer.AddComment("DWARF version number");
2440 Asm->EmitInt16(dwarf::DWARF_VERSION);
2441 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christopher3c5a1912012-12-19 22:02:53 +00002442
2443 const MCSection *ASec = Asm->getObjFileLowering().getDwarfAbbrevSection();
2444 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASec->getLabelBeginName()),
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002445 DwarfAbbrevSectionSym);
2446 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2447 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2448
Eric Christopher3c5a1912012-12-19 22:02:53 +00002449 emitDIE(Die, &SkeletonAbbrevs);
Eric Christopher16485a52012-12-15 00:04:04 +00002450 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(Section->getLabelEndName(),
Eric Christophercdf218d2012-12-10 19:51:21 +00002451 SkeletonCU->getUniqueID()));
Eric Christopher3c5a1912012-12-19 22:02:53 +00002452}
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002453
Eric Christopher3c5a1912012-12-19 22:02:53 +00002454void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
2455 assert(useSplitDwarf() && "No split dwarf debug info?");
2456 emitAbbrevs(Section, &SkeletonAbbrevs);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002457}
2458
Eric Christopherd692c1d2012-12-11 19:42:09 +00002459// Emit the .debug_info.dwo section for separated dwarf. This contains the
2460// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002461void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00002462 assert(useSplitDwarf() && "No split dwarf debug info?");
Eric Christophera2de8262012-12-15 00:04:07 +00002463 InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Eric Christopher3c5a1912012-12-19 22:02:53 +00002464 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2465 DwarfAbbrevDWOSectionSym);
2466}
2467
2468// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2469// abbreviations for the .debug_info.dwo section.
2470void DwarfDebug::emitDebugAbbrevDWO() {
2471 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopher48fef592012-12-20 21:58:40 +00002472 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2473 &Abbreviations);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002474}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002475
2476// Emit the .debug_str.dwo section for separated dwarf. This contains the
2477// string section and is identical in format to traditional .debug_str
2478// sections.
2479void DwarfDebug::emitDebugStrDWO() {
2480 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherb800ff72013-01-07 22:40:45 +00002481 const MCSection *OffSec = Asm->getObjFileLowering()
2482 .getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00002483 const MCSymbol *StrSym = DwarfStrSectionSym;
2484 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
2485 OffSec, StrSym);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002486}