blob: e81b933ee590afd2f908692e2cd030a781a94fe2 [file] [log] [blame]
Bill Wendling2f921f82009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattnerb14490d2010-03-09 00:39:24 +000013
Devang Patel80ae3492009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000016#include "DIE.h"
Eric Christopher45731982013-08-08 23:45:55 +000017#include "DIEHash.h"
Eric Christopher4996c702011-11-07 09:24:32 +000018#include "DwarfAccelTable.h"
Devang Patel5eb43192011-04-12 17:40:32 +000019#include "DwarfCompileUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/ADT/Triple.h"
David Greene829b3e82009-08-19 21:52:55 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/DIBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Constants.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/Module.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000032#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000033#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000034#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000035#include "llvm/MC/MCSymbol.h"
Devang Patel6c74a872010-04-27 19:46:33 +000036#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000039#include "llvm/Support/FormattedStream.h"
Eric Christopher67646432013-07-26 17:02:41 +000040#include "llvm/Support/MD5.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000041#include "llvm/Support/Path.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/Support/Timer.h"
43#include "llvm/Support/ValueHandle.h"
44#include "llvm/Target/TargetFrameLowering.h"
45#include "llvm/Target/TargetLoweringObjectFile.h"
46#include "llvm/Target/TargetMachine.h"
47#include "llvm/Target/TargetOptions.h"
48#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000049using namespace llvm;
50
Eric Christopher7f2b5512013-07-23 22:16:41 +000051static cl::opt<bool>
52DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
53 cl::desc("Disable debug info printing"));
Devang Patel6c74a872010-04-27 19:46:33 +000054
Eric Christopher7f2b5512013-07-23 22:16:41 +000055static cl::opt<bool> UnknownLocations(
56 "use-unknown-locations", cl::Hidden,
57 cl::desc("Make an absence of debug location information explicit."),
58 cl::init(false));
Dan Gohman7421ae42010-05-07 01:08:53 +000059
Eric Christopher7f2b5512013-07-23 22:16:41 +000060static cl::opt<bool>
61GenerateDwarfPubNamesSection("generate-dwarf-pubnames", cl::Hidden,
62 cl::init(false),
63 cl::desc("Generate DWARF pubnames section"));
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +000064
Eric Christopher67646432013-07-26 17:02:41 +000065static cl::opt<bool>
66GenerateODRHash("generate-odr-hash", cl::Hidden,
67 cl::desc("Add an ODR hash to external type DIEs."),
68 cl::init(false));
69
Eric Christopherd29614f2013-08-13 01:21:55 +000070static cl::opt<bool>
71GenerateCUHash("generate-cu-hash", cl::Hidden,
72 cl::desc("Add the CU hash as the dwo_id."),
73 cl::init(false));
74
Eric Christopher20b76a72012-08-23 22:36:40 +000075namespace {
Eric Christopher7f2b5512013-07-23 22:16:41 +000076enum DefaultOnOff {
77 Default,
78 Enable,
79 Disable
80};
Eric Christopher20b76a72012-08-23 22:36:40 +000081}
Eric Christopher4996c702011-11-07 09:24:32 +000082
Eric Christopher7f2b5512013-07-23 22:16:41 +000083static cl::opt<DefaultOnOff>
84DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
85 cl::desc("Output prototype dwarf accelerator tables."),
86 cl::values(clEnumVal(Default, "Default for platform"),
87 clEnumVal(Enable, "Enabled"),
88 clEnumVal(Disable, "Disabled"), clEnumValEnd),
89 cl::init(Default));
Eric Christopher20b76a72012-08-23 22:36:40 +000090
Eric Christopher7f2b5512013-07-23 22:16:41 +000091static cl::opt<DefaultOnOff>
92DarwinGDBCompat("darwin-gdb-compat", cl::Hidden,
93 cl::desc("Compatibility with Darwin gdb."),
94 cl::values(clEnumVal(Default, "Default for platform"),
95 clEnumVal(Enable, "Enabled"),
96 clEnumVal(Disable, "Disabled"), clEnumValEnd),
97 cl::init(Default));
Eric Christopher978fbff2012-08-23 07:10:46 +000098
Eric Christopher7f2b5512013-07-23 22:16:41 +000099static cl::opt<DefaultOnOff>
100SplitDwarf("split-dwarf", cl::Hidden,
101 cl::desc("Output prototype dwarf split debug info."),
102 cl::values(clEnumVal(Default, "Default for platform"),
103 clEnumVal(Enable, "Enabled"),
104 clEnumVal(Disable, "Disabled"), clEnumValEnd),
105 cl::init(Default));
Eric Christopher29424312012-11-12 22:22:20 +0000106
Bill Wendlingfcc14142010-04-07 09:28:04 +0000107namespace {
Craig Topperd3a34f82013-07-16 01:17:10 +0000108 const char *const DWARFGroupName = "DWARF Emission";
109 const char *const DbgTimerName = "DWARF Debug Writer";
David Blaikie684fc532013-05-06 23:33:07 +0000110
111 struct CompareFirst {
112 template <typename T> bool operator()(const T &lhs, const T &rhs) const {
113 return lhs.first < rhs.first;
114 }
115 };
Bill Wendlingfcc14142010-04-07 09:28:04 +0000116} // end anonymous namespace
117
Bill Wendling2f921f82009-05-15 09:23:25 +0000118//===----------------------------------------------------------------------===//
119
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000120// Configuration values for initial hash set sizes (log2).
121//
Bill Wendling2f921f82009-05-15 09:23:25 +0000122static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +0000123
124namespace llvm {
125
Nick Lewycky019d2552011-07-29 03:49:23 +0000126DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +0000127 DIType Ty = Var.getType();
128 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
129 // addresses instead.
130 if (Var.isBlockByrefVariable()) {
131 /* Byref variables, in Blocks, are declared by the programmer as
132 "SomeType VarName;", but the compiler creates a
133 __Block_byref_x_VarName struct, and gives the variable VarName
134 either the struct, or a pointer to the struct, as its type. This
135 is necessary for various behind-the-scenes things the compiler
136 needs to do with by-reference variables in blocks.
Eric Christopher6a841382012-11-19 22:42:10 +0000137
Devang Patelf20c4f72011-04-12 22:53:02 +0000138 However, as far as the original *programmer* is concerned, the
139 variable should still have type 'SomeType', as originally declared.
Eric Christopher6a841382012-11-19 22:42:10 +0000140
Devang Patelf20c4f72011-04-12 22:53:02 +0000141 The following function dives into the __Block_byref_x_VarName
142 struct to find the original type of the variable. This will be
143 passed back to the code generating the type for the Debug
144 Information Entry for the variable 'VarName'. 'VarName' will then
145 have the original type 'SomeType' in its debug information.
Eric Christopher6a841382012-11-19 22:42:10 +0000146
Devang Patelf20c4f72011-04-12 22:53:02 +0000147 The original type 'SomeType' will be the type of the field named
148 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher6a841382012-11-19 22:42:10 +0000149
Devang Patelf20c4f72011-04-12 22:53:02 +0000150 NOTE: In order for this to not completely fail on the debugger
151 side, the Debug Information Entry for the variable VarName needs to
152 have a DW_AT_location that tells the debugger how to unwind through
153 the pointers and __Block_byref_x_VarName struct to find the actual
154 value of the variable. The function addBlockByrefType does this. */
155 DIType subType = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000156 uint16_t tag = Ty.getTag();
Eric Christopher6a841382012-11-19 22:42:10 +0000157
Devang Patelf20c4f72011-04-12 22:53:02 +0000158 if (tag == dwarf::DW_TAG_pointer_type) {
159 DIDerivedType DTy = DIDerivedType(Ty);
160 subType = DTy.getTypeDerivedFrom();
161 }
Eric Christopher6a841382012-11-19 22:42:10 +0000162
Devang Patelf20c4f72011-04-12 22:53:02 +0000163 DICompositeType blockStruct = DICompositeType(subType);
164 DIArray Elements = blockStruct.getTypeArray();
Eric Christopher6a841382012-11-19 22:42:10 +0000165
Devang Patelf20c4f72011-04-12 22:53:02 +0000166 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
167 DIDescriptor Element = Elements.getElement(i);
168 DIDerivedType DT = DIDerivedType(Element);
169 if (getName() == DT.getName())
170 return (DT.getTypeDerivedFrom());
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000171 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000172 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000173 return Ty;
174}
Bill Wendling2f921f82009-05-15 09:23:25 +0000175
Chris Lattnerf5d06362010-04-05 04:09:20 +0000176} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000177
Manman Renac8062b2013-07-02 23:40:10 +0000178/// Return Dwarf Version by checking module flags.
179static unsigned getDwarfVersionFromModule(const Module *M) {
Manman Ren8bfde892013-07-16 23:21:16 +0000180 Value *Val = M->getModuleFlag("Dwarf Version");
181 if (!Val)
182 return dwarf::DWARF_VERSION;
183 return cast<ConstantInt>(Val)->getZExtValue();
Manman Renac8062b2013-07-02 23:40:10 +0000184}
185
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000186DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Eric Christopherd79f5482012-12-10 19:51:13 +0000187 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000188 AbbreviationsSet(InitAbbreviationsSetSize),
Eric Christopher27614582013-01-08 22:22:06 +0000189 SourceIdMap(DIEValueAllocator),
Eric Christopherc8a310e2012-12-10 23:34:43 +0000190 PrevLabel(NULL), GlobalCUIndexCount(0),
Eric Christopher27614582013-01-08 22:22:06 +0000191 InfoHolder(A, &AbbreviationsSet, &Abbreviations, "info_string",
192 DIEValueAllocator),
Eric Christopher3c5a1912012-12-19 22:02:53 +0000193 SkeletonAbbrevSet(InitAbbreviationsSetSize),
Eric Christopher27614582013-01-08 22:22:06 +0000194 SkeletonHolder(A, &SkeletonAbbrevSet, &SkeletonAbbrevs, "skel_string",
195 DIEValueAllocator) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000196
Rafael Espindolaa7160962011-05-06 14:56:22 +0000197 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattnere58b5472010-04-04 23:10:38 +0000198 DwarfStrSectionSym = TextSectionSym = 0;
Eric Christopher74804332013-02-07 21:19:50 +0000199 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = DwarfLineSectionSym = 0;
Eric Christopher55863be2013-04-07 03:43:09 +0000200 DwarfAddrSectionSym = 0;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000201 DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000202 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopherad9fe892012-04-02 17:58:52 +0000203
Eric Christopher978fbff2012-08-23 07:10:46 +0000204 // Turn on accelerator tables and older gdb compatibility
205 // for Darwin.
Eric Christopher203e12b2013-04-27 01:07:52 +0000206 bool IsDarwin = Triple(A->getTargetTriple()).isOSDarwin();
Eric Christopher20b76a72012-08-23 22:36:40 +0000207 if (DarwinGDBCompat == Default) {
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000208 if (IsDarwin)
209 IsDarwinGDBCompat = true;
Eric Christopher20b76a72012-08-23 22:36:40 +0000210 else
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000211 IsDarwinGDBCompat = false;
Eric Christopher20b76a72012-08-23 22:36:40 +0000212 } else
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000213 IsDarwinGDBCompat = DarwinGDBCompat == Enable ? true : false;
Eric Christopher4977f212012-08-23 22:36:36 +0000214
Eric Christopher20b76a72012-08-23 22:36:40 +0000215 if (DwarfAccelTables == Default) {
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000216 if (IsDarwin)
217 HasDwarfAccelTables = true;
Eric Christopher20b76a72012-08-23 22:36:40 +0000218 else
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000219 HasDwarfAccelTables = false;
Eric Christopher20b76a72012-08-23 22:36:40 +0000220 } else
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000221 HasDwarfAccelTables = DwarfAccelTables == Enable ? true : false;
Eric Christopher20b76a72012-08-23 22:36:40 +0000222
Eric Christophercdf218d2012-12-10 19:51:21 +0000223 if (SplitDwarf == Default)
224 HasSplitDwarf = false;
Eric Christopher29424312012-11-12 22:22:20 +0000225 else
Eric Christophercdf218d2012-12-10 19:51:21 +0000226 HasSplitDwarf = SplitDwarf == Enable ? true : false;
Eric Christopher29424312012-11-12 22:22:20 +0000227
Manman Renac8062b2013-07-02 23:40:10 +0000228 DwarfVersion = getDwarfVersionFromModule(MMI->getModule());
229
Dan Gohman6e681a52010-06-18 15:56:31 +0000230 {
231 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopher58f41952012-11-19 22:42:15 +0000232 beginModule();
Torok Edwinf8dba242010-04-07 10:44:46 +0000233 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000234}
235DwarfDebug::~DwarfDebug() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000236}
237
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000238// Switch to the specified MCSection and emit an assembler
239// temporary label to it if SymbolStem is specified.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000240static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christophera7b61892011-11-07 09:18:38 +0000241 const char *SymbolStem = 0) {
242 Asm->OutStreamer.SwitchSection(Section);
243 if (!SymbolStem) return 0;
244
245 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
246 Asm->OutStreamer.EmitLabel(TmpSym);
247 return TmpSym;
248}
249
Eric Christophere698f532012-12-20 21:58:36 +0000250MCSymbol *DwarfUnits::getStringPoolSym() {
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000251 return Asm->GetTempSymbol(StringPref);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000252}
253
Eric Christophere698f532012-12-20 21:58:36 +0000254MCSymbol *DwarfUnits::getStringPoolEntry(StringRef Str) {
255 std::pair<MCSymbol*, unsigned> &Entry =
Eric Christopher27614582013-01-08 22:22:06 +0000256 StringPool.GetOrCreateValue(Str).getValue();
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000257 if (Entry.first) return Entry.first;
258
259 Entry.second = NextStringPoolNumber++;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000260 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000261}
262
Eric Christopher2cbd5762013-01-07 19:32:41 +0000263unsigned DwarfUnits::getStringPoolIndex(StringRef Str) {
264 std::pair<MCSymbol*, unsigned> &Entry =
Eric Christopher27614582013-01-08 22:22:06 +0000265 StringPool.GetOrCreateValue(Str).getValue();
Eric Christopher2cbd5762013-01-07 19:32:41 +0000266 if (Entry.first) return Entry.second;
267
268 Entry.second = NextStringPoolNumber++;
269 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
270 return Entry.second;
271}
272
David Blaikiedea547b2013-06-28 18:47:14 +0000273unsigned DwarfUnits::getAddrPoolIndex(const MCSymbol *Sym) {
David Blaikie8466ca82013-07-01 23:55:52 +0000274 return getAddrPoolIndex(MCSymbolRefExpr::Create(Sym, Asm->OutContext));
275}
276
Ulrich Weigand8b3d2262013-07-02 18:46:46 +0000277unsigned DwarfUnits::getAddrPoolIndex(const MCExpr *Sym) {
278 std::pair<DenseMap<const MCExpr *, unsigned>::iterator, bool> P =
David Blaikieb8ef7852013-06-28 18:47:19 +0000279 AddressPool.insert(std::make_pair(Sym, NextAddrPoolNumber));
David Blaikiea67de2b2013-06-28 18:55:13 +0000280 if (P.second)
281 ++NextAddrPoolNumber;
David Blaikieb8ef7852013-06-28 18:47:19 +0000282 return P.first->second;
Eric Christopher962c9082013-01-15 23:56:56 +0000283}
284
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000285// Define a unique number for the abbreviation.
286//
Eric Christopherc8a310e2012-12-10 23:34:43 +0000287void DwarfUnits::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000288 // Check the set for priors.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000289 DIEAbbrev *InSet = AbbreviationsSet->GetOrInsertNode(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000290
291 // If it's newly added.
292 if (InSet == &Abbrev) {
293 // Add to abbreviation list.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000294 Abbreviations->push_back(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000295
296 // Assign the vector position + 1 as its number.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000297 Abbrev.setNumber(Abbreviations->size());
Bill Wendling2f921f82009-05-15 09:23:25 +0000298 } else {
299 // Assign existing abbreviation number.
300 Abbrev.setNumber(InSet->getNumber());
301 }
302}
303
Eric Christopherd9843b32011-11-10 19:25:34 +0000304static bool isObjCClass(StringRef Name) {
305 return Name.startswith("+") || Name.startswith("-");
306}
307
308static bool hasObjCCategory(StringRef Name) {
309 if (!isObjCClass(Name)) return false;
310
311 size_t pos = Name.find(')');
312 if (pos != std::string::npos) {
313 if (Name[pos+1] != ' ') return false;
314 return true;
315 }
316 return false;
317}
318
319static void getObjCClassCategory(StringRef In, StringRef &Class,
320 StringRef &Category) {
321 if (!hasObjCCategory(In)) {
322 Class = In.slice(In.find('[') + 1, In.find(' '));
323 Category = "";
324 return;
325 }
326
327 Class = In.slice(In.find('[') + 1, In.find('('));
328 Category = In.slice(In.find('[') + 1, In.find(' '));
329 return;
330}
331
332static StringRef getObjCMethodName(StringRef In) {
333 return In.slice(In.find(' ') + 1, In.find(']'));
334}
335
336// Add the various names to the Dwarf accelerator table names.
337static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
338 DIE* Die) {
339 if (!SP.isDefinition()) return;
Eric Christopher6a841382012-11-19 22:42:10 +0000340
Eric Christopherd9843b32011-11-10 19:25:34 +0000341 TheCU->addAccelName(SP.getName(), Die);
342
343 // If the linkage name is different than the name, go ahead and output
344 // that as well into the name table.
345 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
346 TheCU->addAccelName(SP.getLinkageName(), Die);
347
348 // If this is an Objective-C selector name add it to the ObjC accelerator
349 // too.
350 if (isObjCClass(SP.getName())) {
351 StringRef Class, Category;
352 getObjCClassCategory(SP.getName(), Class, Category);
353 TheCU->addAccelObjC(Class, Die);
354 if (Category != "")
355 TheCU->addAccelObjC(Category, Die);
356 // Also add the base method name to the name table.
357 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
358 }
359}
360
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000361// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
362// and DW_AT_high_pc attributes. If there are global variables in this
363// scope then create and insert DIEs for these variables.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000364DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
365 const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000366 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +0000367
Chris Lattner3a383cb2010-04-05 00:13:49 +0000368 assert(SPDie && "Unable to find subprogram DIE!");
369 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000370
Bill Wendlingf720bf62012-11-07 05:19:04 +0000371 // If we're updating an abstract DIE, then we will be adding the children and
372 // object pointer later on. But what we don't want to do is process the
373 // concrete DIE twice.
Manman Ren14a029d2013-03-12 18:27:15 +0000374 DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode);
375 if (AbsSPDIE) {
376 bool InSameCU = (AbsSPDIE->getCompileUnit() == SPCU->getCUDie());
Bill Wendlingf720bf62012-11-07 05:19:04 +0000377 // Pick up abstract subprogram DIE.
Devang Patela37a95e2010-07-07 22:20:57 +0000378 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Manman Ren14a029d2013-03-12 18:27:15 +0000379 // If AbsSPDIE belongs to a different CU, use DW_FORM_ref_addr instead of
380 // DW_FORM_ref4.
Devang Patelf20c4f72011-04-12 22:53:02 +0000381 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
Manman Ren14a029d2013-03-12 18:27:15 +0000382 InSameCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
383 AbsSPDIE);
Devang Patela37a95e2010-07-07 22:20:57 +0000384 SPCU->addDie(SPDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000385 } else {
386 DISubprogram SPDecl = SP.getFunctionDeclaration();
387 if (!SPDecl.isSubprogram()) {
388 // There is not any need to generate specification DIE for a function
389 // defined at compile unit level. If a function is defined inside another
390 // function then gdb prefers the definition at top level and but does not
391 // expect specification DIE in parent function. So avoid creating
392 // specification DIE for a function defined inside a function.
393 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
394 !SP.getContext().isFile() &&
395 !isSubprogramContext(SP.getContext())) {
396 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
397
398 // Add arguments.
399 DICompositeType SPTy = SP.getType();
400 DIArray Args = SPTy.getTypeArray();
Eric Christopher31b05762013-08-08 01:41:00 +0000401 uint16_t SPTag = SPTy.getTag();
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000402 if (SPTag == dwarf::DW_TAG_subroutine_type)
403 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
404 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
405 DIType ATy = DIType(Args.getElement(i));
406 SPCU->addType(Arg, ATy);
407 if (ATy.isArtificial())
408 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
409 if (ATy.isObjectPointer())
410 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer,
411 dwarf::DW_FORM_ref4, Arg);
412 SPDie->addChild(Arg);
413 }
414 DIE *SPDeclDie = SPDie;
415 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Eric Christopher6a841382012-11-19 22:42:10 +0000416 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification,
417 dwarf::DW_FORM_ref4, SPDeclDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000418 SPCU->addDie(SPDie);
419 }
420 }
Devang Patela37a95e2010-07-07 22:20:57 +0000421 }
422
Eric Christopher962c9082013-01-15 23:56:56 +0000423 SPCU->addLabelAddress(SPDie, dwarf::DW_AT_low_pc,
424 Asm->GetTempSymbol("func_begin",
425 Asm->getFunctionNumber()));
426 SPCU->addLabelAddress(SPDie, dwarf::DW_AT_high_pc,
427 Asm->GetTempSymbol("func_end",
428 Asm->getFunctionNumber()));
Chris Lattner3a383cb2010-04-05 00:13:49 +0000429 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
430 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000431 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000432
Eric Christopherd9843b32011-11-10 19:25:34 +0000433 // Add name to the name table, we do this here because we're guaranteed
434 // to have concrete versions of our DW_TAG_subprogram nodes.
435 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher6a841382012-11-19 22:42:10 +0000436
Chris Lattner3a383cb2010-04-05 00:13:49 +0000437 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000438}
439
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000440// Construct new DW_TAG_lexical_block for this scope and attach
441// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher6a841382012-11-19 22:42:10 +0000442DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000443 LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000444 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
445 if (Scope->isAbstractScope())
446 return ScopeDIE;
447
Craig Topper977e9cd2013-07-03 04:24:43 +0000448 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +0000449 if (Ranges.empty())
450 return 0;
451
Eric Christopherdc42ea82013-07-03 01:57:28 +0000452 // If we have multiple ranges, emit them into the range section.
Devang Patel6c74a872010-04-27 19:46:33 +0000453 if (Ranges.size() > 1) {
454 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000455 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000456 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000457 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Eric Christopher6a841382012-11-19 22:42:10 +0000458 DebugRangeSymbols.size()
Chandler Carruth5da3f052012-11-01 09:14:31 +0000459 * Asm->getDataLayout().getPointerSize());
Craig Topperd8e43652013-07-03 04:17:25 +0000460 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +0000461 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000462 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
463 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000464 }
Eric Christopherc1110832013-07-03 01:22:29 +0000465
466 // Terminate the range list.
Devang Patel6c74a872010-04-27 19:46:33 +0000467 DebugRangeSymbols.push_back(NULL);
468 DebugRangeSymbols.push_back(NULL);
469 return ScopeDIE;
470 }
471
Eric Christopherdc42ea82013-07-03 01:57:28 +0000472 // Construct the address range for this DIE.
Craig Topperd8e43652013-07-03 04:17:25 +0000473 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
Eric Christopher962c9082013-01-15 23:56:56 +0000474 MCSymbol *Start = getLabelBeforeInsn(RI->first);
475 MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000476
Devang Patel9fc11702010-05-25 23:40:22 +0000477 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000478
Chris Lattnere13c3722010-03-09 01:58:53 +0000479 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
480 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000481
Eric Christopher962c9082013-01-15 23:56:56 +0000482 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, Start);
483 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000484
485 return ScopeDIE;
486}
487
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000488// This scope represents inlined body of a function. Construct DIE to
489// represent this concrete inlined copy of the function.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000490DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
491 LexicalScope *Scope) {
Craig Topper977e9cd2013-07-03 04:24:43 +0000492 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000493 assert(Ranges.empty() == false &&
494 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000495
Devang Patelf098ce22011-07-27 00:34:13 +0000496 if (!Scope->getScopeNode())
497 return NULL;
498 DIScope DS(Scope->getScopeNode());
499 DISubprogram InlinedSP = getDISubprogram(DS);
Manman Ren4213c392013-05-29 17:16:59 +0000500 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Devang Patelf098ce22011-07-27 00:34:13 +0000501 if (!OriginDIE) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000502 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patelf098ce22011-07-27 00:34:13 +0000503 return NULL;
504 }
505
Devang Patel73bc1722011-05-05 17:54:26 +0000506 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patelf20c4f72011-04-12 22:53:02 +0000507 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Manman Ren4213c392013-05-29 17:16:59 +0000508 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000509
Devang Patelf098ce22011-07-27 00:34:13 +0000510 if (Ranges.size() > 1) {
511 // .debug_range section has not been laid out yet. Emit offset in
512 // .debug_range as a uint, size 4, for now. emitDIE will handle
513 // DW_AT_ranges appropriately.
514 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Eric Christopher6a841382012-11-19 22:42:10 +0000515 DebugRangeSymbols.size()
Chandler Carruth5da3f052012-11-01 09:14:31 +0000516 * Asm->getDataLayout().getPointerSize());
Craig Topperd8e43652013-07-03 04:17:25 +0000517 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Devang Patelf098ce22011-07-27 00:34:13 +0000518 RE = Ranges.end(); RI != RE; ++RI) {
519 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
520 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
521 }
522 DebugRangeSymbols.push_back(NULL);
523 DebugRangeSymbols.push_back(NULL);
524 } else {
Craig Topperd8e43652013-07-03 04:17:25 +0000525 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000526 MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
527 MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
528
529 if (StartLabel == 0 || EndLabel == 0)
530 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
531
532 assert(StartLabel->isDefined() &&
533 "Invalid starting label for an inlined scope!");
534 assert(EndLabel->isDefined() && "Invalid end label for an inlined scope!");
535
Eric Christopher962c9082013-01-15 23:56:56 +0000536 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, StartLabel);
537 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000538 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000539
540 InlinedSubprogramDIEs.insert(OriginDIE);
541
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000542 // Add the call site information to the DIE.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000543 DILocation DL(Scope->getInlinedAt());
Eric Christopher0925c622012-03-26 21:38:38 +0000544 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
Manman Ren1e427202013-03-07 01:42:00 +0000545 getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
546 TheCU->getUniqueID()));
Devang Patelf20c4f72011-04-12 22:53:02 +0000547 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000548
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000549 // Track the start label for this inlined function.
550 //.debug_inlined section specification does not clearly state how
551 // to emit inlined scopes that are split into multiple instruction ranges.
552 // For now, use the first instruction range and emit low_pc/high_pc pair and
553 // corresponding the .debug_inlined section entry for this pair.
554 if (Asm->MAI->doesDwarfUseInlineInfoSection()) {
555 MCSymbol *StartLabel = getLabelBeforeInsn(Ranges.begin()->first);
Craig Topper2b4a2012013-07-03 04:40:27 +0000556 InlineInfoMap::iterator I = InlineInfo.find(InlinedSP);
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000557
558 if (I == InlineInfo.end()) {
559 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
560 InlinedSPNodes.push_back(InlinedSP);
561 } else
562 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
563 }
564
Eric Christopher8dda5d02011-12-04 06:02:38 +0000565 // Add name to the name table, we do this here because we're guaranteed
566 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
567 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher6a841382012-11-19 22:42:10 +0000568
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000569 return ScopeDIE;
570}
571
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000572// Construct a DIE for this scope.
Devang Patel3acc70e2011-08-15 22:04:40 +0000573DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000574 if (!Scope || !Scope->getScopeNode())
575 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000576
Manman Ren53f3f9f2013-01-31 20:05:14 +0000577 DIScope DS(Scope->getScopeNode());
578 // Early return to avoid creating dangling variable|scope DIEs.
Manman Ren4213c392013-05-29 17:16:59 +0000579 if (!Scope->getInlinedAt() && DS.isSubprogram() && Scope->isAbstractScope() &&
580 !TheCU->getDIE(DS))
581 return NULL;
Manman Ren53f3f9f2013-01-31 20:05:14 +0000582
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000583 SmallVector<DIE *, 8> Children;
Eric Christophere3417762012-09-12 23:36:19 +0000584 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000585
586 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000587 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000588 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
589 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Eric Christopher6a841382012-11-19 22:42:10 +0000590 if (DIE *Arg =
Eric Christophere3417762012-09-12 23:36:19 +0000591 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000592 Children.push_back(Arg);
Eric Christophere3417762012-09-12 23:36:19 +0000593 if (ArgDV->isObjectPointer()) ObjectPointer = Arg;
594 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000595
Eric Christopherf84354b2011-10-03 15:49:16 +0000596 // Collect lexical scope children first.
Craig Topper977e9cd2013-07-03 04:24:43 +0000597 const SmallVectorImpl<DbgVariable *> &Variables =ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000598 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Eric Christopher6a841382012-11-19 22:42:10 +0000599 if (DIE *Variable =
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000600 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope())) {
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000601 Children.push_back(Variable);
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000602 if (Variables[i]->isObjectPointer()) ObjectPointer = Variable;
603 }
Craig Topper977e9cd2013-07-03 04:24:43 +0000604 const SmallVectorImpl<LexicalScope *> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000605 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000606 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000607 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000608 DIE *ScopeDIE = NULL;
609 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000610 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000611 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000612 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000613 if (Scope->isAbstractScope()) {
Manman Ren4213c392013-05-29 17:16:59 +0000614 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000615 // Note down abstract DIE.
616 if (ScopeDIE)
617 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
618 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000619 else
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000620 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000621 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000622 else {
623 // There is no need to emit empty lexical block DIE.
David Blaikie684fc532013-05-06 23:33:07 +0000624 std::pair<ImportedEntityMap::const_iterator,
625 ImportedEntityMap::const_iterator> Range = std::equal_range(
626 ScopesWithImportedEntities.begin(), ScopesWithImportedEntities.end(),
Timur Iskhodzhanovec4afe62013-05-07 07:47:47 +0000627 std::pair<const MDNode *, const MDNode *>(DS, (const MDNode*)0),
628 CompareFirst());
David Blaikie684fc532013-05-06 23:33:07 +0000629 if (Children.empty() && Range.first == Range.second)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000630 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000631 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000632 for (ImportedEntityMap::const_iterator i = Range.first; i != Range.second;
633 ++i)
David Blaikie4dd2de72013-05-08 06:01:38 +0000634 constructImportedEntityDIE(TheCU, i->second, ScopeDIE);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000635 }
Eric Christopher6a841382012-11-19 22:42:10 +0000636
Devang Patel23b2ae62010-03-29 22:59:58 +0000637 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000638
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000639 // Add children
Craig Topperd8e43652013-07-03 04:17:25 +0000640 for (SmallVectorImpl<DIE *>::iterator I = Children.begin(),
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000641 E = Children.end(); I != E; ++I)
642 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000643
Eric Christophere3417762012-09-12 23:36:19 +0000644 if (DS.isSubprogram() && ObjectPointer != NULL)
645 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer,
646 dwarf::DW_FORM_ref4, ObjectPointer);
647
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000648 if (DS.isSubprogram())
Eric Christopherd9843b32011-11-10 19:25:34 +0000649 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000650
Eric Christopherd9843b32011-11-10 19:25:34 +0000651 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000652}
653
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000654// Look up the source id with the given directory and source file names.
655// If none currently exists, create a new id and insert it in the
656// SourceIds map. This can update DirectoryNames and SourceFileNames maps
657// as well.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000658unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000659 StringRef DirName, unsigned CUID) {
660 // If we use .loc in assembly, we can't separate .file entries according to
661 // compile units. Thus all files will belong to the default compile unit.
662 if (Asm->TM.hasMCUseLoc() &&
663 Asm->OutStreamer.getKind() == MCStreamer::SK_AsmStreamer)
664 CUID = 0;
665
Devang Patel871d0b12010-09-16 20:57:49 +0000666 // If FE did not provide a file name, then assume stdin.
667 if (FileName.empty())
Manman Ren1e427202013-03-07 01:42:00 +0000668 return getOrCreateSourceID("<stdin>", StringRef(), CUID);
Devang Patele01b75c2011-03-24 20:30:50 +0000669
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000670 // TODO: this might not belong here. See if we can factor this better.
671 if (DirName == CompilationDir)
672 DirName = "";
673
Manman Ren1e427202013-03-07 01:42:00 +0000674 // FileIDCUMap stores the current ID for the given compile unit.
675 unsigned SrcId = FileIDCUMap[CUID] + 1;
Devang Patel871d0b12010-09-16 20:57:49 +0000676
Manman Ren1e427202013-03-07 01:42:00 +0000677 // We look up the CUID/file/dir by concatenating them with a zero byte.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000678 SmallString<128> NamePair;
Manman Ren5b22f9f2013-04-06 01:02:38 +0000679 NamePair += utostr(CUID);
Manman Ren1e427202013-03-07 01:42:00 +0000680 NamePair += '\0';
Benjamin Kramer71b19732012-03-11 14:56:26 +0000681 NamePair += DirName;
682 NamePair += '\0'; // Zero bytes are not allowed in paths.
683 NamePair += FileName;
684
685 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
686 if (Ent.getValue() != SrcId)
687 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000688
Manman Ren1e427202013-03-07 01:42:00 +0000689 FileIDCUMap[CUID] = SrcId;
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000690 // Print out a .file directive to specify files for .loc directives.
Manman Ren1e427202013-03-07 01:42:00 +0000691 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
Bill Wendling2b128d72009-05-20 23:19:06 +0000692
693 return SrcId;
694}
695
Eric Christopher48fef592012-12-20 21:58:40 +0000696// Create new CompileUnit for the given metadata node with tag
697// DW_TAG_compile_unit.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000698CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000699 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +0000700 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000701 CompilationDir = DIUnit.getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000702
703 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyb42d1462012-12-03 18:45:45 +0000704 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
Eric Christopherc57baee2013-05-08 00:58:51 +0000705 DIUnit.getLanguage(), Die, N, Asm,
Eric Christophere698f532012-12-20 21:58:36 +0000706 this, &InfoHolder);
Manman Ren1e427202013-03-07 01:42:00 +0000707
708 FileIDCUMap[NewCU->getUniqueID()] = 0;
709 // Call this to emit a .file directive if it wasn't emitted for the source
710 // file this CU comes from yet.
711 getOrCreateSourceID(FN, CompilationDir, NewCU->getUniqueID());
712
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000713 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000714 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
715 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000716 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000717
Eric Christopherb1b94512012-08-01 18:19:01 +0000718 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher52ce7182013-04-09 19:23:15 +0000719 // into an entity. We're using 0 (or a NULL label) for this. For
720 // split dwarf it's in the skeleton CU so omit it here.
721 if (!useSplitDwarf())
722 NewCU->addLabelAddress(Die, dwarf::DW_AT_low_pc, NULL);
Manman Ren4e042a62013-02-05 21:52:47 +0000723
724 // Define start line table label for each Compile Unit.
725 MCSymbol *LineTableStartSym = Asm->GetTempSymbol("line_table_start",
726 NewCU->getUniqueID());
727 Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
728 NewCU->getUniqueID());
729
Manman Ren9d4c7352013-05-21 00:57:22 +0000730 // Use a single line table if we are using .loc and generating assembly.
731 bool UseTheFirstCU =
732 (Asm->TM.hasMCUseLoc() &&
733 Asm->OutStreamer.getKind() == MCStreamer::SK_AsmStreamer) ||
734 (NewCU->getUniqueID() == 0);
735
Devang Pateld22ed622010-03-22 23:11:36 +0000736 // DW_AT_stmt_list is a offset of line number information for this
Eric Christopher52ce7182013-04-09 19:23:15 +0000737 // compile unit in debug_line section. For split dwarf this is
738 // left in the skeleton CU and so not included.
Manman Rend2c95eb2013-02-09 00:41:44 +0000739 // The line table entries are not always emitted in assembly, so it
740 // is not okay to use line_table_start here.
Eric Christopher52ce7182013-04-09 19:23:15 +0000741 if (!useSplitDwarf()) {
742 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
743 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Manman Ren9d4c7352013-05-21 00:57:22 +0000744 UseTheFirstCU ?
Eric Christopher52ce7182013-04-09 19:23:15 +0000745 Asm->GetTempSymbol("section_line") : LineTableStartSym);
Manman Ren9d4c7352013-05-21 00:57:22 +0000746 else if (UseTheFirstCU)
Eric Christopher52ce7182013-04-09 19:23:15 +0000747 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
748 else
749 NewCU->addDelta(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
750 LineTableStartSym, DwarfLineSectionSym);
751 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000752
Eric Christopher52ce7182013-04-09 19:23:15 +0000753 // If we're using split dwarf the compilation dir is going to be in the
754 // skeleton CU and so we don't need to duplicate it here.
755 if (!useSplitDwarf() && !CompilationDir.empty())
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000756 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000757 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000758 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000759
Devang Patel2d9caf92009-11-25 17:36:49 +0000760 StringRef Flags = DIUnit.getFlags();
761 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000762 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000763
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000764 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000765 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000766 dwarf::DW_FORM_data1, RVer);
767
Devang Patel1a0df9a2010-05-10 22:49:55 +0000768 if (!FirstCU)
769 FirstCU = NewCU;
Eric Christopher7a2cdf72013-02-05 07:31:55 +0000770
Eric Christopherc8a310e2012-12-10 23:34:43 +0000771 InfoHolder.addUnit(NewCU);
772
Devang Patel1a0df9a2010-05-10 22:49:55 +0000773 CUMap.insert(std::make_pair(N, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000774 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000775}
776
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000777// Construct subprogram DIE.
Eric Christopher6a841382012-11-19 22:42:10 +0000778void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
Devang Patel1f4f98d2011-08-15 23:36:40 +0000779 const MDNode *N) {
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000780 CompileUnit *&CURef = SPMap[N];
781 if (CURef)
782 return;
783 CURef = TheCU;
784
Devang Patel80ae3492009-08-28 23:24:31 +0000785 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000786 if (!SP.isDefinition())
787 // This is a method declaration which will be handled while constructing
788 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000789 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000790
Devang Patel89543712011-08-15 17:24:54 +0000791 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000792
793 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000794 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000795
796 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +0000797 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +0000798
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +0000799 // Expose as global, if requested.
800 if (GenerateDwarfPubNamesSection)
801 TheCU->addGlobalName(SP.getName(), SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000802}
803
David Blaikie1fd43652013-05-07 21:35:53 +0000804void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
David Blaikief55abea2013-04-22 06:12:31 +0000805 const MDNode *N) {
David Blaikie1fd43652013-05-07 21:35:53 +0000806 DIImportedEntity Module(N);
David Blaikief55abea2013-04-22 06:12:31 +0000807 if (!Module.Verify())
808 return;
David Blaikie684fc532013-05-06 23:33:07 +0000809 if (DIE *D = TheCU->getOrCreateContextDIE(Module.getContext()))
David Blaikie4dd2de72013-05-08 06:01:38 +0000810 constructImportedEntityDIE(TheCU, Module, D);
David Blaikie684fc532013-05-06 23:33:07 +0000811}
812
David Blaikie4dd2de72013-05-08 06:01:38 +0000813void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU, const MDNode *N,
David Blaikie684fc532013-05-06 23:33:07 +0000814 DIE *Context) {
David Blaikie1fd43652013-05-07 21:35:53 +0000815 DIImportedEntity Module(N);
David Blaikie684fc532013-05-06 23:33:07 +0000816 if (!Module.Verify())
817 return;
David Blaikie4dd2de72013-05-08 06:01:38 +0000818 return constructImportedEntityDIE(TheCU, Module, Context);
David Blaikie684fc532013-05-06 23:33:07 +0000819}
820
David Blaikie4dd2de72013-05-08 06:01:38 +0000821void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
David Blaikie1fd43652013-05-07 21:35:53 +0000822 const DIImportedEntity &Module,
David Blaikie684fc532013-05-06 23:33:07 +0000823 DIE *Context) {
824 assert(Module.Verify() &&
825 "Use one of the MDNode * overloads to handle invalid metadata");
826 assert(Context && "Should always have a context for an imported_module");
David Blaikie1fd43652013-05-07 21:35:53 +0000827 DIE *IMDie = new DIE(Module.getTag());
David Blaikief55abea2013-04-22 06:12:31 +0000828 TheCU->insertDIE(Module, IMDie);
David Blaikie1fd43652013-05-07 21:35:53 +0000829 DIE *EntityDie;
830 DIDescriptor Entity = Module.getEntity();
831 if (Entity.isNameSpace())
832 EntityDie = TheCU->getOrCreateNameSpace(DINameSpace(Entity));
833 else if (Entity.isSubprogram())
834 EntityDie = TheCU->getOrCreateSubprogramDIE(DISubprogram(Entity));
David Blaikie3b6038b2013-05-08 06:01:41 +0000835 else if (Entity.isType())
836 EntityDie = TheCU->getOrCreateTypeDIE(DIType(Entity));
David Blaikie1fd43652013-05-07 21:35:53 +0000837 else
David Blaikie3b6038b2013-05-08 06:01:41 +0000838 EntityDie = TheCU->getDIE(Entity);
David Blaikief55abea2013-04-22 06:12:31 +0000839 unsigned FileID = getOrCreateSourceID(Module.getContext().getFilename(),
840 Module.getContext().getDirectory(),
841 TheCU->getUniqueID());
842 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_file, 0, FileID);
843 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_line, 0, Module.getLineNumber());
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000844 TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, dwarf::DW_FORM_ref4,
845 EntityDie);
David Blaikiee63d5d12013-05-20 22:50:35 +0000846 StringRef Name = Module.getName();
847 if (!Name.empty())
848 TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
David Blaikie684fc532013-05-06 23:33:07 +0000849 Context->addChild(IMDie);
David Blaikief55abea2013-04-22 06:12:31 +0000850}
851
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000852// Emit all Dwarf sections that should come prior to the content. Create
853// global DIEs and emit initial debug info sections. This is invoked by
854// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000855void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000856 if (DisableDebugInfoPrinting)
857 return;
858
Eric Christopher58f41952012-11-19 22:42:15 +0000859 const Module *M = MMI->getModule();
860
Nick Lewycky019d2552011-07-29 03:49:23 +0000861 // If module has named metadata anchors then use them, otherwise scan the
862 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000863 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiedc69ebb2013-03-11 23:39:23 +0000864 if (!CU_Nodes)
Devang Patel07bb9ee2011-08-15 23:47:24 +0000865 return;
Devang Patele02e5852011-05-03 16:45:22 +0000866
David Blaikiedc69ebb2013-03-11 23:39:23 +0000867 // Emit initial sections so we can reference labels later.
868 emitSectionLabels();
869
870 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
871 DICompileUnit CUNode(CU_Nodes->getOperand(i));
872 CompileUnit *CU = constructCompileUnit(CUNode);
David Blaikie1fd43652013-05-07 21:35:53 +0000873 DIArray ImportedEntities = CUNode.getImportedEntities();
874 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
David Blaikie684fc532013-05-06 23:33:07 +0000875 ScopesWithImportedEntities.push_back(std::make_pair(
David Blaikie1fd43652013-05-07 21:35:53 +0000876 DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
877 ImportedEntities.getElement(i)));
David Blaikie684fc532013-05-06 23:33:07 +0000878 std::sort(ScopesWithImportedEntities.begin(),
879 ScopesWithImportedEntities.end(), CompareFirst());
David Blaikiedc69ebb2013-03-11 23:39:23 +0000880 DIArray GVs = CUNode.getGlobalVariables();
881 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
882 CU->createGlobalVariableDIE(GVs.getElement(i));
883 DIArray SPs = CUNode.getSubprograms();
884 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
885 constructSubprogramDIE(CU, SPs.getElement(i));
886 DIArray EnumTypes = CUNode.getEnumTypes();
887 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
888 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
889 DIArray RetainedTypes = CUNode.getRetainedTypes();
890 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
891 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
David Blaikief55abea2013-04-22 06:12:31 +0000892 // Emit imported_modules last so that the relevant context is already
893 // available.
David Blaikie1fd43652013-05-07 21:35:53 +0000894 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
895 constructImportedEntityDIE(CU, ImportedEntities.getElement(i));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000896 }
Eric Christopher6a841382012-11-19 22:42:10 +0000897
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000898 // Tell MMI that we have debug info.
899 MMI->setDebugInfoAvailability(true);
Eric Christopher6a841382012-11-19 22:42:10 +0000900
Bill Wendling2b128d72009-05-20 23:19:06 +0000901 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +0000902 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +0000903}
904
Eric Christopher960ac372012-11-22 00:59:49 +0000905// Attach DW_AT_inline attribute with inlined subprogram DIEs.
906void DwarfDebug::computeInlinedDIEs() {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000907 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
908 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopher735401c2012-11-27 00:13:51 +0000909 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000910 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +0000911 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000912 }
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000913 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopher735401c2012-11-27 00:13:51 +0000914 AE = AbstractSPDies.end(); AI != AE; ++AI) {
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000915 DIE *ISP = AI->second;
916 if (InlinedSubprogramDIEs.count(ISP))
917 continue;
918 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
919 }
Eric Christopher960ac372012-11-22 00:59:49 +0000920}
921
922// Collect info for variables that were optimized out.
923void DwarfDebug::collectDeadVariables() {
924 const Module *M = MMI->getModule();
925 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
926
927 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
928 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
929 DICompileUnit TheCU(CU_Nodes->getOperand(i));
930 DIArray Subprograms = TheCU.getSubprograms();
931 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopher735401c2012-11-27 00:13:51 +0000932 DISubprogram SP(Subprograms.getElement(i));
933 if (ProcessedSPNodes.count(SP) != 0) continue;
Manman Ren7504ed42013-07-08 18:33:29 +0000934 if (!SP.isSubprogram()) continue;
Eric Christopher735401c2012-11-27 00:13:51 +0000935 if (!SP.isDefinition()) continue;
936 DIArray Variables = SP.getVariables();
937 if (Variables.getNumElements() == 0) continue;
Eric Christopher960ac372012-11-22 00:59:49 +0000938
Eric Christopher735401c2012-11-27 00:13:51 +0000939 LexicalScope *Scope =
940 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
941 DeadFnScopeMap[SP] = Scope;
Eric Christopher960ac372012-11-22 00:59:49 +0000942
Eric Christopher735401c2012-11-27 00:13:51 +0000943 // Construct subprogram DIE and add variables DIEs.
944 CompileUnit *SPCU = CUMap.lookup(TheCU);
945 assert(SPCU && "Unable to find Compile Unit!");
946 constructSubprogramDIE(SPCU, SP);
947 DIE *ScopeDIE = SPCU->getDIE(SP);
948 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
949 DIVariable DV(Variables.getElement(vi));
Manman Ren7504ed42013-07-08 18:33:29 +0000950 if (!DV.isVariable()) continue;
Chandler Carruth2a1c0d22013-07-27 11:09:58 +0000951 DbgVariable NewVar(DV, NULL);
Eric Christopher735401c2012-11-27 00:13:51 +0000952 if (DIE *VariableDIE =
Chandler Carruth2a1c0d22013-07-27 11:09:58 +0000953 SPCU->constructVariableDIE(&NewVar, Scope->isAbstractScope()))
Eric Christopher735401c2012-11-27 00:13:51 +0000954 ScopeDIE->addChild(VariableDIE);
955 }
Eric Christopher960ac372012-11-22 00:59:49 +0000956 }
957 }
958 }
959 DeleteContainerSeconds(DeadFnScopeMap);
960}
961
Eric Christopher45731982013-08-08 23:45:55 +0000962// Type Signature [7.27] and ODR Hash code.
Eric Christopher67646432013-07-26 17:02:41 +0000963
964/// \brief Grabs the string in whichever attribute is passed in and returns
Eric Christopher8552e222013-08-07 01:18:33 +0000965/// a reference to it. Returns "" if the attribute doesn't exist.
Eric Christopher67646432013-07-26 17:02:41 +0000966static StringRef getDIEStringAttr(DIE *Die, unsigned Attr) {
Eric Christopher8552e222013-08-07 01:18:33 +0000967 DIEValue *V = Die->findAttribute(Attr);
Eric Christopher67646432013-07-26 17:02:41 +0000968
Eric Christopher8552e222013-08-07 01:18:33 +0000969 if (DIEString *S = dyn_cast_or_null<DIEString>(V))
970 return S->getString();
971
Eric Christopher67646432013-07-26 17:02:41 +0000972 return StringRef("");
973}
974
Eric Christopher67646432013-07-26 17:02:41 +0000975/// Return true if the current DIE is contained within an anonymous namespace.
976static bool isContainedInAnonNamespace(DIE *Die) {
977 DIE *Parent = Die->getParent();
978
979 while (Parent) {
Eric Christophere414ece2013-07-29 23:53:08 +0000980 if (Parent->getTag() == dwarf::DW_TAG_namespace &&
981 getDIEStringAttr(Parent, dwarf::DW_AT_name) == "")
Eric Christopher67646432013-07-26 17:02:41 +0000982 return true;
983 Parent = Parent->getParent();
984 }
985
986 return false;
987}
988
Eric Christopheraf15f8d2013-08-07 01:18:24 +0000989/// Test if the current CU language is C++ and that we have
990/// a named type that is not contained in an anonymous namespace.
991static bool shouldAddODRHash(CompileUnit *CU, DIE *Die) {
Eric Christopher341770d2013-08-07 08:35:10 +0000992 return CU->getLanguage() == dwarf::DW_LANG_C_plus_plus &&
993 getDIEStringAttr(Die, dwarf::DW_AT_name) != "" &&
994 !isContainedInAnonNamespace(Die);
Eric Christopher45731982013-08-08 23:45:55 +0000995}
Eric Christopheraf15f8d2013-08-07 01:18:24 +0000996
Eric Christopher960ac372012-11-22 00:59:49 +0000997void DwarfDebug::finalizeModuleInfo() {
998 // Collect info for variables that were optimized out.
999 collectDeadVariables();
1000
1001 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1002 computeInlinedDIEs();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001003
Eric Christopherf8542ec2013-07-29 22:24:32 +00001004 // Split out type units and conditionally add an ODR tag to the split
1005 // out type.
Eric Christopher67646432013-07-26 17:02:41 +00001006 // FIXME: Do type splitting.
1007 for (unsigned i = 0, e = TypeUnits.size(); i != e; ++i) {
Eric Christopher67646432013-07-26 17:02:41 +00001008 DIE *Die = TypeUnits[i];
Eric Christopher45731982013-08-08 23:45:55 +00001009 DIEHash Hash;
Eric Christopheraf15f8d2013-08-07 01:18:24 +00001010 // If we've requested ODR hashes and it's applicable for an ODR hash then
1011 // add the ODR signature now.
Eric Christopher45731982013-08-08 23:45:55 +00001012 // FIXME: This should be added onto the type unit, not the type, but this
1013 // works as an intermediate stage.
Eric Christopheraf15f8d2013-08-07 01:18:24 +00001014 if (GenerateODRHash && shouldAddODRHash(CUMap.begin()->second, Die))
Eric Christopher45731982013-08-08 23:45:55 +00001015 CUMap.begin()->second->addUInt(Die, dwarf::DW_AT_GNU_odr_signature,
1016 dwarf::DW_FORM_data8,
1017 Hash.computeDIEODRSignature(Die));
Eric Christopher67646432013-07-26 17:02:41 +00001018 }
1019
Eric Christopher60eb7692013-08-12 20:27:48 +00001020 // Handle anything that needs to be done on a per-cu basis.
1021 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
1022 CUE = CUMap.end();
1023 CUI != CUE; ++CUI) {
1024 CompileUnit *TheCU = CUI->second;
1025 // Emit DW_AT_containing_type attribute to connect types with their
1026 // vtable holding type.
1027 TheCU->constructContainingTypeDIEs();
1028
1029 // If we're splitting the dwarf out now that we've got the entire
1030 // CU then construct a skeleton CU based upon it.
1031 if (useSplitDwarf()) {
Eric Christopherd29614f2013-08-13 01:21:55 +00001032 uint64_t ID = 0;
1033 if (GenerateCUHash) {
1034 DIEHash CUHash;
1035 ID = CUHash.computeCUSignature(TheCU->getCUDie());
1036 }
Eric Christopher60eb7692013-08-12 20:27:48 +00001037 // This should be a unique identifier when we want to build .dwp files.
1038 TheCU->addUInt(TheCU->getCUDie(), dwarf::DW_AT_GNU_dwo_id,
Eric Christopherd29614f2013-08-13 01:21:55 +00001039 dwarf::DW_FORM_data8, ID);
Eric Christopher60eb7692013-08-12 20:27:48 +00001040 // Now construct the skeleton CU associated.
1041 CompileUnit *SkCU = constructSkeletonCU(CUI->first);
1042 // This should be a unique identifier when we want to build .dwp files.
1043 SkCU->addUInt(SkCU->getCUDie(), dwarf::DW_AT_GNU_dwo_id,
Eric Christopherd29614f2013-08-13 01:21:55 +00001044 dwarf::DW_FORM_data8, ID);
Eric Christopher60eb7692013-08-12 20:27:48 +00001045 }
1046 }
1047
1048 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001049 InfoHolder.computeSizeAndOffsets();
1050 if (useSplitDwarf())
1051 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +00001052}
1053
1054void DwarfDebug::endSections() {
Bill Wendling2b128d72009-05-20 23:19:06 +00001055 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001056 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +00001057 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001058 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +00001059 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +00001060
1061 // End text sections.
Benjamin Kramer15591272012-10-31 13:45:49 +00001062 for (unsigned I = 0, E = SectionMap.size(); I != E; ++I) {
1063 Asm->OutStreamer.SwitchSection(SectionMap[I]);
1064 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", I+1));
Bill Wendling2b128d72009-05-20 23:19:06 +00001065 }
Eric Christopher960ac372012-11-22 00:59:49 +00001066}
Bill Wendling2b128d72009-05-20 23:19:06 +00001067
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001068// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +00001069void DwarfDebug::endModule() {
1070
1071 if (!FirstCU) return;
1072
1073 // End any existing sections.
1074 // TODO: Does this need to happen?
1075 endSections();
1076
1077 // Finalize the debug info for the module.
1078 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001079
Eric Christophercdf218d2012-12-10 19:51:21 +00001080 if (!useSplitDwarf()) {
Eric Christopher95198f502012-11-27 22:43:42 +00001081 // Emit all the DIEs into a debug info section.
1082 emitDebugInfo();
Eric Christopher4c9b1192012-11-27 00:41:54 +00001083
Eric Christopher95198f502012-11-27 22:43:42 +00001084 // Corresponding abbreviations into a abbrev section.
1085 emitAbbreviations();
1086
1087 // Emit info into a debug loc section.
1088 emitDebugLoc();
1089
1090 // Emit info into a debug aranges section.
1091 emitDebugARanges();
1092
1093 // Emit info into a debug ranges section.
1094 emitDebugRanges();
1095
1096 // Emit info into a debug macinfo section.
1097 emitDebugMacInfo();
1098
1099 // Emit inline info.
1100 // TODO: When we don't need the option anymore we
1101 // can remove all of the code that this section
1102 // depends upon.
1103 if (useDarwinGDBCompat())
1104 emitDebugInlineInfo();
1105 } else {
Eric Christopherd692c1d2012-12-11 19:42:09 +00001106 // TODO: Fill this in for separated debug sections and separate
Eric Christopher95198f502012-11-27 22:43:42 +00001107 // out information into new sections.
1108
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001109 // Emit the debug info section and compile units.
Eric Christopher95198f502012-11-27 22:43:42 +00001110 emitDebugInfo();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001111 emitDebugInfoDWO();
Eric Christopher95198f502012-11-27 22:43:42 +00001112
1113 // Corresponding abbreviations into a abbrev section.
1114 emitAbbreviations();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001115 emitDebugAbbrevDWO();
Eric Christopher95198f502012-11-27 22:43:42 +00001116
1117 // Emit info into a debug loc section.
1118 emitDebugLoc();
1119
1120 // Emit info into a debug aranges section.
1121 emitDebugARanges();
1122
1123 // Emit info into a debug ranges section.
1124 emitDebugRanges();
1125
1126 // Emit info into a debug macinfo section.
1127 emitDebugMacInfo();
1128
Eric Christopher962c9082013-01-15 23:56:56 +00001129 // Emit DWO addresses.
1130 InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
1131
Eric Christopher95198f502012-11-27 22:43:42 +00001132 // Emit inline info.
1133 // TODO: When we don't need the option anymore we
1134 // can remove all of the code that this section
1135 // depends upon.
1136 if (useDarwinGDBCompat())
1137 emitDebugInlineInfo();
1138 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001139
Eric Christophera876b822012-08-23 07:32:06 +00001140 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +00001141 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +00001142 emitAccelNames();
1143 emitAccelObjC();
1144 emitAccelNamespaces();
1145 emitAccelTypes();
1146 }
Eric Christopher6a841382012-11-19 22:42:10 +00001147
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001148 // Emit info into a debug pubnames section, if requested.
1149 if (GenerateDwarfPubNamesSection)
1150 emitDebugPubnames();
1151
Devang Patel04d2f2d2009-11-24 01:14:22 +00001152 // Emit info into a debug pubtypes section.
Eric Christopher77826182012-08-23 07:10:56 +00001153 // TODO: When we don't need the option anymore we can
1154 // remove all of the code that adds to the table.
Eric Christopher20b76a72012-08-23 22:36:40 +00001155 if (useDarwinGDBCompat())
Eric Christopher77826182012-08-23 07:10:56 +00001156 emitDebugPubTypes();
Devang Patel04d2f2d2009-11-24 01:14:22 +00001157
Eric Christopher95198f502012-11-27 22:43:42 +00001158 // Finally emit string information into a string table.
Eric Christopher6e20a162012-11-27 06:49:23 +00001159 emitDebugStr();
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001160 if (useSplitDwarf())
1161 emitDebugStrDWO();
Eric Christopher6e20a162012-11-27 06:49:23 +00001162
Devang Pateld0701282010-08-02 17:32:15 +00001163 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001164 SPMap.clear();
Devang Patel1a0df9a2010-05-10 22:49:55 +00001165 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1166 E = CUMap.end(); I != E; ++I)
1167 delete I->second;
Eric Christopher8afd7b62012-12-10 19:51:18 +00001168
Craig Topperd8e43652013-07-03 04:17:25 +00001169 for (SmallVectorImpl<CompileUnit *>::iterator I = SkeletonCUs.begin(),
Eric Christopher5b33b3c2013-02-06 21:53:56 +00001170 E = SkeletonCUs.end(); I != E; ++I)
1171 delete *I;
Eric Christopher8afd7b62012-12-10 19:51:18 +00001172
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001173 // Reset these for the next Module if we have one.
1174 FirstCU = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001175}
1176
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001177// Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001178DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001179 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001180 LLVMContext &Ctx = DV->getContext();
1181 // More then one inlined variable corresponds to one abstract variable.
1182 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001183 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001184 if (AbsDbgVariable)
1185 return AbsDbgVariable;
1186
Devang Patel7e623022011-08-10 20:55:27 +00001187 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001188 if (!Scope)
1189 return NULL;
1190
Devang Patel99819b52011-08-15 19:01:20 +00001191 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patel7e623022011-08-10 20:55:27 +00001192 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001193 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001194 return AbsDbgVariable;
1195}
1196
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001197// If Var is a current function argument then add it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +00001198bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +00001199 DbgVariable *Var, LexicalScope *Scope) {
1200 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001201 return false;
1202 DIVariable DV = Var->getVariable();
1203 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1204 return false;
1205 unsigned ArgNo = DV.getArgNumber();
Eric Christopher6a841382012-11-19 22:42:10 +00001206 if (ArgNo == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001207 return false;
1208
Devang Patel4ab660b2011-03-03 20:02:02 +00001209 size_t Size = CurrentFnArguments.size();
1210 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001211 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001212 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001213 // arguments does the function have at source level.
1214 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001215 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001216 CurrentFnArguments[ArgNo - 1] = Var;
1217 return true;
1218}
1219
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001220// Collect variable information from side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001221void
Nick Lewycky019d2552011-07-29 03:49:23 +00001222DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patel490c8ab2010-05-20 19:57:06 +00001223 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +00001224 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1225 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1226 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00001227 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001228 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001229 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001230 DIVariable DV(Var);
1231 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001232
Devang Patel7e623022011-08-10 20:55:27 +00001233 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001234
Devang Patelcdb7d442009-11-10 23:20:04 +00001235 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001236 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001237 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001238
Devang Patele1c53f22010-05-20 16:36:41 +00001239 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel99819b52011-08-15 19:01:20 +00001240 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patel3e4a9652011-08-15 21:24:36 +00001241 RegVar->setFrameIndex(VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +00001242 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001243 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001244 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +00001245 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +00001246 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001247}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001248
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001249// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1250// defined reg.
Devang Patel9fc11702010-05-25 23:40:22 +00001251static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001252 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001253 return MI->getNumOperands() == 3 &&
1254 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
Adrian Prantl418d1d12013-07-09 20:28:37 +00001255 (MI->getOperand(1).isImm() ||
1256 (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
Devang Patel9fc11702010-05-25 23:40:22 +00001257}
1258
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001259// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher6a841382012-11-19 22:42:10 +00001260static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1261 const MCSymbol *FLabel,
Devang Patel2442a892011-07-08 17:09:57 +00001262 const MCSymbol *SLabel,
1263 const MachineInstr *MI) {
1264 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1265
David Blaikie0252265b2013-06-16 20:34:15 +00001266 assert(MI->getNumOperands() == 3);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001267 if (MI->getOperand(0).isReg()) {
Devang Patel2442a892011-07-08 17:09:57 +00001268 MachineLocation MLoc;
Adrian Prantl418d1d12013-07-09 20:28:37 +00001269 // If the second operand is an immediate, this is a
1270 // register-indirect address.
1271 if (!MI->getOperand(1).isImm())
Adrian Prantld4c0dd42013-04-26 21:57:17 +00001272 MLoc.set(MI->getOperand(0).getReg());
1273 else
1274 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Devang Patel2442a892011-07-08 17:09:57 +00001275 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1276 }
1277 if (MI->getOperand(0).isImm())
1278 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1279 if (MI->getOperand(0).isFPImm())
1280 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1281 if (MI->getOperand(0).isCImm())
1282 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1283
Craig Topperee4dab52012-02-05 08:31:47 +00001284 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001285}
1286
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001287// Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001288void
Devang Patel5c0f85c2010-06-25 22:07:34 +00001289DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1290 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001291
Eric Christopher270a12c2013-07-03 21:37:03 +00001292 // Grab the variable info that was squirreled away in the MMI side-table.
Devang Patel490c8ab2010-05-20 19:57:06 +00001293 collectVariableInfoFromMMITable(MF, Processed);
1294
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001295 for (SmallVectorImpl<const MDNode*>::const_iterator
1296 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1297 ++UVI) {
1298 const MDNode *Var = *UVI;
1299 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001300 continue;
1301
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001302 // History contains relevant DBG_VALUE instructions for Var and instructions
1303 // clobbering it.
1304 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1305 if (History.empty())
1306 continue;
1307 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001308
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001309 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001310 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001311 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1312 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001313 Scope = LScopes.getCurrentFunctionScope();
David Blaikiedc69ebb2013-03-11 23:39:23 +00001314 else if (MDNode *IA = DV.getInlinedAt())
1315 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
1316 else
1317 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001318 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001319 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001320 continue;
1321
1322 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001323 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001324 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1325 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel6c622ef2011-03-01 22:58:55 +00001326 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001327 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001328 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001329 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001330
Eric Christophercc10d202012-10-08 20:48:54 +00001331 // Simplify ranges that are fully coalesced.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001332 if (History.size() <= 1 || (History.size() == 2 &&
1333 MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001334 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001335 continue;
1336 }
1337
Eric Christopher59cc0712013-01-28 17:33:26 +00001338 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001339 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001340
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001341 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1342 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1343 const MachineInstr *Begin = *HI;
1344 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001345
Devang Patele7181b52011-06-01 23:00:17 +00001346 // Check if DBG_VALUE is truncating a range.
1347 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1348 && !Begin->getOperand(0).getReg())
1349 continue;
1350
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001351 // Compute the range for a register location.
1352 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1353 const MCSymbol *SLabel = 0;
1354
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001355 if (HI + 1 == HE)
1356 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001357 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001358 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001359 else {
1360 const MachineInstr *End = HI[1];
Eric Christopher6a841382012-11-19 22:42:10 +00001361 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Devang Patel53b050a2011-07-07 21:44:42 +00001362 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001363 if (End->isDebugValue())
1364 SLabel = getLabelBeforeInsn(End);
1365 else {
1366 // End is a normal instruction clobbering the range.
1367 SLabel = getLabelAfterInsn(End);
1368 assert(SLabel && "Forgot label after clobber instruction");
1369 ++HI;
1370 }
1371 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001372
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001373 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopher365d0832011-12-16 23:42:31 +00001374 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1375 Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001376 }
1377 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001378 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001379
1380 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001381 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1382 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1383 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1384 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001385 if (!DV || !DV.isVariable() || !Processed.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001386 continue;
1387 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1388 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patele0a94bf2010-05-14 21:01:35 +00001389 }
Devang Patel9fc11702010-05-25 23:40:22 +00001390}
Devang Patele0a94bf2010-05-14 21:01:35 +00001391
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001392// Return Label preceding the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001393MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001394 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1395 assert(Label && "Didn't insert label before instruction");
1396 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001397}
1398
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001399// Return Label immediately following the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001400MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001401 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001402}
1403
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001404// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001405void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001406 // Check if source location changes, but ignore DBG_VALUE locations.
1407 if (!MI->isDebugValue()) {
1408 DebugLoc DL = MI->getDebugLoc();
1409 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001410 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001411 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001412 if (DL == PrologEndLoc) {
1413 Flags |= DWARF2_FLAG_PROLOGUE_END;
1414 PrologEndLoc = DebugLoc();
1415 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001416 if (PrologEndLoc.isUnknown())
1417 Flags |= DWARF2_FLAG_IS_STMT;
1418
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001419 if (!DL.isUnknown()) {
1420 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001421 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001422 } else
Devang Patel34a66202011-05-11 19:22:19 +00001423 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001424 }
Devang Patel9fc11702010-05-25 23:40:22 +00001425 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001426
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001427 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001428 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1429 LabelsBeforeInsn.find(MI);
1430
1431 // No label needed.
1432 if (I == LabelsBeforeInsn.end())
1433 return;
1434
1435 // Label already assigned.
1436 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001437 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001438
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001439 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001440 PrevLabel = MMI->getContext().CreateTempSymbol();
1441 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001442 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001443 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001444}
1445
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001446// Process end of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001447void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001448 // Don't create a new label after DBG_VALUE instructions.
1449 // They don't generate code.
1450 if (!MI->isDebugValue())
1451 PrevLabel = 0;
1452
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001453 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1454 LabelsAfterInsn.find(MI);
1455
1456 // No label needed.
1457 if (I == LabelsAfterInsn.end())
1458 return;
1459
1460 // Label already assigned.
1461 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001462 return;
1463
1464 // We need a label after this instruction.
1465 if (!PrevLabel) {
1466 PrevLabel = MMI->getContext().CreateTempSymbol();
1467 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001468 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001469 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001470}
1471
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001472// Each LexicalScope has first instruction and last instruction to mark
1473// beginning and end of a scope respectively. Create an inverse map that list
1474// scopes starts (and ends) with an instruction. One instruction may start (or
1475// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001476void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001477 SmallVector<LexicalScope *, 4> WorkList;
1478 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001479 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001480 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001481
Craig Topper977e9cd2013-07-03 04:24:43 +00001482 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001483 if (!Children.empty())
Craig Topperd8e43652013-07-03 04:17:25 +00001484 for (SmallVectorImpl<LexicalScope *>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001485 SE = Children.end(); SI != SE; ++SI)
1486 WorkList.push_back(*SI);
1487
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001488 if (S->isAbstractScope())
1489 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001490
Craig Topper977e9cd2013-07-03 04:24:43 +00001491 const SmallVectorImpl<InsnRange> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001492 if (Ranges.empty())
1493 continue;
Craig Topperd8e43652013-07-03 04:17:25 +00001494 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +00001495 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001496 assert(RI->first && "InsnRange does not have first instruction!");
1497 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001498 requestLabelBeforeInsn(RI->first);
1499 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001500 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001501 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001502}
1503
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001504// Get MDNode for DebugLoc's scope.
Devang Patel589845d2011-05-09 22:14:49 +00001505static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1506 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1507 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1508 return DL.getScope(Ctx);
1509}
1510
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001511// Walk up the scope chain of given debug loc and find line number info
1512// for the function.
Devang Patel34a66202011-05-11 19:22:19 +00001513static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1514 const MDNode *Scope = getScopeNode(DL, Ctx);
1515 DISubprogram SP = getDISubprogram(Scope);
Manman Ren7504ed42013-07-08 18:33:29 +00001516 if (SP.isSubprogram()) {
Eric Christopher34164192012-04-03 00:43:49 +00001517 // Check for number of operands since the compatibility is
1518 // cheap here.
Eric Christopherb81e2b42012-04-03 17:55:42 +00001519 if (SP->getNumOperands() > 19)
Eric Christopher34164192012-04-03 00:43:49 +00001520 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1521 else
1522 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1523 }
1524
Devang Patel34a66202011-05-11 19:22:19 +00001525 return DebugLoc();
1526}
1527
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001528// Gather pre-function debug information. Assumes being called immediately
1529// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001530void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001531 if (!MMI->hasDebugInfo()) return;
Devang Patel7e623022011-08-10 20:55:27 +00001532 LScopes.initialize(*MF);
1533 if (LScopes.empty()) return;
1534 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001535
Manman Ren4e042a62013-02-05 21:52:47 +00001536 // Set DwarfCompileUnitID in MCContext to the Compile Unit this function
1537 // belongs to.
1538 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1539 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
1540 assert(TheCU && "Unable to find compile unit!");
Manman Ren9d4c7352013-05-21 00:57:22 +00001541 if (Asm->TM.hasMCUseLoc() &&
1542 Asm->OutStreamer.getKind() == MCStreamer::SK_AsmStreamer)
1543 // Use a single line table if we are using .loc and generating assembly.
1544 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1545 else
1546 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001547
Devang Patel6c74a872010-04-27 19:46:33 +00001548 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1549 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001550 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001551 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001552
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001553 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1554
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001555 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001556 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001557 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1558
Devang Patel002d54d2010-05-26 19:37:24 +00001559 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001560 I != E; ++I) {
1561 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001562 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1563 II != IE; ++II) {
1564 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001565
Devang Patel002d54d2010-05-26 19:37:24 +00001566 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001567 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001568
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001569 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001570 const MDNode *Var =
1571 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001572
1573 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001574 if (isDbgValueInDefinedReg(MI))
1575 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1576
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001577 // Check the history of this variable.
1578 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1579 if (History.empty()) {
1580 UserVariables.push_back(Var);
1581 // The first mention of a function argument gets the FunctionBeginSym
1582 // label, so arguments are visible when breaking at function entry.
1583 DIVariable DV(Var);
Manman Ren7504ed42013-07-08 18:33:29 +00001584 if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001585 DISubprogram(getDISubprogram(DV.getContext()))
1586 .describes(MF->getFunction()))
1587 LabelsBeforeInsn[MI] = FunctionBeginSym;
1588 } else {
1589 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1590 const MachineInstr *Prev = History.back();
1591 if (Prev->isDebugValue()) {
1592 // Coalesce identical entries at the end of History.
1593 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001594 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher85a495e2012-10-08 20:48:49 +00001595 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopher6a841382012-11-19 22:42:10 +00001596 << "\t" << *Prev
Devang Patelb7a328e2011-07-07 00:14:27 +00001597 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001598 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001599 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001600
1601 // Terminate old register assignments that don't reach MI;
1602 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1603 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1604 isDbgValueInDefinedReg(Prev)) {
1605 // Previous register assignment needs to terminate at the end of
1606 // its basic block.
1607 MachineBasicBlock::const_iterator LastMI =
1608 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001609 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001610 // Drop DBG_VALUE for empty range.
Eric Christopher85a495e2012-10-08 20:48:49 +00001611 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Devang Patelb7a328e2011-07-07 00:14:27 +00001612 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001613 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001614 } else if (llvm::next(PrevMBB) != PrevMBB->getParent()->end())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001615 // Terminate after LastMI.
1616 History.push_back(LastMI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001617 }
1618 }
1619 }
1620 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001621 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001622 // Not a DBG_VALUE instruction.
1623 if (!MI->isLabel())
1624 AtBlockEntry = false;
1625
Eric Christopher133195782012-10-04 20:46:14 +00001626 // First known non-DBG_VALUE and non-frame setup location marks
1627 // the beginning of the function body.
1628 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1629 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel34a66202011-05-11 19:22:19 +00001630 PrologEndLoc = MI->getDebugLoc();
1631
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001632 // Check if the instruction clobbers any registers with debug vars.
1633 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1634 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1635 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1636 continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001637 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1638 AI.isValid(); ++AI) {
1639 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001640 const MDNode *Var = LiveUserVar[Reg];
1641 if (!Var)
1642 continue;
1643 // Reg is now clobbered.
1644 LiveUserVar[Reg] = 0;
1645
1646 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001647 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1648 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001649 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001650 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1651 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001652 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001653 const MachineInstr *Prev = History.back();
1654 // Sanity-check: Register assignments are terminated at the end of
1655 // their block.
1656 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1657 continue;
1658 // Is the variable still in Reg?
1659 if (!isDbgValueInDefinedReg(Prev) ||
1660 Prev->getOperand(0).getReg() != Reg)
1661 continue;
1662 // Var is clobbered. Make sure the next instruction gets a label.
1663 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001664 }
1665 }
Devang Patel002d54d2010-05-26 19:37:24 +00001666 }
Devang Patel002d54d2010-05-26 19:37:24 +00001667 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001668 }
1669
1670 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1671 I != E; ++I) {
1672 SmallVectorImpl<const MachineInstr*> &History = I->second;
1673 if (History.empty())
1674 continue;
1675
1676 // Make sure the final register assignments are terminated.
1677 const MachineInstr *Prev = History.back();
1678 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1679 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher6a841382012-11-19 22:42:10 +00001680 MachineBasicBlock::const_iterator LastMI =
Devang Patel784077e2011-08-10 23:58:09 +00001681 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001682 if (LastMI == PrevMBB->end())
1683 // Drop DBG_VALUE for empty range.
1684 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001685 else if (PrevMBB != &PrevMBB->getParent()->back()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001686 // Terminate after LastMI.
1687 History.push_back(LastMI);
1688 }
1689 }
1690 // Request labels for the full history.
1691 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1692 const MachineInstr *MI = History[i];
1693 if (MI->isDebugValue())
1694 requestLabelBeforeInsn(MI);
1695 else
1696 requestLabelAfterInsn(MI);
1697 }
1698 }
Devang Patel002d54d2010-05-26 19:37:24 +00001699
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001700 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001701 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001702
1703 // Record beginning of function.
1704 if (!PrologEndLoc.isUnknown()) {
1705 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1706 MF->getFunction()->getContext());
1707 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1708 FnStartDL.getScope(MF->getFunction()->getContext()),
David Blaikie67cb31e2012-12-04 22:02:33 +00001709 // We'd like to list the prologue as "not statements" but GDB behaves
1710 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
David Blaikie5a773bb2012-12-04 21:05:36 +00001711 DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001712 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001713}
1714
Devang Patel7e623022011-08-10 20:55:27 +00001715void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie6f1a8062013-06-05 05:39:59 +00001716 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
1717 DIVariable DV = Var->getVariable();
David Blaikie36d5d2f2013-06-06 21:04:51 +00001718 // Variables with positive arg numbers are parameters.
1719 if (unsigned ArgNum = DV.getArgNumber()) {
1720 // Keep all parameters in order at the start of the variable list to ensure
1721 // function types are correct (no out-of-order parameters)
1722 //
1723 // This could be improved by only doing it for optimized builds (unoptimized
1724 // builds have the right order to begin with), searching from the back (this
1725 // would catch the unoptimized case quickly), or doing a binary search
1726 // rather than linear search.
1727 SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
1728 while (I != Vars.end()) {
1729 unsigned CurNum = (*I)->getVariable().getArgNumber();
1730 // A local (non-parameter) variable has been found, insert immediately
1731 // before it.
1732 if (CurNum == 0)
1733 break;
1734 // A later indexed parameter has been found, insert immediately before it.
David Blaikieb272a752013-06-06 22:28:26 +00001735 if (CurNum > ArgNum)
David Blaikie36d5d2f2013-06-06 21:04:51 +00001736 break;
David Blaikieb272a752013-06-06 22:28:26 +00001737 ++I;
David Blaikie6f1a8062013-06-05 05:39:59 +00001738 }
David Blaikie36d5d2f2013-06-06 21:04:51 +00001739 Vars.insert(I, Var);
1740 return;
David Blaikie6f1a8062013-06-05 05:39:59 +00001741 }
1742
1743 Vars.push_back(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001744}
1745
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001746// Gather and emit post-function debug information.
Chris Lattner76555b52010-01-26 23:18:02 +00001747void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patel7e623022011-08-10 20:55:27 +00001748 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00001749
Devang Patel7e623022011-08-10 20:55:27 +00001750 // Define end label for subprogram.
1751 FunctionEndSym = Asm->GetTempSymbol("func_end",
1752 Asm->getFunctionNumber());
1753 // Assumes in correct section after the entry point.
1754 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Manman Ren4e042a62013-02-05 21:52:47 +00001755 // Set DwarfCompileUnitID in MCContext to default value.
1756 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001757
Devang Patel7e623022011-08-10 20:55:27 +00001758 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1759 collectVariableInfo(MF, ProcessedVars);
Eric Christopher6a841382012-11-19 22:42:10 +00001760
Devang Patel3acc70e2011-08-15 22:04:40 +00001761 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001762 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001763 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001764
Devang Patel7e623022011-08-10 20:55:27 +00001765 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001766 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1767 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1768 LexicalScope *AScope = AList[i];
1769 DISubprogram SP(AScope->getScopeNode());
Manman Ren7504ed42013-07-08 18:33:29 +00001770 if (SP.isSubprogram()) {
Devang Patel7e623022011-08-10 20:55:27 +00001771 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001772 DIArray Variables = SP.getVariables();
1773 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1774 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001775 if (!DV || !DV.isVariable() || !ProcessedVars.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001776 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001777 // Check that DbgVariable for DV wasn't created earlier, when
1778 // findAbstractVariable() was called for inlined instance of DV.
1779 LLVMContext &Ctx = DV->getContext();
1780 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1781 if (AbstractVariables.lookup(CleanDV))
1782 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001783 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1784 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001785 }
1786 }
Devang Patel44403472011-08-12 18:10:19 +00001787 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Manman Ren4213c392013-05-29 17:16:59 +00001788 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001789 }
Eric Christopher6a841382012-11-19 22:42:10 +00001790
Devang Patel3acc70e2011-08-15 22:04:40 +00001791 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Eric Christopher6a841382012-11-19 22:42:10 +00001792
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001793 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopherbb69a272012-08-24 01:14:27 +00001794 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001795
Bill Wendling2b128d72009-05-20 23:19:06 +00001796 // Clear debug info
Craig Topper2b4a2012013-07-03 04:40:27 +00001797 for (ScopeVariablesMap::iterator
Devang Patel7e623022011-08-10 20:55:27 +00001798 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1799 DeleteContainerPointers(I->second);
1800 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001801 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001802 UserVariables.clear();
1803 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001804 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001805 LabelsBeforeInsn.clear();
1806 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001807 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001808}
1809
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001810// Register a source line with debug info. Returns the unique label that was
1811// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001812void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1813 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001814 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001815 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001816 unsigned Src = 1;
1817 if (S) {
1818 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001819
Dan Gohman50849c62010-05-05 23:41:32 +00001820 if (Scope.isCompileUnit()) {
1821 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001822 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001823 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001824 } else if (Scope.isFile()) {
1825 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001826 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001827 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001828 } else if (Scope.isSubprogram()) {
1829 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001830 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001831 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001832 } else if (Scope.isLexicalBlockFile()) {
1833 DILexicalBlockFile DBF(S);
1834 Fn = DBF.getFilename();
1835 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001836 } else if (Scope.isLexicalBlock()) {
1837 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001838 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001839 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001840 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001841 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001842
Manman Ren1e427202013-03-07 01:42:00 +00001843 Src = getOrCreateSourceID(Fn, Dir,
1844 Asm->OutStreamer.getContext().getDwarfCompileUnitID());
Dan Gohman50849c62010-05-05 23:41:32 +00001845 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001846 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001847}
1848
Bill Wendling806535f2009-05-20 23:22:40 +00001849//===----------------------------------------------------------------------===//
1850// Emit Methods
1851//===----------------------------------------------------------------------===//
1852
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001853// Compute the size and offset of a DIE.
Jim Grosbach00e9c612009-11-22 19:20:36 +00001854unsigned
Eric Christopherc8a310e2012-12-10 23:34:43 +00001855DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling480ff322009-05-20 23:21:38 +00001856 // Get the children.
1857 const std::vector<DIE *> &Children = Die->getChildren();
1858
Bill Wendling480ff322009-05-20 23:21:38 +00001859 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001860 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001861
1862 // Get the abbreviation for this DIE.
1863 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopherc8a310e2012-12-10 23:34:43 +00001864 const DIEAbbrev *Abbrev = Abbreviations->at(AbbrevNumber - 1);
Bill Wendling480ff322009-05-20 23:21:38 +00001865
1866 // Set DIE offset
1867 Die->setOffset(Offset);
1868
1869 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001870 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001871
Eric Christopher4887c8f2013-03-29 23:34:06 +00001872 const SmallVectorImpl<DIEValue*> &Values = Die->getValues();
1873 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001874
1875 // Size the DIE attribute values.
1876 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1877 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001878 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001879
1880 // Size the DIE children if any.
1881 if (!Children.empty()) {
1882 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1883 "Children flag not set");
1884
1885 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher1f0cbb82012-11-20 22:14:13 +00001886 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling480ff322009-05-20 23:21:38 +00001887
1888 // End of children marker.
1889 Offset += sizeof(int8_t);
1890 }
1891
1892 Die->setSize(Offset - Die->getOffset());
1893 return Offset;
1894}
1895
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001896// Compute the size and offset of all the DIEs.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001897void DwarfUnits::computeSizeAndOffsets() {
Manman Ren14a029d2013-03-12 18:27:15 +00001898 // Offset from the beginning of debug info section.
Eric Christopherd1c5a312013-05-30 00:43:35 +00001899 unsigned SecOffset = 0;
Eric Christopher4887c8f2013-03-29 23:34:06 +00001900 for (SmallVectorImpl<CompileUnit *>::iterator I = CUs.begin(),
Eric Christopherc8a310e2012-12-10 23:34:43 +00001901 E = CUs.end(); I != E; ++I) {
Eric Christopherd1c5a312013-05-30 00:43:35 +00001902 (*I)->setDebugInfoOffset(SecOffset);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001903 unsigned Offset =
1904 sizeof(int32_t) + // Length of Compilation Unit Info
1905 sizeof(int16_t) + // DWARF version number
1906 sizeof(int32_t) + // Offset Into Abbrev. Section
1907 sizeof(int8_t); // Pointer Size (in bytes)
1908
Manman Ren14a029d2013-03-12 18:27:15 +00001909 unsigned EndOffset = computeSizeAndOffset((*I)->getCUDie(), Offset);
Eric Christopherd1c5a312013-05-30 00:43:35 +00001910 SecOffset += EndOffset;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001911 }
Bill Wendling480ff322009-05-20 23:21:38 +00001912}
1913
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001914// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001915void DwarfDebug::emitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001916 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001917
Bill Wendling480ff322009-05-20 23:21:38 +00001918 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001919 DwarfInfoSectionSym =
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001920 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001921 DwarfAbbrevSectionSym =
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001922 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher3c5a1912012-12-19 22:02:53 +00001923 if (useSplitDwarf())
1924 DwarfAbbrevDWOSectionSym =
1925 emitSectionSym(Asm, TLOF.getDwarfAbbrevDWOSection(),
1926 "section_abbrev_dwo");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001927 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001928
Chris Lattner6629ca92010-04-04 22:59:04 +00001929 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001930 emitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001931
Eric Christopher74804332013-02-07 21:19:50 +00001932 DwarfLineSectionSym =
1933 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001934 emitSectionSym(Asm, TLOF.getDwarfLocSection());
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001935 if (GenerateDwarfPubNamesSection)
1936 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001937 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001938 DwarfStrSectionSym =
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001939 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001940 if (useSplitDwarf()) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001941 DwarfStrDWOSectionSym =
1942 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001943 DwarfAddrSectionSym =
1944 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
1945 }
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001946 DwarfDebugRangeSectionSym = emitSectionSym(Asm, TLOF.getDwarfRangesSection(),
Devang Patel12563b32010-04-16 23:33:45 +00001947 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001948
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001949 DwarfDebugLocSectionSym = emitSectionSym(Asm, TLOF.getDwarfLocSection(),
Devang Patel9fc11702010-05-25 23:40:22 +00001950 "section_debug_loc");
1951
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001952 TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
1953 emitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001954}
1955
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001956// Recursively emits a debug information entry.
Eric Christopher3c5a1912012-12-19 22:02:53 +00001957void DwarfDebug::emitDIE(DIE *Die, std::vector<DIEAbbrev *> *Abbrevs) {
Bill Wendling480ff322009-05-20 23:21:38 +00001958 // Get the abbreviation for this DIE.
1959 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001960 const DIEAbbrev *Abbrev = Abbrevs->at(AbbrevNumber - 1);
Bill Wendling480ff322009-05-20 23:21:38 +00001961
Bill Wendling480ff322009-05-20 23:21:38 +00001962 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001963 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00001964 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1965 Twine::utohexstr(Die->getOffset()) + ":0x" +
1966 Twine::utohexstr(Die->getSize()) + " " +
1967 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00001968 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001969
Eric Christopher4887c8f2013-03-29 23:34:06 +00001970 const SmallVectorImpl<DIEValue*> &Values = Die->getValues();
1971 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001972
1973 // Emit the DIE attribute values.
1974 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1975 unsigned Attr = AbbrevData[i].getAttribute();
1976 unsigned Form = AbbrevData[i].getForm();
1977 assert(Form && "Too many attributes for DIE (check abbreviation)");
1978
Chris Lattner7bde8c02010-04-04 18:52:31 +00001979 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001980 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001981
Bill Wendling480ff322009-05-20 23:21:38 +00001982 switch (Attr) {
Bill Wendling480ff322009-05-20 23:21:38 +00001983 case dwarf::DW_AT_abstract_origin: {
1984 DIEEntry *E = cast<DIEEntry>(Values[i]);
1985 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001986 unsigned Addr = Origin->getOffset();
Manman Ren14a029d2013-03-12 18:27:15 +00001987 if (Form == dwarf::DW_FORM_ref_addr) {
1988 // For DW_FORM_ref_addr, output the offset from beginning of debug info
1989 // section. Origin->getOffset() returns the offset from start of the
1990 // compile unit.
1991 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1992 Addr += Holder.getCUOffset(Origin->getCompileUnit());
1993 }
Manman Renac8062b2013-07-02 23:40:10 +00001994 Asm->OutStreamer.EmitIntValue(Addr,
1995 Form == dwarf::DW_FORM_ref_addr ? DIEEntry::getRefAddrSize(Asm) : 4);
Bill Wendling480ff322009-05-20 23:21:38 +00001996 break;
1997 }
Devang Patel12563b32010-04-16 23:33:45 +00001998 case dwarf::DW_AT_ranges: {
1999 // DW_AT_range Value encodes offset in debug_range section.
2000 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00002001
Nick Lewycky33da3362012-06-22 01:25:12 +00002002 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelda3ef852010-09-02 16:43:44 +00002003 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
2004 V->getValue(),
2005 4);
2006 } else {
2007 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2008 V->getValue(),
2009 DwarfDebugRangeSectionSym,
2010 4);
2011 }
Devang Patel12563b32010-04-16 23:33:45 +00002012 break;
2013 }
Devang Patel9fc11702010-05-25 23:40:22 +00002014 case dwarf::DW_AT_location: {
Nick Lewycky33da3362012-06-22 01:25:12 +00002015 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
2016 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +00002017 Asm->EmitLabelReference(L->getValue(), 4);
Nick Lewycky33da3362012-06-22 01:25:12 +00002018 else
Ulrich Weigand396ba8b2013-07-02 18:46:26 +00002019 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Nick Lewycky33da3362012-06-22 01:25:12 +00002020 } else {
Devang Patel9fc11702010-05-25 23:40:22 +00002021 Values[i]->EmitValue(Asm, Form);
Nick Lewycky33da3362012-06-22 01:25:12 +00002022 }
Devang Patel9fc11702010-05-25 23:40:22 +00002023 break;
2024 }
Devang Patela1bd5a12010-09-29 19:08:08 +00002025 case dwarf::DW_AT_accessibility: {
2026 if (Asm->isVerbose()) {
2027 DIEInteger *V = cast<DIEInteger>(Values[i]);
2028 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
2029 }
2030 Values[i]->EmitValue(Asm, Form);
2031 break;
2032 }
Bill Wendling480ff322009-05-20 23:21:38 +00002033 default:
2034 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002035 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00002036 break;
2037 }
Bill Wendling480ff322009-05-20 23:21:38 +00002038 }
2039
2040 // Emit the DIE children if any.
2041 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2042 const std::vector<DIE *> &Children = Die->getChildren();
2043
2044 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher3c5a1912012-12-19 22:02:53 +00002045 emitDIE(Children[j], Abbrevs);
Bill Wendling480ff322009-05-20 23:21:38 +00002046
Chris Lattner7bde8c02010-04-04 18:52:31 +00002047 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00002048 Asm->OutStreamer.AddComment("End Of Children Mark");
2049 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00002050 }
2051}
2052
Eric Christophera2de8262012-12-15 00:04:07 +00002053// Emit the various dwarf units to the unit section USection with
2054// the abbreviations going into ASection.
2055void DwarfUnits::emitUnits(DwarfDebug *DD,
2056 const MCSection *USection,
2057 const MCSection *ASection,
2058 const MCSymbol *ASectionSym) {
2059 Asm->OutStreamer.SwitchSection(USection);
Eric Christopher4887c8f2013-03-29 23:34:06 +00002060 for (SmallVectorImpl<CompileUnit *>::iterator I = CUs.begin(),
Eric Christophera2de8262012-12-15 00:04:07 +00002061 E = CUs.end(); I != E; ++I) {
2062 CompileUnit *TheCU = *I;
Devang Patel1a0df9a2010-05-10 22:49:55 +00002063 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002064
Devang Patel1a0df9a2010-05-10 22:49:55 +00002065 // Emit the compile units header.
Eric Christophera2de8262012-12-15 00:04:07 +00002066 Asm->OutStreamer
2067 .EmitLabel(Asm->GetTempSymbol(USection->getLabelBeginName(),
2068 TheCU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002069
Devang Patel1a0df9a2010-05-10 22:49:55 +00002070 // Emit size of content not including length itself
2071 unsigned ContentSize = Die->getSize() +
2072 sizeof(int16_t) + // DWARF version number
2073 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00002074 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002075
Devang Patel1a0df9a2010-05-10 22:49:55 +00002076 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2077 Asm->EmitInt32(ContentSize);
2078 Asm->OutStreamer.AddComment("DWARF version number");
Manman Renac8062b2013-07-02 23:40:10 +00002079 Asm->EmitInt16(DD->getDwarfVersion());
Devang Patel1a0df9a2010-05-10 22:49:55 +00002080 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christophera2de8262012-12-15 00:04:07 +00002081 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
2082 ASectionSym);
Devang Patel1a0df9a2010-05-10 22:49:55 +00002083 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002084 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002085
Eric Christopher3c5a1912012-12-19 22:02:53 +00002086 DD->emitDIE(Die, Abbreviations);
Eric Christophera2de8262012-12-15 00:04:07 +00002087 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(USection->getLabelEndName(),
Eli Benderskyb42d1462012-12-03 18:45:45 +00002088 TheCU->getUniqueID()));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002089 }
Bill Wendling480ff322009-05-20 23:21:38 +00002090}
2091
Manman Ren14a029d2013-03-12 18:27:15 +00002092/// For a given compile unit DIE, returns offset from beginning of debug info.
2093unsigned DwarfUnits::getCUOffset(DIE *Die) {
Manman Ren11fec382013-03-13 18:41:27 +00002094 assert(Die->getTag() == dwarf::DW_TAG_compile_unit &&
2095 "Input DIE should be compile unit in getCUOffset.");
Eric Christopherd25f7fc2013-08-08 01:41:05 +00002096 for (SmallVectorImpl<CompileUnit *>::iterator I = CUs.begin(), E = CUs.end();
2097 I != E; ++I) {
Manman Ren14a029d2013-03-12 18:27:15 +00002098 CompileUnit *TheCU = *I;
2099 if (TheCU->getCUDie() == Die)
2100 return TheCU->getDebugInfoOffset();
2101 }
Manman Ren11fec382013-03-13 18:41:27 +00002102 llvm_unreachable("The compile unit DIE should belong to CUs in DwarfUnits.");
Manman Ren14a029d2013-03-12 18:27:15 +00002103}
2104
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002105// Emit the debug info section.
2106void DwarfDebug::emitDebugInfo() {
Eric Christophera2de8262012-12-15 00:04:07 +00002107 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2108
2109 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
2110 Asm->getObjFileLowering().getDwarfAbbrevSection(),
2111 DwarfAbbrevSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002112}
2113
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002114// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00002115void DwarfDebug::emitAbbreviations() {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002116 if (!useSplitDwarf())
2117 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
2118 &Abbreviations);
2119 else
2120 emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
2121}
Bill Wendling480ff322009-05-20 23:21:38 +00002122
Eric Christopher3c5a1912012-12-19 22:02:53 +00002123void DwarfDebug::emitAbbrevs(const MCSection *Section,
2124 std::vector<DIEAbbrev *> *Abbrevs) {
2125 // Check to see if it is worth the effort.
2126 if (!Abbrevs->empty()) {
2127 // Start the debug abbrev section.
2128 Asm->OutStreamer.SwitchSection(Section);
2129
2130 MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Eric Christopher996b2b72012-12-13 03:00:38 +00002131 Asm->OutStreamer.EmitLabel(Begin);
Bill Wendling480ff322009-05-20 23:21:38 +00002132
2133 // For each abbrevation.
Eric Christopher3c5a1912012-12-19 22:02:53 +00002134 for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
Bill Wendling480ff322009-05-20 23:21:38 +00002135 // Get abbreviation data
Eric Christopher3c5a1912012-12-19 22:02:53 +00002136 const DIEAbbrev *Abbrev = Abbrevs->at(i);
Bill Wendling480ff322009-05-20 23:21:38 +00002137
2138 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00002139 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002140
2141 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002142 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00002143 }
2144
2145 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00002146 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002147
Eric Christopher3c5a1912012-12-19 22:02:53 +00002148 MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Eric Christopher996b2b72012-12-13 03:00:38 +00002149 Asm->OutStreamer.EmitLabel(End);
Bill Wendling480ff322009-05-20 23:21:38 +00002150 }
2151}
2152
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002153// Emit the last address of the section and the end of the line matrix.
Devang Patel930143b2009-11-21 02:48:08 +00002154void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002155 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002156 Asm->OutStreamer.AddComment("Extended Op");
2157 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002158
Chris Lattner566cae92010-03-09 23:52:58 +00002159 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002160 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002161 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2162 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2163
2164 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002165
Chris Lattnera179b522010-04-04 19:25:43 +00002166 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Eric Christopherce0cfce2013-01-09 01:35:34 +00002167 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002168
2169 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002170 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2171 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002172 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002173 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002174}
2175
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002176// Emit visible names into a hashed accelerator table section.
Eric Christopher4996c702011-11-07 09:24:32 +00002177void DwarfDebug::emitAccelNames() {
2178 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2179 dwarf::DW_FORM_data4));
2180 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2181 E = CUMap.end(); I != E; ++I) {
2182 CompileUnit *TheCU = I->second;
Eric Christopherd9843b32011-11-10 19:25:34 +00002183 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
2184 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00002185 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002186 StringRef Name = GI->getKey();
Eric Christopher090fcc12012-01-06 23:03:34 +00002187 const std::vector<DIE *> &Entities = GI->second;
Eric Christopherd9843b32011-11-10 19:25:34 +00002188 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2189 DE = Entities.end(); DI != DE; ++DI)
2190 AT.AddName(Name, (*DI));
Eric Christopher4996c702011-11-07 09:24:32 +00002191 }
2192 }
2193
2194 AT.FinalizeTable(Asm, "Names");
2195 Asm->OutStreamer.SwitchSection(
2196 Asm->getObjFileLowering().getDwarfAccelNamesSection());
2197 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
2198 Asm->OutStreamer.EmitLabel(SectionBegin);
2199
2200 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002201 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002202}
2203
Eric Christopher48fef592012-12-20 21:58:40 +00002204// Emit objective C classes and categories into a hashed accelerator table
2205// section.
Eric Christopher4996c702011-11-07 09:24:32 +00002206void DwarfDebug::emitAccelObjC() {
2207 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2208 dwarf::DW_FORM_data4));
2209 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2210 E = CUMap.end(); I != E; ++I) {
2211 CompileUnit *TheCU = I->second;
2212 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
2213 for (StringMap<std::vector<DIE*> >::const_iterator
2214 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002215 StringRef Name = GI->getKey();
Eric Christopher090fcc12012-01-06 23:03:34 +00002216 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher4996c702011-11-07 09:24:32 +00002217 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2218 DE = Entities.end(); DI != DE; ++DI)
2219 AT.AddName(Name, (*DI));
2220 }
2221 }
2222
2223 AT.FinalizeTable(Asm, "ObjC");
2224 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2225 .getDwarfAccelObjCSection());
2226 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2227 Asm->OutStreamer.EmitLabel(SectionBegin);
2228
2229 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002230 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002231}
2232
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002233// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002234void DwarfDebug::emitAccelNamespaces() {
2235 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2236 dwarf::DW_FORM_data4));
2237 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2238 E = CUMap.end(); I != E; ++I) {
2239 CompileUnit *TheCU = I->second;
Eric Christopher66b37db2011-11-10 21:47:55 +00002240 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
2241 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00002242 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002243 StringRef Name = GI->getKey();
Eric Christopher090fcc12012-01-06 23:03:34 +00002244 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher66b37db2011-11-10 21:47:55 +00002245 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2246 DE = Entities.end(); DI != DE; ++DI)
2247 AT.AddName(Name, (*DI));
Eric Christopher4996c702011-11-07 09:24:32 +00002248 }
2249 }
2250
2251 AT.FinalizeTable(Asm, "namespac");
2252 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2253 .getDwarfAccelNamespaceSection());
2254 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2255 Asm->OutStreamer.EmitLabel(SectionBegin);
2256
2257 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002258 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002259}
2260
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002261// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002262void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00002263 std::vector<DwarfAccelTable::Atom> Atoms;
2264 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
2265 dwarf::DW_FORM_data4));
2266 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
2267 dwarf::DW_FORM_data2));
2268 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
2269 dwarf::DW_FORM_data1));
2270 DwarfAccelTable AT(Atoms);
Eric Christopher4996c702011-11-07 09:24:32 +00002271 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2272 E = CUMap.end(); I != E; ++I) {
2273 CompileUnit *TheCU = I->second;
Eric Christopher21bde872012-01-06 04:35:23 +00002274 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
2275 = TheCU->getAccelTypes();
2276 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00002277 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002278 StringRef Name = GI->getKey();
Eric Christopher090fcc12012-01-06 23:03:34 +00002279 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopher21bde872012-01-06 04:35:23 +00002280 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
2281 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
2282 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher4996c702011-11-07 09:24:32 +00002283 }
2284 }
2285
2286 AT.FinalizeTable(Asm, "types");
2287 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2288 .getDwarfAccelTypesSection());
2289 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2290 Asm->OutStreamer.EmitLabel(SectionBegin);
2291
2292 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002293 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002294}
2295
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002296/// emitDebugPubnames - Emit visible names into a debug pubnames section.
2297///
2298void DwarfDebug::emitDebugPubnames() {
2299 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2300
2301 typedef DenseMap<const MDNode*, CompileUnit*> CUMapType;
2302 for (CUMapType::iterator I = CUMap.begin(), E = CUMap.end(); I != E; ++I) {
2303 CompileUnit *TheCU = I->second;
2304 unsigned ID = TheCU->getUniqueID();
2305
2306 if (TheCU->getGlobalNames().empty())
2307 continue;
2308
2309 // Start the dwarf pubnames section.
2310 Asm->OutStreamer.SwitchSection(
2311 Asm->getObjFileLowering().getDwarfPubNamesSection());
2312
2313 Asm->OutStreamer.AddComment("Length of Public Names Info");
2314 Asm->EmitLabelDifference(Asm->GetTempSymbol("pubnames_end", ID),
2315 Asm->GetTempSymbol("pubnames_begin", ID), 4);
2316
2317 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin", ID));
2318
2319 Asm->OutStreamer.AddComment("DWARF Version");
Manman Renac8062b2013-07-02 23:40:10 +00002320 Asm->EmitInt16(DwarfVersion);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002321
2322 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2323 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2324 DwarfInfoSectionSym);
2325
2326 Asm->OutStreamer.AddComment("Compilation Unit Length");
2327 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(), ID),
2328 Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2329 4);
2330
2331 const StringMap<DIE*> &Globals = TheCU->getGlobalNames();
2332 for (StringMap<DIE*>::const_iterator
2333 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2334 const char *Name = GI->getKeyData();
2335 const DIE *Entity = GI->second;
2336
2337 Asm->OutStreamer.AddComment("DIE offset");
2338 Asm->EmitInt32(Entity->getOffset());
2339
2340 if (Asm->isVerbose())
2341 Asm->OutStreamer.AddComment("External Name");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002342 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1));
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002343 }
2344
2345 Asm->OutStreamer.AddComment("End Mark");
2346 Asm->EmitInt32(0);
2347 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end", ID));
2348 }
2349}
2350
Devang Patel04d2f2d2009-11-24 01:14:22 +00002351void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002352 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2353 E = CUMap.end(); I != E; ++I) {
2354 CompileUnit *TheCU = I->second;
Eric Christopher6abc9c52011-11-07 09:18:35 +00002355 // Start the dwarf pubtypes section.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002356 Asm->OutStreamer.SwitchSection(
2357 Asm->getObjFileLowering().getDwarfPubTypesSection());
2358 Asm->OutStreamer.AddComment("Length of Public Types Info");
2359 Asm->EmitLabelDifference(
Eli Benderskyb42d1462012-12-03 18:45:45 +00002360 Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()),
2361 Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002362
Devang Patel1a0df9a2010-05-10 22:49:55 +00002363 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
Eli Benderskyb42d1462012-12-03 18:45:45 +00002364 TheCU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002365
Devang Patel1a0df9a2010-05-10 22:49:55 +00002366 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
Manman Renac8062b2013-07-02 23:40:10 +00002367 Asm->EmitInt16(DwarfVersion);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002368
Devang Patel1a0df9a2010-05-10 22:49:55 +00002369 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Eric Christopher16485a52012-12-15 00:04:04 +00002370 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2371 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyb42d1462012-12-03 18:45:45 +00002372 TheCU->getUniqueID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00002373 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002374
Devang Patel1a0df9a2010-05-10 22:49:55 +00002375 Asm->OutStreamer.AddComment("Compilation Unit Length");
Eric Christopher16485a52012-12-15 00:04:04 +00002376 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(),
Eli Benderskyb42d1462012-12-03 18:45:45 +00002377 TheCU->getUniqueID()),
Eric Christopher16485a52012-12-15 00:04:04 +00002378 Asm->GetTempSymbol(ISec->getLabelBeginName(),
Eli Benderskyb42d1462012-12-03 18:45:45 +00002379 TheCU->getUniqueID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00002380 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002381
Devang Patel1a0df9a2010-05-10 22:49:55 +00002382 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2383 for (StringMap<DIE*>::const_iterator
2384 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2385 const char *Name = GI->getKeyData();
Nick Lewycky019d2552011-07-29 03:49:23 +00002386 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002387
Devang Patel1a0df9a2010-05-10 22:49:55 +00002388 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2389 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002390
Devang Patel1a0df9a2010-05-10 22:49:55 +00002391 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002392 // Emit the name with a terminating null byte.
Eric Christophere3ab3d02013-01-09 01:57:54 +00002393 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002394 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002395
Devang Patel1a0df9a2010-05-10 22:49:55 +00002396 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002397 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00002398 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
Eli Benderskyb42d1462012-12-03 18:45:45 +00002399 TheCU->getUniqueID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00002400 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002401}
2402
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002403// Emit strings into a string section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002404void DwarfUnits::emitStrings(const MCSection *StrSection,
2405 const MCSection *OffsetSection = NULL,
2406 const MCSymbol *StrSecSym = NULL) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002407
Eric Christopher27614582013-01-08 22:22:06 +00002408 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002409
Chris Lattner3d72a672010-03-09 23:38:23 +00002410 // Start the dwarf str section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002411 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling480ff322009-05-20 23:21:38 +00002412
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002413 // Get all of the string pool entries and put them in an array by their ID so
2414 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002415 SmallVector<std::pair<unsigned,
Eric Christopherb800ff72013-01-07 22:40:45 +00002416 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002417
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002418 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
Eric Christopher27614582013-01-08 22:22:06 +00002419 I = StringPool.begin(), E = StringPool.end();
Eric Christopher48fef592012-12-20 21:58:40 +00002420 I != E; ++I)
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002421 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002422
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002423 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002424
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002425 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002426 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002427 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002428
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002429 // Emit the string itself with a terminating null byte.
Benjamin Kramer148db362011-11-09 12:12:04 +00002430 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
Eric Christopherce0cfce2013-01-09 01:35:34 +00002431 Entries[i].second->getKeyLength()+1));
Bill Wendling480ff322009-05-20 23:21:38 +00002432 }
Eric Christopher2cbd5762013-01-07 19:32:41 +00002433
2434 // If we've got an offset section go ahead and emit that now as well.
2435 if (OffsetSection) {
2436 Asm->OutStreamer.SwitchSection(OffsetSection);
2437 unsigned offset = 0;
Eric Christopher962c9082013-01-15 23:56:56 +00002438 unsigned size = 4; // FIXME: DWARF64 is 8.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002439 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Eric Christopherbf7bc492013-01-09 03:52:05 +00002440 Asm->OutStreamer.EmitIntValue(offset, size);
Eric Christopher2cbd5762013-01-07 19:32:41 +00002441 offset += Entries[i].second->getKeyLength() + 1;
2442 }
2443 }
Bill Wendling480ff322009-05-20 23:21:38 +00002444}
2445
Eric Christopher962c9082013-01-15 23:56:56 +00002446// Emit strings into a string section.
2447void DwarfUnits::emitAddresses(const MCSection *AddrSection) {
2448
2449 if (AddressPool.empty()) return;
2450
2451 // Start the dwarf addr section.
2452 Asm->OutStreamer.SwitchSection(AddrSection);
2453
David Blaikiece1960f2013-07-08 17:51:28 +00002454 // Order the address pool entries by ID
David Blaikieac569a62013-07-08 17:33:10 +00002455 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
Eric Christopher962c9082013-01-15 23:56:56 +00002456
David Blaikiece1960f2013-07-08 17:51:28 +00002457 for (DenseMap<const MCExpr *, unsigned>::iterator I = AddressPool.begin(),
2458 E = AddressPool.end();
Eric Christopher962c9082013-01-15 23:56:56 +00002459 I != E; ++I)
David Blaikieac569a62013-07-08 17:33:10 +00002460 Entries[I->second] = I->first;
Eric Christopher962c9082013-01-15 23:56:56 +00002461
2462 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Ulrich Weigand8b3d2262013-07-02 18:46:46 +00002463 // Emit an expression for reference from debug information entries.
David Blaikieac569a62013-07-08 17:33:10 +00002464 if (const MCExpr *Expr = Entries[i])
Ulrich Weigand8b3d2262013-07-02 18:46:46 +00002465 Asm->OutStreamer.EmitValue(Expr, Asm->getDataLayout().getPointerSize());
Eric Christopher962c9082013-01-15 23:56:56 +00002466 else
2467 Asm->OutStreamer.EmitIntValue(0, Asm->getDataLayout().getPointerSize());
2468 }
2469
2470}
2471
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002472// Emit visible names into a debug str section.
2473void DwarfDebug::emitDebugStr() {
2474 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2475 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2476}
2477
Eric Christopher9046f942013-07-02 21:36:07 +00002478// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002479void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002480 if (DotDebugLocEntries.empty())
2481 return;
2482
Eric Christopher4887c8f2013-03-29 23:34:06 +00002483 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Devang Patel116a9d72011-02-04 22:57:18 +00002484 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2485 I != E; ++I) {
2486 DotDebugLocEntry &Entry = *I;
2487 if (I + 1 != DotDebugLocEntries.end())
2488 Entry.Merge(I+1);
2489 }
2490
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002491 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002492 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00002493 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002494 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002495 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2496 unsigned index = 1;
Eric Christopher4887c8f2013-03-29 23:34:06 +00002497 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002498 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002499 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002500 DotDebugLocEntry &Entry = *I;
2501 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002502 if (Entry.isEmpty()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +00002503 Asm->OutStreamer.EmitIntValue(0, Size);
2504 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002505 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002506 } else {
Eric Christopher25f06422013-07-03 22:40:18 +00002507 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
2508 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
2509 DIVariable DV(Entry.getVariable());
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002510 Asm->OutStreamer.AddComment("Loc expr size");
2511 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2512 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2513 Asm->EmitLabelDifference(end, begin, 2);
2514 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002515 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002516 DIBasicType BTy(DV.getType());
2517 if (BTy.Verify() &&
Eric Christopher6a841382012-11-19 22:42:10 +00002518 (BTy.getEncoding() == dwarf::DW_ATE_signed
Devang Patel324f8432011-06-01 22:03:25 +00002519 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2520 Asm->OutStreamer.AddComment("DW_OP_consts");
2521 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002522 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002523 } else {
2524 Asm->OutStreamer.AddComment("DW_OP_constu");
2525 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002526 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002527 }
Devang Pateled9fd452011-07-08 16:49:43 +00002528 } else if (Entry.isLocation()) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002529 MachineLocation Loc = Entry.getLoc();
Eric Christopher6a841382012-11-19 22:42:10 +00002530 if (!DV.hasComplexAddress())
Devang Pateled9fd452011-07-08 16:49:43 +00002531 // Regular entry.
Eric Christopher614a89f2013-07-03 22:40:21 +00002532 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002533 else {
2534 // Complex address entry.
2535 unsigned N = DV.getNumAddrElements();
2536 unsigned i = 0;
2537 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002538 if (Loc.getOffset()) {
Devang Pateled9fd452011-07-08 16:49:43 +00002539 i = 2;
Eric Christopher614a89f2013-07-03 22:40:21 +00002540 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002541 Asm->OutStreamer.AddComment("DW_OP_deref");
2542 Asm->EmitInt8(dwarf::DW_OP_deref);
2543 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2544 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2545 Asm->EmitSLEB128(DV.getAddrElement(1));
2546 } else {
2547 // If first address element is OpPlus then emit
2548 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher614a89f2013-07-03 22:40:21 +00002549 MachineLocation TLoc(Loc.getReg(), DV.getAddrElement(1));
2550 Asm->EmitDwarfRegOp(TLoc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002551 i = 2;
2552 }
2553 } else {
Eric Christopher614a89f2013-07-03 22:40:21 +00002554 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002555 }
Eric Christopher6a841382012-11-19 22:42:10 +00002556
Devang Pateled9fd452011-07-08 16:49:43 +00002557 // Emit remaining complex address elements.
2558 for (; i < N; ++i) {
2559 uint64_t Element = DV.getAddrElement(i);
2560 if (Element == DIBuilder::OpPlus) {
2561 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2562 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002563 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002564 if (!Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002565 Asm->EmitInt8(dwarf::DW_OP_deref);
2566 } else
2567 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002568 }
Devang Patel3e021532011-04-28 02:22:40 +00002569 }
Devang Patel3e021532011-04-28 02:22:40 +00002570 }
Devang Pateled9fd452011-07-08 16:49:43 +00002571 // else ... ignore constant fp. There is not any good way to
2572 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002573 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002574 }
2575 }
Bill Wendling480ff322009-05-20 23:21:38 +00002576}
2577
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002578// Emit visible names into a debug aranges section.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002579void DwarfDebug::emitDebugARanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002580 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002581 Asm->OutStreamer.SwitchSection(
2582 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002583}
2584
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002585// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002586void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002587 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002588 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00002589 Asm->getObjFileLowering().getDwarfRangesSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002590 unsigned char Size = Asm->getDataLayout().getPointerSize();
Eric Christopher4887c8f2013-03-29 23:34:06 +00002591 for (SmallVectorImpl<const MCSymbol *>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002592 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00002593 I != E; ++I) {
2594 if (*I)
Eric Christopherbf7bc492013-01-09 03:52:05 +00002595 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size);
Devang Patel12563b32010-04-16 23:33:45 +00002596 else
Eric Christopherce0cfce2013-01-09 01:35:34 +00002597 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel12563b32010-04-16 23:33:45 +00002598 }
Bill Wendling480ff322009-05-20 23:21:38 +00002599}
2600
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002601// Emit visible names into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00002602void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002603 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002604 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002605 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002606 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002607 }
2608}
2609
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002610// Emit inline info using following format.
2611// Section Header:
2612// 1. length of section
2613// 2. Dwarf version number
2614// 3. address size.
2615//
2616// Entries (one "entry" for each function that was inlined):
2617//
2618// 1. offset into __debug_str section for MIPS linkage name, if exists;
2619// otherwise offset into __debug_str for regular function name.
2620// 2. offset into __debug_str section for regular function name.
2621// 3. an unsigned LEB128 number indicating the number of distinct inlining
2622// instances for the function.
2623//
2624// The rest of the entry consists of a {die_offset, low_pc} pair for each
2625// inlined instance; the die_offset points to the inlined_subroutine die in the
2626// __debug_info section, and the low_pc is the starting address for the
2627// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002628void DwarfDebug::emitDebugInlineInfo() {
Eric Christopher1df94bf2012-03-02 02:11:47 +00002629 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002630 return;
2631
Devang Patel1a0df9a2010-05-10 22:49:55 +00002632 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002633 return;
2634
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002635 Asm->OutStreamer.SwitchSection(
2636 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002637
Chris Lattner566cae92010-03-09 23:52:58 +00002638 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002639 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2640 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002641
Chris Lattnera179b522010-04-04 19:25:43 +00002642 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002643
Chris Lattner566cae92010-03-09 23:52:58 +00002644 Asm->OutStreamer.AddComment("Dwarf Version");
Manman Renac8062b2013-07-02 23:40:10 +00002645 Asm->EmitInt16(DwarfVersion);
Chris Lattner566cae92010-03-09 23:52:58 +00002646 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002647 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002648
Eric Christopher4887c8f2013-03-29 23:34:06 +00002649 for (SmallVectorImpl<const MDNode *>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002650 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002651
Devang Patel32cc43c2010-05-07 20:54:48 +00002652 const MDNode *Node = *I;
Craig Topper2b4a2012013-07-03 04:40:27 +00002653 InlineInfoMap::iterator II = InlineInfo.find(Node);
Eric Christopher4887c8f2013-03-29 23:34:06 +00002654 SmallVectorImpl<InlineInfoLabels> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002655 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002656 StringRef LName = SP.getLinkageName();
2657 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002658
Chris Lattner566cae92010-03-09 23:52:58 +00002659 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher77725312012-03-02 01:57:52 +00002660 if (LName.empty())
Eric Christophere698f532012-12-20 21:58:36 +00002661 Asm->EmitSectionOffset(InfoHolder.getStringPoolEntry(Name),
2662 DwarfStrSectionSym);
Eric Christopher77725312012-03-02 01:57:52 +00002663 else
Benjamin Kramer7c275642013-06-01 17:51:14 +00002664 Asm->EmitSectionOffset(
2665 InfoHolder.getStringPoolEntry(Function::getRealLinkageName(LName)),
2666 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002667
Chris Lattner566cae92010-03-09 23:52:58 +00002668 Asm->OutStreamer.AddComment("Function name");
Eric Christophere698f532012-12-20 21:58:36 +00002669 Asm->EmitSectionOffset(InfoHolder.getStringPoolEntry(Name),
2670 DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00002671 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002672
Eric Christopher4887c8f2013-03-29 23:34:06 +00002673 for (SmallVectorImpl<InlineInfoLabels>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002674 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002675 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002676 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002677
Chris Lattner7bde8c02010-04-04 18:52:31 +00002678 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002679 Asm->OutStreamer.EmitSymbolValue(LI->first,
Eric Christopherbf7bc492013-01-09 03:52:05 +00002680 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002681 }
2682 }
2683
Chris Lattnera179b522010-04-04 19:25:43 +00002684 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002685}
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002686
Eric Christopherd692c1d2012-12-11 19:42:09 +00002687// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002688
2689// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2690// DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
2691// DW_AT_ranges_base, DW_AT_addr_base. If DW_AT_ranges is present,
2692// DW_AT_low_pc and DW_AT_high_pc are not used, and vice versa.
Eric Christophercdf218d2012-12-10 19:51:21 +00002693CompileUnit *DwarfDebug::constructSkeletonCU(const MDNode *N) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002694 DICompileUnit DIUnit(N);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002695 CompilationDir = DIUnit.getDirectory();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002696
2697 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eli Benderskyb42d1462012-12-03 18:45:45 +00002698 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++,
Eric Christopherc57baee2013-05-08 00:58:51 +00002699 DIUnit.getLanguage(), Die, N, Asm,
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002700 this, &SkeletonHolder);
Eric Christopher4c7765f2013-01-17 03:00:04 +00002701
Eric Christopherdae389b2013-02-22 23:50:08 +00002702 NewCU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
2703 DIUnit.getSplitDebugFilename());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002704
Eric Christopher44c6aa62013-04-22 07:51:08 +00002705 // Relocate to the beginning of the addr_base section, else 0 for the
2706 // beginning of the one for this compile unit.
Eric Christopher55863be2013-04-07 03:43:09 +00002707 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2708 NewCU->addLabel(Die, dwarf::DW_AT_GNU_addr_base, dwarf::DW_FORM_sec_offset,
2709 DwarfAddrSectionSym);
2710 else
Eric Christopher44c6aa62013-04-22 07:51:08 +00002711 NewCU->addUInt(Die, dwarf::DW_AT_GNU_addr_base,
2712 dwarf::DW_FORM_sec_offset, 0);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002713
2714 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher962c9082013-01-15 23:56:56 +00002715 // into an entity. We're using 0, or a NULL label for this.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002716 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Eric Christopher962c9082013-01-15 23:56:56 +00002717
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002718 // DW_AT_stmt_list is a offset of line number information for this
2719 // compile unit in debug_line section.
Eric Christopher55863be2013-04-07 03:43:09 +00002720 // FIXME: Should handle multiple compile units.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002721 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher4c7765f2013-01-17 03:00:04 +00002722 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_sec_offset,
Eric Christopher74804332013-02-07 21:19:50 +00002723 DwarfLineSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002724 else
Eric Christopher4c7765f2013-01-17 03:00:04 +00002725 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_sec_offset, 0);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002726
2727 if (!CompilationDir.empty())
Eric Christopher2cbd5762013-01-07 19:32:41 +00002728 NewCU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002729
Eric Christopherc8a310e2012-12-10 23:34:43 +00002730 SkeletonHolder.addUnit(NewCU);
Eric Christopher5b33b3c2013-02-06 21:53:56 +00002731 SkeletonCUs.push_back(NewCU);
Eric Christopherc8a310e2012-12-10 23:34:43 +00002732
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002733 return NewCU;
2734}
2735
Eric Christopher3c5a1912012-12-19 22:02:53 +00002736void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
2737 assert(useSplitDwarf() && "No split dwarf debug info?");
2738 emitAbbrevs(Section, &SkeletonAbbrevs);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002739}
2740
Eric Christopherd692c1d2012-12-11 19:42:09 +00002741// Emit the .debug_info.dwo section for separated dwarf. This contains the
2742// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002743void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00002744 assert(useSplitDwarf() && "No split dwarf debug info?");
Eric Christophera2de8262012-12-15 00:04:07 +00002745 InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Eric Christopher3c5a1912012-12-19 22:02:53 +00002746 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2747 DwarfAbbrevDWOSectionSym);
2748}
2749
2750// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2751// abbreviations for the .debug_info.dwo section.
2752void DwarfDebug::emitDebugAbbrevDWO() {
2753 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopher48fef592012-12-20 21:58:40 +00002754 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2755 &Abbreviations);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002756}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002757
2758// Emit the .debug_str.dwo section for separated dwarf. This contains the
2759// string section and is identical in format to traditional .debug_str
2760// sections.
2761void DwarfDebug::emitDebugStrDWO() {
2762 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherb800ff72013-01-07 22:40:45 +00002763 const MCSection *OffSec = Asm->getObjFileLowering()
2764 .getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00002765 const MCSymbol *StrSym = DwarfStrSectionSym;
2766 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
2767 OffSec, StrSym);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002768}