blob: 52b10d7ca6824f793168edeefeaa520fc99eb146 [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"
Bill Wendling0310d762009-05-15 09:23:25 +000030#include "llvm/Target/TargetData.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
Devang Patel5e06bb82011-04-22 23:10:17 +0000310 DISubprogram SPDecl = SP.getFunctionDeclaration();
Rafael Espindolab0527282011-11-04 19:00:29 +0000311 if (!SPDecl.isSubprogram()) {
Devang Patel5e06bb82011-04-22 23:10:17 +0000312 // There is not any need to generate specification DIE for a function
313 // defined at compile unit level. If a function is defined inside another
314 // function then gdb prefers the definition at top level and but does not
315 // expect specification DIE in parent function. So avoid creating
316 // specification DIE for a function defined inside a function.
317 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
318 !SP.getContext().isFile() &&
319 !isSubprogramContext(SP.getContext())) {
Eric Christopher873cf0a2012-08-24 01:14:27 +0000320 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
Devang Patel5e06bb82011-04-22 23:10:17 +0000321
322 // Add arguments.
323 DICompositeType SPTy = SP.getType();
324 DIArray Args = SPTy.getTypeArray();
325 unsigned SPTag = SPTy.getTag();
326 if (SPTag == dwarf::DW_TAG_subroutine_type)
327 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
328 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Eric Christopher5c38de92012-09-10 23:33:57 +0000329 DIType ATy = DIType(Args.getElement(i));
Devang Patel5e06bb82011-04-22 23:10:17 +0000330 SPCU->addType(Arg, ATy);
331 if (ATy.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000332 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
Eric Christophere5212782012-09-12 23:36:19 +0000333 if (ATy.isObjectPointer())
334 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer, dwarf::DW_FORM_ref4,
335 Arg);
Devang Patel5e06bb82011-04-22 23:10:17 +0000336 SPDie->addChild(Arg);
337 }
338 DIE *SPDeclDie = SPDie;
339 SPDie = new DIE(dwarf::DW_TAG_subprogram);
340 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
341 SPDeclDie);
342 SPCU->addDie(SPDie);
343 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000344 }
Devang Patel8aa61472010-07-07 22:20:57 +0000345 // Pick up abstract subprogram DIE.
346 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
347 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000348 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
349 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000350 SPCU->addDie(SPDie);
351 }
352
Devang Patel3cbee302011-04-12 22:53:02 +0000353 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
354 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
355 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
356 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000357 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
358 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000359 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000360
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000361 // Add name to the name table, we do this here because we're guaranteed
362 // to have concrete versions of our DW_TAG_subprogram nodes.
363 addSubprogramNames(SPCU, SP, SPDie);
364
Chris Lattnerd38fee82010-04-05 00:13:49 +0000365 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000366}
367
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000368/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000369/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000370DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
371 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000372 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
373 if (Scope->isAbstractScope())
374 return ScopeDIE;
375
Devang Patelbf47fdb2011-08-10 20:55:27 +0000376 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000377 if (Ranges.empty())
378 return 0;
379
Devang Patelbf47fdb2011-08-10 20:55:27 +0000380 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000381 if (Ranges.size() > 1) {
382 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000383 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000384 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000385 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000386 DebugRangeSymbols.size()
387 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000388 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000389 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000390 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
391 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000392 }
393 DebugRangeSymbols.push_back(NULL);
394 DebugRangeSymbols.push_back(NULL);
395 return ScopeDIE;
396 }
397
Devang Patelc3f5f782010-05-25 23:40:22 +0000398 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
399 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000400
Devang Patelc3f5f782010-05-25 23:40:22 +0000401 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000402
Chris Lattnerb7db7332010-03-09 01:58:53 +0000403 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
404 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000405
Devang Patel3cbee302011-04-12 22:53:02 +0000406 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
407 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000408
409 return ScopeDIE;
410}
411
Devang Patel2c4ceb12009-11-21 02:48:08 +0000412/// constructInlinedScopeDIE - This scope represents inlined body of
413/// a function. Construct DIE to represent this concrete inlined copy
414/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000415DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
416 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000417 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000418 assert(Ranges.empty() == false &&
419 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000420
Devang Patel26a92002011-07-27 00:34:13 +0000421 if (!Scope->getScopeNode())
422 return NULL;
423 DIScope DS(Scope->getScopeNode());
424 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000425 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
426 if (!OriginDIE) {
427 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
428 return NULL;
429 }
430
Devang Patelbf47fdb2011-08-10 20:55:27 +0000431 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000432 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
433 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000434
Devang Patel0afbf232010-07-08 22:39:20 +0000435 if (StartLabel == 0 || EndLabel == 0) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000436 llvm_unreachable("Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000437 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000438 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000439 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000440 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000441 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000442
Devang Pateld96efb82011-05-05 17:54:26 +0000443 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000444 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
445 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000446
Devang Patel26a92002011-07-27 00:34:13 +0000447 if (Ranges.size() > 1) {
448 // .debug_range section has not been laid out yet. Emit offset in
449 // .debug_range as a uint, size 4, for now. emitDIE will handle
450 // DW_AT_ranges appropriately.
451 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000452 DebugRangeSymbols.size()
453 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000454 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000455 RE = Ranges.end(); RI != RE; ++RI) {
456 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
457 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
458 }
459 DebugRangeSymbols.push_back(NULL);
460 DebugRangeSymbols.push_back(NULL);
461 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000462 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
463 StartLabel);
464 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
465 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000466 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000467
468 InlinedSubprogramDIEs.insert(OriginDIE);
469
470 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000471 //.debug_inlined section specification does not clearly state how
472 // to emit inlined scope that is split into multiple instruction ranges.
473 // For now, use first instruction range and emit low_pc/high_pc pair and
474 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000475 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000476 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000477
478 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000479 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000480 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000481 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000482 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000483
Devang Patel53bb5c92009-11-10 23:06:00 +0000484 DILocation DL(Scope->getInlinedAt());
Eric Christopher08212002012-03-26 21:38:38 +0000485 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
486 GetOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patel3cbee302011-04-12 22:53:02 +0000487 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000488
Eric Christopher309bedd2011-12-04 06:02:38 +0000489 // Add name to the name table, we do this here because we're guaranteed
490 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
491 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
492
Devang Patel53bb5c92009-11-10 23:06:00 +0000493 return ScopeDIE;
494}
495
Devang Patel2c4ceb12009-11-21 02:48:08 +0000496/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000497DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000498 if (!Scope || !Scope->getScopeNode())
499 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000500
Nick Lewycky746cb672011-10-26 22:55:33 +0000501 SmallVector<DIE *, 8> Children;
Eric Christophere5212782012-09-12 23:36:19 +0000502 DIE *ObjectPointer = NULL;
Devang Patel0478c152011-03-01 22:58:55 +0000503
504 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000505 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000506 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
507 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000508 if (DIE *Arg =
Eric Christophere5212782012-09-12 23:36:19 +0000509 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope())) {
Devang Patel0478c152011-03-01 22:58:55 +0000510 Children.push_back(Arg);
Eric Christophere5212782012-09-12 23:36:19 +0000511 if (ArgDV->isObjectPointer()) ObjectPointer = Arg;
512 }
Devang Patel0478c152011-03-01 22:58:55 +0000513
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000514 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000515 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000516 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000517 if (DIE *Variable =
Eric Christopher7b451cf2012-09-21 22:18:52 +0000518 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope())) {
Devang Patel5bc9fec2011-02-19 01:31:27 +0000519 Children.push_back(Variable);
Eric Christopher7b451cf2012-09-21 22:18:52 +0000520 if (Variables[i]->isObjectPointer()) ObjectPointer = Variable;
521 }
Devang Patelbf47fdb2011-08-10 20:55:27 +0000522 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000523 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000524 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000525 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000526 DIScope DS(Scope->getScopeNode());
527 DIE *ScopeDIE = NULL;
528 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000529 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000530 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000531 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000532 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000533 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000534 // Note down abstract DIE.
535 if (ScopeDIE)
536 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
537 }
Devang Patel3c91b052010-03-08 20:52:55 +0000538 else
Devang Pateld3024342011-08-15 22:24:32 +0000539 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000540 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000541 else {
542 // There is no need to emit empty lexical block DIE.
543 if (Children.empty())
544 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000545 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000546 }
547
Devang Patelaead63c2010-03-29 22:59:58 +0000548 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000549
Devang Patel5bc9fec2011-02-19 01:31:27 +0000550 // Add children
551 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
552 E = Children.end(); I != E; ++I)
553 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000554
Eric Christophere5212782012-09-12 23:36:19 +0000555 if (DS.isSubprogram() && ObjectPointer != NULL)
556 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer,
557 dwarf::DW_FORM_ref4, ObjectPointer);
558
Jim Grosbach1e20b962010-07-21 21:21:52 +0000559 if (DS.isSubprogram())
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000560 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000561
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000562 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000563}
564
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000565/// GetOrCreateSourceID - Look up the source id with the given directory and
566/// source file names. If none currently exists, create a new id and insert it
567/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
568/// maps as well.
Devang Patel23670e52011-03-24 20:30:50 +0000569unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
570 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000571 // If FE did not provide a file name, then assume stdin.
572 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000573 return GetOrCreateSourceID("<stdin>", StringRef());
574
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000575 // TODO: this might not belong here. See if we can factor this better.
576 if (DirName == CompilationDir)
577 DirName = "";
578
Nick Lewycky44d798d2011-10-17 23:05:28 +0000579 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel1905a182010-09-16 20:57:49 +0000580
Benjamin Kramer74612c22012-03-11 14:56:26 +0000581 // We look up the file/dir pair by concatenating them with a zero byte.
582 SmallString<128> NamePair;
583 NamePair += DirName;
584 NamePair += '\0'; // Zero bytes are not allowed in paths.
585 NamePair += FileName;
586
587 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
588 if (Ent.getValue() != SrcId)
589 return Ent.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000590
Rafael Espindola5c055632010-11-18 02:04:25 +0000591 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer74612c22012-03-11 14:56:26 +0000592 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000593
594 return SrcId;
595}
596
Jim Grosbach1e20b962010-07-21 21:21:52 +0000597/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000598/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000599CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000600 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000601 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000602 CompilationDir = DIUnit.getDirectory();
603 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000604
605 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopheredf8bc82012-09-10 23:34:00 +0000606 CompileUnit *NewCU = new CompileUnit(ID, DIUnit.getLanguage(), Die,
607 Asm, this);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000608 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000609 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
610 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000611 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher6635cad2012-08-01 18:19:01 +0000612 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
613 // into an entity.
614 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000615 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000616 // compile unit in debug_line section.
Rafael Espindola2241e512012-06-22 13:24:07 +0000617 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindola597a7662011-05-04 17:44:06 +0000618 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000619 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000620 else
Devang Patel3cbee302011-04-12 22:53:02 +0000621 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000622
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000623 if (!CompilationDir.empty())
624 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000625 if (DIUnit.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000626 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000627
Devang Patel65dbc902009-11-25 17:36:49 +0000628 StringRef Flags = DIUnit.getFlags();
629 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000630 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000631
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000632 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000633 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000634 dwarf::DW_FORM_data1, RVer);
635
Devang Patel163a9f72010-05-10 22:49:55 +0000636 if (!FirstCU)
637 FirstCU = NewCU;
638 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000639 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000640}
641
Devang Patel163a9f72010-05-10 22:49:55 +0000642/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000643void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
644 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000645 CompileUnit *&CURef = SPMap[N];
646 if (CURef)
647 return;
648 CURef = TheCU;
649
Devang Patele4b27562009-08-28 23:24:31 +0000650 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000651 if (!SP.isDefinition())
652 // This is a method declaration which will be handled while constructing
653 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000654 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000655
Devang Pateldbc64af2011-08-15 17:24:54 +0000656 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000657
658 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000659 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000660
661 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000662 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000663
Devang Patel13e16b62009-06-26 01:49:18 +0000664 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000665}
666
Devang Patel02e603f2011-08-15 23:47:24 +0000667/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
668/// as llvm.dbg.enum and llvm.dbg.ty
669void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000670 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
671 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
672 const MDNode *N = NMD->getOperand(i);
673 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
674 constructSubprogramDIE(CU, N);
675 }
676
677 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
678 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
679 const MDNode *N = NMD->getOperand(i);
680 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000681 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000682 }
683
Devang Patel02e603f2011-08-15 23:47:24 +0000684 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
685 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
686 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000687 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
688 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000689 }
690
691 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
692 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
693 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000694 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
695 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000696 }
697}
698
699/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
700/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
701bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
702 DebugInfoFinder DbgFinder;
703 DbgFinder.processModule(*M);
704
705 bool HasDebugInfo = false;
706 // Scan all the compile-units to see if there are any marked as the main
707 // unit. If not, we do not generate debug info.
708 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
709 E = DbgFinder.compile_unit_end(); I != E; ++I) {
710 if (DICompileUnit(*I).isMain()) {
711 HasDebugInfo = true;
712 break;
713 }
714 }
715 if (!HasDebugInfo) return false;
716
717 // Create all the compile unit DIEs.
718 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
719 E = DbgFinder.compile_unit_end(); I != E; ++I)
720 constructCompileUnit(*I);
721
722 // Create DIEs for each global variable.
723 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
724 E = DbgFinder.global_variable_end(); I != E; ++I) {
725 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000726 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000727 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000728 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000729
Devang Patel02e603f2011-08-15 23:47:24 +0000730 // Create DIEs for each subprogram.
731 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
732 E = DbgFinder.subprogram_end(); I != E; ++I) {
733 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000734 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
735 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000736 }
737
738 return HasDebugInfo;
739}
740
Devang Patel2c4ceb12009-11-21 02:48:08 +0000741/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000742/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000743/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000744void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000745 if (DisableDebugInfoPrinting)
746 return;
747
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000748 // If module has named metadata anchors then use them, otherwise scan the
749 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000750 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
751 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000752 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
753 DICompileUnit CUNode(CU_Nodes->getOperand(i));
754 CompileUnit *CU = constructCompileUnit(CUNode);
755 DIArray GVs = CUNode.getGlobalVariables();
756 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000757 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000758 DIArray SPs = CUNode.getSubprograms();
759 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
760 constructSubprogramDIE(CU, SPs.getElement(i));
761 DIArray EnumTypes = CUNode.getEnumTypes();
762 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
763 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
764 DIArray RetainedTypes = CUNode.getRetainedTypes();
765 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
766 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
767 }
Devang Patel02e603f2011-08-15 23:47:24 +0000768 } else if (!collectLegacyDebugInfo(M))
769 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000770
Devang Patel02e603f2011-08-15 23:47:24 +0000771 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000772
Chris Lattnerd850ac72010-04-05 02:19:28 +0000773 // Tell MMI that we have debug info.
774 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000775
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000776 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000777 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000778
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000779 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000780 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000781}
782
Devang Patel2c4ceb12009-11-21 02:48:08 +0000783/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000784///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000785void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000786 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000787 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000788 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000789
Devang Patel94c7ddb2011-08-16 22:09:43 +0000790 // Collect info for variables that were optimized out.
791 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
792 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
793 DICompileUnit TheCU(CU_Nodes->getOperand(i));
794 DIArray Subprograms = TheCU.getSubprograms();
795 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
796 DISubprogram SP(Subprograms.getElement(i));
797 if (ProcessedSPNodes.count(SP) != 0) continue;
798 if (!SP.Verify()) continue;
799 if (!SP.isDefinition()) continue;
Devang Patel93d39be2011-08-19 23:28:12 +0000800 DIArray Variables = SP.getVariables();
801 if (Variables.getNumElements() == 0) continue;
802
Devang Patel94c7ddb2011-08-16 22:09:43 +0000803 LexicalScope *Scope =
804 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
805 DeadFnScopeMap[SP] = Scope;
806
807 // Construct subprogram DIE and add variables DIEs.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000808 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky746cb672011-10-26 22:55:33 +0000809 assert(SPCU && "Unable to find Compile Unit!");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000810 constructSubprogramDIE(SPCU, SP);
811 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel93d39be2011-08-19 23:28:12 +0000812 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
813 DIVariable DV(Variables.getElement(vi));
814 if (!DV.Verify()) continue;
815 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000816 if (DIE *VariableDIE =
Devang Patel93d39be2011-08-19 23:28:12 +0000817 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Patel94c7ddb2011-08-16 22:09:43 +0000818 ScopeDIE->addChild(VariableDIE);
819 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000820 }
821 }
822 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000823
Devang Patel53bb5c92009-11-10 23:06:00 +0000824 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
825 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
826 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
827 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000828 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000829 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000830 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
831 AE = AbstractSPDies.end(); AI != AE; ++AI) {
832 DIE *ISP = AI->second;
833 if (InlinedSubprogramDIEs.count(ISP))
834 continue;
835 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
836 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000837
Eric Christopher6635cad2012-08-01 18:19:01 +0000838 // Emit DW_AT_containing_type attribute to connect types with their
839 // vtable holding type.
Devang Pateldbc64af2011-08-15 17:24:54 +0000840 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
841 CUE = CUMap.end(); CUI != CUE; ++CUI) {
842 CompileUnit *TheCU = CUI->second;
843 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000844 }
845
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000846 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000847 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000848 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000849 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000850 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000851
852 // End text sections.
853 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000854 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000855 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000856 }
857
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000858 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000859 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000860
861 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000862 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000863
864 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000865 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000866
Eric Christopher9d9f5a52012-08-23 07:32:06 +0000867 // Emit info into the dwarf accelerator table sections.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000868 if (useDwarfAccelTables()) {
Eric Christopher09ac3d82011-11-07 09:24:32 +0000869 emitAccelNames();
870 emitAccelObjC();
871 emitAccelNamespaces();
872 emitAccelTypes();
873 }
874
Devang Patel193f7202009-11-24 01:14:22 +0000875 // Emit info into a debug pubtypes section.
Eric Christopher360f0062012-08-23 07:10:56 +0000876 // TODO: When we don't need the option anymore we can
877 // remove all of the code that adds to the table.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000878 if (useDarwinGDBCompat())
Eric Christopher360f0062012-08-23 07:10:56 +0000879 emitDebugPubTypes();
Devang Patel193f7202009-11-24 01:14:22 +0000880
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000881 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000882 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000883
884 // Emit info into a debug aranges section.
885 EmitDebugARanges();
886
887 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000888 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000889
890 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000891 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000892
893 // Emit inline info.
Eric Christopher9eb1a942012-08-23 07:32:02 +0000894 // TODO: When we don't need the option anymore we
895 // can remove all of the code that this section
896 // depends upon.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000897 if (useDarwinGDBCompat())
Eric Christopher9eb1a942012-08-23 07:32:02 +0000898 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000899
Eric Christopher7550f7d2012-03-02 00:30:24 +0000900 // Emit info into a debug str section.
901 emitDebugStr();
902
Devang Patele9a1cca2010-08-02 17:32:15 +0000903 // clean up.
904 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000905 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000906 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
907 E = CUMap.end(); I != E; ++I)
908 delete I->second;
909 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000910}
911
Devang Patel53bb5c92009-11-10 23:06:00 +0000912/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000913DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000914 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000915 LLVMContext &Ctx = DV->getContext();
916 // More then one inlined variable corresponds to one abstract variable.
917 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000918 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000919 if (AbsDbgVariable)
920 return AbsDbgVariable;
921
Devang Patelbf47fdb2011-08-10 20:55:27 +0000922 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000923 if (!Scope)
924 return NULL;
925
Devang Patel5a1a67c2011-08-15 19:01:20 +0000926 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000927 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000928 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000929 return AbsDbgVariable;
930}
931
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000932/// addCurrentFnArgument - If Var is a current function argument then add
933/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000934bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000935 DbgVariable *Var, LexicalScope *Scope) {
936 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000937 return false;
938 DIVariable DV = Var->getVariable();
939 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
940 return false;
941 unsigned ArgNo = DV.getArgNumber();
942 if (ArgNo == 0)
943 return false;
944
Devang Patelcb3a6572011-03-03 20:02:02 +0000945 size_t Size = CurrentFnArguments.size();
946 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000947 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000948 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000949 // arguments does the function have at source level.
950 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000951 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000952 CurrentFnArguments[ArgNo - 1] = Var;
953 return true;
954}
955
Devang Patelee432862010-05-20 19:57:06 +0000956/// collectVariableInfoFromMMITable - Collect variable information from
957/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000958void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000959DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000960 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000961 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
962 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
963 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000964 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000965 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000966 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000967 DIVariable DV(Var);
968 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000969
Devang Patelbf47fdb2011-08-10 20:55:27 +0000970 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000971
Devang Patelfb0ee432009-11-10 23:20:04 +0000972 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000973 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000974 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000975
Devang Patel26c1e562010-05-20 16:36:41 +0000976 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000977 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000978 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000979 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000980 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000981 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000982 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000983 }
Devang Patelee432862010-05-20 19:57:06 +0000984}
Devang Patel90a48ad2010-03-15 18:33:46 +0000985
Jim Grosbach1e20b962010-07-21 21:21:52 +0000986/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000987/// DBG_VALUE instruction, is in a defined reg.
988static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000989 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000990 return MI->getNumOperands() == 3 &&
991 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
992 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000993}
994
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000995/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000996/// at MI.
997static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
998 const MCSymbol *FLabel,
999 const MCSymbol *SLabel,
1000 const MachineInstr *MI) {
1001 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1002
1003 if (MI->getNumOperands() != 3) {
1004 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1005 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1006 }
1007 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1008 MachineLocation MLoc;
1009 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1010 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1011 }
1012 if (MI->getOperand(0).isImm())
1013 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1014 if (MI->getOperand(0).isFPImm())
1015 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1016 if (MI->getOperand(0).isCImm())
1017 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1018
Craig Topper5e25ee82012-02-05 08:31:47 +00001019 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +00001020}
1021
Devang Patelbf47fdb2011-08-10 20:55:27 +00001022/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001023void
Devang Patel78e127d2010-06-25 22:07:34 +00001024DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1025 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001026
Devang Patelee432862010-05-20 19:57:06 +00001027 /// collection info from MMI table.
1028 collectVariableInfoFromMMITable(MF, Processed);
1029
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001030 for (SmallVectorImpl<const MDNode*>::const_iterator
1031 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1032 ++UVI) {
1033 const MDNode *Var = *UVI;
1034 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001035 continue;
1036
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001037 // History contains relevant DBG_VALUE instructions for Var and instructions
1038 // clobbering it.
1039 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1040 if (History.empty())
1041 continue;
1042 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001043
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001044 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001045 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001046 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1047 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001048 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001049 else {
1050 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001051 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001052 else {
1053 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001054 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001055 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001056 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001057 }
1058 }
Devang Patelee432862010-05-20 19:57:06 +00001059 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001060 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001061 continue;
1062
1063 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001064 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001065 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1066 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001067 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001068 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001069 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001070 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001071
1072 // Simple ranges that are fully coalesced.
1073 if (History.size() <= 1 || (History.size() == 2 &&
1074 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001075 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001076 continue;
1077 }
1078
1079 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001080 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001081
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001082 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1083 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1084 const MachineInstr *Begin = *HI;
1085 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001086
Devang Patel4ada1d72011-06-01 23:00:17 +00001087 // Check if DBG_VALUE is truncating a range.
1088 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1089 && !Begin->getOperand(0).getReg())
1090 continue;
1091
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001092 // Compute the range for a register location.
1093 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1094 const MCSymbol *SLabel = 0;
1095
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001096 if (HI + 1 == HE)
1097 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001098 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001099 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001100 else {
1101 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001102 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1103 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001104 if (End->isDebugValue())
1105 SLabel = getLabelBeforeInsn(End);
1106 else {
1107 // End is a normal instruction clobbering the range.
1108 SLabel = getLabelAfterInsn(End);
1109 assert(SLabel && "Forgot label after clobber instruction");
1110 ++HI;
1111 }
1112 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001113
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001114 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001115 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1116 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001117 }
1118 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001119 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001120
1121 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001122 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1123 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1124 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1125 DIVariable DV(Variables.getElement(i));
1126 if (!DV || !DV.Verify() || !Processed.insert(DV))
1127 continue;
1128 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1129 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001130 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001131}
Devang Patel98e1cac2010-05-14 21:01:35 +00001132
Devang Patelc3f5f782010-05-25 23:40:22 +00001133/// getLabelBeforeInsn - Return Label preceding the instruction.
1134const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001135 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1136 assert(Label && "Didn't insert label before instruction");
1137 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001138}
1139
1140/// getLabelAfterInsn - Return Label immediately following the instruction.
1141const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001142 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001143}
1144
Devang Patelcbbe2872010-10-26 17:49:02 +00001145/// beginInstruction - Process beginning of an instruction.
1146void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001147 // Check if source location changes, but ignore DBG_VALUE locations.
1148 if (!MI->isDebugValue()) {
1149 DebugLoc DL = MI->getDebugLoc();
1150 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001151 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001152 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001153 if (DL == PrologEndLoc) {
1154 Flags |= DWARF2_FLAG_PROLOGUE_END;
1155 PrologEndLoc = DebugLoc();
1156 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001157 if (PrologEndLoc.isUnknown())
1158 Flags |= DWARF2_FLAG_IS_STMT;
1159
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001160 if (!DL.isUnknown()) {
1161 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001162 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001163 } else
Devang Patel4243e672011-05-11 19:22:19 +00001164 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001165 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001166 }
Devang Patelaead63c2010-03-29 22:59:58 +00001167
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001168 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001169 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1170 LabelsBeforeInsn.find(MI);
1171
1172 // No label needed.
1173 if (I == LabelsBeforeInsn.end())
1174 return;
1175
1176 // Label already assigned.
1177 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001178 return;
Devang Patel553881b2010-03-29 17:20:31 +00001179
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001180 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001181 PrevLabel = MMI->getContext().CreateTempSymbol();
1182 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001183 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001184 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001185}
1186
Devang Patelcbbe2872010-10-26 17:49:02 +00001187/// endInstruction - Process end of an instruction.
1188void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001189 // Don't create a new label after DBG_VALUE instructions.
1190 // They don't generate code.
1191 if (!MI->isDebugValue())
1192 PrevLabel = 0;
1193
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001194 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1195 LabelsAfterInsn.find(MI);
1196
1197 // No label needed.
1198 if (I == LabelsAfterInsn.end())
1199 return;
1200
1201 // Label already assigned.
1202 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001203 return;
1204
1205 // We need a label after this instruction.
1206 if (!PrevLabel) {
1207 PrevLabel = MMI->getContext().CreateTempSymbol();
1208 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001209 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001210 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001211}
1212
Jim Grosbach1e20b962010-07-21 21:21:52 +00001213/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001214/// Each LexicalScope has first instruction and last instruction to mark
1215/// beginning and end of a scope respectively. Create an inverse map that list
1216/// scopes starts (and ends) with an instruction. One instruction may start (or
1217/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001218void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001219 SmallVector<LexicalScope *, 4> WorkList;
1220 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001221 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001222 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001223
Devang Patelbf47fdb2011-08-10 20:55:27 +00001224 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001225 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001226 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001227 SE = Children.end(); SI != SE; ++SI)
1228 WorkList.push_back(*SI);
1229
Devang Patel53bb5c92009-11-10 23:06:00 +00001230 if (S->isAbstractScope())
1231 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001232
Devang Patelbf47fdb2011-08-10 20:55:27 +00001233 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001234 if (Ranges.empty())
1235 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001236 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001237 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001238 assert(RI->first && "InsnRange does not have first instruction!");
1239 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001240 requestLabelBeforeInsn(RI->first);
1241 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001242 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001243 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001244}
1245
Devang Patela3f48672011-05-09 22:14:49 +00001246/// getScopeNode - Get MDNode for DebugLoc's scope.
1247static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1248 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1249 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1250 return DL.getScope(Ctx);
1251}
1252
Devang Patel4243e672011-05-11 19:22:19 +00001253/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
Eric Christopher6126a1e2012-04-03 00:43:49 +00001254/// line number info for the function.
Devang Patel4243e672011-05-11 19:22:19 +00001255static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1256 const MDNode *Scope = getScopeNode(DL, Ctx);
1257 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher6126a1e2012-04-03 00:43:49 +00001258 if (SP.Verify()) {
1259 // Check for number of operands since the compatibility is
1260 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001261 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-04-03 00:43:49 +00001262 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1263 else
1264 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1265 }
1266
Devang Patel4243e672011-05-11 19:22:19 +00001267 return DebugLoc();
1268}
1269
Devang Patel2c4ceb12009-11-21 02:48:08 +00001270/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001271/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001272void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001273 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001274 LScopes.initialize(*MF);
1275 if (LScopes.empty()) return;
1276 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001277
Devang Pateleac9c072010-04-27 19:46:33 +00001278 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1279 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001280 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001281 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001282
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001283 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1284
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001285 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001286 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1287 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1288
Devang Patelb2b31a62010-05-26 19:37:24 +00001289 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001290 I != E; ++I) {
1291 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001292 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1293 II != IE; ++II) {
1294 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001295
Devang Patelb2b31a62010-05-26 19:37:24 +00001296 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001297 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001298
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001299 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001300 const MDNode *Var =
1301 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001302
1303 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001304 if (isDbgValueInDefinedReg(MI))
1305 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1306
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001307 // Check the history of this variable.
1308 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1309 if (History.empty()) {
1310 UserVariables.push_back(Var);
1311 // The first mention of a function argument gets the FunctionBeginSym
1312 // label, so arguments are visible when breaking at function entry.
1313 DIVariable DV(Var);
1314 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1315 DISubprogram(getDISubprogram(DV.getContext()))
1316 .describes(MF->getFunction()))
1317 LabelsBeforeInsn[MI] = FunctionBeginSym;
1318 } else {
1319 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1320 const MachineInstr *Prev = History.back();
1321 if (Prev->isDebugValue()) {
1322 // Coalesce identical entries at the end of History.
1323 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001324 Prev->isIdenticalTo(History[History.size() - 2])) {
1325 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1326 << "\t" << *Prev
1327 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001328 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001329 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001330
1331 // Terminate old register assignments that don't reach MI;
1332 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1333 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1334 isDbgValueInDefinedReg(Prev)) {
1335 // Previous register assignment needs to terminate at the end of
1336 // its basic block.
1337 MachineBasicBlock::const_iterator LastMI =
1338 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001339 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001340 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001341 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1342 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001343 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001344 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001345 else {
1346 // Terminate after LastMI.
1347 History.push_back(LastMI);
1348 }
1349 }
1350 }
1351 }
1352 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001353 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001354 // Not a DBG_VALUE instruction.
1355 if (!MI->isLabel())
1356 AtBlockEntry = false;
1357
Devang Patel4243e672011-05-11 19:22:19 +00001358 // First known non DBG_VALUE location marks beginning of function
1359 // body.
1360 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1361 PrologEndLoc = MI->getDebugLoc();
1362
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001363 // Check if the instruction clobbers any registers with debug vars.
1364 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1365 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1366 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1367 continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001368 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1369 AI.isValid(); ++AI) {
1370 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001371 const MDNode *Var = LiveUserVar[Reg];
1372 if (!Var)
1373 continue;
1374 // Reg is now clobbered.
1375 LiveUserVar[Reg] = 0;
1376
1377 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001378 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1379 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001380 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001381 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1382 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001383 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001384 const MachineInstr *Prev = History.back();
1385 // Sanity-check: Register assignments are terminated at the end of
1386 // their block.
1387 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1388 continue;
1389 // Is the variable still in Reg?
1390 if (!isDbgValueInDefinedReg(Prev) ||
1391 Prev->getOperand(0).getReg() != Reg)
1392 continue;
1393 // Var is clobbered. Make sure the next instruction gets a label.
1394 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001395 }
1396 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001397 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001398 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001399 }
1400
1401 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1402 I != E; ++I) {
1403 SmallVectorImpl<const MachineInstr*> &History = I->second;
1404 if (History.empty())
1405 continue;
1406
1407 // Make sure the final register assignments are terminated.
1408 const MachineInstr *Prev = History.back();
1409 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1410 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001411 MachineBasicBlock::const_iterator LastMI =
1412 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001413 if (LastMI == PrevMBB->end())
1414 // Drop DBG_VALUE for empty range.
1415 History.pop_back();
1416 else {
1417 // Terminate after LastMI.
1418 History.push_back(LastMI);
1419 }
1420 }
1421 // Request labels for the full history.
1422 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1423 const MachineInstr *MI = History[i];
1424 if (MI->isDebugValue())
1425 requestLabelBeforeInsn(MI);
1426 else
1427 requestLabelAfterInsn(MI);
1428 }
1429 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001430
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001431 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001432 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001433
1434 // Record beginning of function.
1435 if (!PrologEndLoc.isUnknown()) {
1436 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1437 MF->getFunction()->getContext());
1438 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1439 FnStartDL.getScope(MF->getFunction()->getContext()),
Eric Christopher09e47502012-09-10 23:34:06 +00001440 0);
Devang Patel4243e672011-05-11 19:22:19 +00001441 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001442}
1443
Devang Patelbf47fdb2011-08-10 20:55:27 +00001444void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1445// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1446 ScopeVariables[LS].push_back(Var);
1447// Vars.push_back(Var);
1448}
1449
Devang Patel2c4ceb12009-11-21 02:48:08 +00001450/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001451///
Chris Lattnereec791a2010-01-26 23:18:02 +00001452void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001453 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001454
Devang Patelbf47fdb2011-08-10 20:55:27 +00001455 // Define end label for subprogram.
1456 FunctionEndSym = Asm->GetTempSymbol("func_end",
1457 Asm->getFunctionNumber());
1458 // Assumes in correct section after the entry point.
1459 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1460
1461 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1462 collectVariableInfo(MF, ProcessedVars);
1463
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001464 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001465 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001466 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001467
Devang Patelbf47fdb2011-08-10 20:55:27 +00001468 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001469 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1470 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1471 LexicalScope *AScope = AList[i];
1472 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001473 if (SP.Verify()) {
1474 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001475 DIArray Variables = SP.getVariables();
1476 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1477 DIVariable DV(Variables.getElement(i));
1478 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1479 continue;
Alexey Samsonovb67bd332012-07-06 08:45:08 +00001480 // Check that DbgVariable for DV wasn't created earlier, when
1481 // findAbstractVariable() was called for inlined instance of DV.
1482 LLVMContext &Ctx = DV->getContext();
1483 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1484 if (AbstractVariables.lookup(CleanDV))
1485 continue;
Devang Patel93d39be2011-08-19 23:28:12 +00001486 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1487 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001488 }
1489 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001490 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001491 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001492 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001493
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001494 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001495
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001496 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001497 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001498
Devang Patelbf47fdb2011-08-10 20:55:27 +00001499 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1500 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001501
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001502 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001503 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1504 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1505 DeleteContainerPointers(I->second);
1506 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001507 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001508 UserVariables.clear();
1509 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001510 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001511 LabelsBeforeInsn.clear();
1512 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001513 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001514}
1515
Chris Lattnerc6087842010-03-09 04:54:43 +00001516/// recordSourceLine - Register a source line with debug info. Returns the
1517/// unique label that was emitted and which provides correspondence to
1518/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001519void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1520 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001521 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001522 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001523 unsigned Src = 1;
1524 if (S) {
1525 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001526
Dan Gohman1cc0d622010-05-05 23:41:32 +00001527 if (Scope.isCompileUnit()) {
1528 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001529 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001530 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001531 } else if (Scope.isFile()) {
1532 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001533 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001534 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001535 } else if (Scope.isSubprogram()) {
1536 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001537 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001538 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001539 } else if (Scope.isLexicalBlockFile()) {
1540 DILexicalBlockFile DBF(S);
1541 Fn = DBF.getFilename();
1542 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001543 } else if (Scope.isLexicalBlock()) {
1544 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001545 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001546 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001547 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001548 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001549
Devang Patel23670e52011-03-24 20:30:50 +00001550 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001551 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001552 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001553}
1554
Bill Wendling829e67b2009-05-20 23:22:40 +00001555//===----------------------------------------------------------------------===//
1556// Emit Methods
1557//===----------------------------------------------------------------------===//
1558
Devang Patel2c4ceb12009-11-21 02:48:08 +00001559/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001560///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001561unsigned
1562DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001563 // Get the children.
1564 const std::vector<DIE *> &Children = Die->getChildren();
1565
Bill Wendling94d04b82009-05-20 23:21:38 +00001566 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001567 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001568
1569 // Get the abbreviation for this DIE.
1570 unsigned AbbrevNumber = Die->getAbbrevNumber();
1571 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1572
1573 // Set DIE offset
1574 Die->setOffset(Offset);
1575
1576 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001577 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001578
1579 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1580 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1581
1582 // Size the DIE attribute values.
1583 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1584 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001585 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001586
1587 // Size the DIE children if any.
1588 if (!Children.empty()) {
1589 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1590 "Children flag not set");
1591
1592 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001593 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001594
1595 // End of children marker.
1596 Offset += sizeof(int8_t);
1597 }
1598
1599 Die->setSize(Offset - Die->getOffset());
1600 return Offset;
1601}
1602
Devang Patel2c4ceb12009-11-21 02:48:08 +00001603/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001604///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001605void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001606 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1607 E = CUMap.end(); I != E; ++I) {
1608 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001609 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001610 sizeof(int32_t) + // Length of Compilation Unit Info
1611 sizeof(int16_t) + // DWARF version number
1612 sizeof(int32_t) + // Offset Into Abbrev. Section
1613 sizeof(int8_t); // Pointer Size (in bytes)
1614 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001615 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001616}
1617
Chris Lattner9c69e285532010-04-04 22:59:04 +00001618/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1619/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001620void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001621 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001622
Bill Wendling94d04b82009-05-20 23:21:38 +00001623 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001624 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001625 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001626 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001627 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001628 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001629
Chris Lattner9c69e285532010-04-04 22:59:04 +00001630 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001631 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001632
Devang Patelaf608bd2010-08-24 00:06:12 +00001633 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001634 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
Chris Lattner11b8f302010-04-04 23:02:02 +00001635 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001636 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001637 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001638 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1639 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001640
Devang Patelc3f5f782010-05-25 23:40:22 +00001641 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1642 "section_debug_loc");
1643
Chris Lattner9c69e285532010-04-04 22:59:04 +00001644 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001645 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001646}
1647
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001648/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001649///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001650void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001651 // Get the abbreviation for this DIE.
1652 unsigned AbbrevNumber = Die->getAbbrevNumber();
1653 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1654
Bill Wendling94d04b82009-05-20 23:21:38 +00001655 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001656 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001657 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1658 Twine::utohexstr(Die->getOffset()) + ":0x" +
1659 Twine::utohexstr(Die->getSize()) + " " +
1660 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001661 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001662
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001663 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001664 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1665
1666 // Emit the DIE attribute values.
1667 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1668 unsigned Attr = AbbrevData[i].getAttribute();
1669 unsigned Form = AbbrevData[i].getForm();
1670 assert(Form && "Too many attributes for DIE (check abbreviation)");
1671
Chris Lattner3f53c832010-04-04 18:52:31 +00001672 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001673 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001674
Bill Wendling94d04b82009-05-20 23:21:38 +00001675 switch (Attr) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001676 case dwarf::DW_AT_abstract_origin: {
1677 DIEEntry *E = cast<DIEEntry>(Values[i]);
1678 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001679 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001680 Asm->EmitInt32(Addr);
1681 break;
1682 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001683 case dwarf::DW_AT_ranges: {
1684 // DW_AT_range Value encodes offset in debug_range section.
1685 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001686
Nick Lewyckyffccd922012-06-22 01:25:12 +00001687 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001688 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1689 V->getValue(),
1690 4);
1691 } else {
1692 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1693 V->getValue(),
1694 DwarfDebugRangeSectionSym,
1695 4);
1696 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001697 break;
1698 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001699 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-06-22 01:25:12 +00001700 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1701 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1702 Asm->EmitLabelReference(L->getValue(), 4);
1703 else
1704 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1705 } else {
Devang Patelc3f5f782010-05-25 23:40:22 +00001706 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00001707 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001708 break;
1709 }
Devang Patel2a361602010-09-29 19:08:08 +00001710 case dwarf::DW_AT_accessibility: {
1711 if (Asm->isVerbose()) {
1712 DIEInteger *V = cast<DIEInteger>(Values[i]);
1713 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1714 }
1715 Values[i]->EmitValue(Asm, Form);
1716 break;
1717 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001718 default:
1719 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001720 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001721 break;
1722 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001723 }
1724
1725 // Emit the DIE children if any.
1726 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1727 const std::vector<DIE *> &Children = Die->getChildren();
1728
1729 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001730 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001731
Chris Lattner3f53c832010-04-04 18:52:31 +00001732 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001733 Asm->OutStreamer.AddComment("End Of Children Mark");
1734 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001735 }
1736}
1737
Devang Patel8a241142009-12-09 18:24:21 +00001738/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001739///
Devang Patel8a241142009-12-09 18:24:21 +00001740void DwarfDebug::emitDebugInfo() {
1741 // Start debug info section.
1742 Asm->OutStreamer.SwitchSection(
1743 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001744 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1745 E = CUMap.end(); I != E; ++I) {
1746 CompileUnit *TheCU = I->second;
1747 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001748
Devang Patel163a9f72010-05-10 22:49:55 +00001749 // Emit the compile units header.
1750 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1751 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001752
Devang Patel163a9f72010-05-10 22:49:55 +00001753 // Emit size of content not including length itself
1754 unsigned ContentSize = Die->getSize() +
1755 sizeof(int16_t) + // DWARF version number
1756 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001757 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001758
Devang Patel163a9f72010-05-10 22:49:55 +00001759 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1760 Asm->EmitInt32(ContentSize);
1761 Asm->OutStreamer.AddComment("DWARF version number");
1762 Asm->EmitInt16(dwarf::DWARF_VERSION);
1763 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1764 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1765 DwarfAbbrevSectionSym);
1766 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1767 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001768
Devang Patel163a9f72010-05-10 22:49:55 +00001769 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001770 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1771 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001772}
1773
Devang Patel2c4ceb12009-11-21 02:48:08 +00001774/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001775///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001776void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001777 // Check to see if it is worth the effort.
1778 if (!Abbreviations.empty()) {
1779 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001780 Asm->OutStreamer.SwitchSection(
1781 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001782
Chris Lattnerc0215722010-04-04 19:25:43 +00001783 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001784
1785 // For each abbrevation.
1786 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1787 // Get abbreviation data
1788 const DIEAbbrev *Abbrev = Abbreviations[i];
1789
1790 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001791 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001792
1793 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001794 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001795 }
1796
1797 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001798 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001799
Chris Lattnerc0215722010-04-04 19:25:43 +00001800 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001801 }
1802}
1803
Devang Patel2c4ceb12009-11-21 02:48:08 +00001804/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001805/// the line matrix.
1806///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001807void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001808 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001809 Asm->OutStreamer.AddComment("Extended Op");
1810 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001811
Chris Lattner233f52b2010-03-09 23:52:58 +00001812 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001813 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001814 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1815 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1816
1817 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001818
Chris Lattnerc0215722010-04-04 19:25:43 +00001819 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001820 Asm->getTargetData().getPointerSize(),
1821 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001822
1823 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001824 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1825 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001826 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001827 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001828}
1829
Eric Christopher09ac3d82011-11-07 09:24:32 +00001830/// emitAccelNames - Emit visible names into a hashed accelerator table
1831/// section.
1832void DwarfDebug::emitAccelNames() {
1833 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1834 dwarf::DW_FORM_data4));
1835 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1836 E = CUMap.end(); I != E; ++I) {
1837 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001838 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1839 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001840 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1841 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001842 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001843 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1844 DE = Entities.end(); DI != DE; ++DI)
1845 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001846 }
1847 }
1848
1849 AT.FinalizeTable(Asm, "Names");
1850 Asm->OutStreamer.SwitchSection(
1851 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1852 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1853 Asm->OutStreamer.EmitLabel(SectionBegin);
1854
1855 // Emit the full data.
1856 AT.Emit(Asm, SectionBegin, this);
1857}
1858
1859/// emitAccelObjC - Emit objective C classes and categories into a hashed
1860/// accelerator table section.
1861void DwarfDebug::emitAccelObjC() {
1862 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1863 dwarf::DW_FORM_data4));
1864 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1865 E = CUMap.end(); I != E; ++I) {
1866 CompileUnit *TheCU = I->second;
1867 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1868 for (StringMap<std::vector<DIE*> >::const_iterator
1869 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1870 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001871 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-11-07 09:24:32 +00001872 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1873 DE = Entities.end(); DI != DE; ++DI)
1874 AT.AddName(Name, (*DI));
1875 }
1876 }
1877
1878 AT.FinalizeTable(Asm, "ObjC");
1879 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1880 .getDwarfAccelObjCSection());
1881 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1882 Asm->OutStreamer.EmitLabel(SectionBegin);
1883
1884 // Emit the full data.
1885 AT.Emit(Asm, SectionBegin, this);
1886}
1887
1888/// emitAccelNamespace - Emit namespace dies into a hashed accelerator
1889/// table.
1890void DwarfDebug::emitAccelNamespaces() {
1891 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1892 dwarf::DW_FORM_data4));
1893 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1894 E = CUMap.end(); I != E; ++I) {
1895 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001896 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
1897 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001898 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1899 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001900 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001901 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1902 DE = Entities.end(); DI != DE; ++DI)
1903 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001904 }
1905 }
1906
1907 AT.FinalizeTable(Asm, "namespac");
1908 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1909 .getDwarfAccelNamespaceSection());
1910 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
1911 Asm->OutStreamer.EmitLabel(SectionBegin);
1912
1913 // Emit the full data.
1914 AT.Emit(Asm, SectionBegin, this);
1915}
1916
1917/// emitAccelTypes() - Emit type dies into a hashed accelerator table.
1918void DwarfDebug::emitAccelTypes() {
Eric Christopherc36145f2012-01-06 04:35:23 +00001919 std::vector<DwarfAccelTable::Atom> Atoms;
1920 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1921 dwarf::DW_FORM_data4));
1922 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
1923 dwarf::DW_FORM_data2));
1924 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
1925 dwarf::DW_FORM_data1));
1926 DwarfAccelTable AT(Atoms);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001927 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1928 E = CUMap.end(); I != E; ++I) {
1929 CompileUnit *TheCU = I->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001930 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
1931 = TheCU->getAccelTypes();
1932 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001933 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1934 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001935 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001936 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
1937 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
1938 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001939 }
1940 }
1941
1942 AT.FinalizeTable(Asm, "types");
1943 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1944 .getDwarfAccelTypesSection());
1945 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
1946 Asm->OutStreamer.EmitLabel(SectionBegin);
1947
1948 // Emit the full data.
1949 AT.Emit(Asm, SectionBegin, this);
1950}
1951
Devang Patel193f7202009-11-24 01:14:22 +00001952void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001953 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1954 E = CUMap.end(); I != E; ++I) {
1955 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00001956 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-05-10 22:49:55 +00001957 Asm->OutStreamer.SwitchSection(
1958 Asm->getObjFileLowering().getDwarfPubTypesSection());
1959 Asm->OutStreamer.AddComment("Length of Public Types Info");
1960 Asm->EmitLabelDifference(
1961 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1962 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001963
Devang Patel163a9f72010-05-10 22:49:55 +00001964 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1965 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001966
Devang Patel163a9f72010-05-10 22:49:55 +00001967 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1968 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001969
Devang Patel163a9f72010-05-10 22:49:55 +00001970 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1971 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1972 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001973
Devang Patel163a9f72010-05-10 22:49:55 +00001974 Asm->OutStreamer.AddComment("Compilation Unit Length");
1975 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1976 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1977 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001978
Devang Patel163a9f72010-05-10 22:49:55 +00001979 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1980 for (StringMap<DIE*>::const_iterator
1981 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1982 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001983 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001984
Devang Patel163a9f72010-05-10 22:49:55 +00001985 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1986 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001987
Devang Patel163a9f72010-05-10 22:49:55 +00001988 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00001989 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00001990 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1991 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001992
Devang Patel163a9f72010-05-10 22:49:55 +00001993 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001994 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001995 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1996 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001997 }
Devang Patel193f7202009-11-24 01:14:22 +00001998}
1999
Devang Patel2c4ceb12009-11-21 02:48:08 +00002000/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002001///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002002void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002003 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002004 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002005
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002006 // Start the dwarf str section.
2007 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002008 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002009
Chris Lattnerbc733f52010-03-13 02:17:42 +00002010 // Get all of the string pool entries and put them in an array by their ID so
2011 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002012 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00002013 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002014
Chris Lattnerbc733f52010-03-13 02:17:42 +00002015 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2016 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2017 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002018
Chris Lattnerbc733f52010-03-13 02:17:42 +00002019 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002020
Chris Lattnerbc733f52010-03-13 02:17:42 +00002021 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002022 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002023 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002024
Benjamin Kramer983c4572011-11-09 18:16:11 +00002025 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00002026 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
2027 Entries[i].second->getKeyLength()+1),
2028 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002029 }
2030}
2031
Devang Patel2c4ceb12009-11-21 02:48:08 +00002032/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002033///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002034void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002035 if (DotDebugLocEntries.empty())
2036 return;
2037
Devang Patel6c3ea902011-02-04 22:57:18 +00002038 for (SmallVector<DotDebugLocEntry, 4>::iterator
2039 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2040 I != E; ++I) {
2041 DotDebugLocEntry &Entry = *I;
2042 if (I + 1 != DotDebugLocEntries.end())
2043 Entry.Merge(I+1);
2044 }
2045
Daniel Dunbar83320a02011-03-16 22:16:39 +00002046 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002047 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002048 Asm->getObjFileLowering().getDwarfLocSection());
2049 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002050 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2051 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002052 for (SmallVector<DotDebugLocEntry, 4>::iterator
2053 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002054 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002055 DotDebugLocEntry &Entry = *I;
2056 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002057 if (Entry.isEmpty()) {
2058 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2059 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002060 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002061 } else {
2062 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2063 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002064 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002065 Asm->OutStreamer.AddComment("Loc expr size");
2066 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2067 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2068 Asm->EmitLabelDifference(end, begin, 2);
2069 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002070 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002071 DIBasicType BTy(DV.getType());
2072 if (BTy.Verify() &&
2073 (BTy.getEncoding() == dwarf::DW_ATE_signed
2074 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2075 Asm->OutStreamer.AddComment("DW_OP_consts");
2076 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002077 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002078 } else {
2079 Asm->OutStreamer.AddComment("DW_OP_constu");
2080 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002081 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002082 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002083 } else if (Entry.isLocation()) {
2084 if (!DV.hasComplexAddress())
2085 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002086 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002087 else {
2088 // Complex address entry.
2089 unsigned N = DV.getNumAddrElements();
2090 unsigned i = 0;
2091 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2092 if (Entry.Loc.getOffset()) {
2093 i = 2;
2094 Asm->EmitDwarfRegOp(Entry.Loc);
2095 Asm->OutStreamer.AddComment("DW_OP_deref");
2096 Asm->EmitInt8(dwarf::DW_OP_deref);
2097 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2098 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2099 Asm->EmitSLEB128(DV.getAddrElement(1));
2100 } else {
2101 // If first address element is OpPlus then emit
2102 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2103 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2104 Asm->EmitDwarfRegOp(Loc);
2105 i = 2;
2106 }
2107 } else {
2108 Asm->EmitDwarfRegOp(Entry.Loc);
2109 }
2110
2111 // Emit remaining complex address elements.
2112 for (; i < N; ++i) {
2113 uint64_t Element = DV.getAddrElement(i);
2114 if (Element == DIBuilder::OpPlus) {
2115 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2116 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher50120762012-05-08 18:56:00 +00002117 } else if (Element == DIBuilder::OpDeref) {
Eric Christophera80f2d12012-05-08 21:24:39 +00002118 if (!Entry.Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002119 Asm->EmitInt8(dwarf::DW_OP_deref);
2120 } else
2121 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002122 }
Devang Patelc26f5442011-04-28 02:22:40 +00002123 }
Devang Patelc26f5442011-04-28 02:22:40 +00002124 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002125 // else ... ignore constant fp. There is not any good way to
2126 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002127 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002128 }
2129 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002130}
2131
2132/// EmitDebugARanges - Emit visible names into a debug aranges section.
2133///
2134void DwarfDebug::EmitDebugARanges() {
2135 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002136 Asm->OutStreamer.SwitchSection(
2137 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002138}
2139
Devang Patel2c4ceb12009-11-21 02:48:08 +00002140/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002141///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002142void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002143 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002144 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002145 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002146 unsigned char Size = Asm->getTargetData().getPointerSize();
2147 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002148 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002149 I != E; ++I) {
2150 if (*I)
2151 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002152 else
Devang Pateleac9c072010-04-27 19:46:33 +00002153 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002154 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002155}
2156
Devang Patel2c4ceb12009-11-21 02:48:08 +00002157/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002158///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002159void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002160 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002161 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002162 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002163 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002164 }
2165}
2166
Devang Patel2c4ceb12009-11-21 02:48:08 +00002167/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002168/// Section Header:
2169/// 1. length of section
2170/// 2. Dwarf version number
2171/// 3. address size.
2172///
2173/// Entries (one "entry" for each function that was inlined):
2174///
2175/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2176/// otherwise offset into __debug_str for regular function name.
2177/// 2. offset into __debug_str section for regular function name.
2178/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2179/// instances for the function.
2180///
2181/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2182/// inlined instance; the die_offset points to the inlined_subroutine die in the
2183/// __debug_info section, and the low_pc is the starting address for the
2184/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002185void DwarfDebug::emitDebugInlineInfo() {
Eric Christopherb83e2bb2012-03-02 02:11:47 +00002186 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002187 return;
2188
Devang Patel163a9f72010-05-10 22:49:55 +00002189 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002190 return;
2191
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002192 Asm->OutStreamer.SwitchSection(
2193 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002194
Chris Lattner233f52b2010-03-09 23:52:58 +00002195 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002196 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2197 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002198
Chris Lattnerc0215722010-04-04 19:25:43 +00002199 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002200
Chris Lattner233f52b2010-03-09 23:52:58 +00002201 Asm->OutStreamer.AddComment("Dwarf Version");
2202 Asm->EmitInt16(dwarf::DWARF_VERSION);
2203 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002204 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002205
Devang Patele9f8f5e2010-05-07 20:54:48 +00002206 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002207 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002208
Devang Patele9f8f5e2010-05-07 20:54:48 +00002209 const MDNode *Node = *I;
2210 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002211 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002212 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002213 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002214 StringRef LName = SP.getLinkageName();
2215 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002216
Chris Lattner233f52b2010-03-09 23:52:58 +00002217 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher50e26612012-03-02 01:57:52 +00002218 if (LName.empty())
2219 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2220 else
Chris Lattner6189ed12010-04-04 23:25:33 +00002221 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2222 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002223
Chris Lattner233f52b2010-03-09 23:52:58 +00002224 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002225 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002226 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002227
Devang Patel53bb5c92009-11-10 23:06:00 +00002228 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002229 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002230 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002231 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002232
Chris Lattner3f53c832010-04-04 18:52:31 +00002233 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002234 Asm->OutStreamer.EmitSymbolValue(LI->first,
2235 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002236 }
2237 }
2238
Chris Lattnerc0215722010-04-04 19:25:43 +00002239 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002240}