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