blob: bc0e84b0f17cbbf6c19c3b8ef48260bbf7b1229f [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
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +000057static cl::opt<bool> GenerateDwarfPubNamesSection("generate-dwarf-pubnames",
58 cl::Hidden, cl::ZeroOrMore, cl::init(false),
59 cl::desc("Generate DWARF pubnames section"));
60
Eric Christopher20b76a72012-08-23 22:36:40 +000061namespace {
62 enum DefaultOnOff {
63 Default, Enable, Disable
64 };
65}
Eric Christopher4996c702011-11-07 09:24:32 +000066
Eric Christopher20b76a72012-08-23 22:36:40 +000067static cl::opt<DefaultOnOff> DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
68 cl::desc("Output prototype dwarf accelerator tables."),
69 cl::values(
70 clEnumVal(Default, "Default for platform"),
71 clEnumVal(Enable, "Enabled"),
72 clEnumVal(Disable, "Disabled"),
73 clEnumValEnd),
74 cl::init(Default));
75
76static cl::opt<DefaultOnOff> DarwinGDBCompat("darwin-gdb-compat", cl::Hidden,
Eric Christopher978fbff2012-08-23 07:10:46 +000077 cl::desc("Compatibility with Darwin gdb."),
Eric Christopher20b76a72012-08-23 22:36:40 +000078 cl::values(
79 clEnumVal(Default, "Default for platform"),
80 clEnumVal(Enable, "Enabled"),
81 clEnumVal(Disable, "Disabled"),
82 clEnumValEnd),
83 cl::init(Default));
Eric Christopher978fbff2012-08-23 07:10:46 +000084
Eric Christophercdf218d2012-12-10 19:51:21 +000085static cl::opt<DefaultOnOff> SplitDwarf("split-dwarf", cl::Hidden,
86 cl::desc("Output prototype dwarf split debug info."),
Eric Christopher29424312012-11-12 22:22:20 +000087 cl::values(
88 clEnumVal(Default, "Default for platform"),
89 clEnumVal(Enable, "Enabled"),
90 clEnumVal(Disable, "Disabled"),
91 clEnumValEnd),
92 cl::init(Default));
93
Bill Wendlingfcc14142010-04-07 09:28:04 +000094namespace {
95 const char *DWARFGroupName = "DWARF Emission";
96 const char *DbgTimerName = "DWARF Debug Writer";
97} // end anonymous namespace
98
Bill Wendling2f921f82009-05-15 09:23:25 +000099//===----------------------------------------------------------------------===//
100
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000101// Configuration values for initial hash set sizes (log2).
102//
Bill Wendling2f921f82009-05-15 09:23:25 +0000103static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +0000104
105namespace llvm {
106
Nick Lewycky019d2552011-07-29 03:49:23 +0000107DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +0000108 DIType Ty = Var.getType();
109 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
110 // addresses instead.
111 if (Var.isBlockByrefVariable()) {
112 /* Byref variables, in Blocks, are declared by the programmer as
113 "SomeType VarName;", but the compiler creates a
114 __Block_byref_x_VarName struct, and gives the variable VarName
115 either the struct, or a pointer to the struct, as its type. This
116 is necessary for various behind-the-scenes things the compiler
117 needs to do with by-reference variables in blocks.
Eric Christopher6a841382012-11-19 22:42:10 +0000118
Devang Patelf20c4f72011-04-12 22:53:02 +0000119 However, as far as the original *programmer* is concerned, the
120 variable should still have type 'SomeType', as originally declared.
Eric Christopher6a841382012-11-19 22:42:10 +0000121
Devang Patelf20c4f72011-04-12 22:53:02 +0000122 The following function dives into the __Block_byref_x_VarName
123 struct to find the original type of the variable. This will be
124 passed back to the code generating the type for the Debug
125 Information Entry for the variable 'VarName'. 'VarName' will then
126 have the original type 'SomeType' in its debug information.
Eric Christopher6a841382012-11-19 22:42:10 +0000127
Devang Patelf20c4f72011-04-12 22:53:02 +0000128 The original type 'SomeType' will be the type of the field named
129 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher6a841382012-11-19 22:42:10 +0000130
Devang Patelf20c4f72011-04-12 22:53:02 +0000131 NOTE: In order for this to not completely fail on the debugger
132 side, the Debug Information Entry for the variable VarName needs to
133 have a DW_AT_location that tells the debugger how to unwind through
134 the pointers and __Block_byref_x_VarName struct to find the actual
135 value of the variable. The function addBlockByrefType does this. */
136 DIType subType = Ty;
137 unsigned tag = Ty.getTag();
Eric Christopher6a841382012-11-19 22:42:10 +0000138
Devang Patelf20c4f72011-04-12 22:53:02 +0000139 if (tag == dwarf::DW_TAG_pointer_type) {
140 DIDerivedType DTy = DIDerivedType(Ty);
141 subType = DTy.getTypeDerivedFrom();
142 }
Eric Christopher6a841382012-11-19 22:42:10 +0000143
Devang Patelf20c4f72011-04-12 22:53:02 +0000144 DICompositeType blockStruct = DICompositeType(subType);
145 DIArray Elements = blockStruct.getTypeArray();
Eric Christopher6a841382012-11-19 22:42:10 +0000146
Devang Patelf20c4f72011-04-12 22:53:02 +0000147 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
148 DIDescriptor Element = Elements.getElement(i);
149 DIDerivedType DT = DIDerivedType(Element);
150 if (getName() == DT.getName())
151 return (DT.getTypeDerivedFrom());
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000152 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000153 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000154 return Ty;
155}
Bill Wendling2f921f82009-05-15 09:23:25 +0000156
Chris Lattnerf5d06362010-04-05 04:09:20 +0000157} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000158
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000159DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Eric Christopherd79f5482012-12-10 19:51:13 +0000160 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000161 AbbreviationsSet(InitAbbreviationsSetSize),
Eric Christopher27614582013-01-08 22:22:06 +0000162 SourceIdMap(DIEValueAllocator),
Eric Christopherc8a310e2012-12-10 23:34:43 +0000163 PrevLabel(NULL), GlobalCUIndexCount(0),
Eric Christopher27614582013-01-08 22:22:06 +0000164 InfoHolder(A, &AbbreviationsSet, &Abbreviations, "info_string",
165 DIEValueAllocator),
Eric Christopher3c5a1912012-12-19 22:02:53 +0000166 SkeletonAbbrevSet(InitAbbreviationsSetSize),
Eric Christopher27614582013-01-08 22:22:06 +0000167 SkeletonHolder(A, &SkeletonAbbrevSet, &SkeletonAbbrevs, "skel_string",
168 DIEValueAllocator) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000169
Rafael Espindolaa7160962011-05-06 14:56:22 +0000170 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattnere58b5472010-04-04 23:10:38 +0000171 DwarfStrSectionSym = TextSectionSym = 0;
Eric Christopher74804332013-02-07 21:19:50 +0000172 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = DwarfLineSectionSym = 0;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000173 DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000174 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopherad9fe892012-04-02 17:58:52 +0000175
Eric Christopher978fbff2012-08-23 07:10:46 +0000176 // Turn on accelerator tables and older gdb compatibility
177 // for Darwin.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000178 bool IsDarwin = Triple(M->getTargetTriple()).isOSDarwin();
Eric Christopher20b76a72012-08-23 22:36:40 +0000179 if (DarwinGDBCompat == Default) {
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000180 if (IsDarwin)
181 IsDarwinGDBCompat = true;
Eric Christopher20b76a72012-08-23 22:36:40 +0000182 else
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000183 IsDarwinGDBCompat = false;
Eric Christopher20b76a72012-08-23 22:36:40 +0000184 } else
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000185 IsDarwinGDBCompat = DarwinGDBCompat == Enable ? true : false;
Eric Christopher4977f212012-08-23 22:36:36 +0000186
Eric Christopher20b76a72012-08-23 22:36:40 +0000187 if (DwarfAccelTables == Default) {
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000188 if (IsDarwin)
189 HasDwarfAccelTables = true;
Eric Christopher20b76a72012-08-23 22:36:40 +0000190 else
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000191 HasDwarfAccelTables = false;
Eric Christopher20b76a72012-08-23 22:36:40 +0000192 } else
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000193 HasDwarfAccelTables = DwarfAccelTables == Enable ? true : false;
Eric Christopher20b76a72012-08-23 22:36:40 +0000194
Eric Christophercdf218d2012-12-10 19:51:21 +0000195 if (SplitDwarf == Default)
196 HasSplitDwarf = false;
Eric Christopher29424312012-11-12 22:22:20 +0000197 else
Eric Christophercdf218d2012-12-10 19:51:21 +0000198 HasSplitDwarf = SplitDwarf == Enable ? true : false;
Eric Christopher29424312012-11-12 22:22:20 +0000199
Dan Gohman6e681a52010-06-18 15:56:31 +0000200 {
201 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopher58f41952012-11-19 22:42:15 +0000202 beginModule();
Torok Edwinf8dba242010-04-07 10:44:46 +0000203 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000204}
205DwarfDebug::~DwarfDebug() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000206}
207
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000208// Switch to the specified MCSection and emit an assembler
209// temporary label to it if SymbolStem is specified.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000210static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christophera7b61892011-11-07 09:18:38 +0000211 const char *SymbolStem = 0) {
212 Asm->OutStreamer.SwitchSection(Section);
213 if (!SymbolStem) return 0;
214
215 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
216 Asm->OutStreamer.EmitLabel(TmpSym);
217 return TmpSym;
218}
219
Eric Christophere698f532012-12-20 21:58:36 +0000220MCSymbol *DwarfUnits::getStringPoolSym() {
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000221 return Asm->GetTempSymbol(StringPref);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000222}
223
Eric Christophere698f532012-12-20 21:58:36 +0000224MCSymbol *DwarfUnits::getStringPoolEntry(StringRef Str) {
225 std::pair<MCSymbol*, unsigned> &Entry =
Eric Christopher27614582013-01-08 22:22:06 +0000226 StringPool.GetOrCreateValue(Str).getValue();
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000227 if (Entry.first) return Entry.first;
228
229 Entry.second = NextStringPoolNumber++;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000230 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000231}
232
Eric Christopher2cbd5762013-01-07 19:32:41 +0000233unsigned DwarfUnits::getStringPoolIndex(StringRef Str) {
234 std::pair<MCSymbol*, unsigned> &Entry =
Eric Christopher27614582013-01-08 22:22:06 +0000235 StringPool.GetOrCreateValue(Str).getValue();
Eric Christopher2cbd5762013-01-07 19:32:41 +0000236 if (Entry.first) return Entry.second;
237
238 Entry.second = NextStringPoolNumber++;
239 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
240 return Entry.second;
241}
242
Eric Christopher962c9082013-01-15 23:56:56 +0000243unsigned DwarfUnits::getAddrPoolIndex(MCSymbol *Sym) {
244 std::pair<MCSymbol*, unsigned> &Entry = AddressPool[Sym];
245 if (Entry.first) return Entry.second;
246
247 Entry.second = NextAddrPoolNumber++;
248 Entry.first = Sym;
249 return Entry.second;
250}
251
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000252// Define a unique number for the abbreviation.
253//
Eric Christopherc8a310e2012-12-10 23:34:43 +0000254void DwarfUnits::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000255 // Profile the node so that we can make it unique.
256 FoldingSetNodeID ID;
257 Abbrev.Profile(ID);
258
259 // Check the set for priors.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000260 DIEAbbrev *InSet = AbbreviationsSet->GetOrInsertNode(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000261
262 // If it's newly added.
263 if (InSet == &Abbrev) {
264 // Add to abbreviation list.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000265 Abbreviations->push_back(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000266
267 // Assign the vector position + 1 as its number.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000268 Abbrev.setNumber(Abbreviations->size());
Bill Wendling2f921f82009-05-15 09:23:25 +0000269 } else {
270 // Assign existing abbreviation number.
271 Abbrev.setNumber(InSet->getNumber());
272 }
273}
274
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000275// If special LLVM prefix that is used to inform the asm
276// printer to not emit usual symbol prefix before the symbol name is used then
277// return linkage name after skipping this special LLVM prefix.
Devang Patel43ef34d2010-01-05 01:46:14 +0000278static StringRef getRealLinkageName(StringRef LinkageName) {
279 char One = '\1';
280 if (LinkageName.startswith(StringRef(&One, 1)))
281 return LinkageName.substr(1);
282 return LinkageName;
283}
284
Eric Christopherd9843b32011-11-10 19:25:34 +0000285static bool isObjCClass(StringRef Name) {
286 return Name.startswith("+") || Name.startswith("-");
287}
288
289static bool hasObjCCategory(StringRef Name) {
290 if (!isObjCClass(Name)) return false;
291
292 size_t pos = Name.find(')');
293 if (pos != std::string::npos) {
294 if (Name[pos+1] != ' ') return false;
295 return true;
296 }
297 return false;
298}
299
300static void getObjCClassCategory(StringRef In, StringRef &Class,
301 StringRef &Category) {
302 if (!hasObjCCategory(In)) {
303 Class = In.slice(In.find('[') + 1, In.find(' '));
304 Category = "";
305 return;
306 }
307
308 Class = In.slice(In.find('[') + 1, In.find('('));
309 Category = In.slice(In.find('[') + 1, In.find(' '));
310 return;
311}
312
313static StringRef getObjCMethodName(StringRef In) {
314 return In.slice(In.find(' ') + 1, In.find(']'));
315}
316
317// Add the various names to the Dwarf accelerator table names.
318static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
319 DIE* Die) {
320 if (!SP.isDefinition()) return;
Eric Christopher6a841382012-11-19 22:42:10 +0000321
Eric Christopherd9843b32011-11-10 19:25:34 +0000322 TheCU->addAccelName(SP.getName(), Die);
323
324 // If the linkage name is different than the name, go ahead and output
325 // that as well into the name table.
326 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
327 TheCU->addAccelName(SP.getLinkageName(), Die);
328
329 // If this is an Objective-C selector name add it to the ObjC accelerator
330 // too.
331 if (isObjCClass(SP.getName())) {
332 StringRef Class, Category;
333 getObjCClassCategory(SP.getName(), Class, Category);
334 TheCU->addAccelObjC(Class, Die);
335 if (Category != "")
336 TheCU->addAccelObjC(Category, Die);
337 // Also add the base method name to the name table.
338 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
339 }
340}
341
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000342// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
343// and DW_AT_high_pc attributes. If there are global variables in this
344// scope then create and insert DIEs for these variables.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000345DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
346 const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000347 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +0000348
Chris Lattner3a383cb2010-04-05 00:13:49 +0000349 assert(SPDie && "Unable to find subprogram DIE!");
350 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000351
Bill Wendlingf720bf62012-11-07 05:19:04 +0000352 // If we're updating an abstract DIE, then we will be adding the children and
353 // object pointer later on. But what we don't want to do is process the
354 // concrete DIE twice.
Devang Patela37a95e2010-07-07 22:20:57 +0000355 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
Bill Wendlingf720bf62012-11-07 05:19:04 +0000356 // Pick up abstract subprogram DIE.
Devang Patela37a95e2010-07-07 22:20:57 +0000357 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf20c4f72011-04-12 22:53:02 +0000358 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
359 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patela37a95e2010-07-07 22:20:57 +0000360 SPCU->addDie(SPDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000361 } else {
362 DISubprogram SPDecl = SP.getFunctionDeclaration();
363 if (!SPDecl.isSubprogram()) {
364 // There is not any need to generate specification DIE for a function
365 // defined at compile unit level. If a function is defined inside another
366 // function then gdb prefers the definition at top level and but does not
367 // expect specification DIE in parent function. So avoid creating
368 // specification DIE for a function defined inside a function.
369 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
370 !SP.getContext().isFile() &&
371 !isSubprogramContext(SP.getContext())) {
372 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
373
374 // Add arguments.
375 DICompositeType SPTy = SP.getType();
376 DIArray Args = SPTy.getTypeArray();
377 unsigned SPTag = SPTy.getTag();
378 if (SPTag == dwarf::DW_TAG_subroutine_type)
379 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
380 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
381 DIType ATy = DIType(Args.getElement(i));
382 SPCU->addType(Arg, ATy);
383 if (ATy.isArtificial())
384 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
385 if (ATy.isObjectPointer())
386 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer,
387 dwarf::DW_FORM_ref4, Arg);
388 SPDie->addChild(Arg);
389 }
390 DIE *SPDeclDie = SPDie;
391 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Eric Christopher6a841382012-11-19 22:42:10 +0000392 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification,
393 dwarf::DW_FORM_ref4, SPDeclDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000394 SPCU->addDie(SPDie);
395 }
396 }
Devang Patela37a95e2010-07-07 22:20:57 +0000397 }
398
Eric Christopher962c9082013-01-15 23:56:56 +0000399 SPCU->addLabelAddress(SPDie, dwarf::DW_AT_low_pc,
400 Asm->GetTempSymbol("func_begin",
401 Asm->getFunctionNumber()));
402 SPCU->addLabelAddress(SPDie, dwarf::DW_AT_high_pc,
403 Asm->GetTempSymbol("func_end",
404 Asm->getFunctionNumber()));
Chris Lattner3a383cb2010-04-05 00:13:49 +0000405 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
406 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000407 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000408
Eric Christopherd9843b32011-11-10 19:25:34 +0000409 // Add name to the name table, we do this here because we're guaranteed
410 // to have concrete versions of our DW_TAG_subprogram nodes.
411 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher6a841382012-11-19 22:42:10 +0000412
Chris Lattner3a383cb2010-04-05 00:13:49 +0000413 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000414}
415
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000416// Construct new DW_TAG_lexical_block for this scope and attach
417// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher6a841382012-11-19 22:42:10 +0000418DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000419 LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000420 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
421 if (Scope->isAbstractScope())
422 return ScopeDIE;
423
Devang Patel7e623022011-08-10 20:55:27 +0000424 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +0000425 if (Ranges.empty())
426 return 0;
427
Devang Patel7e623022011-08-10 20:55:27 +0000428 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel6c74a872010-04-27 19:46:33 +0000429 if (Ranges.size() > 1) {
430 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000431 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000432 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000433 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Eric Christopher6a841382012-11-19 22:42:10 +0000434 DebugRangeSymbols.size()
Chandler Carruth5da3f052012-11-01 09:14:31 +0000435 * Asm->getDataLayout().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000436 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +0000437 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000438 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
439 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000440 }
441 DebugRangeSymbols.push_back(NULL);
442 DebugRangeSymbols.push_back(NULL);
443 return ScopeDIE;
444 }
445
Eric Christopher962c9082013-01-15 23:56:56 +0000446 MCSymbol *Start = getLabelBeforeInsn(RI->first);
447 MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000448
Devang Patel9fc11702010-05-25 23:40:22 +0000449 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000450
Chris Lattnere13c3722010-03-09 01:58:53 +0000451 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
452 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000453
Eric Christopher962c9082013-01-15 23:56:56 +0000454 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, Start);
455 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000456
457 return ScopeDIE;
458}
459
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000460// This scope represents inlined body of a function. Construct DIE to
461// represent this concrete inlined copy of the function.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000462DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
463 LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +0000464 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000465 assert(Ranges.empty() == false &&
466 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000467
Devang Patelf098ce22011-07-27 00:34:13 +0000468 if (!Scope->getScopeNode())
469 return NULL;
470 DIScope DS(Scope->getScopeNode());
471 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patelf098ce22011-07-27 00:34:13 +0000472 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
473 if (!OriginDIE) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000474 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patelf098ce22011-07-27 00:34:13 +0000475 return NULL;
476 }
477
Devang Patel7e623022011-08-10 20:55:27 +0000478 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Eric Christopher962c9082013-01-15 23:56:56 +0000479 MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
480 MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000481
Devang Patel4c6bd662010-07-08 22:39:20 +0000482 if (StartLabel == 0 || EndLabel == 0) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000483 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000484 }
Chris Lattnere13c3722010-03-09 01:58:53 +0000485 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000486 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +0000487 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000488 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000489
Devang Patel73bc1722011-05-05 17:54:26 +0000490 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patelf20c4f72011-04-12 22:53:02 +0000491 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
492 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000493
Devang Patelf098ce22011-07-27 00:34:13 +0000494 if (Ranges.size() > 1) {
495 // .debug_range section has not been laid out yet. Emit offset in
496 // .debug_range as a uint, size 4, for now. emitDIE will handle
497 // DW_AT_ranges appropriately.
498 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Eric Christopher6a841382012-11-19 22:42:10 +0000499 DebugRangeSymbols.size()
Chandler Carruth5da3f052012-11-01 09:14:31 +0000500 * Asm->getDataLayout().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000501 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patelf098ce22011-07-27 00:34:13 +0000502 RE = Ranges.end(); RI != RE; ++RI) {
503 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
504 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
505 }
506 DebugRangeSymbols.push_back(NULL);
507 DebugRangeSymbols.push_back(NULL);
508 } else {
Eric Christopher962c9082013-01-15 23:56:56 +0000509 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, StartLabel);
510 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000511 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000512
513 InlinedSubprogramDIEs.insert(OriginDIE);
514
515 // Track the start label for this inlined function.
Devang Patelf098ce22011-07-27 00:34:13 +0000516 //.debug_inlined section specification does not clearly state how
517 // to emit inlined scope that is split into multiple instruction ranges.
518 // For now, use first instruction range and emit low_pc/high_pc pair and
519 // corresponding .debug_inlined section entry for this pair.
Devang Patel32cc43c2010-05-07 20:54:48 +0000520 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000521 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000522
523 if (I == InlineInfo.end()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000524 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000525 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000526 } else
Chris Lattner085b6522010-03-09 00:31:02 +0000527 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000528
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000529 DILocation DL(Scope->getInlinedAt());
Eric Christopher0925c622012-03-26 21:38:38 +0000530 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
Manman Ren1e427202013-03-07 01:42:00 +0000531 getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
532 TheCU->getUniqueID()));
Devang Patelf20c4f72011-04-12 22:53:02 +0000533 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000534
Eric Christopher8dda5d02011-12-04 06:02:38 +0000535 // Add name to the name table, we do this here because we're guaranteed
536 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
537 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher6a841382012-11-19 22:42:10 +0000538
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000539 return ScopeDIE;
540}
541
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000542// Construct a DIE for this scope.
Devang Patel3acc70e2011-08-15 22:04:40 +0000543DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000544 if (!Scope || !Scope->getScopeNode())
545 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000546
Manman Ren53f3f9f2013-01-31 20:05:14 +0000547 DIScope DS(Scope->getScopeNode());
548 // Early return to avoid creating dangling variable|scope DIEs.
549 if (!Scope->getInlinedAt() && DS.isSubprogram() && Scope->isAbstractScope() &&
550 !TheCU->getDIE(DS))
551 return NULL;
552
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000553 SmallVector<DIE *, 8> Children;
Eric Christophere3417762012-09-12 23:36:19 +0000554 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000555
556 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000557 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000558 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
559 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Eric Christopher6a841382012-11-19 22:42:10 +0000560 if (DIE *Arg =
Eric Christophere3417762012-09-12 23:36:19 +0000561 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000562 Children.push_back(Arg);
Eric Christophere3417762012-09-12 23:36:19 +0000563 if (ArgDV->isObjectPointer()) ObjectPointer = Arg;
564 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000565
Eric Christopherf84354b2011-10-03 15:49:16 +0000566 // Collect lexical scope children first.
Devang Patel7e623022011-08-10 20:55:27 +0000567 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000568 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Eric Christopher6a841382012-11-19 22:42:10 +0000569 if (DIE *Variable =
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000570 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope())) {
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000571 Children.push_back(Variable);
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000572 if (Variables[i]->isObjectPointer()) ObjectPointer = Variable;
573 }
Devang Patel7e623022011-08-10 20:55:27 +0000574 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000575 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000576 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000577 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000578 DIE *ScopeDIE = NULL;
579 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000580 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000581 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000582 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000583 if (Scope->isAbstractScope()) {
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000584 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000585 // Note down abstract DIE.
586 if (ScopeDIE)
587 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
588 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000589 else
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000590 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000591 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000592 else {
593 // There is no need to emit empty lexical block DIE.
594 if (Children.empty())
595 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000596 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000597 }
Eric Christopher6a841382012-11-19 22:42:10 +0000598
Devang Patel23b2ae62010-03-29 22:59:58 +0000599 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000600
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000601 // Add children
602 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
603 E = Children.end(); I != E; ++I)
604 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000605
Eric Christophere3417762012-09-12 23:36:19 +0000606 if (DS.isSubprogram() && ObjectPointer != NULL)
607 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer,
608 dwarf::DW_FORM_ref4, ObjectPointer);
609
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000610 if (DS.isSubprogram())
Eric Christopherd9843b32011-11-10 19:25:34 +0000611 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000612
Eric Christopherd9843b32011-11-10 19:25:34 +0000613 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000614}
615
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000616// Look up the source id with the given directory and source file names.
617// If none currently exists, create a new id and insert it in the
618// SourceIds map. This can update DirectoryNames and SourceFileNames maps
619// as well.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000620unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000621 StringRef DirName, unsigned CUID) {
622 // If we use .loc in assembly, we can't separate .file entries according to
623 // compile units. Thus all files will belong to the default compile unit.
624 if (Asm->TM.hasMCUseLoc() &&
625 Asm->OutStreamer.getKind() == MCStreamer::SK_AsmStreamer)
626 CUID = 0;
627
Devang Patel871d0b12010-09-16 20:57:49 +0000628 // If FE did not provide a file name, then assume stdin.
629 if (FileName.empty())
Manman Ren1e427202013-03-07 01:42:00 +0000630 return getOrCreateSourceID("<stdin>", StringRef(), CUID);
Devang Patele01b75c2011-03-24 20:30:50 +0000631
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000632 // TODO: this might not belong here. See if we can factor this better.
633 if (DirName == CompilationDir)
634 DirName = "";
635
Manman Ren1e427202013-03-07 01:42:00 +0000636 // FileIDCUMap stores the current ID for the given compile unit.
637 unsigned SrcId = FileIDCUMap[CUID] + 1;
Devang Patel871d0b12010-09-16 20:57:49 +0000638
Manman Ren1e427202013-03-07 01:42:00 +0000639 // We look up the CUID/file/dir by concatenating them with a zero byte.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000640 SmallString<128> NamePair;
Manman Ren1e427202013-03-07 01:42:00 +0000641 NamePair += CUID;
642 NamePair += '\0';
Benjamin Kramer71b19732012-03-11 14:56:26 +0000643 NamePair += DirName;
644 NamePair += '\0'; // Zero bytes are not allowed in paths.
645 NamePair += FileName;
646
647 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
648 if (Ent.getValue() != SrcId)
649 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000650
Manman Ren1e427202013-03-07 01:42:00 +0000651 FileIDCUMap[CUID] = SrcId;
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000652 // Print out a .file directive to specify files for .loc directives.
Manman Ren1e427202013-03-07 01:42:00 +0000653 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
Bill Wendling2b128d72009-05-20 23:19:06 +0000654
655 return SrcId;
656}
657
Eric Christopher48fef592012-12-20 21:58:40 +0000658// Create new CompileUnit for the given metadata node with tag
659// DW_TAG_compile_unit.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000660CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000661 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +0000662 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000663 CompilationDir = DIUnit.getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000664
665 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyb42d1462012-12-03 18:45:45 +0000666 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
Eric Christophere698f532012-12-20 21:58:36 +0000667 DIUnit.getLanguage(), Die, Asm,
668 this, &InfoHolder);
Manman Ren1e427202013-03-07 01:42:00 +0000669
670 FileIDCUMap[NewCU->getUniqueID()] = 0;
671 // Call this to emit a .file directive if it wasn't emitted for the source
672 // file this CU comes from yet.
673 getOrCreateSourceID(FN, CompilationDir, NewCU->getUniqueID());
674
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000675 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000676 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
677 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000678 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopherb1b94512012-08-01 18:19:01 +0000679 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher962c9082013-01-15 23:56:56 +0000680 // into an entity. We're using 0 (or a NULL label) for this.
681 NewCU->addLabelAddress(Die, dwarf::DW_AT_low_pc, NULL);
Manman Ren4e042a62013-02-05 21:52:47 +0000682
683 // Define start line table label for each Compile Unit.
684 MCSymbol *LineTableStartSym = Asm->GetTempSymbol("line_table_start",
685 NewCU->getUniqueID());
686 Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
687 NewCU->getUniqueID());
688
Devang Pateld22ed622010-03-22 23:11:36 +0000689 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +0000690 // compile unit in debug_line section.
Manman Rend2c95eb2013-02-09 00:41:44 +0000691 // The line table entries are not always emitted in assembly, so it
692 // is not okay to use line_table_start here.
Rafael Espindolad7bdaf52012-06-22 13:24:07 +0000693 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindolaa7558912011-05-04 17:44:06 +0000694 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Manman Rend2c95eb2013-02-09 00:41:44 +0000695 NewCU->getUniqueID() == 0 ?
696 Asm->GetTempSymbol("section_line") : LineTableStartSym);
Manman Rend2c38d62013-02-06 00:59:41 +0000697 else if (NewCU->getUniqueID() == 0)
698 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Devang Patelea636392010-08-31 23:50:19 +0000699 else
Manman Ren4e042a62013-02-05 21:52:47 +0000700 NewCU->addDelta(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Eric Christopher74804332013-02-07 21:19:50 +0000701 LineTableStartSym, DwarfLineSectionSym);
Bill Wendling2b128d72009-05-20 23:19:06 +0000702
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000703 if (!CompilationDir.empty())
704 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000705 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000706 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000707
Devang Patel2d9caf92009-11-25 17:36:49 +0000708 StringRef Flags = DIUnit.getFlags();
709 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000710 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000711
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000712 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000713 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000714 dwarf::DW_FORM_data1, RVer);
715
Devang Patel1a0df9a2010-05-10 22:49:55 +0000716 if (!FirstCU)
717 FirstCU = NewCU;
Eric Christopher7a2cdf72013-02-05 07:31:55 +0000718
Eric Christopher411e6742013-02-05 07:32:03 +0000719 if (useSplitDwarf()) {
720 // This should be a unique identifier when we want to build .dwp files.
Eric Christopher7a2cdf72013-02-05 07:31:55 +0000721 NewCU->addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8, 0);
Eric Christopher411e6742013-02-05 07:32:03 +0000722 // Now construct the skeleton CU associated.
723 constructSkeletonCU(N);
724 }
Eric Christopher9c2ecd92012-11-30 23:59:06 +0000725
Eric Christopherc8a310e2012-12-10 23:34:43 +0000726 InfoHolder.addUnit(NewCU);
727
Devang Patel1a0df9a2010-05-10 22:49:55 +0000728 CUMap.insert(std::make_pair(N, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000729 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000730}
731
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000732// Construct subprogram DIE.
Eric Christopher6a841382012-11-19 22:42:10 +0000733void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
Devang Patel1f4f98d2011-08-15 23:36:40 +0000734 const MDNode *N) {
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000735 CompileUnit *&CURef = SPMap[N];
736 if (CURef)
737 return;
738 CURef = TheCU;
739
Devang Patel80ae3492009-08-28 23:24:31 +0000740 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000741 if (!SP.isDefinition())
742 // This is a method declaration which will be handled while constructing
743 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000744 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000745
Devang Patel89543712011-08-15 17:24:54 +0000746 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000747
748 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000749 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000750
751 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +0000752 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +0000753
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +0000754 // Expose as global, if requested.
755 if (GenerateDwarfPubNamesSection)
756 TheCU->addGlobalName(SP.getName(), SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000757}
758
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000759// Collect debug info from named mdnodes such as llvm.dbg.enum and llvm.dbg.ty.
Eric Christopher58f41952012-11-19 22:42:15 +0000760void DwarfDebug::collectInfoFromNamedMDNodes(const Module *M) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000761 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
762 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
763 const MDNode *N = NMD->getOperand(i);
764 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
765 constructSubprogramDIE(CU, N);
766 }
Eric Christopher6a841382012-11-19 22:42:10 +0000767
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000768 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
769 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
770 const MDNode *N = NMD->getOperand(i);
771 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000772 CU->createGlobalVariableDIE(N);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000773 }
Eric Christopher6a841382012-11-19 22:42:10 +0000774
Devang Patel07bb9ee2011-08-15 23:47:24 +0000775 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
776 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
777 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000778 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
779 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000780 }
Eric Christopher6a841382012-11-19 22:42:10 +0000781
Devang Patel07bb9ee2011-08-15 23:47:24 +0000782 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
783 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
784 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000785 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
786 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000787 }
788}
789
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000790// Collect debug info using DebugInfoFinder.
791// FIXME - Remove this when dragonegg switches to DIBuilder.
Eric Christopher58f41952012-11-19 22:42:15 +0000792bool DwarfDebug::collectLegacyDebugInfo(const Module *M) {
Devang Patel07bb9ee2011-08-15 23:47:24 +0000793 DebugInfoFinder DbgFinder;
794 DbgFinder.processModule(*M);
Eric Christopher6a841382012-11-19 22:42:10 +0000795
Devang Patel07bb9ee2011-08-15 23:47:24 +0000796 bool HasDebugInfo = false;
797 // Scan all the compile-units to see if there are any marked as the main
798 // unit. If not, we do not generate debug info.
799 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
800 E = DbgFinder.compile_unit_end(); I != E; ++I) {
801 if (DICompileUnit(*I).isMain()) {
802 HasDebugInfo = true;
803 break;
804 }
805 }
806 if (!HasDebugInfo) return false;
Eric Christopher6a841382012-11-19 22:42:10 +0000807
Eric Christopher74804332013-02-07 21:19:50 +0000808 // Emit initial sections so we can refer to them later.
809 emitSectionLabels();
810
Devang Patel07bb9ee2011-08-15 23:47:24 +0000811 // Create all the compile unit DIEs.
812 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
813 E = DbgFinder.compile_unit_end(); I != E; ++I)
814 constructCompileUnit(*I);
Eric Christopher6a841382012-11-19 22:42:10 +0000815
Devang Patel07bb9ee2011-08-15 23:47:24 +0000816 // Create DIEs for each global variable.
817 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
818 E = DbgFinder.global_variable_end(); I != E; ++I) {
819 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000820 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000821 CU->createGlobalVariableDIE(N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000822 }
Eric Christopher6a841382012-11-19 22:42:10 +0000823
Devang Patel07bb9ee2011-08-15 23:47:24 +0000824 // Create DIEs for each subprogram.
825 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
826 E = DbgFinder.subprogram_end(); I != E; ++I) {
827 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000828 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
829 constructSubprogramDIE(CU, N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000830 }
831
832 return HasDebugInfo;
833}
834
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000835// Emit all Dwarf sections that should come prior to the content. Create
836// global DIEs and emit initial debug info sections. This is invoked by
837// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000838void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000839 if (DisableDebugInfoPrinting)
840 return;
841
Eric Christopher58f41952012-11-19 22:42:15 +0000842 const Module *M = MMI->getModule();
843
Nick Lewycky019d2552011-07-29 03:49:23 +0000844 // If module has named metadata anchors then use them, otherwise scan the
845 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000846 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
847 if (CU_Nodes) {
Eric Christopher74804332013-02-07 21:19:50 +0000848 // Emit initial sections so we can reference labels later.
849 emitSectionLabels();
850
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000851 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
852 DICompileUnit CUNode(CU_Nodes->getOperand(i));
853 CompileUnit *CU = constructCompileUnit(CUNode);
854 DIArray GVs = CUNode.getGlobalVariables();
855 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000856 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000857 DIArray SPs = CUNode.getSubprograms();
858 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
859 constructSubprogramDIE(CU, SPs.getElement(i));
860 DIArray EnumTypes = CUNode.getEnumTypes();
861 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
862 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
863 DIArray RetainedTypes = CUNode.getRetainedTypes();
864 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
865 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
866 }
Devang Patel07bb9ee2011-08-15 23:47:24 +0000867 } else if (!collectLegacyDebugInfo(M))
868 return;
Devang Patele02e5852011-05-03 16:45:22 +0000869
Devang Patel07bb9ee2011-08-15 23:47:24 +0000870 collectInfoFromNamedMDNodes(M);
Eric Christopher6a841382012-11-19 22:42:10 +0000871
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000872 // Tell MMI that we have debug info.
873 MMI->setDebugInfoAvailability(true);
Eric Christopher6a841382012-11-19 22:42:10 +0000874
Bill Wendling2b128d72009-05-20 23:19:06 +0000875 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +0000876 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +0000877}
878
Eric Christopher960ac372012-11-22 00:59:49 +0000879// Attach DW_AT_inline attribute with inlined subprogram DIEs.
880void DwarfDebug::computeInlinedDIEs() {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000881 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
882 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopher735401c2012-11-27 00:13:51 +0000883 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000884 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +0000885 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000886 }
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000887 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopher735401c2012-11-27 00:13:51 +0000888 AE = AbstractSPDies.end(); AI != AE; ++AI) {
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000889 DIE *ISP = AI->second;
890 if (InlinedSubprogramDIEs.count(ISP))
891 continue;
892 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
893 }
Eric Christopher960ac372012-11-22 00:59:49 +0000894}
895
896// Collect info for variables that were optimized out.
897void DwarfDebug::collectDeadVariables() {
898 const Module *M = MMI->getModule();
899 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
900
901 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
902 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
903 DICompileUnit TheCU(CU_Nodes->getOperand(i));
904 DIArray Subprograms = TheCU.getSubprograms();
905 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopher735401c2012-11-27 00:13:51 +0000906 DISubprogram SP(Subprograms.getElement(i));
907 if (ProcessedSPNodes.count(SP) != 0) continue;
908 if (!SP.Verify()) continue;
909 if (!SP.isDefinition()) continue;
910 DIArray Variables = SP.getVariables();
911 if (Variables.getNumElements() == 0) continue;
Eric Christopher960ac372012-11-22 00:59:49 +0000912
Eric Christopher735401c2012-11-27 00:13:51 +0000913 LexicalScope *Scope =
914 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
915 DeadFnScopeMap[SP] = Scope;
Eric Christopher960ac372012-11-22 00:59:49 +0000916
Eric Christopher735401c2012-11-27 00:13:51 +0000917 // Construct subprogram DIE and add variables DIEs.
918 CompileUnit *SPCU = CUMap.lookup(TheCU);
919 assert(SPCU && "Unable to find Compile Unit!");
920 constructSubprogramDIE(SPCU, SP);
921 DIE *ScopeDIE = SPCU->getDIE(SP);
922 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
923 DIVariable DV(Variables.getElement(vi));
924 if (!DV.Verify()) continue;
925 DbgVariable *NewVar = new DbgVariable(DV, NULL);
926 if (DIE *VariableDIE =
927 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
928 ScopeDIE->addChild(VariableDIE);
929 }
Eric Christopher960ac372012-11-22 00:59:49 +0000930 }
931 }
932 }
933 DeleteContainerSeconds(DeadFnScopeMap);
934}
935
936void DwarfDebug::finalizeModuleInfo() {
937 // Collect info for variables that were optimized out.
938 collectDeadVariables();
939
940 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
941 computeInlinedDIEs();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000942
Eric Christopherb1b94512012-08-01 18:19:01 +0000943 // Emit DW_AT_containing_type attribute to connect types with their
944 // vtable holding type.
Devang Patel89543712011-08-15 17:24:54 +0000945 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
Eric Christopher735401c2012-11-27 00:13:51 +0000946 CUE = CUMap.end(); CUI != CUE; ++CUI) {
Devang Patel89543712011-08-15 17:24:54 +0000947 CompileUnit *TheCU = CUI->second;
948 TheCU->constructContainingTypeDIEs();
Devang Pateleb57c592009-12-03 19:11:07 +0000949 }
950
Eric Christopher960ac372012-11-22 00:59:49 +0000951 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000952 InfoHolder.computeSizeAndOffsets();
953 if (useSplitDwarf())
954 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +0000955}
956
957void DwarfDebug::endSections() {
Bill Wendling2b128d72009-05-20 23:19:06 +0000958 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000959 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000960 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000961 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000962 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +0000963
964 // End text sections.
Benjamin Kramer15591272012-10-31 13:45:49 +0000965 for (unsigned I = 0, E = SectionMap.size(); I != E; ++I) {
966 Asm->OutStreamer.SwitchSection(SectionMap[I]);
967 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", I+1));
Bill Wendling2b128d72009-05-20 23:19:06 +0000968 }
Eric Christopher960ac372012-11-22 00:59:49 +0000969}
Bill Wendling2b128d72009-05-20 23:19:06 +0000970
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000971// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +0000972void DwarfDebug::endModule() {
973
974 if (!FirstCU) return;
975
976 // End any existing sections.
977 // TODO: Does this need to happen?
978 endSections();
979
980 // Finalize the debug info for the module.
981 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000982
Eric Christophercdf218d2012-12-10 19:51:21 +0000983 if (!useSplitDwarf()) {
Eric Christopher95198f502012-11-27 22:43:42 +0000984 // Emit all the DIEs into a debug info section.
985 emitDebugInfo();
Eric Christopher4c9b1192012-11-27 00:41:54 +0000986
Eric Christopher95198f502012-11-27 22:43:42 +0000987 // Corresponding abbreviations into a abbrev section.
988 emitAbbreviations();
989
990 // Emit info into a debug loc section.
991 emitDebugLoc();
992
993 // Emit info into a debug aranges section.
994 emitDebugARanges();
995
996 // Emit info into a debug ranges section.
997 emitDebugRanges();
998
999 // Emit info into a debug macinfo section.
1000 emitDebugMacInfo();
1001
1002 // Emit inline info.
1003 // TODO: When we don't need the option anymore we
1004 // can remove all of the code that this section
1005 // depends upon.
1006 if (useDarwinGDBCompat())
1007 emitDebugInlineInfo();
1008 } else {
Eric Christopherd692c1d2012-12-11 19:42:09 +00001009 // TODO: Fill this in for separated debug sections and separate
Eric Christopher95198f502012-11-27 22:43:42 +00001010 // out information into new sections.
1011
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001012 // Emit the debug info section and compile units.
Eric Christopher95198f502012-11-27 22:43:42 +00001013 emitDebugInfo();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001014 emitDebugInfoDWO();
Eric Christopher95198f502012-11-27 22:43:42 +00001015
1016 // Corresponding abbreviations into a abbrev section.
1017 emitAbbreviations();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001018 emitDebugAbbrevDWO();
Eric Christopher95198f502012-11-27 22:43:42 +00001019
1020 // Emit info into a debug loc section.
1021 emitDebugLoc();
1022
1023 // Emit info into a debug aranges section.
1024 emitDebugARanges();
1025
1026 // Emit info into a debug ranges section.
1027 emitDebugRanges();
1028
1029 // Emit info into a debug macinfo section.
1030 emitDebugMacInfo();
1031
Eric Christopher962c9082013-01-15 23:56:56 +00001032 // Emit DWO addresses.
1033 InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
1034
Eric Christopher95198f502012-11-27 22:43:42 +00001035 // Emit inline info.
1036 // TODO: When we don't need the option anymore we
1037 // can remove all of the code that this section
1038 // depends upon.
1039 if (useDarwinGDBCompat())
1040 emitDebugInlineInfo();
1041 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001042
Eric Christophera876b822012-08-23 07:32:06 +00001043 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +00001044 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +00001045 emitAccelNames();
1046 emitAccelObjC();
1047 emitAccelNamespaces();
1048 emitAccelTypes();
1049 }
Eric Christopher6a841382012-11-19 22:42:10 +00001050
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001051 // Emit info into a debug pubnames section, if requested.
1052 if (GenerateDwarfPubNamesSection)
1053 emitDebugPubnames();
1054
Devang Patel04d2f2d2009-11-24 01:14:22 +00001055 // Emit info into a debug pubtypes section.
Eric Christopher77826182012-08-23 07:10:56 +00001056 // TODO: When we don't need the option anymore we can
1057 // remove all of the code that adds to the table.
Eric Christopher20b76a72012-08-23 22:36:40 +00001058 if (useDarwinGDBCompat())
Eric Christopher77826182012-08-23 07:10:56 +00001059 emitDebugPubTypes();
Devang Patel04d2f2d2009-11-24 01:14:22 +00001060
Eric Christopher95198f502012-11-27 22:43:42 +00001061 // Finally emit string information into a string table.
Eric Christopher6e20a162012-11-27 06:49:23 +00001062 emitDebugStr();
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001063 if (useSplitDwarf())
1064 emitDebugStrDWO();
Eric Christopher6e20a162012-11-27 06:49:23 +00001065
Devang Pateld0701282010-08-02 17:32:15 +00001066 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001067 SPMap.clear();
Devang Patel1a0df9a2010-05-10 22:49:55 +00001068 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1069 E = CUMap.end(); I != E; ++I)
1070 delete I->second;
Eric Christopher8afd7b62012-12-10 19:51:18 +00001071
Eric Christopher5b33b3c2013-02-06 21:53:56 +00001072 for (SmallVector<CompileUnit *, 1>::iterator I = SkeletonCUs.begin(),
1073 E = SkeletonCUs.end(); I != E; ++I)
1074 delete *I;
Eric Christopher8afd7b62012-12-10 19:51:18 +00001075
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001076 // Reset these for the next Module if we have one.
1077 FirstCU = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001078}
1079
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001080// Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001081DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001082 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001083 LLVMContext &Ctx = DV->getContext();
1084 // More then one inlined variable corresponds to one abstract variable.
1085 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001086 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001087 if (AbsDbgVariable)
1088 return AbsDbgVariable;
1089
Devang Patel7e623022011-08-10 20:55:27 +00001090 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001091 if (!Scope)
1092 return NULL;
1093
Devang Patel99819b52011-08-15 19:01:20 +00001094 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patel7e623022011-08-10 20:55:27 +00001095 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001096 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001097 return AbsDbgVariable;
1098}
1099
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001100// If Var is a current function argument then add it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +00001101bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +00001102 DbgVariable *Var, LexicalScope *Scope) {
1103 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001104 return false;
1105 DIVariable DV = Var->getVariable();
1106 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1107 return false;
1108 unsigned ArgNo = DV.getArgNumber();
Eric Christopher6a841382012-11-19 22:42:10 +00001109 if (ArgNo == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001110 return false;
1111
Devang Patel4ab660b2011-03-03 20:02:02 +00001112 size_t Size = CurrentFnArguments.size();
1113 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001114 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001115 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001116 // arguments does the function have at source level.
1117 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001118 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001119 CurrentFnArguments[ArgNo - 1] = Var;
1120 return true;
1121}
1122
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001123// Collect variable information from side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001124void
Nick Lewycky019d2552011-07-29 03:49:23 +00001125DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patel490c8ab2010-05-20 19:57:06 +00001126 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +00001127 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1128 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1129 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00001130 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001131 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001132 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001133 DIVariable DV(Var);
1134 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001135
Devang Patel7e623022011-08-10 20:55:27 +00001136 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001137
Devang Patelcdb7d442009-11-10 23:20:04 +00001138 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001139 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001140 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001141
Devang Patele1c53f22010-05-20 16:36:41 +00001142 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel99819b52011-08-15 19:01:20 +00001143 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patel3e4a9652011-08-15 21:24:36 +00001144 RegVar->setFrameIndex(VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +00001145 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001146 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001147 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +00001148 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +00001149 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001150}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001151
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001152// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1153// defined reg.
Devang Patel9fc11702010-05-25 23:40:22 +00001154static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001155 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001156 return MI->getNumOperands() == 3 &&
1157 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
1158 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +00001159}
1160
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001161// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher6a841382012-11-19 22:42:10 +00001162static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1163 const MCSymbol *FLabel,
Devang Patel2442a892011-07-08 17:09:57 +00001164 const MCSymbol *SLabel,
1165 const MachineInstr *MI) {
1166 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1167
1168 if (MI->getNumOperands() != 3) {
1169 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1170 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1171 }
1172 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1173 MachineLocation MLoc;
1174 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1175 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1176 }
1177 if (MI->getOperand(0).isImm())
1178 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1179 if (MI->getOperand(0).isFPImm())
1180 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1181 if (MI->getOperand(0).isCImm())
1182 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1183
Craig Topperee4dab52012-02-05 08:31:47 +00001184 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001185}
1186
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001187// Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001188void
Devang Patel5c0f85c2010-06-25 22:07:34 +00001189DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1190 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001191
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001192 // collection info from MMI table.
Devang Patel490c8ab2010-05-20 19:57:06 +00001193 collectVariableInfoFromMMITable(MF, Processed);
1194
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001195 for (SmallVectorImpl<const MDNode*>::const_iterator
1196 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1197 ++UVI) {
1198 const MDNode *Var = *UVI;
1199 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001200 continue;
1201
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001202 // History contains relevant DBG_VALUE instructions for Var and instructions
1203 // clobbering it.
1204 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1205 if (History.empty())
1206 continue;
1207 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001208
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001209 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001210 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001211 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1212 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001213 Scope = LScopes.getCurrentFunctionScope();
Devang Patel8fb9fd62011-07-20 22:18:50 +00001214 else {
1215 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patel7e623022011-08-10 20:55:27 +00001216 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel8fb9fd62011-07-20 22:18:50 +00001217 else {
1218 if (MDNode *IA = DV.getInlinedAt())
Devang Patel7e623022011-08-10 20:55:27 +00001219 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001220 else
Devang Patel7e623022011-08-10 20:55:27 +00001221 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001222 }
1223 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001224 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001225 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001226 continue;
1227
1228 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001229 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001230 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1231 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel6c622ef2011-03-01 22:58:55 +00001232 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001233 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001234 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001235 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001236
Eric Christophercc10d202012-10-08 20:48:54 +00001237 // Simplify ranges that are fully coalesced.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001238 if (History.size() <= 1 || (History.size() == 2 &&
1239 MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001240 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001241 continue;
1242 }
1243
Eric Christopher59cc0712013-01-28 17:33:26 +00001244 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001245 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001246
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001247 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1248 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1249 const MachineInstr *Begin = *HI;
1250 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001251
Devang Patele7181b52011-06-01 23:00:17 +00001252 // Check if DBG_VALUE is truncating a range.
1253 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1254 && !Begin->getOperand(0).getReg())
1255 continue;
1256
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001257 // Compute the range for a register location.
1258 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1259 const MCSymbol *SLabel = 0;
1260
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001261 if (HI + 1 == HE)
1262 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001263 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001264 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001265 else {
1266 const MachineInstr *End = HI[1];
Eric Christopher6a841382012-11-19 22:42:10 +00001267 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Devang Patel53b050a2011-07-07 21:44:42 +00001268 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001269 if (End->isDebugValue())
1270 SLabel = getLabelBeforeInsn(End);
1271 else {
1272 // End is a normal instruction clobbering the range.
1273 SLabel = getLabelAfterInsn(End);
1274 assert(SLabel && "Forgot label after clobber instruction");
1275 ++HI;
1276 }
1277 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001278
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001279 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopher365d0832011-12-16 23:42:31 +00001280 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1281 Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001282 }
1283 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001284 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001285
1286 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001287 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1288 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1289 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1290 DIVariable DV(Variables.getElement(i));
1291 if (!DV || !DV.Verify() || !Processed.insert(DV))
1292 continue;
1293 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1294 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patele0a94bf2010-05-14 21:01:35 +00001295 }
Devang Patel9fc11702010-05-25 23:40:22 +00001296}
Devang Patele0a94bf2010-05-14 21:01:35 +00001297
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001298// Return Label preceding the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001299MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001300 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1301 assert(Label && "Didn't insert label before instruction");
1302 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001303}
1304
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001305// Return Label immediately following the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001306MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001307 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001308}
1309
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001310// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001311void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001312 // Check if source location changes, but ignore DBG_VALUE locations.
1313 if (!MI->isDebugValue()) {
1314 DebugLoc DL = MI->getDebugLoc();
1315 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001316 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001317 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001318 if (DL == PrologEndLoc) {
1319 Flags |= DWARF2_FLAG_PROLOGUE_END;
1320 PrologEndLoc = DebugLoc();
1321 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001322 if (PrologEndLoc.isUnknown())
1323 Flags |= DWARF2_FLAG_IS_STMT;
1324
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001325 if (!DL.isUnknown()) {
1326 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001327 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001328 } else
Devang Patel34a66202011-05-11 19:22:19 +00001329 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001330 }
Devang Patel9fc11702010-05-25 23:40:22 +00001331 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001332
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001333 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001334 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1335 LabelsBeforeInsn.find(MI);
1336
1337 // No label needed.
1338 if (I == LabelsBeforeInsn.end())
1339 return;
1340
1341 // Label already assigned.
1342 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001343 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001344
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001345 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001346 PrevLabel = MMI->getContext().CreateTempSymbol();
1347 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001348 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001349 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001350}
1351
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001352// Process end of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001353void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001354 // Don't create a new label after DBG_VALUE instructions.
1355 // They don't generate code.
1356 if (!MI->isDebugValue())
1357 PrevLabel = 0;
1358
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001359 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1360 LabelsAfterInsn.find(MI);
1361
1362 // No label needed.
1363 if (I == LabelsAfterInsn.end())
1364 return;
1365
1366 // Label already assigned.
1367 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001368 return;
1369
1370 // We need a label after this instruction.
1371 if (!PrevLabel) {
1372 PrevLabel = MMI->getContext().CreateTempSymbol();
1373 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001374 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001375 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001376}
1377
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001378// Each LexicalScope has first instruction and last instruction to mark
1379// beginning and end of a scope respectively. Create an inverse map that list
1380// scopes starts (and ends) with an instruction. One instruction may start (or
1381// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001382void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001383 SmallVector<LexicalScope *, 4> WorkList;
1384 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001385 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001386 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001387
Devang Patel7e623022011-08-10 20:55:27 +00001388 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001389 if (!Children.empty())
Devang Patel7e623022011-08-10 20:55:27 +00001390 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001391 SE = Children.end(); SI != SE; ++SI)
1392 WorkList.push_back(*SI);
1393
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001394 if (S->isAbstractScope())
1395 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001396
Devang Patel7e623022011-08-10 20:55:27 +00001397 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001398 if (Ranges.empty())
1399 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001400 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +00001401 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001402 assert(RI->first && "InsnRange does not have first instruction!");
1403 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001404 requestLabelBeforeInsn(RI->first);
1405 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001406 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001407 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001408}
1409
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001410// Get MDNode for DebugLoc's scope.
Devang Patel589845d2011-05-09 22:14:49 +00001411static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1412 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1413 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1414 return DL.getScope(Ctx);
1415}
1416
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001417// Walk up the scope chain of given debug loc and find line number info
1418// for the function.
Devang Patel34a66202011-05-11 19:22:19 +00001419static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1420 const MDNode *Scope = getScopeNode(DL, Ctx);
1421 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher34164192012-04-03 00:43:49 +00001422 if (SP.Verify()) {
1423 // Check for number of operands since the compatibility is
1424 // cheap here.
Eric Christopherb81e2b42012-04-03 17:55:42 +00001425 if (SP->getNumOperands() > 19)
Eric Christopher34164192012-04-03 00:43:49 +00001426 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1427 else
1428 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1429 }
1430
Devang Patel34a66202011-05-11 19:22:19 +00001431 return DebugLoc();
1432}
1433
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001434// Gather pre-function debug information. Assumes being called immediately
1435// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001436void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001437 if (!MMI->hasDebugInfo()) return;
Devang Patel7e623022011-08-10 20:55:27 +00001438 LScopes.initialize(*MF);
1439 if (LScopes.empty()) return;
1440 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001441
Manman Ren4e042a62013-02-05 21:52:47 +00001442 // Set DwarfCompileUnitID in MCContext to the Compile Unit this function
1443 // belongs to.
1444 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1445 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
1446 assert(TheCU && "Unable to find compile unit!");
1447 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
1448
Devang Patel6c74a872010-04-27 19:46:33 +00001449 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1450 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001451 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001452 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001453
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001454 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1455
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001456 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001457 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001458 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1459
Devang Patel002d54d2010-05-26 19:37:24 +00001460 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001461 I != E; ++I) {
1462 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001463 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1464 II != IE; ++II) {
1465 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001466
Devang Patel002d54d2010-05-26 19:37:24 +00001467 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001468 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001469
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001470 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001471 const MDNode *Var =
1472 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001473
1474 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001475 if (isDbgValueInDefinedReg(MI))
1476 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1477
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001478 // Check the history of this variable.
1479 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1480 if (History.empty()) {
1481 UserVariables.push_back(Var);
1482 // The first mention of a function argument gets the FunctionBeginSym
1483 // label, so arguments are visible when breaking at function entry.
1484 DIVariable DV(Var);
1485 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1486 DISubprogram(getDISubprogram(DV.getContext()))
1487 .describes(MF->getFunction()))
1488 LabelsBeforeInsn[MI] = FunctionBeginSym;
1489 } else {
1490 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1491 const MachineInstr *Prev = History.back();
1492 if (Prev->isDebugValue()) {
1493 // Coalesce identical entries at the end of History.
1494 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001495 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher85a495e2012-10-08 20:48:49 +00001496 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopher6a841382012-11-19 22:42:10 +00001497 << "\t" << *Prev
Devang Patelb7a328e2011-07-07 00:14:27 +00001498 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001499 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001500 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001501
1502 // Terminate old register assignments that don't reach MI;
1503 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1504 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1505 isDbgValueInDefinedReg(Prev)) {
1506 // Previous register assignment needs to terminate at the end of
1507 // its basic block.
1508 MachineBasicBlock::const_iterator LastMI =
1509 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001510 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001511 // Drop DBG_VALUE for empty range.
Eric Christopher85a495e2012-10-08 20:48:49 +00001512 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Devang Patelb7a328e2011-07-07 00:14:27 +00001513 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001514 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001515 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001516 else {
1517 // Terminate after LastMI.
1518 History.push_back(LastMI);
1519 }
1520 }
1521 }
1522 }
1523 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001524 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001525 // Not a DBG_VALUE instruction.
1526 if (!MI->isLabel())
1527 AtBlockEntry = false;
1528
Eric Christopher133195782012-10-04 20:46:14 +00001529 // First known non-DBG_VALUE and non-frame setup location marks
1530 // the beginning of the function body.
1531 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1532 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel34a66202011-05-11 19:22:19 +00001533 PrologEndLoc = MI->getDebugLoc();
1534
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001535 // Check if the instruction clobbers any registers with debug vars.
1536 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1537 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1538 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1539 continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001540 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1541 AI.isValid(); ++AI) {
1542 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001543 const MDNode *Var = LiveUserVar[Reg];
1544 if (!Var)
1545 continue;
1546 // Reg is now clobbered.
1547 LiveUserVar[Reg] = 0;
1548
1549 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001550 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1551 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001552 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001553 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1554 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001555 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001556 const MachineInstr *Prev = History.back();
1557 // Sanity-check: Register assignments are terminated at the end of
1558 // their block.
1559 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1560 continue;
1561 // Is the variable still in Reg?
1562 if (!isDbgValueInDefinedReg(Prev) ||
1563 Prev->getOperand(0).getReg() != Reg)
1564 continue;
1565 // Var is clobbered. Make sure the next instruction gets a label.
1566 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001567 }
1568 }
Devang Patel002d54d2010-05-26 19:37:24 +00001569 }
Devang Patel002d54d2010-05-26 19:37:24 +00001570 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001571 }
1572
1573 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1574 I != E; ++I) {
1575 SmallVectorImpl<const MachineInstr*> &History = I->second;
1576 if (History.empty())
1577 continue;
1578
1579 // Make sure the final register assignments are terminated.
1580 const MachineInstr *Prev = History.back();
1581 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1582 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher6a841382012-11-19 22:42:10 +00001583 MachineBasicBlock::const_iterator LastMI =
Devang Patel784077e2011-08-10 23:58:09 +00001584 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001585 if (LastMI == PrevMBB->end())
1586 // Drop DBG_VALUE for empty range.
1587 History.pop_back();
1588 else {
1589 // Terminate after LastMI.
1590 History.push_back(LastMI);
1591 }
1592 }
1593 // Request labels for the full history.
1594 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1595 const MachineInstr *MI = History[i];
1596 if (MI->isDebugValue())
1597 requestLabelBeforeInsn(MI);
1598 else
1599 requestLabelAfterInsn(MI);
1600 }
1601 }
Devang Patel002d54d2010-05-26 19:37:24 +00001602
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001603 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001604 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001605
1606 // Record beginning of function.
1607 if (!PrologEndLoc.isUnknown()) {
1608 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1609 MF->getFunction()->getContext());
1610 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1611 FnStartDL.getScope(MF->getFunction()->getContext()),
David Blaikie67cb31e2012-12-04 22:02:33 +00001612 // We'd like to list the prologue as "not statements" but GDB behaves
1613 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
David Blaikie5a773bb2012-12-04 21:05:36 +00001614 DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001615 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001616}
1617
Devang Patel7e623022011-08-10 20:55:27 +00001618void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1619// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1620 ScopeVariables[LS].push_back(Var);
1621// Vars.push_back(Var);
1622}
1623
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001624// Gather and emit post-function debug information.
Chris Lattner76555b52010-01-26 23:18:02 +00001625void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patel7e623022011-08-10 20:55:27 +00001626 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00001627
Devang Patel7e623022011-08-10 20:55:27 +00001628 // Define end label for subprogram.
1629 FunctionEndSym = Asm->GetTempSymbol("func_end",
1630 Asm->getFunctionNumber());
1631 // Assumes in correct section after the entry point.
1632 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Manman Ren4e042a62013-02-05 21:52:47 +00001633 // Set DwarfCompileUnitID in MCContext to default value.
1634 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001635
Devang Patel7e623022011-08-10 20:55:27 +00001636 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1637 collectVariableInfo(MF, ProcessedVars);
Eric Christopher6a841382012-11-19 22:42:10 +00001638
Devang Patel3acc70e2011-08-15 22:04:40 +00001639 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001640 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001641 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001642
Devang Patel7e623022011-08-10 20:55:27 +00001643 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001644 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1645 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1646 LexicalScope *AScope = AList[i];
1647 DISubprogram SP(AScope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +00001648 if (SP.Verify()) {
1649 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001650 DIArray Variables = SP.getVariables();
1651 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1652 DIVariable DV(Variables.getElement(i));
1653 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1654 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001655 // Check that DbgVariable for DV wasn't created earlier, when
1656 // findAbstractVariable() was called for inlined instance of DV.
1657 LLVMContext &Ctx = DV->getContext();
1658 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1659 if (AbstractVariables.lookup(CleanDV))
1660 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001661 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1662 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001663 }
1664 }
Devang Patel44403472011-08-12 18:10:19 +00001665 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Patel3acc70e2011-08-15 22:04:40 +00001666 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001667 }
Eric Christopher6a841382012-11-19 22:42:10 +00001668
Devang Patel3acc70e2011-08-15 22:04:40 +00001669 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Eric Christopher6a841382012-11-19 22:42:10 +00001670
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001671 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopherbb69a272012-08-24 01:14:27 +00001672 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001673
Devang Patel7e623022011-08-10 20:55:27 +00001674 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1675 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00001676
Bill Wendling2b128d72009-05-20 23:19:06 +00001677 // Clear debug info
Devang Patel7e623022011-08-10 20:55:27 +00001678 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1679 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1680 DeleteContainerPointers(I->second);
1681 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001682 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001683 UserVariables.clear();
1684 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001685 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001686 LabelsBeforeInsn.clear();
1687 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001688 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001689}
1690
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001691// Register a source line with debug info. Returns the unique label that was
1692// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001693void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1694 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001695 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001696 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001697 unsigned Src = 1;
1698 if (S) {
1699 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001700
Dan Gohman50849c62010-05-05 23:41:32 +00001701 if (Scope.isCompileUnit()) {
1702 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001703 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001704 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001705 } else if (Scope.isFile()) {
1706 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001707 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001708 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001709 } else if (Scope.isSubprogram()) {
1710 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001711 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001712 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001713 } else if (Scope.isLexicalBlockFile()) {
1714 DILexicalBlockFile DBF(S);
1715 Fn = DBF.getFilename();
1716 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001717 } else if (Scope.isLexicalBlock()) {
1718 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001719 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001720 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001721 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001722 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001723
Manman Ren1e427202013-03-07 01:42:00 +00001724 Src = getOrCreateSourceID(Fn, Dir,
1725 Asm->OutStreamer.getContext().getDwarfCompileUnitID());
Dan Gohman50849c62010-05-05 23:41:32 +00001726 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001727 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001728}
1729
Bill Wendling806535f2009-05-20 23:22:40 +00001730//===----------------------------------------------------------------------===//
1731// Emit Methods
1732//===----------------------------------------------------------------------===//
1733
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001734// Compute the size and offset of a DIE.
Jim Grosbach00e9c612009-11-22 19:20:36 +00001735unsigned
Eric Christopherc8a310e2012-12-10 23:34:43 +00001736DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling480ff322009-05-20 23:21:38 +00001737 // Get the children.
1738 const std::vector<DIE *> &Children = Die->getChildren();
1739
Bill Wendling480ff322009-05-20 23:21:38 +00001740 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001741 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001742
1743 // Get the abbreviation for this DIE.
1744 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopherc8a310e2012-12-10 23:34:43 +00001745 const DIEAbbrev *Abbrev = Abbreviations->at(AbbrevNumber - 1);
Bill Wendling480ff322009-05-20 23:21:38 +00001746
1747 // Set DIE offset
1748 Die->setOffset(Offset);
1749
1750 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001751 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001752
1753 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1754 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1755
1756 // Size the DIE attribute values.
1757 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1758 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001759 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001760
1761 // Size the DIE children if any.
1762 if (!Children.empty()) {
1763 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1764 "Children flag not set");
1765
1766 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher1f0cbb82012-11-20 22:14:13 +00001767 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling480ff322009-05-20 23:21:38 +00001768
1769 // End of children marker.
1770 Offset += sizeof(int8_t);
1771 }
1772
1773 Die->setSize(Offset - Die->getOffset());
1774 return Offset;
1775}
1776
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001777// Compute the size and offset of all the DIEs.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001778void DwarfUnits::computeSizeAndOffsets() {
1779 for (SmallVector<CompileUnit *, 1>::iterator I = CUs.begin(),
1780 E = CUs.end(); I != E; ++I) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001781 unsigned Offset =
1782 sizeof(int32_t) + // Length of Compilation Unit Info
1783 sizeof(int16_t) + // DWARF version number
1784 sizeof(int32_t) + // Offset Into Abbrev. Section
1785 sizeof(int8_t); // Pointer Size (in bytes)
1786
Eric Christopherc8a310e2012-12-10 23:34:43 +00001787 computeSizeAndOffset((*I)->getCUDie(), Offset);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001788 }
Bill Wendling480ff322009-05-20 23:21:38 +00001789}
1790
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001791// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001792void DwarfDebug::emitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001793 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001794
Bill Wendling480ff322009-05-20 23:21:38 +00001795 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001796 DwarfInfoSectionSym =
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001797 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001798 DwarfAbbrevSectionSym =
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001799 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher3c5a1912012-12-19 22:02:53 +00001800 if (useSplitDwarf())
1801 DwarfAbbrevDWOSectionSym =
1802 emitSectionSym(Asm, TLOF.getDwarfAbbrevDWOSection(),
1803 "section_abbrev_dwo");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001804 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001805
Chris Lattner6629ca92010-04-04 22:59:04 +00001806 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001807 emitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001808
Eric Christopher74804332013-02-07 21:19:50 +00001809 DwarfLineSectionSym =
1810 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001811 emitSectionSym(Asm, TLOF.getDwarfLocSection());
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001812 if (GenerateDwarfPubNamesSection)
1813 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001814 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001815 DwarfStrSectionSym =
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001816 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
1817 if (useSplitDwarf())
1818 DwarfStrDWOSectionSym =
1819 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001820 DwarfDebugRangeSectionSym = emitSectionSym(Asm, TLOF.getDwarfRangesSection(),
Devang Patel12563b32010-04-16 23:33:45 +00001821 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001822
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001823 DwarfDebugLocSectionSym = emitSectionSym(Asm, TLOF.getDwarfLocSection(),
Devang Patel9fc11702010-05-25 23:40:22 +00001824 "section_debug_loc");
1825
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001826 TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
1827 emitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001828}
1829
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001830// Recursively emits a debug information entry.
Eric Christopher3c5a1912012-12-19 22:02:53 +00001831void DwarfDebug::emitDIE(DIE *Die, std::vector<DIEAbbrev *> *Abbrevs) {
Bill Wendling480ff322009-05-20 23:21:38 +00001832 // Get the abbreviation for this DIE.
1833 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001834 const DIEAbbrev *Abbrev = Abbrevs->at(AbbrevNumber - 1);
Bill Wendling480ff322009-05-20 23:21:38 +00001835
Bill Wendling480ff322009-05-20 23:21:38 +00001836 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001837 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00001838 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1839 Twine::utohexstr(Die->getOffset()) + ":0x" +
1840 Twine::utohexstr(Die->getSize()) + " " +
1841 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00001842 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001843
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001844 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00001845 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1846
1847 // Emit the DIE attribute values.
1848 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1849 unsigned Attr = AbbrevData[i].getAttribute();
1850 unsigned Form = AbbrevData[i].getForm();
1851 assert(Form && "Too many attributes for DIE (check abbreviation)");
1852
Chris Lattner7bde8c02010-04-04 18:52:31 +00001853 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001854 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001855
Bill Wendling480ff322009-05-20 23:21:38 +00001856 switch (Attr) {
Bill Wendling480ff322009-05-20 23:21:38 +00001857 case dwarf::DW_AT_abstract_origin: {
1858 DIEEntry *E = cast<DIEEntry>(Values[i]);
1859 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001860 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00001861 Asm->EmitInt32(Addr);
1862 break;
1863 }
Devang Patel12563b32010-04-16 23:33:45 +00001864 case dwarf::DW_AT_ranges: {
1865 // DW_AT_range Value encodes offset in debug_range section.
1866 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00001867
Nick Lewycky33da3362012-06-22 01:25:12 +00001868 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelda3ef852010-09-02 16:43:44 +00001869 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1870 V->getValue(),
1871 4);
1872 } else {
1873 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1874 V->getValue(),
1875 DwarfDebugRangeSectionSym,
1876 4);
1877 }
Devang Patel12563b32010-04-16 23:33:45 +00001878 break;
1879 }
Devang Patel9fc11702010-05-25 23:40:22 +00001880 case dwarf::DW_AT_location: {
Nick Lewycky33da3362012-06-22 01:25:12 +00001881 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1882 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1883 Asm->EmitLabelReference(L->getValue(), 4);
1884 else
1885 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1886 } else {
Devang Patel9fc11702010-05-25 23:40:22 +00001887 Values[i]->EmitValue(Asm, Form);
Nick Lewycky33da3362012-06-22 01:25:12 +00001888 }
Devang Patel9fc11702010-05-25 23:40:22 +00001889 break;
1890 }
Devang Patela1bd5a12010-09-29 19:08:08 +00001891 case dwarf::DW_AT_accessibility: {
1892 if (Asm->isVerbose()) {
1893 DIEInteger *V = cast<DIEInteger>(Values[i]);
1894 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1895 }
1896 Values[i]->EmitValue(Asm, Form);
1897 break;
1898 }
Bill Wendling480ff322009-05-20 23:21:38 +00001899 default:
1900 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001901 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001902 break;
1903 }
Bill Wendling480ff322009-05-20 23:21:38 +00001904 }
1905
1906 // Emit the DIE children if any.
1907 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1908 const std::vector<DIE *> &Children = Die->getChildren();
1909
1910 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher3c5a1912012-12-19 22:02:53 +00001911 emitDIE(Children[j], Abbrevs);
Bill Wendling480ff322009-05-20 23:21:38 +00001912
Chris Lattner7bde8c02010-04-04 18:52:31 +00001913 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00001914 Asm->OutStreamer.AddComment("End Of Children Mark");
1915 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001916 }
1917}
1918
Eric Christophera2de8262012-12-15 00:04:07 +00001919// Emit the various dwarf units to the unit section USection with
1920// the abbreviations going into ASection.
1921void DwarfUnits::emitUnits(DwarfDebug *DD,
1922 const MCSection *USection,
1923 const MCSection *ASection,
1924 const MCSymbol *ASectionSym) {
1925 Asm->OutStreamer.SwitchSection(USection);
1926 for (SmallVector<CompileUnit *, 1>::iterator I = CUs.begin(),
1927 E = CUs.end(); I != E; ++I) {
1928 CompileUnit *TheCU = *I;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001929 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001930
Devang Patel1a0df9a2010-05-10 22:49:55 +00001931 // Emit the compile units header.
Eric Christophera2de8262012-12-15 00:04:07 +00001932 Asm->OutStreamer
1933 .EmitLabel(Asm->GetTempSymbol(USection->getLabelBeginName(),
1934 TheCU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001935
Devang Patel1a0df9a2010-05-10 22:49:55 +00001936 // Emit size of content not including length itself
1937 unsigned ContentSize = Die->getSize() +
1938 sizeof(int16_t) + // DWARF version number
1939 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00001940 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001941
Devang Patel1a0df9a2010-05-10 22:49:55 +00001942 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1943 Asm->EmitInt32(ContentSize);
1944 Asm->OutStreamer.AddComment("DWARF version number");
1945 Asm->EmitInt16(dwarf::DWARF_VERSION);
1946 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christophera2de8262012-12-15 00:04:07 +00001947 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
1948 ASectionSym);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001949 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth5da3f052012-11-01 09:14:31 +00001950 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001951
Eric Christopher3c5a1912012-12-19 22:02:53 +00001952 DD->emitDIE(Die, Abbreviations);
Eric Christophera2de8262012-12-15 00:04:07 +00001953 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(USection->getLabelEndName(),
Eli Benderskyb42d1462012-12-03 18:45:45 +00001954 TheCU->getUniqueID()));
Devang Patel1a0df9a2010-05-10 22:49:55 +00001955 }
Bill Wendling480ff322009-05-20 23:21:38 +00001956}
1957
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001958// Emit the debug info section.
1959void DwarfDebug::emitDebugInfo() {
Eric Christophera2de8262012-12-15 00:04:07 +00001960 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1961
1962 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
1963 Asm->getObjFileLowering().getDwarfAbbrevSection(),
1964 DwarfAbbrevSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001965}
1966
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001967// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00001968void DwarfDebug::emitAbbreviations() {
Eric Christopher3c5a1912012-12-19 22:02:53 +00001969 if (!useSplitDwarf())
1970 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
1971 &Abbreviations);
1972 else
1973 emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
1974}
Bill Wendling480ff322009-05-20 23:21:38 +00001975
Eric Christopher3c5a1912012-12-19 22:02:53 +00001976void DwarfDebug::emitAbbrevs(const MCSection *Section,
1977 std::vector<DIEAbbrev *> *Abbrevs) {
1978 // Check to see if it is worth the effort.
1979 if (!Abbrevs->empty()) {
1980 // Start the debug abbrev section.
1981 Asm->OutStreamer.SwitchSection(Section);
1982
1983 MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Eric Christopher996b2b72012-12-13 03:00:38 +00001984 Asm->OutStreamer.EmitLabel(Begin);
Bill Wendling480ff322009-05-20 23:21:38 +00001985
1986 // For each abbrevation.
Eric Christopher3c5a1912012-12-19 22:02:53 +00001987 for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
Bill Wendling480ff322009-05-20 23:21:38 +00001988 // Get abbreviation data
Eric Christopher3c5a1912012-12-19 22:02:53 +00001989 const DIEAbbrev *Abbrev = Abbrevs->at(i);
Bill Wendling480ff322009-05-20 23:21:38 +00001990
1991 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00001992 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00001993
1994 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001995 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00001996 }
1997
1998 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00001999 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002000
Eric Christopher3c5a1912012-12-19 22:02:53 +00002001 MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Eric Christopher996b2b72012-12-13 03:00:38 +00002002 Asm->OutStreamer.EmitLabel(End);
Bill Wendling480ff322009-05-20 23:21:38 +00002003 }
2004}
2005
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002006// Emit the last address of the section and the end of the line matrix.
Devang Patel930143b2009-11-21 02:48:08 +00002007void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002008 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002009 Asm->OutStreamer.AddComment("Extended Op");
2010 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002011
Chris Lattner566cae92010-03-09 23:52:58 +00002012 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002013 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002014 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2015 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2016
2017 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002018
Chris Lattnera179b522010-04-04 19:25:43 +00002019 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Eric Christopherce0cfce2013-01-09 01:35:34 +00002020 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002021
2022 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002023 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2024 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002025 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002026 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002027}
2028
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002029// Emit visible names into a hashed accelerator table section.
Eric Christopher4996c702011-11-07 09:24:32 +00002030void DwarfDebug::emitAccelNames() {
2031 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2032 dwarf::DW_FORM_data4));
2033 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2034 E = CUMap.end(); I != E; ++I) {
2035 CompileUnit *TheCU = I->second;
Eric Christopherd9843b32011-11-10 19:25:34 +00002036 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
2037 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00002038 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2039 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00002040 const std::vector<DIE *> &Entities = GI->second;
Eric Christopherd9843b32011-11-10 19:25:34 +00002041 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2042 DE = Entities.end(); DI != DE; ++DI)
2043 AT.AddName(Name, (*DI));
Eric Christopher4996c702011-11-07 09:24:32 +00002044 }
2045 }
2046
2047 AT.FinalizeTable(Asm, "Names");
2048 Asm->OutStreamer.SwitchSection(
2049 Asm->getObjFileLowering().getDwarfAccelNamesSection());
2050 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
2051 Asm->OutStreamer.EmitLabel(SectionBegin);
2052
2053 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002054 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002055}
2056
Eric Christopher48fef592012-12-20 21:58:40 +00002057// Emit objective C classes and categories into a hashed accelerator table
2058// section.
Eric Christopher4996c702011-11-07 09:24:32 +00002059void DwarfDebug::emitAccelObjC() {
2060 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2061 dwarf::DW_FORM_data4));
2062 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2063 E = CUMap.end(); I != E; ++I) {
2064 CompileUnit *TheCU = I->second;
2065 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
2066 for (StringMap<std::vector<DIE*> >::const_iterator
2067 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2068 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00002069 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher4996c702011-11-07 09:24:32 +00002070 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2071 DE = Entities.end(); DI != DE; ++DI)
2072 AT.AddName(Name, (*DI));
2073 }
2074 }
2075
2076 AT.FinalizeTable(Asm, "ObjC");
2077 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2078 .getDwarfAccelObjCSection());
2079 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2080 Asm->OutStreamer.EmitLabel(SectionBegin);
2081
2082 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002083 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002084}
2085
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002086// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002087void DwarfDebug::emitAccelNamespaces() {
2088 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2089 dwarf::DW_FORM_data4));
2090 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2091 E = CUMap.end(); I != E; ++I) {
2092 CompileUnit *TheCU = I->second;
Eric Christopher66b37db2011-11-10 21:47:55 +00002093 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
2094 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00002095 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2096 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00002097 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher66b37db2011-11-10 21:47:55 +00002098 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2099 DE = Entities.end(); DI != DE; ++DI)
2100 AT.AddName(Name, (*DI));
Eric Christopher4996c702011-11-07 09:24:32 +00002101 }
2102 }
2103
2104 AT.FinalizeTable(Asm, "namespac");
2105 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2106 .getDwarfAccelNamespaceSection());
2107 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2108 Asm->OutStreamer.EmitLabel(SectionBegin);
2109
2110 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002111 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002112}
2113
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002114// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002115void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00002116 std::vector<DwarfAccelTable::Atom> Atoms;
2117 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2118 dwarf::DW_FORM_data4));
2119 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
2120 dwarf::DW_FORM_data2));
2121 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
2122 dwarf::DW_FORM_data1));
2123 DwarfAccelTable AT(Atoms);
Eric Christopher4996c702011-11-07 09:24:32 +00002124 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2125 E = CUMap.end(); I != E; ++I) {
2126 CompileUnit *TheCU = I->second;
Eric Christopher21bde872012-01-06 04:35:23 +00002127 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
2128 = TheCU->getAccelTypes();
2129 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00002130 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
2131 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00002132 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopher21bde872012-01-06 04:35:23 +00002133 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
2134 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
2135 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher4996c702011-11-07 09:24:32 +00002136 }
2137 }
2138
2139 AT.FinalizeTable(Asm, "types");
2140 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2141 .getDwarfAccelTypesSection());
2142 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2143 Asm->OutStreamer.EmitLabel(SectionBegin);
2144
2145 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002146 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002147}
2148
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002149/// emitDebugPubnames - Emit visible names into a debug pubnames section.
2150///
2151void DwarfDebug::emitDebugPubnames() {
2152 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2153
2154 typedef DenseMap<const MDNode*, CompileUnit*> CUMapType;
2155 for (CUMapType::iterator I = CUMap.begin(), E = CUMap.end(); I != E; ++I) {
2156 CompileUnit *TheCU = I->second;
2157 unsigned ID = TheCU->getUniqueID();
2158
2159 if (TheCU->getGlobalNames().empty())
2160 continue;
2161
2162 // Start the dwarf pubnames section.
2163 Asm->OutStreamer.SwitchSection(
2164 Asm->getObjFileLowering().getDwarfPubNamesSection());
2165
2166 Asm->OutStreamer.AddComment("Length of Public Names Info");
2167 Asm->EmitLabelDifference(Asm->GetTempSymbol("pubnames_end", ID),
2168 Asm->GetTempSymbol("pubnames_begin", ID), 4);
2169
2170 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin", ID));
2171
2172 Asm->OutStreamer.AddComment("DWARF Version");
2173 Asm->EmitInt16(dwarf::DWARF_VERSION);
2174
2175 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2176 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2177 DwarfInfoSectionSym);
2178
2179 Asm->OutStreamer.AddComment("Compilation Unit Length");
2180 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(), ID),
2181 Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2182 4);
2183
2184 const StringMap<DIE*> &Globals = TheCU->getGlobalNames();
2185 for (StringMap<DIE*>::const_iterator
2186 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2187 const char *Name = GI->getKeyData();
2188 const DIE *Entity = GI->second;
2189
2190 Asm->OutStreamer.AddComment("DIE offset");
2191 Asm->EmitInt32(Entity->getOffset());
2192
2193 if (Asm->isVerbose())
2194 Asm->OutStreamer.AddComment("External Name");
2195 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
2196 }
2197
2198 Asm->OutStreamer.AddComment("End Mark");
2199 Asm->EmitInt32(0);
2200 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end", ID));
2201 }
2202}
2203
Devang Patel04d2f2d2009-11-24 01:14:22 +00002204void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002205 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2206 E = CUMap.end(); I != E; ++I) {
2207 CompileUnit *TheCU = I->second;
Eric Christopher6abc9c52011-11-07 09:18:35 +00002208 // Start the dwarf pubtypes section.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002209 Asm->OutStreamer.SwitchSection(
2210 Asm->getObjFileLowering().getDwarfPubTypesSection());
2211 Asm->OutStreamer.AddComment("Length of Public Types Info");
2212 Asm->EmitLabelDifference(
Eli Benderskyb42d1462012-12-03 18:45:45 +00002213 Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()),
2214 Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002215
Devang Patel1a0df9a2010-05-10 22:49:55 +00002216 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
Eli Benderskyb42d1462012-12-03 18:45:45 +00002217 TheCU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002218
Devang Patel1a0df9a2010-05-10 22:49:55 +00002219 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2220 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002221
Devang Patel1a0df9a2010-05-10 22:49:55 +00002222 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Eric Christopher16485a52012-12-15 00:04:04 +00002223 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2224 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyb42d1462012-12-03 18:45:45 +00002225 TheCU->getUniqueID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00002226 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002227
Devang Patel1a0df9a2010-05-10 22:49:55 +00002228 Asm->OutStreamer.AddComment("Compilation Unit Length");
Eric Christopher16485a52012-12-15 00:04:04 +00002229 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(),
Eli Benderskyb42d1462012-12-03 18:45:45 +00002230 TheCU->getUniqueID()),
Eric Christopher16485a52012-12-15 00:04:04 +00002231 Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyb42d1462012-12-03 18:45:45 +00002232 TheCU->getUniqueID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00002233 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002234
Devang Patel1a0df9a2010-05-10 22:49:55 +00002235 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2236 for (StringMap<DIE*>::const_iterator
2237 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2238 const char *Name = GI->getKeyData();
Nick Lewycky019d2552011-07-29 03:49:23 +00002239 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002240
Devang Patel1a0df9a2010-05-10 22:49:55 +00002241 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2242 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002243
Devang Patel1a0df9a2010-05-10 22:49:55 +00002244 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002245 // Emit the name with a terminating null byte.
Eric Christophere3ab3d02013-01-09 01:57:54 +00002246 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002247 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002248
Devang Patel1a0df9a2010-05-10 22:49:55 +00002249 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002250 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00002251 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
Eli Benderskyb42d1462012-12-03 18:45:45 +00002252 TheCU->getUniqueID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00002253 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002254}
2255
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002256// Emit strings into a string section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002257void DwarfUnits::emitStrings(const MCSection *StrSection,
2258 const MCSection *OffsetSection = NULL,
2259 const MCSymbol *StrSecSym = NULL) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002260
Eric Christopher27614582013-01-08 22:22:06 +00002261 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002262
Chris Lattner3d72a672010-03-09 23:38:23 +00002263 // Start the dwarf str section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002264 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling480ff322009-05-20 23:21:38 +00002265
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002266 // Get all of the string pool entries and put them in an array by their ID so
2267 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002268 SmallVector<std::pair<unsigned,
Eric Christopherb800ff72013-01-07 22:40:45 +00002269 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002270
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002271 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
Eric Christopher27614582013-01-08 22:22:06 +00002272 I = StringPool.begin(), E = StringPool.end();
Eric Christopher48fef592012-12-20 21:58:40 +00002273 I != E; ++I)
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002274 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002275
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002276 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002277
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002278 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002279 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002280 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002281
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002282 // Emit the string itself with a terminating null byte.
Benjamin Kramer148db362011-11-09 12:12:04 +00002283 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
Eric Christopherce0cfce2013-01-09 01:35:34 +00002284 Entries[i].second->getKeyLength()+1));
Bill Wendling480ff322009-05-20 23:21:38 +00002285 }
Eric Christopher2cbd5762013-01-07 19:32:41 +00002286
2287 // If we've got an offset section go ahead and emit that now as well.
2288 if (OffsetSection) {
2289 Asm->OutStreamer.SwitchSection(OffsetSection);
2290 unsigned offset = 0;
Eric Christopher962c9082013-01-15 23:56:56 +00002291 unsigned size = 4; // FIXME: DWARF64 is 8.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002292 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Eric Christopherbf7bc492013-01-09 03:52:05 +00002293 Asm->OutStreamer.EmitIntValue(offset, size);
Eric Christopher2cbd5762013-01-07 19:32:41 +00002294 offset += Entries[i].second->getKeyLength() + 1;
2295 }
2296 }
Bill Wendling480ff322009-05-20 23:21:38 +00002297}
2298
Eric Christopher962c9082013-01-15 23:56:56 +00002299// Emit strings into a string section.
2300void DwarfUnits::emitAddresses(const MCSection *AddrSection) {
2301
2302 if (AddressPool.empty()) return;
2303
2304 // Start the dwarf addr section.
2305 Asm->OutStreamer.SwitchSection(AddrSection);
2306
2307 // Get all of the string pool entries and put them in an array by their ID so
2308 // we can sort them.
2309 SmallVector<std::pair<unsigned,
2310 std::pair<MCSymbol*, unsigned>* >, 64> Entries;
2311
2312 for (DenseMap<MCSymbol*, std::pair<MCSymbol*, unsigned> >::iterator
2313 I = AddressPool.begin(), E = AddressPool.end();
2314 I != E; ++I)
2315 Entries.push_back(std::make_pair(I->second.second, &(I->second)));
2316
2317 array_pod_sort(Entries.begin(), Entries.end());
2318
2319 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
2320 // Emit a label for reference from debug information entries.
2321 MCSymbol *Sym = Entries[i].second->first;
2322 if (Sym)
2323 Asm->EmitLabelReference(Entries[i].second->first,
2324 Asm->getDataLayout().getPointerSize());
2325 else
2326 Asm->OutStreamer.EmitIntValue(0, Asm->getDataLayout().getPointerSize());
2327 }
2328
2329}
2330
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002331// Emit visible names into a debug str section.
2332void DwarfDebug::emitDebugStr() {
2333 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2334 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2335}
2336
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002337// Emit visible names into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002338void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002339 if (DotDebugLocEntries.empty())
2340 return;
2341
Devang Patel116a9d72011-02-04 22:57:18 +00002342 for (SmallVector<DotDebugLocEntry, 4>::iterator
2343 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2344 I != E; ++I) {
2345 DotDebugLocEntry &Entry = *I;
2346 if (I + 1 != DotDebugLocEntries.end())
2347 Entry.Merge(I+1);
2348 }
2349
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002350 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002351 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00002352 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002353 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002354 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2355 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002356 for (SmallVector<DotDebugLocEntry, 4>::iterator
2357 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002358 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002359 DotDebugLocEntry &Entry = *I;
2360 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002361 if (Entry.isEmpty()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +00002362 Asm->OutStreamer.EmitIntValue(0, Size);
2363 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002364 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002365 } else {
Eric Christopherbf7bc492013-01-09 03:52:05 +00002366 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size);
2367 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size);
Devang Patel3e021532011-04-28 02:22:40 +00002368 DIVariable DV(Entry.Variable);
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002369 Asm->OutStreamer.AddComment("Loc expr size");
2370 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2371 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2372 Asm->EmitLabelDifference(end, begin, 2);
2373 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002374 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002375 DIBasicType BTy(DV.getType());
2376 if (BTy.Verify() &&
Eric Christopher6a841382012-11-19 22:42:10 +00002377 (BTy.getEncoding() == dwarf::DW_ATE_signed
Devang Patel324f8432011-06-01 22:03:25 +00002378 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2379 Asm->OutStreamer.AddComment("DW_OP_consts");
2380 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002381 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002382 } else {
2383 Asm->OutStreamer.AddComment("DW_OP_constu");
2384 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002385 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002386 }
Devang Pateled9fd452011-07-08 16:49:43 +00002387 } else if (Entry.isLocation()) {
Eric Christopher6a841382012-11-19 22:42:10 +00002388 if (!DV.hasComplexAddress())
Devang Pateled9fd452011-07-08 16:49:43 +00002389 // Regular entry.
Devang Patel3e021532011-04-28 02:22:40 +00002390 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Pateled9fd452011-07-08 16:49:43 +00002391 else {
2392 // Complex address entry.
2393 unsigned N = DV.getNumAddrElements();
2394 unsigned i = 0;
2395 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2396 if (Entry.Loc.getOffset()) {
2397 i = 2;
2398 Asm->EmitDwarfRegOp(Entry.Loc);
2399 Asm->OutStreamer.AddComment("DW_OP_deref");
2400 Asm->EmitInt8(dwarf::DW_OP_deref);
2401 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2402 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2403 Asm->EmitSLEB128(DV.getAddrElement(1));
2404 } else {
2405 // If first address element is OpPlus then emit
2406 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2407 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2408 Asm->EmitDwarfRegOp(Loc);
2409 i = 2;
2410 }
2411 } else {
2412 Asm->EmitDwarfRegOp(Entry.Loc);
2413 }
Eric Christopher6a841382012-11-19 22:42:10 +00002414
Devang Pateled9fd452011-07-08 16:49:43 +00002415 // Emit remaining complex address elements.
2416 for (; i < N; ++i) {
2417 uint64_t Element = DV.getAddrElement(i);
2418 if (Element == DIBuilder::OpPlus) {
2419 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2420 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002421 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher8d2a77d2012-05-08 21:24:39 +00002422 if (!Entry.Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002423 Asm->EmitInt8(dwarf::DW_OP_deref);
2424 } else
2425 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002426 }
Devang Patel3e021532011-04-28 02:22:40 +00002427 }
Devang Patel3e021532011-04-28 02:22:40 +00002428 }
Devang Pateled9fd452011-07-08 16:49:43 +00002429 // else ... ignore constant fp. There is not any good way to
2430 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002431 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002432 }
2433 }
Bill Wendling480ff322009-05-20 23:21:38 +00002434}
2435
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002436// Emit visible names into a debug aranges section.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002437void DwarfDebug::emitDebugARanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002438 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002439 Asm->OutStreamer.SwitchSection(
2440 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002441}
2442
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002443// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002444void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002445 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002446 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00002447 Asm->getObjFileLowering().getDwarfRangesSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002448 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6c74a872010-04-27 19:46:33 +00002449 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002450 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00002451 I != E; ++I) {
2452 if (*I)
Eric Christopherbf7bc492013-01-09 03:52:05 +00002453 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size);
Devang Patel12563b32010-04-16 23:33:45 +00002454 else
Eric Christopherce0cfce2013-01-09 01:35:34 +00002455 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel12563b32010-04-16 23:33:45 +00002456 }
Bill Wendling480ff322009-05-20 23:21:38 +00002457}
2458
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002459// Emit visible names into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00002460void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002461 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002462 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002463 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002464 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002465 }
2466}
2467
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002468// Emit inline info using following format.
2469// Section Header:
2470// 1. length of section
2471// 2. Dwarf version number
2472// 3. address size.
2473//
2474// Entries (one "entry" for each function that was inlined):
2475//
2476// 1. offset into __debug_str section for MIPS linkage name, if exists;
2477// otherwise offset into __debug_str for regular function name.
2478// 2. offset into __debug_str section for regular function name.
2479// 3. an unsigned LEB128 number indicating the number of distinct inlining
2480// instances for the function.
2481//
2482// The rest of the entry consists of a {die_offset, low_pc} pair for each
2483// inlined instance; the die_offset points to the inlined_subroutine die in the
2484// __debug_info section, and the low_pc is the starting address for the
2485// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002486void DwarfDebug::emitDebugInlineInfo() {
Eric Christopher1df94bf2012-03-02 02:11:47 +00002487 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002488 return;
2489
Devang Patel1a0df9a2010-05-10 22:49:55 +00002490 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002491 return;
2492
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002493 Asm->OutStreamer.SwitchSection(
2494 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002495
Chris Lattner566cae92010-03-09 23:52:58 +00002496 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002497 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2498 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002499
Chris Lattnera179b522010-04-04 19:25:43 +00002500 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002501
Chris Lattner566cae92010-03-09 23:52:58 +00002502 Asm->OutStreamer.AddComment("Dwarf Version");
2503 Asm->EmitInt16(dwarf::DWARF_VERSION);
2504 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002505 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002506
Devang Patel32cc43c2010-05-07 20:54:48 +00002507 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002508 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002509
Devang Patel32cc43c2010-05-07 20:54:48 +00002510 const MDNode *Node = *I;
2511 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00002512 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002513 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002514 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002515 StringRef LName = SP.getLinkageName();
2516 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002517
Chris Lattner566cae92010-03-09 23:52:58 +00002518 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher77725312012-03-02 01:57:52 +00002519 if (LName.empty())
Eric Christophere698f532012-12-20 21:58:36 +00002520 Asm->EmitSectionOffset(InfoHolder.getStringPoolEntry(Name),
2521 DwarfStrSectionSym);
Eric Christopher77725312012-03-02 01:57:52 +00002522 else
Eric Christophere698f532012-12-20 21:58:36 +00002523 Asm->EmitSectionOffset(InfoHolder
2524 .getStringPoolEntry(getRealLinkageName(LName)),
Chris Lattner70a4fce2010-04-04 23:25:33 +00002525 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002526
Chris Lattner566cae92010-03-09 23:52:58 +00002527 Asm->OutStreamer.AddComment("Function name");
Eric Christophere698f532012-12-20 21:58:36 +00002528 Asm->EmitSectionOffset(InfoHolder.getStringPoolEntry(Name),
2529 DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00002530 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002531
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002532 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002533 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002534 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002535 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002536
Chris Lattner7bde8c02010-04-04 18:52:31 +00002537 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002538 Asm->OutStreamer.EmitSymbolValue(LI->first,
Eric Christopherbf7bc492013-01-09 03:52:05 +00002539 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002540 }
2541 }
2542
Chris Lattnera179b522010-04-04 19:25:43 +00002543 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002544}
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002545
Eric Christopherd692c1d2012-12-11 19:42:09 +00002546// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002547
2548// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2549// DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
2550// DW_AT_ranges_base, DW_AT_addr_base. If DW_AT_ranges is present,
2551// DW_AT_low_pc and DW_AT_high_pc are not used, and vice versa.
Eric Christophercdf218d2012-12-10 19:51:21 +00002552CompileUnit *DwarfDebug::constructSkeletonCU(const MDNode *N) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002553 DICompileUnit DIUnit(N);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002554 CompilationDir = DIUnit.getDirectory();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002555
2556 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyb42d1462012-12-03 18:45:45 +00002557 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
Eric Christophere698f532012-12-20 21:58:36 +00002558 DIUnit.getLanguage(), Die, Asm,
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002559 this, &SkeletonHolder);
Eric Christopher4c7765f2013-01-17 03:00:04 +00002560
Eric Christopherdae389b2013-02-22 23:50:08 +00002561 NewCU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
2562 DIUnit.getSplitDebugFilename());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002563
Eric Christopher7a2cdf72013-02-05 07:31:55 +00002564 // This should be a unique identifier when we want to build .dwp files.
2565 NewCU->addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8, 0);
Eric Christopher18266172013-01-17 02:59:59 +00002566
2567 // FIXME: The addr base should be relative for each compile unit, however,
2568 // this one is going to be 0 anyhow.
2569 NewCU->addUInt(Die, dwarf::DW_AT_GNU_addr_base, dwarf::DW_FORM_sec_offset, 0);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002570
2571 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher962c9082013-01-15 23:56:56 +00002572 // into an entity. We're using 0, or a NULL label for this.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002573 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Eric Christopher962c9082013-01-15 23:56:56 +00002574
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002575 // DW_AT_stmt_list is a offset of line number information for this
2576 // compile unit in debug_line section.
2577 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher4c7765f2013-01-17 03:00:04 +00002578 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_sec_offset,
Eric Christopher74804332013-02-07 21:19:50 +00002579 DwarfLineSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002580 else
Eric Christopher4c7765f2013-01-17 03:00:04 +00002581 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_sec_offset, 0);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002582
2583 if (!CompilationDir.empty())
Eric Christopher2cbd5762013-01-07 19:32:41 +00002584 NewCU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002585
Eric Christopherc8a310e2012-12-10 23:34:43 +00002586 SkeletonHolder.addUnit(NewCU);
Eric Christopher5b33b3c2013-02-06 21:53:56 +00002587 SkeletonCUs.push_back(NewCU);
Eric Christopherc8a310e2012-12-10 23:34:43 +00002588
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002589 return NewCU;
2590}
2591
Eric Christopher3c5a1912012-12-19 22:02:53 +00002592void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
2593 assert(useSplitDwarf() && "No split dwarf debug info?");
2594 emitAbbrevs(Section, &SkeletonAbbrevs);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002595}
2596
Eric Christopherd692c1d2012-12-11 19:42:09 +00002597// Emit the .debug_info.dwo section for separated dwarf. This contains the
2598// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002599void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00002600 assert(useSplitDwarf() && "No split dwarf debug info?");
Eric Christophera2de8262012-12-15 00:04:07 +00002601 InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Eric Christopher3c5a1912012-12-19 22:02:53 +00002602 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2603 DwarfAbbrevDWOSectionSym);
2604}
2605
2606// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2607// abbreviations for the .debug_info.dwo section.
2608void DwarfDebug::emitDebugAbbrevDWO() {
2609 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopher48fef592012-12-20 21:58:40 +00002610 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2611 &Abbreviations);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002612}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002613
2614// Emit the .debug_str.dwo section for separated dwarf. This contains the
2615// string section and is identical in format to traditional .debug_str
2616// sections.
2617void DwarfDebug::emitDebugStrDWO() {
2618 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherb800ff72013-01-07 22:40:45 +00002619 const MCSection *OffSec = Asm->getObjFileLowering()
2620 .getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00002621 const MCSymbol *StrSym = DwarfStrSectionSym;
2622 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
2623 OffSec, StrSym);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002624}