blob: 367b52307925ecd720c13d692d184ff5bff3c111 [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattner6cde3e62010-03-09 00:39:24 +000013
Devang Patele4b27562009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000016#include "DIE.h"
Eric Christopher09ac3d82011-11-07 09:24:32 +000017#include "DwarfAccelTable.h"
Devang Patel8b9df622011-04-12 17:40:32 +000018#include "DwarfCompileUnit.h"
Bill Wendling57fbba42010-04-05 22:59:21 +000019#include "llvm/Constants.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000020#include "llvm/DebugInfo.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000021#include "llvm/DIBuilder.h"
Bill Wendling0310d762009-05-15 09:23:25 +000022#include "llvm/Module.h"
Devang Patele449d1f2011-01-20 00:02:16 +000023#include "llvm/Instructions.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000026#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000027#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000028#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000029#include "llvm/MC/MCSymbol.h"
Micah Villmow3574eca2012-10-08 16:38:25 +000030#include "llvm/DataLayout.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000031#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000033#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000035#include "llvm/Target/TargetOptions.h"
Devang Patel9a31f0f2010-10-25 20:45:32 +000036#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000037#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000038#include "llvm/ADT/StringExtras.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000039#include "llvm/ADT/Triple.h"
Devang Pateleac9c072010-04-27 19:46:33 +000040#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000041#include "llvm/Support/Debug.h"
42#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000043#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000044#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000045#include "llvm/Support/Timer.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000046#include "llvm/Support/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000047using namespace llvm;
48
Jim Grosbach1e20b962010-07-21 21:21:52 +000049static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel61409622010-07-07 20:12:52 +000050 cl::Hidden,
Devang Pateleac9c072010-04-27 19:46:33 +000051 cl::desc("Disable debug info printing"));
52
Dan Gohman281d65d2010-05-07 01:08:53 +000053static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner7a2bdde2011-04-15 05:18:47 +000054 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman281d65d2010-05-07 01:08:53 +000055 cl::init(false));
56
Eric Christopher20f47ab2012-08-23 22:36:40 +000057namespace {
58 enum DefaultOnOff {
59 Default, Enable, Disable
60 };
61}
Eric Christopher09ac3d82011-11-07 09:24:32 +000062
Eric Christopher20f47ab2012-08-23 22:36:40 +000063static cl::opt<DefaultOnOff> DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
64 cl::desc("Output prototype dwarf accelerator tables."),
65 cl::values(
66 clEnumVal(Default, "Default for platform"),
67 clEnumVal(Enable, "Enabled"),
68 clEnumVal(Disable, "Disabled"),
69 clEnumValEnd),
70 cl::init(Default));
71
72static cl::opt<DefaultOnOff> DarwinGDBCompat("darwin-gdb-compat", cl::Hidden,
Eric Christopher10cb7442012-08-23 07:10:46 +000073 cl::desc("Compatibility with Darwin gdb."),
Eric Christopher20f47ab2012-08-23 22:36:40 +000074 cl::values(
75 clEnumVal(Default, "Default for platform"),
76 clEnumVal(Enable, "Enabled"),
77 clEnumVal(Disable, "Disabled"),
78 clEnumValEnd),
79 cl::init(Default));
Eric Christopher10cb7442012-08-23 07:10:46 +000080
Bill Wendling5f017e82010-04-07 09:28:04 +000081namespace {
82 const char *DWARFGroupName = "DWARF Emission";
83 const char *DbgTimerName = "DWARF Debug Writer";
84} // end anonymous namespace
85
Bill Wendling0310d762009-05-15 09:23:25 +000086//===----------------------------------------------------------------------===//
87
88/// Configuration values for initial hash set sizes (log2).
89///
Bill Wendling0310d762009-05-15 09:23:25 +000090static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000091
92namespace llvm {
93
Nick Lewycky3bbb6f72011-07-29 03:49:23 +000094DIType DbgVariable::getType() const {
Devang Patel3cbee302011-04-12 22:53:02 +000095 DIType Ty = Var.getType();
96 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
97 // addresses instead.
98 if (Var.isBlockByrefVariable()) {
99 /* Byref variables, in Blocks, are declared by the programmer as
100 "SomeType VarName;", but the compiler creates a
101 __Block_byref_x_VarName struct, and gives the variable VarName
102 either the struct, or a pointer to the struct, as its type. This
103 is necessary for various behind-the-scenes things the compiler
104 needs to do with by-reference variables in blocks.
105
106 However, as far as the original *programmer* is concerned, the
107 variable should still have type 'SomeType', as originally declared.
108
109 The following function dives into the __Block_byref_x_VarName
110 struct to find the original type of the variable. This will be
111 passed back to the code generating the type for the Debug
112 Information Entry for the variable 'VarName'. 'VarName' will then
113 have the original type 'SomeType' in its debug information.
114
115 The original type 'SomeType' will be the type of the field named
116 'VarName' inside the __Block_byref_x_VarName struct.
117
118 NOTE: In order for this to not completely fail on the debugger
119 side, the Debug Information Entry for the variable VarName needs to
120 have a DW_AT_location that tells the debugger how to unwind through
121 the pointers and __Block_byref_x_VarName struct to find the actual
122 value of the variable. The function addBlockByrefType does this. */
123 DIType subType = Ty;
124 unsigned tag = Ty.getTag();
125
126 if (tag == dwarf::DW_TAG_pointer_type) {
127 DIDerivedType DTy = DIDerivedType(Ty);
128 subType = DTy.getTypeDerivedFrom();
129 }
130
131 DICompositeType blockStruct = DICompositeType(subType);
132 DIArray Elements = blockStruct.getTypeArray();
133
134 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
135 DIDescriptor Element = Elements.getElement(i);
136 DIDerivedType DT = DIDerivedType(Element);
137 if (getName() == DT.getName())
138 return (DT.getTypeDerivedFrom());
Devang Patel8bd11de2010-08-09 21:01:39 +0000139 }
Devang Patel8bd11de2010-08-09 21:01:39 +0000140 }
Devang Patel3cbee302011-04-12 22:53:02 +0000141 return Ty;
142}
Bill Wendling0310d762009-05-15 09:23:25 +0000143
Chris Lattnerea761862010-04-05 04:09:20 +0000144} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000145
Chris Lattner49cd6642010-04-05 05:11:15 +0000146DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000147 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000148 AbbreviationsSet(InitAbbreviationsSetSize),
Benjamin Kramerf33a79c2012-06-09 10:34:15 +0000149 SourceIdMap(DIEValueAllocator), StringPool(DIEValueAllocator),
Eric Christopher6635cad2012-08-01 18:19:01 +0000150 PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000151 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000152
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000153 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000154 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000155 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000156 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopher60777d82012-04-02 17:58:52 +0000157
Eric Christopher10cb7442012-08-23 07:10:46 +0000158 // Turn on accelerator tables and older gdb compatibility
159 // for Darwin.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000160 bool isDarwin = Triple(M->getTargetTriple()).isOSDarwin();
161 if (DarwinGDBCompat == Default) {
162 if (isDarwin)
163 isDarwinGDBCompat = true;
164 else
165 isDarwinGDBCompat = false;
166 } else
167 isDarwinGDBCompat = DarwinGDBCompat == Enable ? true : false;
Eric Christopherc1610fa2012-08-23 22:36:36 +0000168
Eric Christopher20f47ab2012-08-23 22:36:40 +0000169 if (DwarfAccelTables == Default) {
170 if (isDarwin)
171 hasDwarfAccelTables = true;
172 else
173 hasDwarfAccelTables = false;
174 } else
175 hasDwarfAccelTables = DwarfAccelTables == Enable ? true : false;
176
Dan Gohman03c3dc72010-06-18 15:56:31 +0000177 {
178 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
179 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000180 }
Bill Wendling0310d762009-05-15 09:23:25 +0000181}
182DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000183}
184
Eric Christopherd8a87522011-11-07 09:18:38 +0000185/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
186/// temporary label to it if SymbolStem is specified.
187static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
188 const char *SymbolStem = 0) {
189 Asm->OutStreamer.SwitchSection(Section);
190 if (!SymbolStem) return 0;
191
192 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
193 Asm->OutStreamer.EmitLabel(TmpSym);
194 return TmpSym;
195}
196
Nick Lewycky390c40d2011-10-27 06:44:11 +0000197MCSymbol *DwarfDebug::getStringPool() {
198 return Asm->GetTempSymbol("section_str");
199}
200
Chris Lattnerbc733f52010-03-13 02:17:42 +0000201MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
202 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
203 if (Entry.first) return Entry.first;
204
205 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000206 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000207}
208
Devang Patel2c4ceb12009-11-21 02:48:08 +0000209/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000210///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000211void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000212 // Profile the node so that we can make it unique.
213 FoldingSetNodeID ID;
214 Abbrev.Profile(ID);
215
216 // Check the set for priors.
217 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
218
219 // If it's newly added.
220 if (InSet == &Abbrev) {
221 // Add to abbreviation list.
222 Abbreviations.push_back(&Abbrev);
223
224 // Assign the vector position + 1 as its number.
225 Abbrev.setNumber(Abbreviations.size());
226 } else {
227 // Assign existing abbreviation number.
228 Abbrev.setNumber(InSet->getNumber());
229 }
230}
231
Jim Grosbach1e20b962010-07-21 21:21:52 +0000232/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000233/// printer to not emit usual symbol prefix before the symbol name is used then
234/// return linkage name after skipping this special LLVM prefix.
235static StringRef getRealLinkageName(StringRef LinkageName) {
236 char One = '\1';
237 if (LinkageName.startswith(StringRef(&One, 1)))
238 return LinkageName.substr(1);
239 return LinkageName;
240}
241
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000242static bool isObjCClass(StringRef Name) {
243 return Name.startswith("+") || Name.startswith("-");
244}
245
246static bool hasObjCCategory(StringRef Name) {
247 if (!isObjCClass(Name)) return false;
248
249 size_t pos = Name.find(')');
250 if (pos != std::string::npos) {
251 if (Name[pos+1] != ' ') return false;
252 return true;
253 }
254 return false;
255}
256
257static void getObjCClassCategory(StringRef In, StringRef &Class,
258 StringRef &Category) {
259 if (!hasObjCCategory(In)) {
260 Class = In.slice(In.find('[') + 1, In.find(' '));
261 Category = "";
262 return;
263 }
264
265 Class = In.slice(In.find('[') + 1, In.find('('));
266 Category = In.slice(In.find('[') + 1, In.find(' '));
267 return;
268}
269
270static StringRef getObjCMethodName(StringRef In) {
271 return In.slice(In.find(' ') + 1, In.find(']'));
272}
273
274// Add the various names to the Dwarf accelerator table names.
275static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
276 DIE* Die) {
277 if (!SP.isDefinition()) return;
278
279 TheCU->addAccelName(SP.getName(), Die);
280
281 // If the linkage name is different than the name, go ahead and output
282 // that as well into the name table.
283 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
284 TheCU->addAccelName(SP.getLinkageName(), Die);
285
286 // If this is an Objective-C selector name add it to the ObjC accelerator
287 // too.
288 if (isObjCClass(SP.getName())) {
289 StringRef Class, Category;
290 getObjCClassCategory(SP.getName(), Class, Category);
291 TheCU->addAccelObjC(Class, Die);
292 if (Category != "")
293 TheCU->addAccelObjC(Category, Die);
294 // Also add the base method name to the name table.
295 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
296 }
297}
298
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000299/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000300/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
301/// If there are global variables in this scope then create and insert
302/// DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000303DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
304 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000305 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000306
Chris Lattnerd38fee82010-04-05 00:13:49 +0000307 assert(SPDie && "Unable to find subprogram DIE!");
308 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000309
Bill Wendling168c1902012-11-07 05:19:04 +0000310 // If we're updating an abstract DIE, then we will be adding the children and
311 // object pointer later on. But what we don't want to do is process the
312 // concrete DIE twice.
Devang Patel8aa61472010-07-07 22:20:57 +0000313 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
Bill Wendling168c1902012-11-07 05:19:04 +0000314 // Pick up abstract subprogram DIE.
Devang Patel8aa61472010-07-07 22:20:57 +0000315 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000316 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
317 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000318 SPCU->addDie(SPDie);
Bill Wendlinga4c76932012-11-07 04:42:18 +0000319 } else {
320 DISubprogram SPDecl = SP.getFunctionDeclaration();
321 if (!SPDecl.isSubprogram()) {
322 // There is not any need to generate specification DIE for a function
323 // defined at compile unit level. If a function is defined inside another
324 // function then gdb prefers the definition at top level and but does not
325 // expect specification DIE in parent function. So avoid creating
326 // specification DIE for a function defined inside a function.
327 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
328 !SP.getContext().isFile() &&
329 !isSubprogramContext(SP.getContext())) {
330 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
331
332 // Add arguments.
333 DICompositeType SPTy = SP.getType();
334 DIArray Args = SPTy.getTypeArray();
335 unsigned SPTag = SPTy.getTag();
336 if (SPTag == dwarf::DW_TAG_subroutine_type)
337 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
338 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
339 DIType ATy = DIType(Args.getElement(i));
340 SPCU->addType(Arg, ATy);
341 if (ATy.isArtificial())
342 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
343 if (ATy.isObjectPointer())
344 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer,
345 dwarf::DW_FORM_ref4, Arg);
346 SPDie->addChild(Arg);
347 }
348 DIE *SPDeclDie = SPDie;
349 SPDie = new DIE(dwarf::DW_TAG_subprogram);
350 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
351 SPDeclDie);
352 SPCU->addDie(SPDie);
353 }
354 }
Devang Patel8aa61472010-07-07 22:20:57 +0000355 }
356
Devang Patel3cbee302011-04-12 22:53:02 +0000357 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
358 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
359 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
360 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000361 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
362 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000363 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000364
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000365 // Add name to the name table, we do this here because we're guaranteed
366 // to have concrete versions of our DW_TAG_subprogram nodes.
367 addSubprogramNames(SPCU, SP, SPDie);
368
Chris Lattnerd38fee82010-04-05 00:13:49 +0000369 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000370}
371
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000372/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000373/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000374DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
375 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000376 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
377 if (Scope->isAbstractScope())
378 return ScopeDIE;
379
Devang Patelbf47fdb2011-08-10 20:55:27 +0000380 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000381 if (Ranges.empty())
382 return 0;
383
Devang Patelbf47fdb2011-08-10 20:55:27 +0000384 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000385 if (Ranges.size() > 1) {
386 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000387 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000388 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000389 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000390 DebugRangeSymbols.size()
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000391 * Asm->getDataLayout().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000392 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000393 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000394 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
395 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000396 }
397 DebugRangeSymbols.push_back(NULL);
398 DebugRangeSymbols.push_back(NULL);
399 return ScopeDIE;
400 }
401
Devang Patelc3f5f782010-05-25 23:40:22 +0000402 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
403 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000404
Devang Patelc3f5f782010-05-25 23:40:22 +0000405 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000406
Chris Lattnerb7db7332010-03-09 01:58:53 +0000407 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
408 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000409
Devang Patel3cbee302011-04-12 22:53:02 +0000410 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
411 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000412
413 return ScopeDIE;
414}
415
Devang Patel2c4ceb12009-11-21 02:48:08 +0000416/// constructInlinedScopeDIE - This scope represents inlined body of
417/// a function. Construct DIE to represent this concrete inlined copy
418/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000419DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
420 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000421 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000422 assert(Ranges.empty() == false &&
423 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000424
Devang Patel26a92002011-07-27 00:34:13 +0000425 if (!Scope->getScopeNode())
426 return NULL;
427 DIScope DS(Scope->getScopeNode());
428 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000429 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
430 if (!OriginDIE) {
Bill Wendlinge0aae5b2012-10-30 17:51:02 +0000431 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patel26a92002011-07-27 00:34:13 +0000432 return NULL;
433 }
434
Devang Patelbf47fdb2011-08-10 20:55:27 +0000435 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000436 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
437 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000438
Devang Patel0afbf232010-07-08 22:39:20 +0000439 if (StartLabel == 0 || EndLabel == 0) {
Bill Wendlinge0aae5b2012-10-30 17:51:02 +0000440 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000441 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000442 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000443 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000444 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000445 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000446
Devang Pateld96efb82011-05-05 17:54:26 +0000447 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000448 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
449 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000450
Devang Patel26a92002011-07-27 00:34:13 +0000451 if (Ranges.size() > 1) {
452 // .debug_range section has not been laid out yet. Emit offset in
453 // .debug_range as a uint, size 4, for now. emitDIE will handle
454 // DW_AT_ranges appropriately.
455 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000456 DebugRangeSymbols.size()
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000457 * Asm->getDataLayout().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000458 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000459 RE = Ranges.end(); RI != RE; ++RI) {
460 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
461 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
462 }
463 DebugRangeSymbols.push_back(NULL);
464 DebugRangeSymbols.push_back(NULL);
465 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000466 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
467 StartLabel);
468 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
469 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000470 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000471
472 InlinedSubprogramDIEs.insert(OriginDIE);
473
474 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000475 //.debug_inlined section specification does not clearly state how
476 // to emit inlined scope that is split into multiple instruction ranges.
477 // For now, use first instruction range and emit low_pc/high_pc pair and
478 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000479 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000480 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000481
482 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000483 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000484 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000485 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000486 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000487
Devang Patel53bb5c92009-11-10 23:06:00 +0000488 DILocation DL(Scope->getInlinedAt());
Eric Christopher08212002012-03-26 21:38:38 +0000489 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
490 GetOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patel3cbee302011-04-12 22:53:02 +0000491 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000492
Eric Christopher309bedd2011-12-04 06:02:38 +0000493 // Add name to the name table, we do this here because we're guaranteed
494 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
495 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
496
Devang Patel53bb5c92009-11-10 23:06:00 +0000497 return ScopeDIE;
498}
499
Devang Patel2c4ceb12009-11-21 02:48:08 +0000500/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000501DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000502 if (!Scope || !Scope->getScopeNode())
503 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000504
Nick Lewycky746cb672011-10-26 22:55:33 +0000505 SmallVector<DIE *, 8> Children;
Eric Christophere5212782012-09-12 23:36:19 +0000506 DIE *ObjectPointer = NULL;
Devang Patel0478c152011-03-01 22:58:55 +0000507
508 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000509 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000510 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
511 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000512 if (DIE *Arg =
Eric Christophere5212782012-09-12 23:36:19 +0000513 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope())) {
Devang Patel0478c152011-03-01 22:58:55 +0000514 Children.push_back(Arg);
Eric Christophere5212782012-09-12 23:36:19 +0000515 if (ArgDV->isObjectPointer()) ObjectPointer = Arg;
516 }
Devang Patel0478c152011-03-01 22:58:55 +0000517
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000518 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000519 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000520 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000521 if (DIE *Variable =
Eric Christopher7b451cf2012-09-21 22:18:52 +0000522 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope())) {
Devang Patel5bc9fec2011-02-19 01:31:27 +0000523 Children.push_back(Variable);
Eric Christopher7b451cf2012-09-21 22:18:52 +0000524 if (Variables[i]->isObjectPointer()) ObjectPointer = Variable;
525 }
Devang Patelbf47fdb2011-08-10 20:55:27 +0000526 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000527 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000528 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000529 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000530 DIScope DS(Scope->getScopeNode());
531 DIE *ScopeDIE = NULL;
532 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000533 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000534 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000535 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000536 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000537 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000538 // Note down abstract DIE.
539 if (ScopeDIE)
540 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
541 }
Devang Patel3c91b052010-03-08 20:52:55 +0000542 else
Devang Pateld3024342011-08-15 22:24:32 +0000543 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000544 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000545 else {
546 // There is no need to emit empty lexical block DIE.
547 if (Children.empty())
548 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000549 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000550 }
551
Devang Patelaead63c2010-03-29 22:59:58 +0000552 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000553
Devang Patel5bc9fec2011-02-19 01:31:27 +0000554 // Add children
555 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
556 E = Children.end(); I != E; ++I)
557 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000558
Eric Christophere5212782012-09-12 23:36:19 +0000559 if (DS.isSubprogram() && ObjectPointer != NULL)
560 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer,
561 dwarf::DW_FORM_ref4, ObjectPointer);
562
Jim Grosbach1e20b962010-07-21 21:21:52 +0000563 if (DS.isSubprogram())
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000564 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000565
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000566 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000567}
568
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000569/// GetOrCreateSourceID - Look up the source id with the given directory and
570/// source file names. If none currently exists, create a new id and insert it
571/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
572/// maps as well.
Devang Patel23670e52011-03-24 20:30:50 +0000573unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
574 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000575 // If FE did not provide a file name, then assume stdin.
576 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000577 return GetOrCreateSourceID("<stdin>", StringRef());
578
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000579 // TODO: this might not belong here. See if we can factor this better.
580 if (DirName == CompilationDir)
581 DirName = "";
582
Nick Lewycky44d798d2011-10-17 23:05:28 +0000583 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel1905a182010-09-16 20:57:49 +0000584
Benjamin Kramer74612c22012-03-11 14:56:26 +0000585 // We look up the file/dir pair by concatenating them with a zero byte.
586 SmallString<128> NamePair;
587 NamePair += DirName;
588 NamePair += '\0'; // Zero bytes are not allowed in paths.
589 NamePair += FileName;
590
591 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
592 if (Ent.getValue() != SrcId)
593 return Ent.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000594
Rafael Espindola5c055632010-11-18 02:04:25 +0000595 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer74612c22012-03-11 14:56:26 +0000596 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000597
598 return SrcId;
599}
600
Jim Grosbach1e20b962010-07-21 21:21:52 +0000601/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000602/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000603CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000604 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000605 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000606 CompilationDir = DIUnit.getDirectory();
607 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000608
609 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopheredf8bc82012-09-10 23:34:00 +0000610 CompileUnit *NewCU = new CompileUnit(ID, DIUnit.getLanguage(), Die,
611 Asm, this);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000612 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000613 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
614 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000615 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher6635cad2012-08-01 18:19:01 +0000616 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
617 // into an entity.
618 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000619 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000620 // compile unit in debug_line section.
Rafael Espindola2241e512012-06-22 13:24:07 +0000621 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindola597a7662011-05-04 17:44:06 +0000622 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000623 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000624 else
Devang Patel3cbee302011-04-12 22:53:02 +0000625 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000626
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000627 if (!CompilationDir.empty())
628 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000629 if (DIUnit.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000630 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000631
Devang Patel65dbc902009-11-25 17:36:49 +0000632 StringRef Flags = DIUnit.getFlags();
633 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000634 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000635
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000636 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000637 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000638 dwarf::DW_FORM_data1, RVer);
639
Devang Patel163a9f72010-05-10 22:49:55 +0000640 if (!FirstCU)
641 FirstCU = NewCU;
642 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000643 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000644}
645
Devang Patel163a9f72010-05-10 22:49:55 +0000646/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000647void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
648 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000649 CompileUnit *&CURef = SPMap[N];
650 if (CURef)
651 return;
652 CURef = TheCU;
653
Devang Patele4b27562009-08-28 23:24:31 +0000654 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000655 if (!SP.isDefinition())
656 // This is a method declaration which will be handled while constructing
657 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000658 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000659
Devang Pateldbc64af2011-08-15 17:24:54 +0000660 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000661
662 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000663 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000664
665 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000666 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000667
Devang Patel13e16b62009-06-26 01:49:18 +0000668 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000669}
670
Devang Patel02e603f2011-08-15 23:47:24 +0000671/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
672/// as llvm.dbg.enum and llvm.dbg.ty
673void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000674 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
675 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
676 const MDNode *N = NMD->getOperand(i);
677 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
678 constructSubprogramDIE(CU, N);
679 }
680
681 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
682 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
683 const MDNode *N = NMD->getOperand(i);
684 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000685 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000686 }
687
Devang Patel02e603f2011-08-15 23:47:24 +0000688 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
689 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
690 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000691 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
692 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000693 }
694
695 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
696 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
697 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000698 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
699 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000700 }
701}
702
703/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
704/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
705bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
706 DebugInfoFinder DbgFinder;
707 DbgFinder.processModule(*M);
708
709 bool HasDebugInfo = false;
710 // Scan all the compile-units to see if there are any marked as the main
711 // unit. If not, we do not generate debug info.
712 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
713 E = DbgFinder.compile_unit_end(); I != E; ++I) {
714 if (DICompileUnit(*I).isMain()) {
715 HasDebugInfo = true;
716 break;
717 }
718 }
719 if (!HasDebugInfo) return false;
720
721 // Create all the compile unit DIEs.
722 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
723 E = DbgFinder.compile_unit_end(); I != E; ++I)
724 constructCompileUnit(*I);
725
726 // Create DIEs for each global variable.
727 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
728 E = DbgFinder.global_variable_end(); I != E; ++I) {
729 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000730 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000731 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000732 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000733
Devang Patel02e603f2011-08-15 23:47:24 +0000734 // Create DIEs for each subprogram.
735 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
736 E = DbgFinder.subprogram_end(); I != E; ++I) {
737 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000738 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
739 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000740 }
741
742 return HasDebugInfo;
743}
744
Devang Patel2c4ceb12009-11-21 02:48:08 +0000745/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000746/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000747/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000748void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000749 if (DisableDebugInfoPrinting)
750 return;
751
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000752 // If module has named metadata anchors then use them, otherwise scan the
753 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000754 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
755 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000756 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
757 DICompileUnit CUNode(CU_Nodes->getOperand(i));
758 CompileUnit *CU = constructCompileUnit(CUNode);
759 DIArray GVs = CUNode.getGlobalVariables();
760 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000761 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000762 DIArray SPs = CUNode.getSubprograms();
763 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
764 constructSubprogramDIE(CU, SPs.getElement(i));
765 DIArray EnumTypes = CUNode.getEnumTypes();
766 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
767 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
768 DIArray RetainedTypes = CUNode.getRetainedTypes();
769 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
770 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
771 }
Devang Patel02e603f2011-08-15 23:47:24 +0000772 } else if (!collectLegacyDebugInfo(M))
773 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000774
Devang Patel02e603f2011-08-15 23:47:24 +0000775 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000776
Chris Lattnerd850ac72010-04-05 02:19:28 +0000777 // Tell MMI that we have debug info.
778 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000779
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000780 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000781 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000782
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000783 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000784 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000785}
786
Devang Patel2c4ceb12009-11-21 02:48:08 +0000787/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000788///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000789void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000790 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000791 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000792 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000793
Devang Patel94c7ddb2011-08-16 22:09:43 +0000794 // Collect info for variables that were optimized out.
795 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
796 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
797 DICompileUnit TheCU(CU_Nodes->getOperand(i));
798 DIArray Subprograms = TheCU.getSubprograms();
799 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
800 DISubprogram SP(Subprograms.getElement(i));
801 if (ProcessedSPNodes.count(SP) != 0) continue;
802 if (!SP.Verify()) continue;
803 if (!SP.isDefinition()) continue;
Devang Patel93d39be2011-08-19 23:28:12 +0000804 DIArray Variables = SP.getVariables();
805 if (Variables.getNumElements() == 0) continue;
806
Devang Patel94c7ddb2011-08-16 22:09:43 +0000807 LexicalScope *Scope =
808 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
809 DeadFnScopeMap[SP] = Scope;
Bill Wendlinga4c76932012-11-07 04:42:18 +0000810
Devang Patel94c7ddb2011-08-16 22:09:43 +0000811 // Construct subprogram DIE and add variables DIEs.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000812 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky746cb672011-10-26 22:55:33 +0000813 assert(SPCU && "Unable to find Compile Unit!");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000814 constructSubprogramDIE(SPCU, SP);
815 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel93d39be2011-08-19 23:28:12 +0000816 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
817 DIVariable DV(Variables.getElement(vi));
818 if (!DV.Verify()) continue;
819 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000820 if (DIE *VariableDIE =
Devang Patel93d39be2011-08-19 23:28:12 +0000821 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Patel94c7ddb2011-08-16 22:09:43 +0000822 ScopeDIE->addChild(VariableDIE);
823 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000824 }
825 }
826 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000827
Devang Patel53bb5c92009-11-10 23:06:00 +0000828 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
829 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
830 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
831 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000832 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000833 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000834 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
835 AE = AbstractSPDies.end(); AI != AE; ++AI) {
836 DIE *ISP = AI->second;
837 if (InlinedSubprogramDIEs.count(ISP))
838 continue;
839 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
840 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000841
Eric Christopher6635cad2012-08-01 18:19:01 +0000842 // Emit DW_AT_containing_type attribute to connect types with their
843 // vtable holding type.
Devang Pateldbc64af2011-08-15 17:24:54 +0000844 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
845 CUE = CUMap.end(); CUI != CUE; ++CUI) {
846 CompileUnit *TheCU = CUI->second;
847 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000848 }
849
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000850 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000851 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000852 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000853 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000854 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000855
856 // End text sections.
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +0000857 for (unsigned I = 0, E = SectionMap.size(); I != E; ++I) {
858 Asm->OutStreamer.SwitchSection(SectionMap[I]);
859 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", I+1));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000860 }
861
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000862 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000863 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000864
865 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000866 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000867
868 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000869 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000870
Eric Christopher9d9f5a52012-08-23 07:32:06 +0000871 // Emit info into the dwarf accelerator table sections.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000872 if (useDwarfAccelTables()) {
Eric Christopher09ac3d82011-11-07 09:24:32 +0000873 emitAccelNames();
874 emitAccelObjC();
875 emitAccelNamespaces();
876 emitAccelTypes();
877 }
878
Devang Patel193f7202009-11-24 01:14:22 +0000879 // Emit info into a debug pubtypes section.
Eric Christopher360f0062012-08-23 07:10:56 +0000880 // TODO: When we don't need the option anymore we can
881 // remove all of the code that adds to the table.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000882 if (useDarwinGDBCompat())
Eric Christopher360f0062012-08-23 07:10:56 +0000883 emitDebugPubTypes();
Devang Patel193f7202009-11-24 01:14:22 +0000884
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000885 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000886 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000887
888 // Emit info into a debug aranges section.
889 EmitDebugARanges();
890
891 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000892 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000893
894 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000895 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000896
897 // Emit inline info.
Eric Christopher9eb1a942012-08-23 07:32:02 +0000898 // TODO: When we don't need the option anymore we
899 // can remove all of the code that this section
900 // depends upon.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000901 if (useDarwinGDBCompat())
Eric Christopher9eb1a942012-08-23 07:32:02 +0000902 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000903
Eric Christopher7550f7d2012-03-02 00:30:24 +0000904 // Emit info into a debug str section.
905 emitDebugStr();
906
Devang Patele9a1cca2010-08-02 17:32:15 +0000907 // clean up.
908 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000909 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000910 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
911 E = CUMap.end(); I != E; ++I)
912 delete I->second;
913 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000914}
915
Devang Patel53bb5c92009-11-10 23:06:00 +0000916/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000917DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000918 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000919 LLVMContext &Ctx = DV->getContext();
920 // More then one inlined variable corresponds to one abstract variable.
921 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000922 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000923 if (AbsDbgVariable)
924 return AbsDbgVariable;
925
Devang Patelbf47fdb2011-08-10 20:55:27 +0000926 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000927 if (!Scope)
928 return NULL;
929
Devang Patel5a1a67c2011-08-15 19:01:20 +0000930 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000931 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000932 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000933 return AbsDbgVariable;
934}
935
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000936/// addCurrentFnArgument - If Var is a current function argument then add
937/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000938bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000939 DbgVariable *Var, LexicalScope *Scope) {
940 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000941 return false;
942 DIVariable DV = Var->getVariable();
943 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
944 return false;
945 unsigned ArgNo = DV.getArgNumber();
946 if (ArgNo == 0)
947 return false;
948
Devang Patelcb3a6572011-03-03 20:02:02 +0000949 size_t Size = CurrentFnArguments.size();
950 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000951 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000952 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000953 // arguments does the function have at source level.
954 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000955 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000956 CurrentFnArguments[ArgNo - 1] = Var;
957 return true;
958}
959
Devang Patelee432862010-05-20 19:57:06 +0000960/// collectVariableInfoFromMMITable - Collect variable information from
961/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000962void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000963DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000964 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000965 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
966 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
967 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000968 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000969 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000970 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000971 DIVariable DV(Var);
972 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000973
Devang Patelbf47fdb2011-08-10 20:55:27 +0000974 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000975
Devang Patelfb0ee432009-11-10 23:20:04 +0000976 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000977 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000978 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000979
Devang Patel26c1e562010-05-20 16:36:41 +0000980 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000981 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000982 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000983 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000984 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000985 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000986 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000987 }
Devang Patelee432862010-05-20 19:57:06 +0000988}
Devang Patel90a48ad2010-03-15 18:33:46 +0000989
Jim Grosbach1e20b962010-07-21 21:21:52 +0000990/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000991/// DBG_VALUE instruction, is in a defined reg.
992static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000993 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000994 return MI->getNumOperands() == 3 &&
995 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
996 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000997}
998
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000999/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +00001000/// at MI.
1001static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1002 const MCSymbol *FLabel,
1003 const MCSymbol *SLabel,
1004 const MachineInstr *MI) {
1005 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1006
1007 if (MI->getNumOperands() != 3) {
1008 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1009 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1010 }
1011 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1012 MachineLocation MLoc;
1013 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1014 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1015 }
1016 if (MI->getOperand(0).isImm())
1017 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1018 if (MI->getOperand(0).isFPImm())
1019 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1020 if (MI->getOperand(0).isCImm())
1021 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1022
Craig Topper5e25ee82012-02-05 08:31:47 +00001023 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +00001024}
1025
Devang Patelbf47fdb2011-08-10 20:55:27 +00001026/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001027void
Devang Patel78e127d2010-06-25 22:07:34 +00001028DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1029 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001030
Devang Patelee432862010-05-20 19:57:06 +00001031 /// collection info from MMI table.
1032 collectVariableInfoFromMMITable(MF, Processed);
1033
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001034 for (SmallVectorImpl<const MDNode*>::const_iterator
1035 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1036 ++UVI) {
1037 const MDNode *Var = *UVI;
1038 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001039 continue;
1040
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001041 // History contains relevant DBG_VALUE instructions for Var and instructions
1042 // clobbering it.
1043 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1044 if (History.empty())
1045 continue;
1046 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001047
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001048 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001049 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001050 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1051 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001052 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001053 else {
1054 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001055 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001056 else {
1057 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001058 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001059 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001060 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001061 }
1062 }
Devang Patelee432862010-05-20 19:57:06 +00001063 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001064 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001065 continue;
1066
1067 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001068 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001069 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1070 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001071 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001072 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001073 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001074 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001075
Eric Christopherc56e3f02012-10-08 20:48:54 +00001076 // Simplify ranges that are fully coalesced.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001077 if (History.size() <= 1 || (History.size() == 2 &&
1078 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001079 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001080 continue;
1081 }
1082
1083 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001084 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001085
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001086 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1087 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1088 const MachineInstr *Begin = *HI;
1089 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001090
Devang Patel4ada1d72011-06-01 23:00:17 +00001091 // Check if DBG_VALUE is truncating a range.
1092 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1093 && !Begin->getOperand(0).getReg())
1094 continue;
1095
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001096 // Compute the range for a register location.
1097 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1098 const MCSymbol *SLabel = 0;
1099
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001100 if (HI + 1 == HE)
1101 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001102 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001103 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001104 else {
1105 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001106 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1107 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001108 if (End->isDebugValue())
1109 SLabel = getLabelBeforeInsn(End);
1110 else {
1111 // End is a normal instruction clobbering the range.
1112 SLabel = getLabelAfterInsn(End);
1113 assert(SLabel && "Forgot label after clobber instruction");
1114 ++HI;
1115 }
1116 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001117
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001118 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001119 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1120 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001121 }
1122 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001123 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001124
1125 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001126 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1127 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1128 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1129 DIVariable DV(Variables.getElement(i));
1130 if (!DV || !DV.Verify() || !Processed.insert(DV))
1131 continue;
1132 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1133 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001134 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001135}
Devang Patel98e1cac2010-05-14 21:01:35 +00001136
Devang Patelc3f5f782010-05-25 23:40:22 +00001137/// getLabelBeforeInsn - Return Label preceding the instruction.
1138const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001139 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1140 assert(Label && "Didn't insert label before instruction");
1141 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001142}
1143
1144/// getLabelAfterInsn - Return Label immediately following the instruction.
1145const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001146 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001147}
1148
Devang Patelcbbe2872010-10-26 17:49:02 +00001149/// beginInstruction - Process beginning of an instruction.
1150void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001151 // Check if source location changes, but ignore DBG_VALUE locations.
1152 if (!MI->isDebugValue()) {
1153 DebugLoc DL = MI->getDebugLoc();
1154 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001155 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001156 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001157 if (DL == PrologEndLoc) {
1158 Flags |= DWARF2_FLAG_PROLOGUE_END;
1159 PrologEndLoc = DebugLoc();
1160 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001161 if (PrologEndLoc.isUnknown())
1162 Flags |= DWARF2_FLAG_IS_STMT;
1163
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001164 if (!DL.isUnknown()) {
1165 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001166 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001167 } else
Devang Patel4243e672011-05-11 19:22:19 +00001168 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001169 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001170 }
Devang Patelaead63c2010-03-29 22:59:58 +00001171
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001172 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001173 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1174 LabelsBeforeInsn.find(MI);
1175
1176 // No label needed.
1177 if (I == LabelsBeforeInsn.end())
1178 return;
1179
1180 // Label already assigned.
1181 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001182 return;
Devang Patel553881b2010-03-29 17:20:31 +00001183
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001184 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001185 PrevLabel = MMI->getContext().CreateTempSymbol();
1186 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001187 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001188 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001189}
1190
Devang Patelcbbe2872010-10-26 17:49:02 +00001191/// endInstruction - Process end of an instruction.
1192void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001193 // Don't create a new label after DBG_VALUE instructions.
1194 // They don't generate code.
1195 if (!MI->isDebugValue())
1196 PrevLabel = 0;
1197
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001198 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1199 LabelsAfterInsn.find(MI);
1200
1201 // No label needed.
1202 if (I == LabelsAfterInsn.end())
1203 return;
1204
1205 // Label already assigned.
1206 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001207 return;
1208
1209 // We need a label after this instruction.
1210 if (!PrevLabel) {
1211 PrevLabel = MMI->getContext().CreateTempSymbol();
1212 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001213 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001214 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001215}
1216
Jim Grosbach1e20b962010-07-21 21:21:52 +00001217/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001218/// Each LexicalScope has first instruction and last instruction to mark
1219/// beginning and end of a scope respectively. Create an inverse map that list
1220/// scopes starts (and ends) with an instruction. One instruction may start (or
1221/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001222void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001223 SmallVector<LexicalScope *, 4> WorkList;
1224 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001225 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001226 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001227
Devang Patelbf47fdb2011-08-10 20:55:27 +00001228 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001229 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001230 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001231 SE = Children.end(); SI != SE; ++SI)
1232 WorkList.push_back(*SI);
1233
Devang Patel53bb5c92009-11-10 23:06:00 +00001234 if (S->isAbstractScope())
1235 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001236
Devang Patelbf47fdb2011-08-10 20:55:27 +00001237 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001238 if (Ranges.empty())
1239 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001240 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001241 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001242 assert(RI->first && "InsnRange does not have first instruction!");
1243 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001244 requestLabelBeforeInsn(RI->first);
1245 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001246 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001247 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001248}
1249
Devang Patela3f48672011-05-09 22:14:49 +00001250/// getScopeNode - Get MDNode for DebugLoc's scope.
1251static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1252 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1253 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1254 return DL.getScope(Ctx);
1255}
1256
Devang Patel4243e672011-05-11 19:22:19 +00001257/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
Eric Christopher6126a1e2012-04-03 00:43:49 +00001258/// line number info for the function.
Devang Patel4243e672011-05-11 19:22:19 +00001259static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1260 const MDNode *Scope = getScopeNode(DL, Ctx);
1261 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher6126a1e2012-04-03 00:43:49 +00001262 if (SP.Verify()) {
1263 // Check for number of operands since the compatibility is
1264 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001265 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-04-03 00:43:49 +00001266 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1267 else
1268 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1269 }
1270
Devang Patel4243e672011-05-11 19:22:19 +00001271 return DebugLoc();
1272}
1273
Devang Patel2c4ceb12009-11-21 02:48:08 +00001274/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001275/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001276void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001277 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001278 LScopes.initialize(*MF);
1279 if (LScopes.empty()) return;
1280 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001281
Devang Pateleac9c072010-04-27 19:46:33 +00001282 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1283 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001284 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001285 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001286
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001287 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1288
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001289 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001290 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1291 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1292
Devang Patelb2b31a62010-05-26 19:37:24 +00001293 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001294 I != E; ++I) {
1295 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001296 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1297 II != IE; ++II) {
1298 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001299
Devang Patelb2b31a62010-05-26 19:37:24 +00001300 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001301 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001302
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001303 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001304 const MDNode *Var =
1305 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001306
1307 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001308 if (isDbgValueInDefinedReg(MI))
1309 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1310
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001311 // Check the history of this variable.
1312 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1313 if (History.empty()) {
1314 UserVariables.push_back(Var);
1315 // The first mention of a function argument gets the FunctionBeginSym
1316 // label, so arguments are visible when breaking at function entry.
1317 DIVariable DV(Var);
1318 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1319 DISubprogram(getDISubprogram(DV.getContext()))
1320 .describes(MF->getFunction()))
1321 LabelsBeforeInsn[MI] = FunctionBeginSym;
1322 } else {
1323 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1324 const MachineInstr *Prev = History.back();
1325 if (Prev->isDebugValue()) {
1326 // Coalesce identical entries at the end of History.
1327 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001328 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher02c1a642012-10-08 20:48:49 +00001329 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Devang Patel79862892011-07-07 00:14:27 +00001330 << "\t" << *Prev
1331 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001332 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001333 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001334
1335 // Terminate old register assignments that don't reach MI;
1336 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1337 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1338 isDbgValueInDefinedReg(Prev)) {
1339 // Previous register assignment needs to terminate at the end of
1340 // its basic block.
1341 MachineBasicBlock::const_iterator LastMI =
1342 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001343 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001344 // Drop DBG_VALUE for empty range.
Eric Christopher02c1a642012-10-08 20:48:49 +00001345 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Devang Patel79862892011-07-07 00:14:27 +00001346 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001347 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001348 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001349 else {
1350 // Terminate after LastMI.
1351 History.push_back(LastMI);
1352 }
1353 }
1354 }
1355 }
1356 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001357 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001358 // Not a DBG_VALUE instruction.
1359 if (!MI->isLabel())
1360 AtBlockEntry = false;
1361
Eric Christopher0313ced2012-10-04 20:46:14 +00001362 // First known non-DBG_VALUE and non-frame setup location marks
1363 // the beginning of the function body.
1364 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1365 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel4243e672011-05-11 19:22:19 +00001366 PrologEndLoc = MI->getDebugLoc();
1367
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001368 // Check if the instruction clobbers any registers with debug vars.
1369 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1370 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1371 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1372 continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001373 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1374 AI.isValid(); ++AI) {
1375 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001376 const MDNode *Var = LiveUserVar[Reg];
1377 if (!Var)
1378 continue;
1379 // Reg is now clobbered.
1380 LiveUserVar[Reg] = 0;
1381
1382 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001383 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1384 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001385 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001386 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1387 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001388 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001389 const MachineInstr *Prev = History.back();
1390 // Sanity-check: Register assignments are terminated at the end of
1391 // their block.
1392 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1393 continue;
1394 // Is the variable still in Reg?
1395 if (!isDbgValueInDefinedReg(Prev) ||
1396 Prev->getOperand(0).getReg() != Reg)
1397 continue;
1398 // Var is clobbered. Make sure the next instruction gets a label.
1399 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001400 }
1401 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001402 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001403 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001404 }
1405
1406 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1407 I != E; ++I) {
1408 SmallVectorImpl<const MachineInstr*> &History = I->second;
1409 if (History.empty())
1410 continue;
1411
1412 // Make sure the final register assignments are terminated.
1413 const MachineInstr *Prev = History.back();
1414 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1415 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001416 MachineBasicBlock::const_iterator LastMI =
1417 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001418 if (LastMI == PrevMBB->end())
1419 // Drop DBG_VALUE for empty range.
1420 History.pop_back();
1421 else {
1422 // Terminate after LastMI.
1423 History.push_back(LastMI);
1424 }
1425 }
1426 // Request labels for the full history.
1427 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1428 const MachineInstr *MI = History[i];
1429 if (MI->isDebugValue())
1430 requestLabelBeforeInsn(MI);
1431 else
1432 requestLabelAfterInsn(MI);
1433 }
1434 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001435
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001436 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001437 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001438
1439 // Record beginning of function.
1440 if (!PrologEndLoc.isUnknown()) {
1441 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1442 MF->getFunction()->getContext());
1443 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1444 FnStartDL.getScope(MF->getFunction()->getContext()),
Eric Christopher09e47502012-09-10 23:34:06 +00001445 0);
Devang Patel4243e672011-05-11 19:22:19 +00001446 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001447}
1448
Devang Patelbf47fdb2011-08-10 20:55:27 +00001449void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1450// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1451 ScopeVariables[LS].push_back(Var);
1452// Vars.push_back(Var);
1453}
1454
Devang Patel2c4ceb12009-11-21 02:48:08 +00001455/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001456///
Chris Lattnereec791a2010-01-26 23:18:02 +00001457void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001458 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001459
Devang Patelbf47fdb2011-08-10 20:55:27 +00001460 // Define end label for subprogram.
1461 FunctionEndSym = Asm->GetTempSymbol("func_end",
1462 Asm->getFunctionNumber());
1463 // Assumes in correct section after the entry point.
1464 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1465
1466 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1467 collectVariableInfo(MF, ProcessedVars);
1468
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001469 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001470 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001471 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001472
Devang Patelbf47fdb2011-08-10 20:55:27 +00001473 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001474 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1475 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1476 LexicalScope *AScope = AList[i];
1477 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001478 if (SP.Verify()) {
1479 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001480 DIArray Variables = SP.getVariables();
1481 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1482 DIVariable DV(Variables.getElement(i));
1483 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1484 continue;
Alexey Samsonovb67bd332012-07-06 08:45:08 +00001485 // Check that DbgVariable for DV wasn't created earlier, when
1486 // findAbstractVariable() was called for inlined instance of DV.
1487 LLVMContext &Ctx = DV->getContext();
1488 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1489 if (AbstractVariables.lookup(CleanDV))
1490 continue;
Devang Patel93d39be2011-08-19 23:28:12 +00001491 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1492 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001493 }
1494 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001495 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001496 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001497 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001498
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001499 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001500
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001501 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001502 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001503
Devang Patelbf47fdb2011-08-10 20:55:27 +00001504 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1505 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001506
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001507 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001508 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1509 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1510 DeleteContainerPointers(I->second);
1511 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001512 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001513 UserVariables.clear();
1514 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001515 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001516 LabelsBeforeInsn.clear();
1517 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001518 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001519}
1520
Chris Lattnerc6087842010-03-09 04:54:43 +00001521/// recordSourceLine - Register a source line with debug info. Returns the
1522/// unique label that was emitted and which provides correspondence to
1523/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001524void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1525 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001526 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001527 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001528 unsigned Src = 1;
1529 if (S) {
1530 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001531
Dan Gohman1cc0d622010-05-05 23:41:32 +00001532 if (Scope.isCompileUnit()) {
1533 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001534 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001535 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001536 } else if (Scope.isFile()) {
1537 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001538 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001539 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001540 } else if (Scope.isSubprogram()) {
1541 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001542 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001543 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001544 } else if (Scope.isLexicalBlockFile()) {
1545 DILexicalBlockFile DBF(S);
1546 Fn = DBF.getFilename();
1547 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001548 } else if (Scope.isLexicalBlock()) {
1549 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001550 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001551 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001552 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001553 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001554
Devang Patel23670e52011-03-24 20:30:50 +00001555 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001556 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001557 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001558}
1559
Bill Wendling829e67b2009-05-20 23:22:40 +00001560//===----------------------------------------------------------------------===//
1561// Emit Methods
1562//===----------------------------------------------------------------------===//
1563
Devang Patel2c4ceb12009-11-21 02:48:08 +00001564/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001565///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001566unsigned
1567DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001568 // Get the children.
1569 const std::vector<DIE *> &Children = Die->getChildren();
1570
Bill Wendling94d04b82009-05-20 23:21:38 +00001571 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001572 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001573
1574 // Get the abbreviation for this DIE.
1575 unsigned AbbrevNumber = Die->getAbbrevNumber();
1576 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1577
1578 // Set DIE offset
1579 Die->setOffset(Offset);
1580
1581 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001582 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001583
1584 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1585 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1586
1587 // Size the DIE attribute values.
1588 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1589 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001590 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001591
1592 // Size the DIE children if any.
1593 if (!Children.empty()) {
1594 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1595 "Children flag not set");
1596
1597 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001598 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001599
1600 // End of children marker.
1601 Offset += sizeof(int8_t);
1602 }
1603
1604 Die->setSize(Offset - Die->getOffset());
1605 return Offset;
1606}
1607
Devang Patel2c4ceb12009-11-21 02:48:08 +00001608/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001609///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001610void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001611 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1612 E = CUMap.end(); I != E; ++I) {
1613 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001614 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001615 sizeof(int32_t) + // Length of Compilation Unit Info
1616 sizeof(int16_t) + // DWARF version number
1617 sizeof(int32_t) + // Offset Into Abbrev. Section
1618 sizeof(int8_t); // Pointer Size (in bytes)
1619 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001620 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001621}
1622
Chris Lattner9c69e285532010-04-04 22:59:04 +00001623/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1624/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001625void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001626 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001627
Bill Wendling94d04b82009-05-20 23:21:38 +00001628 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001629 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001630 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001631 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001632 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001633 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001634
Chris Lattner9c69e285532010-04-04 22:59:04 +00001635 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001636 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001637
Devang Patelaf608bd2010-08-24 00:06:12 +00001638 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001639 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
Chris Lattner11b8f302010-04-04 23:02:02 +00001640 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001641 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001642 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001643 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1644 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001645
Devang Patelc3f5f782010-05-25 23:40:22 +00001646 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1647 "section_debug_loc");
1648
Chris Lattner9c69e285532010-04-04 22:59:04 +00001649 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001650 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001651}
1652
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001653/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001654///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001655void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001656 // Get the abbreviation for this DIE.
1657 unsigned AbbrevNumber = Die->getAbbrevNumber();
1658 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1659
Bill Wendling94d04b82009-05-20 23:21:38 +00001660 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001661 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001662 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1663 Twine::utohexstr(Die->getOffset()) + ":0x" +
1664 Twine::utohexstr(Die->getSize()) + " " +
1665 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001666 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001667
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001668 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001669 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1670
1671 // Emit the DIE attribute values.
1672 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1673 unsigned Attr = AbbrevData[i].getAttribute();
1674 unsigned Form = AbbrevData[i].getForm();
1675 assert(Form && "Too many attributes for DIE (check abbreviation)");
1676
Chris Lattner3f53c832010-04-04 18:52:31 +00001677 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001678 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001679
Bill Wendling94d04b82009-05-20 23:21:38 +00001680 switch (Attr) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001681 case dwarf::DW_AT_abstract_origin: {
1682 DIEEntry *E = cast<DIEEntry>(Values[i]);
1683 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001684 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001685 Asm->EmitInt32(Addr);
1686 break;
1687 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001688 case dwarf::DW_AT_ranges: {
1689 // DW_AT_range Value encodes offset in debug_range section.
1690 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001691
Nick Lewyckyffccd922012-06-22 01:25:12 +00001692 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001693 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1694 V->getValue(),
1695 4);
1696 } else {
1697 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1698 V->getValue(),
1699 DwarfDebugRangeSectionSym,
1700 4);
1701 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001702 break;
1703 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001704 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-06-22 01:25:12 +00001705 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1706 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1707 Asm->EmitLabelReference(L->getValue(), 4);
1708 else
1709 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1710 } else {
Devang Patelc3f5f782010-05-25 23:40:22 +00001711 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00001712 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001713 break;
1714 }
Devang Patel2a361602010-09-29 19:08:08 +00001715 case dwarf::DW_AT_accessibility: {
1716 if (Asm->isVerbose()) {
1717 DIEInteger *V = cast<DIEInteger>(Values[i]);
1718 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1719 }
1720 Values[i]->EmitValue(Asm, Form);
1721 break;
1722 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001723 default:
1724 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001725 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001726 break;
1727 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001728 }
1729
1730 // Emit the DIE children if any.
1731 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1732 const std::vector<DIE *> &Children = Die->getChildren();
1733
1734 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001735 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001736
Chris Lattner3f53c832010-04-04 18:52:31 +00001737 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001738 Asm->OutStreamer.AddComment("End Of Children Mark");
1739 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001740 }
1741}
1742
Devang Patel8a241142009-12-09 18:24:21 +00001743/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001744///
Devang Patel8a241142009-12-09 18:24:21 +00001745void DwarfDebug::emitDebugInfo() {
1746 // Start debug info section.
1747 Asm->OutStreamer.SwitchSection(
1748 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001749 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1750 E = CUMap.end(); I != E; ++I) {
1751 CompileUnit *TheCU = I->second;
1752 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001753
Devang Patel163a9f72010-05-10 22:49:55 +00001754 // Emit the compile units header.
1755 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1756 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001757
Devang Patel163a9f72010-05-10 22:49:55 +00001758 // Emit size of content not including length itself
1759 unsigned ContentSize = Die->getSize() +
1760 sizeof(int16_t) + // DWARF version number
1761 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001762 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001763
Devang Patel163a9f72010-05-10 22:49:55 +00001764 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1765 Asm->EmitInt32(ContentSize);
1766 Asm->OutStreamer.AddComment("DWARF version number");
1767 Asm->EmitInt16(dwarf::DWARF_VERSION);
1768 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1769 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1770 DwarfAbbrevSectionSym);
1771 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001772 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001773
Devang Patel163a9f72010-05-10 22:49:55 +00001774 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001775 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1776 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001777}
1778
Devang Patel2c4ceb12009-11-21 02:48:08 +00001779/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001780///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001781void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001782 // Check to see if it is worth the effort.
1783 if (!Abbreviations.empty()) {
1784 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001785 Asm->OutStreamer.SwitchSection(
1786 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001787
Chris Lattnerc0215722010-04-04 19:25:43 +00001788 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001789
1790 // For each abbrevation.
1791 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1792 // Get abbreviation data
1793 const DIEAbbrev *Abbrev = Abbreviations[i];
1794
1795 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001796 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001797
1798 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001799 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001800 }
1801
1802 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001803 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001804
Chris Lattnerc0215722010-04-04 19:25:43 +00001805 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001806 }
1807}
1808
Devang Patel2c4ceb12009-11-21 02:48:08 +00001809/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001810/// the line matrix.
1811///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001812void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001813 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001814 Asm->OutStreamer.AddComment("Extended Op");
1815 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001816
Chris Lattner233f52b2010-03-09 23:52:58 +00001817 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001818 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001819 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1820 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1821
1822 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001823
Chris Lattnerc0215722010-04-04 19:25:43 +00001824 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001825 Asm->getDataLayout().getPointerSize(),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001826 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001827
1828 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001829 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1830 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001831 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001832 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001833}
1834
Eric Christopher09ac3d82011-11-07 09:24:32 +00001835/// emitAccelNames - Emit visible names into a hashed accelerator table
1836/// section.
1837void DwarfDebug::emitAccelNames() {
1838 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1839 dwarf::DW_FORM_data4));
1840 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1841 E = CUMap.end(); I != E; ++I) {
1842 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001843 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1844 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001845 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1846 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001847 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001848 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1849 DE = Entities.end(); DI != DE; ++DI)
1850 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001851 }
1852 }
1853
1854 AT.FinalizeTable(Asm, "Names");
1855 Asm->OutStreamer.SwitchSection(
1856 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1857 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1858 Asm->OutStreamer.EmitLabel(SectionBegin);
1859
1860 // Emit the full data.
1861 AT.Emit(Asm, SectionBegin, this);
1862}
1863
1864/// emitAccelObjC - Emit objective C classes and categories into a hashed
1865/// accelerator table section.
1866void DwarfDebug::emitAccelObjC() {
1867 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1868 dwarf::DW_FORM_data4));
1869 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1870 E = CUMap.end(); I != E; ++I) {
1871 CompileUnit *TheCU = I->second;
1872 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1873 for (StringMap<std::vector<DIE*> >::const_iterator
1874 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1875 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001876 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-11-07 09:24:32 +00001877 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1878 DE = Entities.end(); DI != DE; ++DI)
1879 AT.AddName(Name, (*DI));
1880 }
1881 }
1882
1883 AT.FinalizeTable(Asm, "ObjC");
1884 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1885 .getDwarfAccelObjCSection());
1886 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1887 Asm->OutStreamer.EmitLabel(SectionBegin);
1888
1889 // Emit the full data.
1890 AT.Emit(Asm, SectionBegin, this);
1891}
1892
1893/// emitAccelNamespace - Emit namespace dies into a hashed accelerator
1894/// table.
1895void DwarfDebug::emitAccelNamespaces() {
1896 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1897 dwarf::DW_FORM_data4));
1898 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1899 E = CUMap.end(); I != E; ++I) {
1900 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001901 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
1902 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001903 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1904 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001905 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001906 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1907 DE = Entities.end(); DI != DE; ++DI)
1908 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001909 }
1910 }
1911
1912 AT.FinalizeTable(Asm, "namespac");
1913 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1914 .getDwarfAccelNamespaceSection());
1915 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
1916 Asm->OutStreamer.EmitLabel(SectionBegin);
1917
1918 // Emit the full data.
1919 AT.Emit(Asm, SectionBegin, this);
1920}
1921
1922/// emitAccelTypes() - Emit type dies into a hashed accelerator table.
1923void DwarfDebug::emitAccelTypes() {
Eric Christopherc36145f2012-01-06 04:35:23 +00001924 std::vector<DwarfAccelTable::Atom> Atoms;
1925 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1926 dwarf::DW_FORM_data4));
1927 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
1928 dwarf::DW_FORM_data2));
1929 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
1930 dwarf::DW_FORM_data1));
1931 DwarfAccelTable AT(Atoms);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001932 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1933 E = CUMap.end(); I != E; ++I) {
1934 CompileUnit *TheCU = I->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001935 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
1936 = TheCU->getAccelTypes();
1937 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001938 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1939 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001940 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001941 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
1942 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
1943 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001944 }
1945 }
1946
1947 AT.FinalizeTable(Asm, "types");
1948 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1949 .getDwarfAccelTypesSection());
1950 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
1951 Asm->OutStreamer.EmitLabel(SectionBegin);
1952
1953 // Emit the full data.
1954 AT.Emit(Asm, SectionBegin, this);
1955}
1956
Devang Patel193f7202009-11-24 01:14:22 +00001957void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001958 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1959 E = CUMap.end(); I != E; ++I) {
1960 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00001961 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-05-10 22:49:55 +00001962 Asm->OutStreamer.SwitchSection(
1963 Asm->getObjFileLowering().getDwarfPubTypesSection());
1964 Asm->OutStreamer.AddComment("Length of Public Types Info");
1965 Asm->EmitLabelDifference(
1966 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1967 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001968
Devang Patel163a9f72010-05-10 22:49:55 +00001969 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1970 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001971
Devang Patel163a9f72010-05-10 22:49:55 +00001972 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1973 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001974
Devang Patel163a9f72010-05-10 22:49:55 +00001975 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1976 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1977 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001978
Devang Patel163a9f72010-05-10 22:49:55 +00001979 Asm->OutStreamer.AddComment("Compilation Unit Length");
1980 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1981 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1982 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001983
Devang Patel163a9f72010-05-10 22:49:55 +00001984 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1985 for (StringMap<DIE*>::const_iterator
1986 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1987 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001988 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001989
Devang Patel163a9f72010-05-10 22:49:55 +00001990 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1991 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001992
Devang Patel163a9f72010-05-10 22:49:55 +00001993 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00001994 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00001995 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1996 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001997
Devang Patel163a9f72010-05-10 22:49:55 +00001998 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001999 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00002000 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
2001 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00002002 }
Devang Patel193f7202009-11-24 01:14:22 +00002003}
2004
Devang Patel2c4ceb12009-11-21 02:48:08 +00002005/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002006///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002007void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002008 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002009 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002010
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002011 // Start the dwarf str section.
2012 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002013 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002014
Chris Lattnerbc733f52010-03-13 02:17:42 +00002015 // Get all of the string pool entries and put them in an array by their ID so
2016 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002017 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00002018 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002019
Chris Lattnerbc733f52010-03-13 02:17:42 +00002020 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2021 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2022 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002023
Chris Lattnerbc733f52010-03-13 02:17:42 +00002024 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002025
Chris Lattnerbc733f52010-03-13 02:17:42 +00002026 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002027 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002028 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002029
Benjamin Kramer983c4572011-11-09 18:16:11 +00002030 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00002031 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
2032 Entries[i].second->getKeyLength()+1),
2033 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002034 }
2035}
2036
Devang Patel2c4ceb12009-11-21 02:48:08 +00002037/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002038///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002039void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002040 if (DotDebugLocEntries.empty())
2041 return;
2042
Devang Patel6c3ea902011-02-04 22:57:18 +00002043 for (SmallVector<DotDebugLocEntry, 4>::iterator
2044 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2045 I != E; ++I) {
2046 DotDebugLocEntry &Entry = *I;
2047 if (I + 1 != DotDebugLocEntries.end())
2048 Entry.Merge(I+1);
2049 }
2050
Daniel Dunbar83320a02011-03-16 22:16:39 +00002051 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002052 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002053 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002054 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002055 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2056 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002057 for (SmallVector<DotDebugLocEntry, 4>::iterator
2058 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002059 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002060 DotDebugLocEntry &Entry = *I;
2061 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002062 if (Entry.isEmpty()) {
2063 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2064 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002065 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002066 } else {
2067 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2068 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002069 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002070 Asm->OutStreamer.AddComment("Loc expr size");
2071 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2072 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2073 Asm->EmitLabelDifference(end, begin, 2);
2074 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002075 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002076 DIBasicType BTy(DV.getType());
2077 if (BTy.Verify() &&
2078 (BTy.getEncoding() == dwarf::DW_ATE_signed
2079 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2080 Asm->OutStreamer.AddComment("DW_OP_consts");
2081 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002082 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002083 } else {
2084 Asm->OutStreamer.AddComment("DW_OP_constu");
2085 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002086 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002087 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002088 } else if (Entry.isLocation()) {
2089 if (!DV.hasComplexAddress())
2090 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002091 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002092 else {
2093 // Complex address entry.
2094 unsigned N = DV.getNumAddrElements();
2095 unsigned i = 0;
2096 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2097 if (Entry.Loc.getOffset()) {
2098 i = 2;
2099 Asm->EmitDwarfRegOp(Entry.Loc);
2100 Asm->OutStreamer.AddComment("DW_OP_deref");
2101 Asm->EmitInt8(dwarf::DW_OP_deref);
2102 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2103 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2104 Asm->EmitSLEB128(DV.getAddrElement(1));
2105 } else {
2106 // If first address element is OpPlus then emit
2107 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2108 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2109 Asm->EmitDwarfRegOp(Loc);
2110 i = 2;
2111 }
2112 } else {
2113 Asm->EmitDwarfRegOp(Entry.Loc);
2114 }
2115
2116 // Emit remaining complex address elements.
2117 for (; i < N; ++i) {
2118 uint64_t Element = DV.getAddrElement(i);
2119 if (Element == DIBuilder::OpPlus) {
2120 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2121 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher50120762012-05-08 18:56:00 +00002122 } else if (Element == DIBuilder::OpDeref) {
Eric Christophera80f2d12012-05-08 21:24:39 +00002123 if (!Entry.Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002124 Asm->EmitInt8(dwarf::DW_OP_deref);
2125 } else
2126 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002127 }
Devang Patelc26f5442011-04-28 02:22:40 +00002128 }
Devang Patelc26f5442011-04-28 02:22:40 +00002129 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002130 // else ... ignore constant fp. There is not any good way to
2131 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002132 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002133 }
2134 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002135}
2136
2137/// EmitDebugARanges - Emit visible names into a debug aranges section.
2138///
2139void DwarfDebug::EmitDebugARanges() {
2140 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002141 Asm->OutStreamer.SwitchSection(
2142 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002143}
2144
Devang Patel2c4ceb12009-11-21 02:48:08 +00002145/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002146///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002147void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002148 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002149 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002150 Asm->getObjFileLowering().getDwarfRangesSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002151 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Pateleac9c072010-04-27 19:46:33 +00002152 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002153 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002154 I != E; ++I) {
2155 if (*I)
2156 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002157 else
Devang Pateleac9c072010-04-27 19:46:33 +00002158 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002159 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002160}
2161
Devang Patel2c4ceb12009-11-21 02:48:08 +00002162/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002163///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002164void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002165 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002166 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002167 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002168 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002169 }
2170}
2171
Devang Patel2c4ceb12009-11-21 02:48:08 +00002172/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002173/// Section Header:
2174/// 1. length of section
2175/// 2. Dwarf version number
2176/// 3. address size.
2177///
2178/// Entries (one "entry" for each function that was inlined):
2179///
2180/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2181/// otherwise offset into __debug_str for regular function name.
2182/// 2. offset into __debug_str section for regular function name.
2183/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2184/// instances for the function.
2185///
2186/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2187/// inlined instance; the die_offset points to the inlined_subroutine die in the
2188/// __debug_info section, and the low_pc is the starting address for the
2189/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002190void DwarfDebug::emitDebugInlineInfo() {
Eric Christopherb83e2bb2012-03-02 02:11:47 +00002191 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002192 return;
2193
Devang Patel163a9f72010-05-10 22:49:55 +00002194 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002195 return;
2196
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002197 Asm->OutStreamer.SwitchSection(
2198 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002199
Chris Lattner233f52b2010-03-09 23:52:58 +00002200 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002201 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2202 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002203
Chris Lattnerc0215722010-04-04 19:25:43 +00002204 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002205
Chris Lattner233f52b2010-03-09 23:52:58 +00002206 Asm->OutStreamer.AddComment("Dwarf Version");
2207 Asm->EmitInt16(dwarf::DWARF_VERSION);
2208 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002209 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002210
Devang Patele9f8f5e2010-05-07 20:54:48 +00002211 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002212 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002213
Devang Patele9f8f5e2010-05-07 20:54:48 +00002214 const MDNode *Node = *I;
2215 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002216 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002217 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002218 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002219 StringRef LName = SP.getLinkageName();
2220 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002221
Chris Lattner233f52b2010-03-09 23:52:58 +00002222 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher50e26612012-03-02 01:57:52 +00002223 if (LName.empty())
2224 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2225 else
Chris Lattner6189ed12010-04-04 23:25:33 +00002226 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2227 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002228
Chris Lattner233f52b2010-03-09 23:52:58 +00002229 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002230 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002231 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002232
Devang Patel53bb5c92009-11-10 23:06:00 +00002233 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002234 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002235 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002236 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002237
Chris Lattner3f53c832010-04-04 18:52:31 +00002238 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002239 Asm->OutStreamer.EmitSymbolValue(LI->first,
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002240 Asm->getDataLayout().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002241 }
2242 }
2243
Chris Lattnerc0215722010-04-04 19:25:43 +00002244 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002245}