blob: cedeffe2f7429cf52b204f1614523d5e2af1b3dc [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);
Devang Patel5e06bb82011-04-22 23:10:17 +0000333 SPDie->addChild(Arg);
334 }
335 DIE *SPDeclDie = SPDie;
336 SPDie = new DIE(dwarf::DW_TAG_subprogram);
337 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
338 SPDeclDie);
339 SPCU->addDie(SPDie);
340 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000341 }
Devang Patel8aa61472010-07-07 22:20:57 +0000342 // Pick up abstract subprogram DIE.
343 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
344 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000345 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
346 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000347 SPCU->addDie(SPDie);
348 }
349
Devang Patel3cbee302011-04-12 22:53:02 +0000350 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
351 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
352 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
353 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000354 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
355 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000356 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000357
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000358 // Add name to the name table, we do this here because we're guaranteed
359 // to have concrete versions of our DW_TAG_subprogram nodes.
360 addSubprogramNames(SPCU, SP, SPDie);
361
Chris Lattnerd38fee82010-04-05 00:13:49 +0000362 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000363}
364
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000365/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000366/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000367DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
368 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000369 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
370 if (Scope->isAbstractScope())
371 return ScopeDIE;
372
Devang Patelbf47fdb2011-08-10 20:55:27 +0000373 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000374 if (Ranges.empty())
375 return 0;
376
Devang Patelbf47fdb2011-08-10 20:55:27 +0000377 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000378 if (Ranges.size() > 1) {
379 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000380 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000381 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000382 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000383 DebugRangeSymbols.size()
384 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000385 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000386 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000387 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
388 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000389 }
390 DebugRangeSymbols.push_back(NULL);
391 DebugRangeSymbols.push_back(NULL);
392 return ScopeDIE;
393 }
394
Devang Patelc3f5f782010-05-25 23:40:22 +0000395 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
396 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000397
Devang Patelc3f5f782010-05-25 23:40:22 +0000398 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000399
Chris Lattnerb7db7332010-03-09 01:58:53 +0000400 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
401 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000402
Devang Patel3cbee302011-04-12 22:53:02 +0000403 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
404 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000405
406 return ScopeDIE;
407}
408
Devang Patel2c4ceb12009-11-21 02:48:08 +0000409/// constructInlinedScopeDIE - This scope represents inlined body of
410/// a function. Construct DIE to represent this concrete inlined copy
411/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000412DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
413 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000414 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000415 assert(Ranges.empty() == false &&
416 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000417
Devang Patel26a92002011-07-27 00:34:13 +0000418 if (!Scope->getScopeNode())
419 return NULL;
420 DIScope DS(Scope->getScopeNode());
421 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000422 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
423 if (!OriginDIE) {
424 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
425 return NULL;
426 }
427
Devang Patelbf47fdb2011-08-10 20:55:27 +0000428 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000429 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
430 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000431
Devang Patel0afbf232010-07-08 22:39:20 +0000432 if (StartLabel == 0 || EndLabel == 0) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000433 llvm_unreachable("Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000434 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000435 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000436 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000437 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000438 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000439
Devang Pateld96efb82011-05-05 17:54:26 +0000440 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000441 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
442 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000443
Devang Patel26a92002011-07-27 00:34:13 +0000444 if (Ranges.size() > 1) {
445 // .debug_range section has not been laid out yet. Emit offset in
446 // .debug_range as a uint, size 4, for now. emitDIE will handle
447 // DW_AT_ranges appropriately.
448 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000449 DebugRangeSymbols.size()
450 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000451 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000452 RE = Ranges.end(); RI != RE; ++RI) {
453 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
454 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
455 }
456 DebugRangeSymbols.push_back(NULL);
457 DebugRangeSymbols.push_back(NULL);
458 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000459 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
460 StartLabel);
461 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
462 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000463 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000464
465 InlinedSubprogramDIEs.insert(OriginDIE);
466
467 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000468 //.debug_inlined section specification does not clearly state how
469 // to emit inlined scope that is split into multiple instruction ranges.
470 // For now, use first instruction range and emit low_pc/high_pc pair and
471 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000472 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000473 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000474
475 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000476 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000477 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000478 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000479 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000480
Devang Patel53bb5c92009-11-10 23:06:00 +0000481 DILocation DL(Scope->getInlinedAt());
Eric Christopher08212002012-03-26 21:38:38 +0000482 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
483 GetOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patel3cbee302011-04-12 22:53:02 +0000484 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000485
Eric Christopher309bedd2011-12-04 06:02:38 +0000486 // Add name to the name table, we do this here because we're guaranteed
487 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
488 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
489
Devang Patel53bb5c92009-11-10 23:06:00 +0000490 return ScopeDIE;
491}
492
Devang Patel2c4ceb12009-11-21 02:48:08 +0000493/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000494DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000495 if (!Scope || !Scope->getScopeNode())
496 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000497
Nick Lewycky746cb672011-10-26 22:55:33 +0000498 SmallVector<DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000499
500 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000501 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000502 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
503 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000504 if (DIE *Arg =
505 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel0478c152011-03-01 22:58:55 +0000506 Children.push_back(Arg);
507
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000508 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000509 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000510 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000511 if (DIE *Variable =
512 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000513 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000514 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000515 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000516 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000517 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000518 DIScope DS(Scope->getScopeNode());
519 DIE *ScopeDIE = NULL;
520 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000521 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000522 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000523 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000524 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000525 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000526 // Note down abstract DIE.
527 if (ScopeDIE)
528 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
529 }
Devang Patel3c91b052010-03-08 20:52:55 +0000530 else
Devang Pateld3024342011-08-15 22:24:32 +0000531 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000532 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000533 else {
534 // There is no need to emit empty lexical block DIE.
535 if (Children.empty())
536 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000537 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000538 }
539
Devang Patelaead63c2010-03-29 22:59:58 +0000540 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000541
Devang Patel5bc9fec2011-02-19 01:31:27 +0000542 // Add children
543 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
544 E = Children.end(); I != E; ++I)
545 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000546
Jim Grosbach1e20b962010-07-21 21:21:52 +0000547 if (DS.isSubprogram())
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000548 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000549
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000550 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000551}
552
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000553/// GetOrCreateSourceID - Look up the source id with the given directory and
554/// source file names. If none currently exists, create a new id and insert it
555/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
556/// maps as well.
Devang Patel23670e52011-03-24 20:30:50 +0000557unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
558 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000559 // If FE did not provide a file name, then assume stdin.
560 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000561 return GetOrCreateSourceID("<stdin>", StringRef());
562
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000563 // TODO: this might not belong here. See if we can factor this better.
564 if (DirName == CompilationDir)
565 DirName = "";
566
Nick Lewycky44d798d2011-10-17 23:05:28 +0000567 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel1905a182010-09-16 20:57:49 +0000568
Benjamin Kramer74612c22012-03-11 14:56:26 +0000569 // We look up the file/dir pair by concatenating them with a zero byte.
570 SmallString<128> NamePair;
571 NamePair += DirName;
572 NamePair += '\0'; // Zero bytes are not allowed in paths.
573 NamePair += FileName;
574
575 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
576 if (Ent.getValue() != SrcId)
577 return Ent.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000578
Rafael Espindola5c055632010-11-18 02:04:25 +0000579 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer74612c22012-03-11 14:56:26 +0000580 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000581
582 return SrcId;
583}
584
Jim Grosbach1e20b962010-07-21 21:21:52 +0000585/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000586/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000587CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000588 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000589 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000590 CompilationDir = DIUnit.getDirectory();
591 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000592
593 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopheredf8bc82012-09-10 23:34:00 +0000594 CompileUnit *NewCU = new CompileUnit(ID, DIUnit.getLanguage(), Die,
595 Asm, this);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000596 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000597 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
598 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000599 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher6635cad2012-08-01 18:19:01 +0000600 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
601 // into an entity.
602 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000603 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000604 // compile unit in debug_line section.
Rafael Espindola2241e512012-06-22 13:24:07 +0000605 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindola597a7662011-05-04 17:44:06 +0000606 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000607 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000608 else
Devang Patel3cbee302011-04-12 22:53:02 +0000609 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000610
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000611 if (!CompilationDir.empty())
612 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000613 if (DIUnit.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000614 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000615
Devang Patel65dbc902009-11-25 17:36:49 +0000616 StringRef Flags = DIUnit.getFlags();
617 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000618 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000619
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000620 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000621 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000622 dwarf::DW_FORM_data1, RVer);
623
Devang Patel163a9f72010-05-10 22:49:55 +0000624 if (!FirstCU)
625 FirstCU = NewCU;
626 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000627 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000628}
629
Devang Patel163a9f72010-05-10 22:49:55 +0000630/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000631void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
632 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000633 CompileUnit *&CURef = SPMap[N];
634 if (CURef)
635 return;
636 CURef = TheCU;
637
Devang Patele4b27562009-08-28 23:24:31 +0000638 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000639 if (!SP.isDefinition())
640 // This is a method declaration which will be handled while constructing
641 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000642 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000643
Devang Pateldbc64af2011-08-15 17:24:54 +0000644 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000645
646 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000647 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000648
649 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000650 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000651
Devang Patel13e16b62009-06-26 01:49:18 +0000652 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000653}
654
Devang Patel02e603f2011-08-15 23:47:24 +0000655/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
656/// as llvm.dbg.enum and llvm.dbg.ty
657void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000658 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
659 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
660 const MDNode *N = NMD->getOperand(i);
661 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
662 constructSubprogramDIE(CU, N);
663 }
664
665 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
666 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
667 const MDNode *N = NMD->getOperand(i);
668 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000669 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000670 }
671
Devang Patel02e603f2011-08-15 23:47:24 +0000672 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
673 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
674 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000675 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
676 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000677 }
678
679 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
680 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
681 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000682 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
683 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000684 }
685}
686
687/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
688/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
689bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
690 DebugInfoFinder DbgFinder;
691 DbgFinder.processModule(*M);
692
693 bool HasDebugInfo = false;
694 // Scan all the compile-units to see if there are any marked as the main
695 // unit. If not, we do not generate debug info.
696 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
697 E = DbgFinder.compile_unit_end(); I != E; ++I) {
698 if (DICompileUnit(*I).isMain()) {
699 HasDebugInfo = true;
700 break;
701 }
702 }
703 if (!HasDebugInfo) return false;
704
705 // Create all the compile unit DIEs.
706 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
707 E = DbgFinder.compile_unit_end(); I != E; ++I)
708 constructCompileUnit(*I);
709
710 // Create DIEs for each global variable.
711 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
712 E = DbgFinder.global_variable_end(); I != E; ++I) {
713 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000714 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000715 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000716 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000717
Devang Patel02e603f2011-08-15 23:47:24 +0000718 // Create DIEs for each subprogram.
719 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
720 E = DbgFinder.subprogram_end(); I != E; ++I) {
721 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000722 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
723 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000724 }
725
726 return HasDebugInfo;
727}
728
Devang Patel2c4ceb12009-11-21 02:48:08 +0000729/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000730/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000731/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000732void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000733 if (DisableDebugInfoPrinting)
734 return;
735
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000736 // If module has named metadata anchors then use them, otherwise scan the
737 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000738 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
739 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000740 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
741 DICompileUnit CUNode(CU_Nodes->getOperand(i));
742 CompileUnit *CU = constructCompileUnit(CUNode);
743 DIArray GVs = CUNode.getGlobalVariables();
744 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000745 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000746 DIArray SPs = CUNode.getSubprograms();
747 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
748 constructSubprogramDIE(CU, SPs.getElement(i));
749 DIArray EnumTypes = CUNode.getEnumTypes();
750 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
751 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
752 DIArray RetainedTypes = CUNode.getRetainedTypes();
753 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
754 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
755 }
Devang Patel02e603f2011-08-15 23:47:24 +0000756 } else if (!collectLegacyDebugInfo(M))
757 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000758
Devang Patel02e603f2011-08-15 23:47:24 +0000759 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000760
Chris Lattnerd850ac72010-04-05 02:19:28 +0000761 // Tell MMI that we have debug info.
762 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000763
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000764 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000765 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000766
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000767 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000768 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000769}
770
Devang Patel2c4ceb12009-11-21 02:48:08 +0000771/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000772///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000773void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000774 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000775 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000776 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000777
Devang Patel94c7ddb2011-08-16 22:09:43 +0000778 // Collect info for variables that were optimized out.
779 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
780 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
781 DICompileUnit TheCU(CU_Nodes->getOperand(i));
782 DIArray Subprograms = TheCU.getSubprograms();
783 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
784 DISubprogram SP(Subprograms.getElement(i));
785 if (ProcessedSPNodes.count(SP) != 0) continue;
786 if (!SP.Verify()) continue;
787 if (!SP.isDefinition()) continue;
Devang Patel93d39be2011-08-19 23:28:12 +0000788 DIArray Variables = SP.getVariables();
789 if (Variables.getNumElements() == 0) continue;
790
Devang Patel94c7ddb2011-08-16 22:09:43 +0000791 LexicalScope *Scope =
792 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
793 DeadFnScopeMap[SP] = Scope;
794
795 // Construct subprogram DIE and add variables DIEs.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000796 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky746cb672011-10-26 22:55:33 +0000797 assert(SPCU && "Unable to find Compile Unit!");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000798 constructSubprogramDIE(SPCU, SP);
799 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel93d39be2011-08-19 23:28:12 +0000800 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
801 DIVariable DV(Variables.getElement(vi));
802 if (!DV.Verify()) continue;
803 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000804 if (DIE *VariableDIE =
Devang Patel93d39be2011-08-19 23:28:12 +0000805 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Patel94c7ddb2011-08-16 22:09:43 +0000806 ScopeDIE->addChild(VariableDIE);
807 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000808 }
809 }
810 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000811
Devang Patel53bb5c92009-11-10 23:06:00 +0000812 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
813 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
814 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
815 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000816 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000817 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000818 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
819 AE = AbstractSPDies.end(); AI != AE; ++AI) {
820 DIE *ISP = AI->second;
821 if (InlinedSubprogramDIEs.count(ISP))
822 continue;
823 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
824 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000825
Eric Christopher6635cad2012-08-01 18:19:01 +0000826 // Emit DW_AT_containing_type attribute to connect types with their
827 // vtable holding type.
Devang Pateldbc64af2011-08-15 17:24:54 +0000828 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
829 CUE = CUMap.end(); CUI != CUE; ++CUI) {
830 CompileUnit *TheCU = CUI->second;
831 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000832 }
833
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000834 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000835 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000836 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000837 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000838 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000839
840 // End text sections.
841 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000842 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000843 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000844 }
845
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000846 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000847 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000848
849 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000850 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000851
852 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000853 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000854
Eric Christopher9d9f5a52012-08-23 07:32:06 +0000855 // Emit info into the dwarf accelerator table sections.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000856 if (useDwarfAccelTables()) {
Eric Christopher09ac3d82011-11-07 09:24:32 +0000857 emitAccelNames();
858 emitAccelObjC();
859 emitAccelNamespaces();
860 emitAccelTypes();
861 }
862
Devang Patel193f7202009-11-24 01:14:22 +0000863 // Emit info into a debug pubtypes section.
Eric Christopher360f0062012-08-23 07:10:56 +0000864 // TODO: When we don't need the option anymore we can
865 // remove all of the code that adds to the table.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000866 if (useDarwinGDBCompat())
Eric Christopher360f0062012-08-23 07:10:56 +0000867 emitDebugPubTypes();
Devang Patel193f7202009-11-24 01:14:22 +0000868
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000869 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000870 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000871
872 // Emit info into a debug aranges section.
873 EmitDebugARanges();
874
875 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000876 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000877
878 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000879 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000880
881 // Emit inline info.
Eric Christopher9eb1a942012-08-23 07:32:02 +0000882 // TODO: When we don't need the option anymore we
883 // can remove all of the code that this section
884 // depends upon.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000885 if (useDarwinGDBCompat())
Eric Christopher9eb1a942012-08-23 07:32:02 +0000886 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000887
Eric Christopher7550f7d2012-03-02 00:30:24 +0000888 // Emit info into a debug str section.
889 emitDebugStr();
890
Devang Patele9a1cca2010-08-02 17:32:15 +0000891 // clean up.
892 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000893 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000894 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
895 E = CUMap.end(); I != E; ++I)
896 delete I->second;
897 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000898}
899
Devang Patel53bb5c92009-11-10 23:06:00 +0000900/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000901DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000902 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000903 LLVMContext &Ctx = DV->getContext();
904 // More then one inlined variable corresponds to one abstract variable.
905 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000906 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000907 if (AbsDbgVariable)
908 return AbsDbgVariable;
909
Devang Patelbf47fdb2011-08-10 20:55:27 +0000910 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000911 if (!Scope)
912 return NULL;
913
Devang Patel5a1a67c2011-08-15 19:01:20 +0000914 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000915 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000916 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000917 return AbsDbgVariable;
918}
919
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000920/// addCurrentFnArgument - If Var is a current function argument then add
921/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000922bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000923 DbgVariable *Var, LexicalScope *Scope) {
924 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000925 return false;
926 DIVariable DV = Var->getVariable();
927 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
928 return false;
929 unsigned ArgNo = DV.getArgNumber();
930 if (ArgNo == 0)
931 return false;
932
Devang Patelcb3a6572011-03-03 20:02:02 +0000933 size_t Size = CurrentFnArguments.size();
934 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000935 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000936 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000937 // arguments does the function have at source level.
938 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000939 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000940 CurrentFnArguments[ArgNo - 1] = Var;
941 return true;
942}
943
Devang Patelee432862010-05-20 19:57:06 +0000944/// collectVariableInfoFromMMITable - Collect variable information from
945/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000946void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000947DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000948 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000949 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
950 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
951 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000952 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000953 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000954 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000955 DIVariable DV(Var);
956 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000957
Devang Patelbf47fdb2011-08-10 20:55:27 +0000958 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000959
Devang Patelfb0ee432009-11-10 23:20:04 +0000960 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000961 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000962 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000963
Devang Patel26c1e562010-05-20 16:36:41 +0000964 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000965 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000966 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000967 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000968 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000969 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000970 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000971 }
Devang Patelee432862010-05-20 19:57:06 +0000972}
Devang Patel90a48ad2010-03-15 18:33:46 +0000973
Jim Grosbach1e20b962010-07-21 21:21:52 +0000974/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000975/// DBG_VALUE instruction, is in a defined reg.
976static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000977 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000978 return MI->getNumOperands() == 3 &&
979 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
980 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000981}
982
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000983/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000984/// at MI.
985static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
986 const MCSymbol *FLabel,
987 const MCSymbol *SLabel,
988 const MachineInstr *MI) {
989 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
990
991 if (MI->getNumOperands() != 3) {
992 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
993 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
994 }
995 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
996 MachineLocation MLoc;
997 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
998 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
999 }
1000 if (MI->getOperand(0).isImm())
1001 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1002 if (MI->getOperand(0).isFPImm())
1003 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1004 if (MI->getOperand(0).isCImm())
1005 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1006
Craig Topper5e25ee82012-02-05 08:31:47 +00001007 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +00001008}
1009
Devang Patelbf47fdb2011-08-10 20:55:27 +00001010/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001011void
Devang Patel78e127d2010-06-25 22:07:34 +00001012DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1013 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001014
Devang Patelee432862010-05-20 19:57:06 +00001015 /// collection info from MMI table.
1016 collectVariableInfoFromMMITable(MF, Processed);
1017
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001018 for (SmallVectorImpl<const MDNode*>::const_iterator
1019 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1020 ++UVI) {
1021 const MDNode *Var = *UVI;
1022 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001023 continue;
1024
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001025 // History contains relevant DBG_VALUE instructions for Var and instructions
1026 // clobbering it.
1027 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1028 if (History.empty())
1029 continue;
1030 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001031
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001032 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001033 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001034 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1035 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001036 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001037 else {
1038 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001039 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001040 else {
1041 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001042 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001043 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001044 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001045 }
1046 }
Devang Patelee432862010-05-20 19:57:06 +00001047 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001048 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001049 continue;
1050
1051 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001052 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001053 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1054 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001055 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001056 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001057 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001058 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001059
1060 // Simple ranges that are fully coalesced.
1061 if (History.size() <= 1 || (History.size() == 2 &&
1062 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001063 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001064 continue;
1065 }
1066
1067 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001068 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001069
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001070 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1071 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1072 const MachineInstr *Begin = *HI;
1073 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001074
Devang Patel4ada1d72011-06-01 23:00:17 +00001075 // Check if DBG_VALUE is truncating a range.
1076 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1077 && !Begin->getOperand(0).getReg())
1078 continue;
1079
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001080 // Compute the range for a register location.
1081 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1082 const MCSymbol *SLabel = 0;
1083
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001084 if (HI + 1 == HE)
1085 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001086 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001087 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001088 else {
1089 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001090 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1091 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001092 if (End->isDebugValue())
1093 SLabel = getLabelBeforeInsn(End);
1094 else {
1095 // End is a normal instruction clobbering the range.
1096 SLabel = getLabelAfterInsn(End);
1097 assert(SLabel && "Forgot label after clobber instruction");
1098 ++HI;
1099 }
1100 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001101
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001102 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001103 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1104 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001105 }
1106 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001107 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001108
1109 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001110 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1111 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1112 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1113 DIVariable DV(Variables.getElement(i));
1114 if (!DV || !DV.Verify() || !Processed.insert(DV))
1115 continue;
1116 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1117 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001118 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001119}
Devang Patel98e1cac2010-05-14 21:01:35 +00001120
Devang Patelc3f5f782010-05-25 23:40:22 +00001121/// getLabelBeforeInsn - Return Label preceding the instruction.
1122const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001123 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1124 assert(Label && "Didn't insert label before instruction");
1125 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001126}
1127
1128/// getLabelAfterInsn - Return Label immediately following the instruction.
1129const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001130 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001131}
1132
Devang Patelcbbe2872010-10-26 17:49:02 +00001133/// beginInstruction - Process beginning of an instruction.
1134void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001135 // Check if source location changes, but ignore DBG_VALUE locations.
1136 if (!MI->isDebugValue()) {
1137 DebugLoc DL = MI->getDebugLoc();
1138 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001139 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001140 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001141 if (DL == PrologEndLoc) {
1142 Flags |= DWARF2_FLAG_PROLOGUE_END;
1143 PrologEndLoc = DebugLoc();
1144 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001145 if (PrologEndLoc.isUnknown())
1146 Flags |= DWARF2_FLAG_IS_STMT;
1147
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001148 if (!DL.isUnknown()) {
1149 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001150 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001151 } else
Devang Patel4243e672011-05-11 19:22:19 +00001152 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001153 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001154 }
Devang Patelaead63c2010-03-29 22:59:58 +00001155
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001156 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001157 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1158 LabelsBeforeInsn.find(MI);
1159
1160 // No label needed.
1161 if (I == LabelsBeforeInsn.end())
1162 return;
1163
1164 // Label already assigned.
1165 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001166 return;
Devang Patel553881b2010-03-29 17:20:31 +00001167
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001168 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001169 PrevLabel = MMI->getContext().CreateTempSymbol();
1170 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001171 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001172 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001173}
1174
Devang Patelcbbe2872010-10-26 17:49:02 +00001175/// endInstruction - Process end of an instruction.
1176void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001177 // Don't create a new label after DBG_VALUE instructions.
1178 // They don't generate code.
1179 if (!MI->isDebugValue())
1180 PrevLabel = 0;
1181
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001182 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1183 LabelsAfterInsn.find(MI);
1184
1185 // No label needed.
1186 if (I == LabelsAfterInsn.end())
1187 return;
1188
1189 // Label already assigned.
1190 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001191 return;
1192
1193 // We need a label after this instruction.
1194 if (!PrevLabel) {
1195 PrevLabel = MMI->getContext().CreateTempSymbol();
1196 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001197 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001198 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001199}
1200
Jim Grosbach1e20b962010-07-21 21:21:52 +00001201/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001202/// Each LexicalScope has first instruction and last instruction to mark
1203/// beginning and end of a scope respectively. Create an inverse map that list
1204/// scopes starts (and ends) with an instruction. One instruction may start (or
1205/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001206void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001207 SmallVector<LexicalScope *, 4> WorkList;
1208 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001209 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001210 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001211
Devang Patelbf47fdb2011-08-10 20:55:27 +00001212 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001213 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001214 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001215 SE = Children.end(); SI != SE; ++SI)
1216 WorkList.push_back(*SI);
1217
Devang Patel53bb5c92009-11-10 23:06:00 +00001218 if (S->isAbstractScope())
1219 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001220
Devang Patelbf47fdb2011-08-10 20:55:27 +00001221 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001222 if (Ranges.empty())
1223 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001224 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001225 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001226 assert(RI->first && "InsnRange does not have first instruction!");
1227 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001228 requestLabelBeforeInsn(RI->first);
1229 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001230 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001231 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001232}
1233
Devang Patela3f48672011-05-09 22:14:49 +00001234/// getScopeNode - Get MDNode for DebugLoc's scope.
1235static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1236 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1237 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1238 return DL.getScope(Ctx);
1239}
1240
Devang Patel4243e672011-05-11 19:22:19 +00001241/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
Eric Christopher6126a1e2012-04-03 00:43:49 +00001242/// line number info for the function.
Devang Patel4243e672011-05-11 19:22:19 +00001243static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1244 const MDNode *Scope = getScopeNode(DL, Ctx);
1245 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher6126a1e2012-04-03 00:43:49 +00001246 if (SP.Verify()) {
1247 // Check for number of operands since the compatibility is
1248 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001249 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-04-03 00:43:49 +00001250 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1251 else
1252 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1253 }
1254
Devang Patel4243e672011-05-11 19:22:19 +00001255 return DebugLoc();
1256}
1257
Devang Patel2c4ceb12009-11-21 02:48:08 +00001258/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001259/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001260void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001261 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001262 LScopes.initialize(*MF);
1263 if (LScopes.empty()) return;
1264 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001265
Devang Pateleac9c072010-04-27 19:46:33 +00001266 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1267 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001268 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001269 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001270
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001271 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1272
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001273 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001274 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1275 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1276
Devang Patelb2b31a62010-05-26 19:37:24 +00001277 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001278 I != E; ++I) {
1279 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001280 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1281 II != IE; ++II) {
1282 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001283
Devang Patelb2b31a62010-05-26 19:37:24 +00001284 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001285 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001286
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001287 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001288 const MDNode *Var =
1289 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001290
1291 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001292 if (isDbgValueInDefinedReg(MI))
1293 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1294
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001295 // Check the history of this variable.
1296 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1297 if (History.empty()) {
1298 UserVariables.push_back(Var);
1299 // The first mention of a function argument gets the FunctionBeginSym
1300 // label, so arguments are visible when breaking at function entry.
1301 DIVariable DV(Var);
1302 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1303 DISubprogram(getDISubprogram(DV.getContext()))
1304 .describes(MF->getFunction()))
1305 LabelsBeforeInsn[MI] = FunctionBeginSym;
1306 } else {
1307 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1308 const MachineInstr *Prev = History.back();
1309 if (Prev->isDebugValue()) {
1310 // Coalesce identical entries at the end of History.
1311 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001312 Prev->isIdenticalTo(History[History.size() - 2])) {
1313 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1314 << "\t" << *Prev
1315 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001316 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001317 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001318
1319 // Terminate old register assignments that don't reach MI;
1320 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1321 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1322 isDbgValueInDefinedReg(Prev)) {
1323 // Previous register assignment needs to terminate at the end of
1324 // its basic block.
1325 MachineBasicBlock::const_iterator LastMI =
1326 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001327 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001328 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001329 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1330 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001331 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001332 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001333 else {
1334 // Terminate after LastMI.
1335 History.push_back(LastMI);
1336 }
1337 }
1338 }
1339 }
1340 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001341 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001342 // Not a DBG_VALUE instruction.
1343 if (!MI->isLabel())
1344 AtBlockEntry = false;
1345
Devang Patel4243e672011-05-11 19:22:19 +00001346 // First known non DBG_VALUE location marks beginning of function
1347 // body.
1348 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1349 PrologEndLoc = MI->getDebugLoc();
1350
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001351 // Check if the instruction clobbers any registers with debug vars.
1352 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1353 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1354 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1355 continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001356 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1357 AI.isValid(); ++AI) {
1358 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001359 const MDNode *Var = LiveUserVar[Reg];
1360 if (!Var)
1361 continue;
1362 // Reg is now clobbered.
1363 LiveUserVar[Reg] = 0;
1364
1365 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001366 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1367 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001368 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001369 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1370 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001371 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001372 const MachineInstr *Prev = History.back();
1373 // Sanity-check: Register assignments are terminated at the end of
1374 // their block.
1375 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1376 continue;
1377 // Is the variable still in Reg?
1378 if (!isDbgValueInDefinedReg(Prev) ||
1379 Prev->getOperand(0).getReg() != Reg)
1380 continue;
1381 // Var is clobbered. Make sure the next instruction gets a label.
1382 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001383 }
1384 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001385 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001386 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001387 }
1388
1389 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1390 I != E; ++I) {
1391 SmallVectorImpl<const MachineInstr*> &History = I->second;
1392 if (History.empty())
1393 continue;
1394
1395 // Make sure the final register assignments are terminated.
1396 const MachineInstr *Prev = History.back();
1397 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1398 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001399 MachineBasicBlock::const_iterator LastMI =
1400 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001401 if (LastMI == PrevMBB->end())
1402 // Drop DBG_VALUE for empty range.
1403 History.pop_back();
1404 else {
1405 // Terminate after LastMI.
1406 History.push_back(LastMI);
1407 }
1408 }
1409 // Request labels for the full history.
1410 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1411 const MachineInstr *MI = History[i];
1412 if (MI->isDebugValue())
1413 requestLabelBeforeInsn(MI);
1414 else
1415 requestLabelAfterInsn(MI);
1416 }
1417 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001418
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001419 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001420 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001421
1422 // Record beginning of function.
1423 if (!PrologEndLoc.isUnknown()) {
1424 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1425 MF->getFunction()->getContext());
1426 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1427 FnStartDL.getScope(MF->getFunction()->getContext()),
Eric Christopher5cf55e12012-07-12 23:30:25 +00001428 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0);
Devang Patel4243e672011-05-11 19:22:19 +00001429 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001430}
1431
Devang Patelbf47fdb2011-08-10 20:55:27 +00001432void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1433// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1434 ScopeVariables[LS].push_back(Var);
1435// Vars.push_back(Var);
1436}
1437
Devang Patel2c4ceb12009-11-21 02:48:08 +00001438/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001439///
Chris Lattnereec791a2010-01-26 23:18:02 +00001440void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001441 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001442
Devang Patelbf47fdb2011-08-10 20:55:27 +00001443 // Define end label for subprogram.
1444 FunctionEndSym = Asm->GetTempSymbol("func_end",
1445 Asm->getFunctionNumber());
1446 // Assumes in correct section after the entry point.
1447 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1448
1449 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1450 collectVariableInfo(MF, ProcessedVars);
1451
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001452 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001453 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001454 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001455
Devang Patelbf47fdb2011-08-10 20:55:27 +00001456 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001457 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1458 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1459 LexicalScope *AScope = AList[i];
1460 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001461 if (SP.Verify()) {
1462 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001463 DIArray Variables = SP.getVariables();
1464 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1465 DIVariable DV(Variables.getElement(i));
1466 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1467 continue;
Alexey Samsonovb67bd332012-07-06 08:45:08 +00001468 // Check that DbgVariable for DV wasn't created earlier, when
1469 // findAbstractVariable() was called for inlined instance of DV.
1470 LLVMContext &Ctx = DV->getContext();
1471 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1472 if (AbstractVariables.lookup(CleanDV))
1473 continue;
Devang Patel93d39be2011-08-19 23:28:12 +00001474 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1475 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001476 }
1477 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001478 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001479 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001480 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001481
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001482 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001483
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001484 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001485 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001486
Devang Patelbf47fdb2011-08-10 20:55:27 +00001487 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1488 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001489
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001490 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001491 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1492 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1493 DeleteContainerPointers(I->second);
1494 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001495 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001496 UserVariables.clear();
1497 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001498 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001499 LabelsBeforeInsn.clear();
1500 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001501 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001502}
1503
Chris Lattnerc6087842010-03-09 04:54:43 +00001504/// recordSourceLine - Register a source line with debug info. Returns the
1505/// unique label that was emitted and which provides correspondence to
1506/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001507void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1508 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001509 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001510 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001511 unsigned Src = 1;
1512 if (S) {
1513 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001514
Dan Gohman1cc0d622010-05-05 23:41:32 +00001515 if (Scope.isCompileUnit()) {
1516 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001517 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001518 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001519 } else if (Scope.isFile()) {
1520 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001521 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001522 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001523 } else if (Scope.isSubprogram()) {
1524 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001525 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001526 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001527 } else if (Scope.isLexicalBlockFile()) {
1528 DILexicalBlockFile DBF(S);
1529 Fn = DBF.getFilename();
1530 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001531 } else if (Scope.isLexicalBlock()) {
1532 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001533 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001534 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001535 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001536 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001537
Devang Patel23670e52011-03-24 20:30:50 +00001538 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001539 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001540 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001541}
1542
Bill Wendling829e67b2009-05-20 23:22:40 +00001543//===----------------------------------------------------------------------===//
1544// Emit Methods
1545//===----------------------------------------------------------------------===//
1546
Devang Patel2c4ceb12009-11-21 02:48:08 +00001547/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001548///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001549unsigned
1550DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001551 // Get the children.
1552 const std::vector<DIE *> &Children = Die->getChildren();
1553
Bill Wendling94d04b82009-05-20 23:21:38 +00001554 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001555 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001556
1557 // Get the abbreviation for this DIE.
1558 unsigned AbbrevNumber = Die->getAbbrevNumber();
1559 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1560
1561 // Set DIE offset
1562 Die->setOffset(Offset);
1563
1564 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001565 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001566
1567 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1568 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1569
1570 // Size the DIE attribute values.
1571 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1572 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001573 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001574
1575 // Size the DIE children if any.
1576 if (!Children.empty()) {
1577 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1578 "Children flag not set");
1579
1580 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001581 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001582
1583 // End of children marker.
1584 Offset += sizeof(int8_t);
1585 }
1586
1587 Die->setSize(Offset - Die->getOffset());
1588 return Offset;
1589}
1590
Devang Patel2c4ceb12009-11-21 02:48:08 +00001591/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001592///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001593void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001594 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1595 E = CUMap.end(); I != E; ++I) {
1596 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001597 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001598 sizeof(int32_t) + // Length of Compilation Unit Info
1599 sizeof(int16_t) + // DWARF version number
1600 sizeof(int32_t) + // Offset Into Abbrev. Section
1601 sizeof(int8_t); // Pointer Size (in bytes)
1602 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001603 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001604}
1605
Chris Lattner9c69e285532010-04-04 22:59:04 +00001606/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1607/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001608void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001609 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001610
Bill Wendling94d04b82009-05-20 23:21:38 +00001611 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001612 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001613 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001614 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001615 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001616 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001617
Chris Lattner9c69e285532010-04-04 22:59:04 +00001618 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001619 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001620
Devang Patelaf608bd2010-08-24 00:06:12 +00001621 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001622 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
Chris Lattner11b8f302010-04-04 23:02:02 +00001623 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001624 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001625 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001626 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1627 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001628
Devang Patelc3f5f782010-05-25 23:40:22 +00001629 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1630 "section_debug_loc");
1631
Chris Lattner9c69e285532010-04-04 22:59:04 +00001632 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001633 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001634}
1635
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001636/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001637///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001638void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001639 // Get the abbreviation for this DIE.
1640 unsigned AbbrevNumber = Die->getAbbrevNumber();
1641 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1642
Bill Wendling94d04b82009-05-20 23:21:38 +00001643 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001644 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001645 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1646 Twine::utohexstr(Die->getOffset()) + ":0x" +
1647 Twine::utohexstr(Die->getSize()) + " " +
1648 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001649 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001650
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001651 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001652 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1653
1654 // Emit the DIE attribute values.
1655 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1656 unsigned Attr = AbbrevData[i].getAttribute();
1657 unsigned Form = AbbrevData[i].getForm();
1658 assert(Form && "Too many attributes for DIE (check abbreviation)");
1659
Chris Lattner3f53c832010-04-04 18:52:31 +00001660 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001661 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001662
Bill Wendling94d04b82009-05-20 23:21:38 +00001663 switch (Attr) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001664 case dwarf::DW_AT_abstract_origin: {
1665 DIEEntry *E = cast<DIEEntry>(Values[i]);
1666 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001667 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001668 Asm->EmitInt32(Addr);
1669 break;
1670 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001671 case dwarf::DW_AT_ranges: {
1672 // DW_AT_range Value encodes offset in debug_range section.
1673 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001674
Nick Lewyckyffccd922012-06-22 01:25:12 +00001675 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001676 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1677 V->getValue(),
1678 4);
1679 } else {
1680 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1681 V->getValue(),
1682 DwarfDebugRangeSectionSym,
1683 4);
1684 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001685 break;
1686 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001687 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-06-22 01:25:12 +00001688 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1689 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1690 Asm->EmitLabelReference(L->getValue(), 4);
1691 else
1692 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1693 } else {
Devang Patelc3f5f782010-05-25 23:40:22 +00001694 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00001695 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001696 break;
1697 }
Devang Patel2a361602010-09-29 19:08:08 +00001698 case dwarf::DW_AT_accessibility: {
1699 if (Asm->isVerbose()) {
1700 DIEInteger *V = cast<DIEInteger>(Values[i]);
1701 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1702 }
1703 Values[i]->EmitValue(Asm, Form);
1704 break;
1705 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001706 default:
1707 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001708 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001709 break;
1710 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001711 }
1712
1713 // Emit the DIE children if any.
1714 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1715 const std::vector<DIE *> &Children = Die->getChildren();
1716
1717 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001718 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001719
Chris Lattner3f53c832010-04-04 18:52:31 +00001720 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001721 Asm->OutStreamer.AddComment("End Of Children Mark");
1722 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001723 }
1724}
1725
Devang Patel8a241142009-12-09 18:24:21 +00001726/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001727///
Devang Patel8a241142009-12-09 18:24:21 +00001728void DwarfDebug::emitDebugInfo() {
1729 // Start debug info section.
1730 Asm->OutStreamer.SwitchSection(
1731 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001732 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1733 E = CUMap.end(); I != E; ++I) {
1734 CompileUnit *TheCU = I->second;
1735 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001736
Devang Patel163a9f72010-05-10 22:49:55 +00001737 // Emit the compile units header.
1738 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1739 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001740
Devang Patel163a9f72010-05-10 22:49:55 +00001741 // Emit size of content not including length itself
1742 unsigned ContentSize = Die->getSize() +
1743 sizeof(int16_t) + // DWARF version number
1744 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001745 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001746
Devang Patel163a9f72010-05-10 22:49:55 +00001747 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1748 Asm->EmitInt32(ContentSize);
1749 Asm->OutStreamer.AddComment("DWARF version number");
1750 Asm->EmitInt16(dwarf::DWARF_VERSION);
1751 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1752 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1753 DwarfAbbrevSectionSym);
1754 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1755 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001756
Devang Patel163a9f72010-05-10 22:49:55 +00001757 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001758 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1759 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001760}
1761
Devang Patel2c4ceb12009-11-21 02:48:08 +00001762/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001763///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001764void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001765 // Check to see if it is worth the effort.
1766 if (!Abbreviations.empty()) {
1767 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001768 Asm->OutStreamer.SwitchSection(
1769 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001770
Chris Lattnerc0215722010-04-04 19:25:43 +00001771 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001772
1773 // For each abbrevation.
1774 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1775 // Get abbreviation data
1776 const DIEAbbrev *Abbrev = Abbreviations[i];
1777
1778 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001779 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001780
1781 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001782 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001783 }
1784
1785 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001786 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001787
Chris Lattnerc0215722010-04-04 19:25:43 +00001788 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001789 }
1790}
1791
Devang Patel2c4ceb12009-11-21 02:48:08 +00001792/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001793/// the line matrix.
1794///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001795void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001796 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001797 Asm->OutStreamer.AddComment("Extended Op");
1798 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001799
Chris Lattner233f52b2010-03-09 23:52:58 +00001800 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001801 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001802 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1803 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1804
1805 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001806
Chris Lattnerc0215722010-04-04 19:25:43 +00001807 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001808 Asm->getTargetData().getPointerSize(),
1809 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001810
1811 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001812 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1813 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001814 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001815 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001816}
1817
Eric Christopher09ac3d82011-11-07 09:24:32 +00001818/// emitAccelNames - Emit visible names into a hashed accelerator table
1819/// section.
1820void DwarfDebug::emitAccelNames() {
1821 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1822 dwarf::DW_FORM_data4));
1823 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1824 E = CUMap.end(); I != E; ++I) {
1825 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001826 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1827 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001828 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1829 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001830 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001831 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1832 DE = Entities.end(); DI != DE; ++DI)
1833 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001834 }
1835 }
1836
1837 AT.FinalizeTable(Asm, "Names");
1838 Asm->OutStreamer.SwitchSection(
1839 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1840 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1841 Asm->OutStreamer.EmitLabel(SectionBegin);
1842
1843 // Emit the full data.
1844 AT.Emit(Asm, SectionBegin, this);
1845}
1846
1847/// emitAccelObjC - Emit objective C classes and categories into a hashed
1848/// accelerator table section.
1849void DwarfDebug::emitAccelObjC() {
1850 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1851 dwarf::DW_FORM_data4));
1852 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1853 E = CUMap.end(); I != E; ++I) {
1854 CompileUnit *TheCU = I->second;
1855 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1856 for (StringMap<std::vector<DIE*> >::const_iterator
1857 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1858 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001859 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-11-07 09:24:32 +00001860 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1861 DE = Entities.end(); DI != DE; ++DI)
1862 AT.AddName(Name, (*DI));
1863 }
1864 }
1865
1866 AT.FinalizeTable(Asm, "ObjC");
1867 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1868 .getDwarfAccelObjCSection());
1869 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1870 Asm->OutStreamer.EmitLabel(SectionBegin);
1871
1872 // Emit the full data.
1873 AT.Emit(Asm, SectionBegin, this);
1874}
1875
1876/// emitAccelNamespace - Emit namespace dies into a hashed accelerator
1877/// table.
1878void DwarfDebug::emitAccelNamespaces() {
1879 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1880 dwarf::DW_FORM_data4));
1881 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1882 E = CUMap.end(); I != E; ++I) {
1883 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001884 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
1885 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001886 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1887 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001888 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001889 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1890 DE = Entities.end(); DI != DE; ++DI)
1891 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001892 }
1893 }
1894
1895 AT.FinalizeTable(Asm, "namespac");
1896 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1897 .getDwarfAccelNamespaceSection());
1898 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
1899 Asm->OutStreamer.EmitLabel(SectionBegin);
1900
1901 // Emit the full data.
1902 AT.Emit(Asm, SectionBegin, this);
1903}
1904
1905/// emitAccelTypes() - Emit type dies into a hashed accelerator table.
1906void DwarfDebug::emitAccelTypes() {
Eric Christopherc36145f2012-01-06 04:35:23 +00001907 std::vector<DwarfAccelTable::Atom> Atoms;
1908 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1909 dwarf::DW_FORM_data4));
1910 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
1911 dwarf::DW_FORM_data2));
1912 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
1913 dwarf::DW_FORM_data1));
1914 DwarfAccelTable AT(Atoms);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001915 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1916 E = CUMap.end(); I != E; ++I) {
1917 CompileUnit *TheCU = I->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001918 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
1919 = TheCU->getAccelTypes();
1920 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001921 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1922 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001923 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001924 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
1925 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
1926 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001927 }
1928 }
1929
1930 AT.FinalizeTable(Asm, "types");
1931 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1932 .getDwarfAccelTypesSection());
1933 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
1934 Asm->OutStreamer.EmitLabel(SectionBegin);
1935
1936 // Emit the full data.
1937 AT.Emit(Asm, SectionBegin, this);
1938}
1939
Devang Patel193f7202009-11-24 01:14:22 +00001940void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001941 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1942 E = CUMap.end(); I != E; ++I) {
1943 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00001944 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-05-10 22:49:55 +00001945 Asm->OutStreamer.SwitchSection(
1946 Asm->getObjFileLowering().getDwarfPubTypesSection());
1947 Asm->OutStreamer.AddComment("Length of Public Types Info");
1948 Asm->EmitLabelDifference(
1949 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1950 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001951
Devang Patel163a9f72010-05-10 22:49:55 +00001952 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1953 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001954
Devang Patel163a9f72010-05-10 22:49:55 +00001955 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1956 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001957
Devang Patel163a9f72010-05-10 22:49:55 +00001958 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1959 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1960 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001961
Devang Patel163a9f72010-05-10 22:49:55 +00001962 Asm->OutStreamer.AddComment("Compilation Unit Length");
1963 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1964 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1965 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001966
Devang Patel163a9f72010-05-10 22:49:55 +00001967 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1968 for (StringMap<DIE*>::const_iterator
1969 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1970 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001971 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001972
Devang Patel163a9f72010-05-10 22:49:55 +00001973 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1974 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001975
Devang Patel163a9f72010-05-10 22:49:55 +00001976 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00001977 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00001978 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1979 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001980
Devang Patel163a9f72010-05-10 22:49:55 +00001981 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001982 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001983 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1984 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001985 }
Devang Patel193f7202009-11-24 01:14:22 +00001986}
1987
Devang Patel2c4ceb12009-11-21 02:48:08 +00001988/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001989///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001990void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001991 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001992 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001993
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001994 // Start the dwarf str section.
1995 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001996 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001997
Chris Lattnerbc733f52010-03-13 02:17:42 +00001998 // Get all of the string pool entries and put them in an array by their ID so
1999 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002000 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00002001 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002002
Chris Lattnerbc733f52010-03-13 02:17:42 +00002003 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2004 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2005 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002006
Chris Lattnerbc733f52010-03-13 02:17:42 +00002007 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002008
Chris Lattnerbc733f52010-03-13 02:17:42 +00002009 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002010 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002011 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002012
Benjamin Kramer983c4572011-11-09 18:16:11 +00002013 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00002014 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
2015 Entries[i].second->getKeyLength()+1),
2016 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002017 }
2018}
2019
Devang Patel2c4ceb12009-11-21 02:48:08 +00002020/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002021///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002022void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002023 if (DotDebugLocEntries.empty())
2024 return;
2025
Devang Patel6c3ea902011-02-04 22:57:18 +00002026 for (SmallVector<DotDebugLocEntry, 4>::iterator
2027 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2028 I != E; ++I) {
2029 DotDebugLocEntry &Entry = *I;
2030 if (I + 1 != DotDebugLocEntries.end())
2031 Entry.Merge(I+1);
2032 }
2033
Daniel Dunbar83320a02011-03-16 22:16:39 +00002034 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002035 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002036 Asm->getObjFileLowering().getDwarfLocSection());
2037 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002038 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2039 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002040 for (SmallVector<DotDebugLocEntry, 4>::iterator
2041 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002042 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002043 DotDebugLocEntry &Entry = *I;
2044 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002045 if (Entry.isEmpty()) {
2046 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2047 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002048 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002049 } else {
2050 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2051 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002052 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002053 Asm->OutStreamer.AddComment("Loc expr size");
2054 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2055 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2056 Asm->EmitLabelDifference(end, begin, 2);
2057 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002058 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002059 DIBasicType BTy(DV.getType());
2060 if (BTy.Verify() &&
2061 (BTy.getEncoding() == dwarf::DW_ATE_signed
2062 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2063 Asm->OutStreamer.AddComment("DW_OP_consts");
2064 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002065 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002066 } else {
2067 Asm->OutStreamer.AddComment("DW_OP_constu");
2068 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002069 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002070 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002071 } else if (Entry.isLocation()) {
2072 if (!DV.hasComplexAddress())
2073 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002074 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002075 else {
2076 // Complex address entry.
2077 unsigned N = DV.getNumAddrElements();
2078 unsigned i = 0;
2079 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2080 if (Entry.Loc.getOffset()) {
2081 i = 2;
2082 Asm->EmitDwarfRegOp(Entry.Loc);
2083 Asm->OutStreamer.AddComment("DW_OP_deref");
2084 Asm->EmitInt8(dwarf::DW_OP_deref);
2085 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2086 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2087 Asm->EmitSLEB128(DV.getAddrElement(1));
2088 } else {
2089 // If first address element is OpPlus then emit
2090 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2091 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2092 Asm->EmitDwarfRegOp(Loc);
2093 i = 2;
2094 }
2095 } else {
2096 Asm->EmitDwarfRegOp(Entry.Loc);
2097 }
2098
2099 // Emit remaining complex address elements.
2100 for (; i < N; ++i) {
2101 uint64_t Element = DV.getAddrElement(i);
2102 if (Element == DIBuilder::OpPlus) {
2103 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2104 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher50120762012-05-08 18:56:00 +00002105 } else if (Element == DIBuilder::OpDeref) {
Eric Christophera80f2d12012-05-08 21:24:39 +00002106 if (!Entry.Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002107 Asm->EmitInt8(dwarf::DW_OP_deref);
2108 } else
2109 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002110 }
Devang Patelc26f5442011-04-28 02:22:40 +00002111 }
Devang Patelc26f5442011-04-28 02:22:40 +00002112 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002113 // else ... ignore constant fp. There is not any good way to
2114 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002115 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002116 }
2117 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002118}
2119
2120/// EmitDebugARanges - Emit visible names into a debug aranges section.
2121///
2122void DwarfDebug::EmitDebugARanges() {
2123 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002124 Asm->OutStreamer.SwitchSection(
2125 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002126}
2127
Devang Patel2c4ceb12009-11-21 02:48:08 +00002128/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002129///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002130void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002131 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002132 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002133 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002134 unsigned char Size = Asm->getTargetData().getPointerSize();
2135 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002136 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002137 I != E; ++I) {
2138 if (*I)
2139 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002140 else
Devang Pateleac9c072010-04-27 19:46:33 +00002141 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002142 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002143}
2144
Devang Patel2c4ceb12009-11-21 02:48:08 +00002145/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002146///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002147void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002148 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002149 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002150 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002151 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002152 }
2153}
2154
Devang Patel2c4ceb12009-11-21 02:48:08 +00002155/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002156/// Section Header:
2157/// 1. length of section
2158/// 2. Dwarf version number
2159/// 3. address size.
2160///
2161/// Entries (one "entry" for each function that was inlined):
2162///
2163/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2164/// otherwise offset into __debug_str for regular function name.
2165/// 2. offset into __debug_str section for regular function name.
2166/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2167/// instances for the function.
2168///
2169/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2170/// inlined instance; the die_offset points to the inlined_subroutine die in the
2171/// __debug_info section, and the low_pc is the starting address for the
2172/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002173void DwarfDebug::emitDebugInlineInfo() {
Eric Christopherb83e2bb2012-03-02 02:11:47 +00002174 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002175 return;
2176
Devang Patel163a9f72010-05-10 22:49:55 +00002177 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002178 return;
2179
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002180 Asm->OutStreamer.SwitchSection(
2181 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002182
Chris Lattner233f52b2010-03-09 23:52:58 +00002183 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002184 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2185 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002186
Chris Lattnerc0215722010-04-04 19:25:43 +00002187 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002188
Chris Lattner233f52b2010-03-09 23:52:58 +00002189 Asm->OutStreamer.AddComment("Dwarf Version");
2190 Asm->EmitInt16(dwarf::DWARF_VERSION);
2191 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002192 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002193
Devang Patele9f8f5e2010-05-07 20:54:48 +00002194 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002195 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002196
Devang Patele9f8f5e2010-05-07 20:54:48 +00002197 const MDNode *Node = *I;
2198 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002199 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002200 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002201 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002202 StringRef LName = SP.getLinkageName();
2203 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002204
Chris Lattner233f52b2010-03-09 23:52:58 +00002205 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher50e26612012-03-02 01:57:52 +00002206 if (LName.empty())
2207 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2208 else
Chris Lattner6189ed12010-04-04 23:25:33 +00002209 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2210 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002211
Chris Lattner233f52b2010-03-09 23:52:58 +00002212 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002213 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002214 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002215
Devang Patel53bb5c92009-11-10 23:06:00 +00002216 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002217 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002218 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002219 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002220
Chris Lattner3f53c832010-04-04 18:52:31 +00002221 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002222 Asm->OutStreamer.EmitSymbolValue(LI->first,
2223 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002224 }
2225 }
2226
Chris Lattnerc0215722010-04-04 19:25:43 +00002227 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002228}