blob: 946ac35465843bf606a12f2cbdc2c1180e539cc0 [file] [log] [blame]
Bill Wendling2f921f82009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattnerb14490d2010-03-09 00:39:24 +000013
Devang Patel80ae3492009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000016#include "DIE.h"
Eric Christopher4996c702011-11-07 09:24:32 +000017#include "DwarfAccelTable.h"
Devang Patel5eb43192011-04-12 17:40:32 +000018#include "DwarfCompileUnit.h"
Bill Wendling30346342010-04-05 22:59:21 +000019#include "llvm/Constants.h"
Bill Wendlinge38859d2012-06-28 00:05:13 +000020#include "llvm/DebugInfo.h"
Bill Wendlingf799efd2012-06-29 08:32:07 +000021#include "llvm/DIBuilder.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000022#include "llvm/Module.h"
Devang Patel2d9e5322011-01-20 00:02:16 +000023#include "llvm/Instructions.h"
David Greene829b3e82009-08-19 21:52:55 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000026#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000027#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000028#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000029#include "llvm/MC/MCSymbol.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000030#include "llvm/Target/TargetData.h"
Anton Korobeynikov2f931282011-01-10 12:39:04 +000031#include "llvm/Target/TargetFrameLowering.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner21dc46e2010-04-04 18:06:11 +000033#include "llvm/Target/TargetMachine.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel61880932010-04-19 19:14:02 +000035#include "llvm/Target/TargetOptions.h"
Devang Patela86114b2010-10-25 20:45:32 +000036#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000037#include "llvm/ADT/STLExtras.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000038#include "llvm/ADT/StringExtras.h"
Bill Wendlingf799efd2012-06-29 08:32:07 +000039#include "llvm/ADT/Triple.h"
Devang Patel6c74a872010-04-27 19:46:33 +000040#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000041#include "llvm/Support/Debug.h"
42#include "llvm/Support/ErrorHandling.h"
Devang Patel1973df22010-01-26 21:39:14 +000043#include "llvm/Support/ValueHandle.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000044#include "llvm/Support/FormattedStream.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000045#include "llvm/Support/Timer.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000046#include "llvm/Support/Path.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000047using namespace llvm;
48
Jim Grosbacha8683bb2010-07-21 21:21:52 +000049static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel30265c42010-07-07 20:12:52 +000050 cl::Hidden,
Devang Patel6c74a872010-04-27 19:46:33 +000051 cl::desc("Disable debug info printing"));
52
Dan Gohman7421ae42010-05-07 01:08:53 +000053static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000054 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman7421ae42010-05-07 01:08:53 +000055 cl::init(false));
56
Eric Christopher20b76a72012-08-23 22:36:40 +000057namespace {
58 enum DefaultOnOff {
59 Default, Enable, Disable
60 };
61}
Eric Christopher4996c702011-11-07 09:24:32 +000062
Eric Christopher20b76a72012-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 Christopher978fbff2012-08-23 07:10:46 +000073 cl::desc("Compatibility with Darwin gdb."),
Eric Christopher20b76a72012-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 Christopher978fbff2012-08-23 07:10:46 +000080
Bill Wendlingfcc14142010-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 Wendling2f921f82009-05-15 09:23:25 +000086//===----------------------------------------------------------------------===//
87
88/// Configuration values for initial hash set sizes (log2).
89///
Bill Wendling2f921f82009-05-15 09:23:25 +000090static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +000091
92namespace llvm {
93
Nick Lewycky019d2552011-07-29 03:49:23 +000094DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-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 Patel6d9f9fe2010-08-09 21:01:39 +0000139 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000140 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000141 return Ty;
142}
Bill Wendling2f921f82009-05-15 09:23:25 +0000143
Chris Lattnerf5d06362010-04-05 04:09:20 +0000144} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000145
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000146DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel1a0df9a2010-05-10 22:49:55 +0000147 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000148 AbbreviationsSet(InitAbbreviationsSetSize),
Benjamin Kramer07480082012-06-09 10:34:15 +0000149 SourceIdMap(DIEValueAllocator), StringPool(DIEValueAllocator),
Eric Christopherb1b94512012-08-01 18:19:01 +0000150 PrevLabel(NULL) {
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000151 NextStringPoolNumber = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000152
Rafael Espindolaa7160962011-05-06 14:56:22 +0000153 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattnere58b5472010-04-04 23:10:38 +0000154 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000155 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000156 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopherad9fe892012-04-02 17:58:52 +0000157
Eric Christopher978fbff2012-08-23 07:10:46 +0000158 // Turn on accelerator tables and older gdb compatibility
159 // for Darwin.
Eric Christopher20b76a72012-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 Christopher4977f212012-08-23 22:36:36 +0000168
Eric Christopher20b76a72012-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 Gohman6e681a52010-06-18 15:56:31 +0000177 {
178 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
179 beginModule(M);
Torok Edwinf8dba242010-04-07 10:44:46 +0000180 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000181}
182DwarfDebug::~DwarfDebug() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000183}
184
Eric Christophera7b61892011-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 Lewyckyd59c0ca2011-10-27 06:44:11 +0000197MCSymbol *DwarfDebug::getStringPool() {
198 return Asm->GetTempSymbol("section_str");
199}
200
Chris Lattnerb7aa9522010-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 Lattnera179b522010-04-04 19:25:43 +0000206 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000207}
208
Devang Patel930143b2009-11-21 02:48:08 +0000209/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000210///
Devang Patel930143b2009-11-21 02:48:08 +0000211void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-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 Grosbacha8683bb2010-07-21 21:21:52 +0000232/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel43ef34d2010-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 Christopherd9843b32011-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 Grosbach042483e2009-11-21 23:12:12 +0000299/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-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 Pateld2dfc5e2011-08-15 22:24:32 +0000303DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
304 const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000305 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +0000306
Chris Lattner3a383cb2010-04-05 00:13:49 +0000307 assert(SPDie && "Unable to find subprogram DIE!");
308 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000309
Devang Patel1d6bbd42011-04-22 23:10:17 +0000310 DISubprogram SPDecl = SP.getFunctionDeclaration();
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000311 if (!SPDecl.isSubprogram()) {
Devang Patel1d6bbd42011-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 Christopherbb69a272012-08-24 01:14:27 +0000320 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
Devang Patel1d6bbd42011-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);
329 DIType ATy = DIType(DIType(Args.getElement(i)));
330 SPCU->addType(Arg, ATy);
331 if (ATy.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +0000332 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel1d6bbd42011-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 Lattner3a383cb2010-04-05 00:13:49 +0000341 }
Devang Patela37a95e2010-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 Patelf20c4f72011-04-12 22:53:02 +0000345 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
346 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patela37a95e2010-07-07 22:20:57 +0000347 SPCU->addDie(SPDie);
348 }
349
Devang Patelf20c4f72011-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 Lattner3a383cb2010-04-05 00:13:49 +0000354 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
355 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000356 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000357
Eric Christopherd9843b32011-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 Lattner3a383cb2010-04-05 00:13:49 +0000362 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000363}
364
Jim Grosbach042483e2009-11-21 23:12:12 +0000365/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +0000366/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000367DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
368 LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000369 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
370 if (Scope->isAbstractScope())
371 return ScopeDIE;
372
Devang Patel7e623022011-08-10 20:55:27 +0000373 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +0000374 if (Ranges.empty())
375 return 0;
376
Devang Patel7e623022011-08-10 20:55:27 +0000377 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel6c74a872010-04-27 19:46:33 +0000378 if (Ranges.size() > 1) {
379 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000380 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000381 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000382 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000383 DebugRangeSymbols.size()
384 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000385 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +0000386 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000387 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
388 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000389 }
390 DebugRangeSymbols.push_back(NULL);
391 DebugRangeSymbols.push_back(NULL);
392 return ScopeDIE;
393 }
394
Devang Patel9fc11702010-05-25 23:40:22 +0000395 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
396 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000397
Devang Patel9fc11702010-05-25 23:40:22 +0000398 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000399
Chris Lattnere13c3722010-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 Grosbacha8683bb2010-07-21 21:21:52 +0000402
Devang Patelf20c4f72011-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 Patelf6eeaeb2009-11-10 23:06:00 +0000405
406 return ScopeDIE;
407}
408
Devang Patel930143b2009-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 Pateld2dfc5e2011-08-15 22:24:32 +0000412DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
413 LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +0000414 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000415 assert(Ranges.empty() == false &&
416 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000417
Devang Patelf098ce22011-07-27 00:34:13 +0000418 if (!Scope->getScopeNode())
419 return NULL;
420 DIScope DS(Scope->getScopeNode());
421 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patelf098ce22011-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 Patel7e623022011-08-10 20:55:27 +0000428 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +0000429 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
430 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000431
Devang Patel4c6bd662010-07-08 22:39:20 +0000432 if (StartLabel == 0 || EndLabel == 0) {
Craig Topperee4dab52012-02-05 08:31:47 +0000433 llvm_unreachable("Unexpected Start and End labels for a inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000434 }
Chris Lattnere13c3722010-03-09 01:58:53 +0000435 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000436 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +0000437 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000438 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000439
Devang Patel73bc1722011-05-05 17:54:26 +0000440 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patelf20c4f72011-04-12 22:53:02 +0000441 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
442 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000443
Devang Patelf098ce22011-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 Patel784077e2011-08-10 23:58:09 +0000449 DebugRangeSymbols.size()
450 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000451 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patelf098ce22011-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 Patel784077e2011-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 Patelf098ce22011-07-27 00:34:13 +0000463 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000464
465 InlinedSubprogramDIEs.insert(OriginDIE);
466
467 // Track the start label for this inlined function.
Devang Patelf098ce22011-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 Patel32cc43c2010-05-07 20:54:48 +0000472 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000473 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000474
475 if (I == InlineInfo.end()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000476 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000477 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000478 } else
Chris Lattner085b6522010-03-09 00:31:02 +0000479 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000480
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000481 DILocation DL(Scope->getInlinedAt());
Eric Christopher0925c622012-03-26 21:38:38 +0000482 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
483 GetOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patelf20c4f72011-04-12 22:53:02 +0000484 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000485
Eric Christopher8dda5d02011-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 Patelf6eeaeb2009-11-10 23:06:00 +0000490 return ScopeDIE;
491}
492
Devang Patel930143b2009-11-21 02:48:08 +0000493/// constructScopeDIE - Construct a DIE for this scope.
Devang Patel3acc70e2011-08-15 22:04:40 +0000494DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000495 if (!Scope || !Scope->getScopeNode())
496 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000497
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000498 SmallVector<DIE *, 8> Children;
Devang Patel6c622ef2011-03-01 22:58:55 +0000499
500 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000501 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000502 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
503 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Patel3acc70e2011-08-15 22:04:40 +0000504 if (DIE *Arg =
505 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel6c622ef2011-03-01 22:58:55 +0000506 Children.push_back(Arg);
507
Eric Christopherf84354b2011-10-03 15:49:16 +0000508 // Collect lexical scope children first.
Devang Patel7e623022011-08-10 20:55:27 +0000509 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000510 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Patel3acc70e2011-08-15 22:04:40 +0000511 if (DIE *Variable =
512 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000513 Children.push_back(Variable);
Devang Patel7e623022011-08-10 20:55:27 +0000514 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000515 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000516 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000517 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000518 DIScope DS(Scope->getScopeNode());
519 DIE *ScopeDIE = NULL;
520 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000521 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000522 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000523 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000524 if (Scope->isAbstractScope()) {
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000525 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000526 // Note down abstract DIE.
527 if (ScopeDIE)
528 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
529 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000530 else
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000531 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000532 }
Devang Patel5f1b4cd2011-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 Pateld2dfc5e2011-08-15 22:24:32 +0000537 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000538 }
539
Devang Patel23b2ae62010-03-29 22:59:58 +0000540 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000541
Devang Patel5f1b4cd2011-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 Patel04d2f2d2009-11-24 01:14:22 +0000546
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000547 if (DS.isSubprogram())
Eric Christopherd9843b32011-11-10 19:25:34 +0000548 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000549
Eric Christopherd9843b32011-11-10 19:25:34 +0000550 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000551}
552
Bill Wendling2b128d72009-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 Patele01b75c2011-03-24 20:30:50 +0000557unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
558 StringRef DirName) {
Devang Patel871d0b12010-09-16 20:57:49 +0000559 // If FE did not provide a file name, then assume stdin.
560 if (FileName.empty())
Devang Patele01b75c2011-03-24 20:30:50 +0000561 return GetOrCreateSourceID("<stdin>", StringRef());
562
Nick Lewyckyd1ee7f82011-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 Lewycky40f8f2f2011-10-17 23:05:28 +0000567 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel871d0b12010-09-16 20:57:49 +0000568
Benjamin Kramer71b19732012-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 Wendling2b128d72009-05-20 23:19:06 +0000578
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000579 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000580 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendling2b128d72009-05-20 23:19:06 +0000581
582 return SrcId;
583}
584
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000585/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel1a0df9a2010-05-10 22:49:55 +0000586/// metadata node with tag DW_TAG_compile_unit.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000587CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000588 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +0000589 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000590 CompilationDir = DIUnit.getDirectory();
591 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000592
593 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher3a2656b2012-02-22 08:46:13 +0000594 CompileUnit *NewCU = new CompileUnit(ID, DIUnit.getLanguage(), Die, Asm, this);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000595 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000596 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
597 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000598 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopherb1b94512012-08-01 18:19:01 +0000599 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
600 // into an entity.
601 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +0000602 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +0000603 // compile unit in debug_line section.
Rafael Espindolad7bdaf52012-06-22 13:24:07 +0000604 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindolaa7558912011-05-04 17:44:06 +0000605 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patelf20c4f72011-04-12 22:53:02 +0000606 Asm->GetTempSymbol("section_line"));
Devang Patelea636392010-08-31 23:50:19 +0000607 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000608 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +0000609
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000610 if (!CompilationDir.empty())
611 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000612 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000613 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000614
Devang Patel2d9caf92009-11-25 17:36:49 +0000615 StringRef Flags = DIUnit.getFlags();
616 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000617 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patelf20c4f72011-04-12 22:53:02 +0000618
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000619 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000620 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000621 dwarf::DW_FORM_data1, RVer);
622
Devang Patel1a0df9a2010-05-10 22:49:55 +0000623 if (!FirstCU)
624 FirstCU = NewCU;
625 CUMap.insert(std::make_pair(N, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000626 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000627}
628
Devang Patel1a0df9a2010-05-10 22:49:55 +0000629/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel1f4f98d2011-08-15 23:36:40 +0000630void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
631 const MDNode *N) {
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000632 CompileUnit *&CURef = SPMap[N];
633 if (CURef)
634 return;
635 CURef = TheCU;
636
Devang Patel80ae3492009-08-28 23:24:31 +0000637 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000638 if (!SP.isDefinition())
639 // This is a method declaration which will be handled while constructing
640 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000641 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000642
Devang Patel89543712011-08-15 17:24:54 +0000643 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000644
645 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000646 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000647
648 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +0000649 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +0000650
Devang Patel0751a282009-06-26 01:49:18 +0000651 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000652}
653
Devang Patel07bb9ee2011-08-15 23:47:24 +0000654/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
655/// as llvm.dbg.enum and llvm.dbg.ty
656void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000657 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
658 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
659 const MDNode *N = NMD->getOperand(i);
660 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
661 constructSubprogramDIE(CU, N);
662 }
663
664 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
665 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
666 const MDNode *N = NMD->getOperand(i);
667 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000668 CU->createGlobalVariableDIE(N);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000669 }
670
Devang Patel07bb9ee2011-08-15 23:47:24 +0000671 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
672 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
673 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000674 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
675 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000676 }
677
678 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
679 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
680 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000681 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
682 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000683 }
684}
685
686/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
687/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
688bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
689 DebugInfoFinder DbgFinder;
690 DbgFinder.processModule(*M);
691
692 bool HasDebugInfo = false;
693 // Scan all the compile-units to see if there are any marked as the main
694 // unit. If not, we do not generate debug info.
695 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
696 E = DbgFinder.compile_unit_end(); I != E; ++I) {
697 if (DICompileUnit(*I).isMain()) {
698 HasDebugInfo = true;
699 break;
700 }
701 }
702 if (!HasDebugInfo) return false;
703
704 // Create all the compile unit DIEs.
705 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
706 E = DbgFinder.compile_unit_end(); I != E; ++I)
707 constructCompileUnit(*I);
708
709 // Create DIEs for each global variable.
710 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
711 E = DbgFinder.global_variable_end(); I != E; ++I) {
712 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000713 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000714 CU->createGlobalVariableDIE(N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000715 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000716
Devang Patel07bb9ee2011-08-15 23:47:24 +0000717 // Create DIEs for each subprogram.
718 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
719 E = DbgFinder.subprogram_end(); I != E; ++I) {
720 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000721 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
722 constructSubprogramDIE(CU, N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000723 }
724
725 return HasDebugInfo;
726}
727
Devang Patel930143b2009-11-21 02:48:08 +0000728/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000729/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky019d2552011-07-29 03:49:23 +0000730/// This is invoked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +0000731void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +0000732 if (DisableDebugInfoPrinting)
733 return;
734
Nick Lewycky019d2552011-07-29 03:49:23 +0000735 // If module has named metadata anchors then use them, otherwise scan the
736 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000737 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
738 if (CU_Nodes) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000739 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
740 DICompileUnit CUNode(CU_Nodes->getOperand(i));
741 CompileUnit *CU = constructCompileUnit(CUNode);
742 DIArray GVs = CUNode.getGlobalVariables();
743 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000744 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000745 DIArray SPs = CUNode.getSubprograms();
746 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
747 constructSubprogramDIE(CU, SPs.getElement(i));
748 DIArray EnumTypes = CUNode.getEnumTypes();
749 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
750 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
751 DIArray RetainedTypes = CUNode.getRetainedTypes();
752 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
753 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
754 }
Devang Patel07bb9ee2011-08-15 23:47:24 +0000755 } else if (!collectLegacyDebugInfo(M))
756 return;
Devang Patele02e5852011-05-03 16:45:22 +0000757
Devang Patel07bb9ee2011-08-15 23:47:24 +0000758 collectInfoFromNamedMDNodes(M);
Devang Patele02e5852011-05-03 16:45:22 +0000759
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000760 // Tell MMI that we have debug info.
761 MMI->setDebugInfoAvailability(true);
Devang Patele02e5852011-05-03 16:45:22 +0000762
Chris Lattnerd442aa32010-04-04 23:17:54 +0000763 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000764 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000765
Bill Wendling2b128d72009-05-20 23:19:06 +0000766 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +0000767 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +0000768}
769
Devang Patel930143b2009-11-21 02:48:08 +0000770/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +0000771///
Devang Patel930143b2009-11-21 02:48:08 +0000772void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000773 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +0000774 const Module *M = MMI->getModule();
Devang Patel7e623022011-08-10 20:55:27 +0000775 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +0000776
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000777 // Collect info for variables that were optimized out.
778 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
779 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
780 DICompileUnit TheCU(CU_Nodes->getOperand(i));
781 DIArray Subprograms = TheCU.getSubprograms();
782 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
783 DISubprogram SP(Subprograms.getElement(i));
784 if (ProcessedSPNodes.count(SP) != 0) continue;
785 if (!SP.Verify()) continue;
786 if (!SP.isDefinition()) continue;
Devang Patel59e27c52011-08-19 23:28:12 +0000787 DIArray Variables = SP.getVariables();
788 if (Variables.getNumElements() == 0) continue;
789
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000790 LexicalScope *Scope =
791 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
792 DeadFnScopeMap[SP] = Scope;
793
794 // Construct subprogram DIE and add variables DIEs.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000795 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000796 assert(SPCU && "Unable to find Compile Unit!");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000797 constructSubprogramDIE(SPCU, SP);
798 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel59e27c52011-08-19 23:28:12 +0000799 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
800 DIVariable DV(Variables.getElement(vi));
801 if (!DV.Verify()) continue;
802 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000803 if (DIE *VariableDIE =
Devang Patel59e27c52011-08-19 23:28:12 +0000804 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000805 ScopeDIE->addChild(VariableDIE);
806 }
Devang Patelf3b2db62010-06-28 18:25:03 +0000807 }
808 }
809 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000810
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000811 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
812 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
813 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
814 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +0000815 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000816 }
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000817 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
818 AE = AbstractSPDies.end(); AI != AE; ++AI) {
819 DIE *ISP = AI->second;
820 if (InlinedSubprogramDIEs.count(ISP))
821 continue;
822 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
823 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000824
Eric Christopherb1b94512012-08-01 18:19:01 +0000825 // Emit DW_AT_containing_type attribute to connect types with their
826 // vtable holding type.
Devang Patel89543712011-08-15 17:24:54 +0000827 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
828 CUE = CUMap.end(); CUI != CUE; ++CUI) {
829 CompileUnit *TheCU = CUI->second;
830 TheCU->constructContainingTypeDIEs();
Devang Pateleb57c592009-12-03 19:11:07 +0000831 }
832
Bill Wendling2b128d72009-05-20 23:19:06 +0000833 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000834 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000835 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000836 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000837 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +0000838
839 // End text sections.
840 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000841 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +0000842 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +0000843 }
844
Bill Wendling2b128d72009-05-20 23:19:06 +0000845 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +0000846 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +0000847
848 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +0000849 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000850
851 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +0000852 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +0000853
Eric Christophera876b822012-08-23 07:32:06 +0000854 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +0000855 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +0000856 emitAccelNames();
857 emitAccelObjC();
858 emitAccelNamespaces();
859 emitAccelTypes();
860 }
861
Devang Patel04d2f2d2009-11-24 01:14:22 +0000862 // Emit info into a debug pubtypes section.
Eric Christopher77826182012-08-23 07:10:56 +0000863 // TODO: When we don't need the option anymore we can
864 // remove all of the code that adds to the table.
Eric Christopher20b76a72012-08-23 22:36:40 +0000865 if (useDarwinGDBCompat())
Eric Christopher77826182012-08-23 07:10:56 +0000866 emitDebugPubTypes();
Devang Patel04d2f2d2009-11-24 01:14:22 +0000867
Bill Wendling2b128d72009-05-20 23:19:06 +0000868 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +0000869 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +0000870
871 // Emit info into a debug aranges section.
872 EmitDebugARanges();
873
874 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +0000875 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +0000876
877 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +0000878 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000879
880 // Emit inline info.
Eric Christopher3a47c3e2012-08-23 07:32:02 +0000881 // TODO: When we don't need the option anymore we
882 // can remove all of the code that this section
883 // depends upon.
Eric Christopher20b76a72012-08-23 22:36:40 +0000884 if (useDarwinGDBCompat())
Eric Christopher3a47c3e2012-08-23 07:32:02 +0000885 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000886
Eric Christopher7524fe42012-03-02 00:30:24 +0000887 // Emit info into a debug str section.
888 emitDebugStr();
889
Devang Pateld0701282010-08-02 17:32:15 +0000890 // clean up.
891 DeleteContainerSeconds(DeadFnScopeMap);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000892 SPMap.clear();
Devang Patel1a0df9a2010-05-10 22:49:55 +0000893 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
894 E = CUMap.end(); I != E; ++I)
895 delete I->second;
896 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +0000897}
898
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000899/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +0000900DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +0000901 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +0000902 LLVMContext &Ctx = DV->getContext();
903 // More then one inlined variable corresponds to one abstract variable.
904 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000905 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000906 if (AbsDbgVariable)
907 return AbsDbgVariable;
908
Devang Patel7e623022011-08-10 20:55:27 +0000909 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000910 if (!Scope)
911 return NULL;
912
Devang Patel99819b52011-08-15 19:01:20 +0000913 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patel7e623022011-08-10 20:55:27 +0000914 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000915 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000916 return AbsDbgVariable;
917}
918
Nick Lewycky019d2552011-07-29 03:49:23 +0000919/// addCurrentFnArgument - If Var is a current function argument then add
920/// it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +0000921bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +0000922 DbgVariable *Var, LexicalScope *Scope) {
923 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000924 return false;
925 DIVariable DV = Var->getVariable();
926 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
927 return false;
928 unsigned ArgNo = DV.getArgNumber();
929 if (ArgNo == 0)
930 return false;
931
Devang Patel4ab660b2011-03-03 20:02:02 +0000932 size_t Size = CurrentFnArguments.size();
933 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +0000934 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +0000935 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +0000936 // arguments does the function have at source level.
937 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +0000938 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +0000939 CurrentFnArguments[ArgNo - 1] = Var;
940 return true;
941}
942
Devang Patel490c8ab2010-05-20 19:57:06 +0000943/// collectVariableInfoFromMMITable - Collect variable information from
944/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000945void
Nick Lewycky019d2552011-07-29 03:49:23 +0000946DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patel490c8ab2010-05-20 19:57:06 +0000947 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +0000948 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
949 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
950 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +0000951 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000952 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +0000953 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +0000954 DIVariable DV(Var);
955 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000956
Devang Patel7e623022011-08-10 20:55:27 +0000957 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000958
Devang Patelcdb7d442009-11-10 23:20:04 +0000959 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +0000960 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +0000961 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000962
Devang Patele1c53f22010-05-20 16:36:41 +0000963 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel99819b52011-08-15 19:01:20 +0000964 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patel3e4a9652011-08-15 21:24:36 +0000965 RegVar->setFrameIndex(VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +0000966 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +0000967 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +0000968 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +0000969 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +0000970 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000971}
Devang Patela3e9c9c2010-03-15 18:33:46 +0000972
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000973/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +0000974/// DBG_VALUE instruction, is in a defined reg.
975static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000976 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000977 return MI->getNumOperands() == 3 &&
978 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
979 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000980}
981
Nick Lewycky019d2552011-07-29 03:49:23 +0000982/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel2442a892011-07-08 17:09:57 +0000983/// at MI.
984static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
985 const MCSymbol *FLabel,
986 const MCSymbol *SLabel,
987 const MachineInstr *MI) {
988 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
989
990 if (MI->getNumOperands() != 3) {
991 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
992 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
993 }
994 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
995 MachineLocation MLoc;
996 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
997 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
998 }
999 if (MI->getOperand(0).isImm())
1000 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1001 if (MI->getOperand(0).isFPImm())
1002 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1003 if (MI->getOperand(0).isCImm())
1004 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1005
Craig Topperee4dab52012-02-05 08:31:47 +00001006 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001007}
1008
Devang Patel7e623022011-08-10 20:55:27 +00001009/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001010void
Devang Patel5c0f85c2010-06-25 22:07:34 +00001011DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1012 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001013
Devang Patel490c8ab2010-05-20 19:57:06 +00001014 /// collection info from MMI table.
1015 collectVariableInfoFromMMITable(MF, Processed);
1016
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001017 for (SmallVectorImpl<const MDNode*>::const_iterator
1018 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1019 ++UVI) {
1020 const MDNode *Var = *UVI;
1021 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001022 continue;
1023
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001024 // History contains relevant DBG_VALUE instructions for Var and instructions
1025 // clobbering it.
1026 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1027 if (History.empty())
1028 continue;
1029 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001030
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001031 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001032 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001033 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1034 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001035 Scope = LScopes.getCurrentFunctionScope();
Devang Patel8fb9fd62011-07-20 22:18:50 +00001036 else {
1037 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patel7e623022011-08-10 20:55:27 +00001038 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel8fb9fd62011-07-20 22:18:50 +00001039 else {
1040 if (MDNode *IA = DV.getInlinedAt())
Devang Patel7e623022011-08-10 20:55:27 +00001041 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001042 else
Devang Patel7e623022011-08-10 20:55:27 +00001043 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001044 }
1045 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001046 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001047 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001048 continue;
1049
1050 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001051 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001052 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1053 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel6c622ef2011-03-01 22:58:55 +00001054 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001055 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001056 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001057 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001058
1059 // Simple ranges that are fully coalesced.
1060 if (History.size() <= 1 || (History.size() == 2 &&
1061 MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001062 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001063 continue;
1064 }
1065
1066 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001067 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001068
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001069 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1070 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1071 const MachineInstr *Begin = *HI;
1072 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001073
Devang Patele7181b52011-06-01 23:00:17 +00001074 // Check if DBG_VALUE is truncating a range.
1075 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1076 && !Begin->getOperand(0).getReg())
1077 continue;
1078
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001079 // Compute the range for a register location.
1080 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1081 const MCSymbol *SLabel = 0;
1082
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001083 if (HI + 1 == HE)
1084 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001085 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001086 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001087 else {
1088 const MachineInstr *End = HI[1];
Devang Patel53b050a2011-07-07 21:44:42 +00001089 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1090 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001091 if (End->isDebugValue())
1092 SLabel = getLabelBeforeInsn(End);
1093 else {
1094 // End is a normal instruction clobbering the range.
1095 SLabel = getLabelAfterInsn(End);
1096 assert(SLabel && "Forgot label after clobber instruction");
1097 ++HI;
1098 }
1099 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001100
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001101 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopher365d0832011-12-16 23:42:31 +00001102 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1103 Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001104 }
1105 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001106 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001107
1108 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001109 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1110 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1111 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1112 DIVariable DV(Variables.getElement(i));
1113 if (!DV || !DV.Verify() || !Processed.insert(DV))
1114 continue;
1115 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1116 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patele0a94bf2010-05-14 21:01:35 +00001117 }
Devang Patel9fc11702010-05-25 23:40:22 +00001118}
Devang Patele0a94bf2010-05-14 21:01:35 +00001119
Devang Patel9fc11702010-05-25 23:40:22 +00001120/// getLabelBeforeInsn - Return Label preceding the instruction.
1121const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001122 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1123 assert(Label && "Didn't insert label before instruction");
1124 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001125}
1126
1127/// getLabelAfterInsn - Return Label immediately following the instruction.
1128const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001129 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001130}
1131
Devang Patelb5694e72010-10-26 17:49:02 +00001132/// beginInstruction - Process beginning of an instruction.
1133void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001134 // Check if source location changes, but ignore DBG_VALUE locations.
1135 if (!MI->isDebugValue()) {
1136 DebugLoc DL = MI->getDebugLoc();
1137 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001138 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001139 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001140 if (DL == PrologEndLoc) {
1141 Flags |= DWARF2_FLAG_PROLOGUE_END;
1142 PrologEndLoc = DebugLoc();
1143 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001144 if (PrologEndLoc.isUnknown())
1145 Flags |= DWARF2_FLAG_IS_STMT;
1146
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001147 if (!DL.isUnknown()) {
1148 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001149 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001150 } else
Devang Patel34a66202011-05-11 19:22:19 +00001151 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001152 }
Devang Patel9fc11702010-05-25 23:40:22 +00001153 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001154
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001155 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001156 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1157 LabelsBeforeInsn.find(MI);
1158
1159 // No label needed.
1160 if (I == LabelsBeforeInsn.end())
1161 return;
1162
1163 // Label already assigned.
1164 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001165 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001166
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001167 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001168 PrevLabel = MMI->getContext().CreateTempSymbol();
1169 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001170 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001171 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001172}
1173
Devang Patelb5694e72010-10-26 17:49:02 +00001174/// endInstruction - Process end of an instruction.
1175void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001176 // Don't create a new label after DBG_VALUE instructions.
1177 // They don't generate code.
1178 if (!MI->isDebugValue())
1179 PrevLabel = 0;
1180
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001181 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1182 LabelsAfterInsn.find(MI);
1183
1184 // No label needed.
1185 if (I == LabelsAfterInsn.end())
1186 return;
1187
1188 // Label already assigned.
1189 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001190 return;
1191
1192 // We need a label after this instruction.
1193 if (!PrevLabel) {
1194 PrevLabel = MMI->getContext().CreateTempSymbol();
1195 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001196 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001197 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001198}
1199
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001200/// identifyScopeMarkers() -
Devang Patel784077e2011-08-10 23:58:09 +00001201/// Each LexicalScope has first instruction and last instruction to mark
1202/// beginning and end of a scope respectively. Create an inverse map that list
1203/// scopes starts (and ends) with an instruction. One instruction may start (or
1204/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001205void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001206 SmallVector<LexicalScope *, 4> WorkList;
1207 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001208 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001209 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001210
Devang Patel7e623022011-08-10 20:55:27 +00001211 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001212 if (!Children.empty())
Devang Patel7e623022011-08-10 20:55:27 +00001213 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001214 SE = Children.end(); SI != SE; ++SI)
1215 WorkList.push_back(*SI);
1216
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001217 if (S->isAbstractScope())
1218 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001219
Devang Patel7e623022011-08-10 20:55:27 +00001220 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001221 if (Ranges.empty())
1222 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001223 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +00001224 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001225 assert(RI->first && "InsnRange does not have first instruction!");
1226 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001227 requestLabelBeforeInsn(RI->first);
1228 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001229 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001230 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001231}
1232
Devang Patel589845d2011-05-09 22:14:49 +00001233/// getScopeNode - Get MDNode for DebugLoc's scope.
1234static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1235 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1236 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1237 return DL.getScope(Ctx);
1238}
1239
Devang Patel34a66202011-05-11 19:22:19 +00001240/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
Eric Christopher34164192012-04-03 00:43:49 +00001241/// line number info for the function.
Devang Patel34a66202011-05-11 19:22:19 +00001242static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1243 const MDNode *Scope = getScopeNode(DL, Ctx);
1244 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher34164192012-04-03 00:43:49 +00001245 if (SP.Verify()) {
1246 // Check for number of operands since the compatibility is
1247 // cheap here.
Eric Christopherb81e2b42012-04-03 17:55:42 +00001248 if (SP->getNumOperands() > 19)
Eric Christopher34164192012-04-03 00:43:49 +00001249 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1250 else
1251 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1252 }
1253
Devang Patel34a66202011-05-11 19:22:19 +00001254 return DebugLoc();
1255}
1256
Devang Patel930143b2009-11-21 02:48:08 +00001257/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00001258/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00001259void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001260 if (!MMI->hasDebugInfo()) return;
Devang Patel7e623022011-08-10 20:55:27 +00001261 LScopes.initialize(*MF);
1262 if (LScopes.empty()) return;
1263 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001264
Devang Patel6c74a872010-04-27 19:46:33 +00001265 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1266 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001267 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001268 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001269
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001270 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1271
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001272 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001273 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1274 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1275
Devang Patel002d54d2010-05-26 19:37:24 +00001276 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001277 I != E; ++I) {
1278 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001279 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1280 II != IE; ++II) {
1281 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001282
Devang Patel002d54d2010-05-26 19:37:24 +00001283 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001284 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001285
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001286 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001287 const MDNode *Var =
1288 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001289
1290 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001291 if (isDbgValueInDefinedReg(MI))
1292 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1293
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001294 // Check the history of this variable.
1295 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1296 if (History.empty()) {
1297 UserVariables.push_back(Var);
1298 // The first mention of a function argument gets the FunctionBeginSym
1299 // label, so arguments are visible when breaking at function entry.
1300 DIVariable DV(Var);
1301 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1302 DISubprogram(getDISubprogram(DV.getContext()))
1303 .describes(MF->getFunction()))
1304 LabelsBeforeInsn[MI] = FunctionBeginSym;
1305 } else {
1306 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1307 const MachineInstr *Prev = History.back();
1308 if (Prev->isDebugValue()) {
1309 // Coalesce identical entries at the end of History.
1310 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001311 Prev->isIdenticalTo(History[History.size() - 2])) {
1312 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1313 << "\t" << *Prev
1314 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001315 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001316 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001317
1318 // Terminate old register assignments that don't reach MI;
1319 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1320 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1321 isDbgValueInDefinedReg(Prev)) {
1322 // Previous register assignment needs to terminate at the end of
1323 // its basic block.
1324 MachineBasicBlock::const_iterator LastMI =
1325 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001326 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001327 // Drop DBG_VALUE for empty range.
Devang Patelb7a328e2011-07-07 00:14:27 +00001328 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1329 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001330 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001331 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001332 else {
1333 // Terminate after LastMI.
1334 History.push_back(LastMI);
1335 }
1336 }
1337 }
1338 }
1339 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001340 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001341 // Not a DBG_VALUE instruction.
1342 if (!MI->isLabel())
1343 AtBlockEntry = false;
1344
Devang Patel34a66202011-05-11 19:22:19 +00001345 // First known non DBG_VALUE location marks beginning of function
1346 // body.
1347 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1348 PrologEndLoc = MI->getDebugLoc();
1349
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001350 // Check if the instruction clobbers any registers with debug vars.
1351 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1352 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1353 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1354 continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001355 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1356 AI.isValid(); ++AI) {
1357 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001358 const MDNode *Var = LiveUserVar[Reg];
1359 if (!Var)
1360 continue;
1361 // Reg is now clobbered.
1362 LiveUserVar[Reg] = 0;
1363
1364 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001365 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1366 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001367 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001368 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1369 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001370 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001371 const MachineInstr *Prev = History.back();
1372 // Sanity-check: Register assignments are terminated at the end of
1373 // their block.
1374 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1375 continue;
1376 // Is the variable still in Reg?
1377 if (!isDbgValueInDefinedReg(Prev) ||
1378 Prev->getOperand(0).getReg() != Reg)
1379 continue;
1380 // Var is clobbered. Make sure the next instruction gets a label.
1381 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001382 }
1383 }
Devang Patel002d54d2010-05-26 19:37:24 +00001384 }
Devang Patel002d54d2010-05-26 19:37:24 +00001385 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001386 }
1387
1388 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1389 I != E; ++I) {
1390 SmallVectorImpl<const MachineInstr*> &History = I->second;
1391 if (History.empty())
1392 continue;
1393
1394 // Make sure the final register assignments are terminated.
1395 const MachineInstr *Prev = History.back();
1396 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1397 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel784077e2011-08-10 23:58:09 +00001398 MachineBasicBlock::const_iterator LastMI =
1399 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001400 if (LastMI == PrevMBB->end())
1401 // Drop DBG_VALUE for empty range.
1402 History.pop_back();
1403 else {
1404 // Terminate after LastMI.
1405 History.push_back(LastMI);
1406 }
1407 }
1408 // Request labels for the full history.
1409 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1410 const MachineInstr *MI = History[i];
1411 if (MI->isDebugValue())
1412 requestLabelBeforeInsn(MI);
1413 else
1414 requestLabelAfterInsn(MI);
1415 }
1416 }
Devang Patel002d54d2010-05-26 19:37:24 +00001417
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001418 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001419 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001420
1421 // Record beginning of function.
1422 if (!PrologEndLoc.isUnknown()) {
1423 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1424 MF->getFunction()->getContext());
1425 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1426 FnStartDL.getScope(MF->getFunction()->getContext()),
Eric Christopherbf570912012-07-12 23:30:25 +00001427 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0);
Devang Patel34a66202011-05-11 19:22:19 +00001428 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001429}
1430
Devang Patel7e623022011-08-10 20:55:27 +00001431void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1432// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1433 ScopeVariables[LS].push_back(Var);
1434// Vars.push_back(Var);
1435}
1436
Devang Patel930143b2009-11-21 02:48:08 +00001437/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00001438///
Chris Lattner76555b52010-01-26 23:18:02 +00001439void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patel7e623022011-08-10 20:55:27 +00001440 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00001441
Devang Patel7e623022011-08-10 20:55:27 +00001442 // Define end label for subprogram.
1443 FunctionEndSym = Asm->GetTempSymbol("func_end",
1444 Asm->getFunctionNumber());
1445 // Assumes in correct section after the entry point.
1446 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1447
1448 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1449 collectVariableInfo(MF, ProcessedVars);
1450
Devang Patel3acc70e2011-08-15 22:04:40 +00001451 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001452 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001453 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001454
Devang Patel7e623022011-08-10 20:55:27 +00001455 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001456 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1457 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1458 LexicalScope *AScope = AList[i];
1459 DISubprogram SP(AScope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +00001460 if (SP.Verify()) {
1461 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001462 DIArray Variables = SP.getVariables();
1463 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1464 DIVariable DV(Variables.getElement(i));
1465 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1466 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001467 // Check that DbgVariable for DV wasn't created earlier, when
1468 // findAbstractVariable() was called for inlined instance of DV.
1469 LLVMContext &Ctx = DV->getContext();
1470 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1471 if (AbstractVariables.lookup(CleanDV))
1472 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001473 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1474 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001475 }
1476 }
Devang Patel44403472011-08-12 18:10:19 +00001477 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Patel3acc70e2011-08-15 22:04:40 +00001478 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001479 }
Devang Patel7e623022011-08-10 20:55:27 +00001480
Devang Patel3acc70e2011-08-15 22:04:40 +00001481 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patel7e623022011-08-10 20:55:27 +00001482
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001483 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopherbb69a272012-08-24 01:14:27 +00001484 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001485
Devang Patel7e623022011-08-10 20:55:27 +00001486 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1487 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00001488
Bill Wendling2b128d72009-05-20 23:19:06 +00001489 // Clear debug info
Devang Patel7e623022011-08-10 20:55:27 +00001490 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1491 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1492 DeleteContainerPointers(I->second);
1493 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001494 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001495 UserVariables.clear();
1496 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001497 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001498 LabelsBeforeInsn.clear();
1499 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001500 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001501}
1502
Chris Lattnerba35a672010-03-09 04:54:43 +00001503/// recordSourceLine - Register a source line with debug info. Returns the
1504/// unique label that was emitted and which provides correspondence to
1505/// the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001506void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1507 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001508 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001509 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001510 unsigned Src = 1;
1511 if (S) {
1512 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001513
Dan Gohman50849c62010-05-05 23:41:32 +00001514 if (Scope.isCompileUnit()) {
1515 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001516 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001517 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001518 } else if (Scope.isFile()) {
1519 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001520 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001521 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001522 } else if (Scope.isSubprogram()) {
1523 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001524 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001525 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001526 } else if (Scope.isLexicalBlockFile()) {
1527 DILexicalBlockFile DBF(S);
1528 Fn = DBF.getFilename();
1529 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001530 } else if (Scope.isLexicalBlock()) {
1531 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001532 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001533 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001534 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001535 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001536
Devang Patele01b75c2011-03-24 20:30:50 +00001537 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00001538 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001539 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001540}
1541
Bill Wendling806535f2009-05-20 23:22:40 +00001542//===----------------------------------------------------------------------===//
1543// Emit Methods
1544//===----------------------------------------------------------------------===//
1545
Devang Patel930143b2009-11-21 02:48:08 +00001546/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00001547///
Jim Grosbach00e9c612009-11-22 19:20:36 +00001548unsigned
1549DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00001550 // Get the children.
1551 const std::vector<DIE *> &Children = Die->getChildren();
1552
Bill Wendling480ff322009-05-20 23:21:38 +00001553 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001554 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001555
1556 // Get the abbreviation for this DIE.
1557 unsigned AbbrevNumber = Die->getAbbrevNumber();
1558 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1559
1560 // Set DIE offset
1561 Die->setOffset(Offset);
1562
1563 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001564 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001565
1566 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1567 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1568
1569 // Size the DIE attribute values.
1570 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1571 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001572 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001573
1574 // Size the DIE children if any.
1575 if (!Children.empty()) {
1576 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1577 "Children flag not set");
1578
1579 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001580 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00001581
1582 // End of children marker.
1583 Offset += sizeof(int8_t);
1584 }
1585
1586 Die->setSize(Offset - Die->getOffset());
1587 return Offset;
1588}
1589
Devang Patel930143b2009-11-21 02:48:08 +00001590/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00001591///
Devang Patel930143b2009-11-21 02:48:08 +00001592void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001593 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1594 E = CUMap.end(); I != E; ++I) {
1595 // Compute size of compile unit header.
Devang Patel28dce702011-04-12 23:10:47 +00001596 unsigned Offset =
Devang Patel1a0df9a2010-05-10 22:49:55 +00001597 sizeof(int32_t) + // Length of Compilation Unit Info
1598 sizeof(int16_t) + // DWARF version number
1599 sizeof(int32_t) + // Offset Into Abbrev. Section
1600 sizeof(int8_t); // Pointer Size (in bytes)
1601 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001602 }
Bill Wendling480ff322009-05-20 23:21:38 +00001603}
1604
Chris Lattner6629ca92010-04-04 22:59:04 +00001605/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1606/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00001607void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001608 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001609
Bill Wendling480ff322009-05-20 23:21:38 +00001610 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001611 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001612 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001613 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001614 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001615 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001616
Chris Lattner6629ca92010-04-04 22:59:04 +00001617 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001618 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001619
Devang Patel4a213872010-08-24 00:06:12 +00001620 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001621 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001622 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001623 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001624 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00001625 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1626 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001627
Devang Patel9fc11702010-05-25 23:40:22 +00001628 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1629 "section_debug_loc");
1630
Chris Lattner6629ca92010-04-04 22:59:04 +00001631 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00001632 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001633}
1634
Nick Lewycky019d2552011-07-29 03:49:23 +00001635/// emitDIE - Recursively emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00001636///
Devang Patel930143b2009-11-21 02:48:08 +00001637void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001638 // Get the abbreviation for this DIE.
1639 unsigned AbbrevNumber = Die->getAbbrevNumber();
1640 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1641
Bill Wendling480ff322009-05-20 23:21:38 +00001642 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001643 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00001644 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1645 Twine::utohexstr(Die->getOffset()) + ":0x" +
1646 Twine::utohexstr(Die->getSize()) + " " +
1647 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00001648 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001649
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001650 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00001651 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1652
1653 // Emit the DIE attribute values.
1654 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1655 unsigned Attr = AbbrevData[i].getAttribute();
1656 unsigned Form = AbbrevData[i].getForm();
1657 assert(Form && "Too many attributes for DIE (check abbreviation)");
1658
Chris Lattner7bde8c02010-04-04 18:52:31 +00001659 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001660 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001661
Bill Wendling480ff322009-05-20 23:21:38 +00001662 switch (Attr) {
Bill Wendling480ff322009-05-20 23:21:38 +00001663 case dwarf::DW_AT_abstract_origin: {
1664 DIEEntry *E = cast<DIEEntry>(Values[i]);
1665 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001666 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00001667 Asm->EmitInt32(Addr);
1668 break;
1669 }
Devang Patel12563b32010-04-16 23:33:45 +00001670 case dwarf::DW_AT_ranges: {
1671 // DW_AT_range Value encodes offset in debug_range section.
1672 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00001673
Nick Lewycky33da3362012-06-22 01:25:12 +00001674 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelda3ef852010-09-02 16:43:44 +00001675 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1676 V->getValue(),
1677 4);
1678 } else {
1679 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1680 V->getValue(),
1681 DwarfDebugRangeSectionSym,
1682 4);
1683 }
Devang Patel12563b32010-04-16 23:33:45 +00001684 break;
1685 }
Devang Patel9fc11702010-05-25 23:40:22 +00001686 case dwarf::DW_AT_location: {
Nick Lewycky33da3362012-06-22 01:25:12 +00001687 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1688 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1689 Asm->EmitLabelReference(L->getValue(), 4);
1690 else
1691 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1692 } else {
Devang Patel9fc11702010-05-25 23:40:22 +00001693 Values[i]->EmitValue(Asm, Form);
Nick Lewycky33da3362012-06-22 01:25:12 +00001694 }
Devang Patel9fc11702010-05-25 23:40:22 +00001695 break;
1696 }
Devang Patela1bd5a12010-09-29 19:08:08 +00001697 case dwarf::DW_AT_accessibility: {
1698 if (Asm->isVerbose()) {
1699 DIEInteger *V = cast<DIEInteger>(Values[i]);
1700 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1701 }
1702 Values[i]->EmitValue(Asm, Form);
1703 break;
1704 }
Bill Wendling480ff322009-05-20 23:21:38 +00001705 default:
1706 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001707 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001708 break;
1709 }
Bill Wendling480ff322009-05-20 23:21:38 +00001710 }
1711
1712 // Emit the DIE children if any.
1713 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1714 const std::vector<DIE *> &Children = Die->getChildren();
1715
1716 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001717 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00001718
Chris Lattner7bde8c02010-04-04 18:52:31 +00001719 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00001720 Asm->OutStreamer.AddComment("End Of Children Mark");
1721 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001722 }
1723}
1724
Devang Patel9ccfb642009-12-09 18:24:21 +00001725/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00001726///
Devang Patel9ccfb642009-12-09 18:24:21 +00001727void DwarfDebug::emitDebugInfo() {
1728 // Start debug info section.
1729 Asm->OutStreamer.SwitchSection(
1730 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001731 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1732 E = CUMap.end(); I != E; ++I) {
1733 CompileUnit *TheCU = I->second;
1734 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001735
Devang Patel1a0df9a2010-05-10 22:49:55 +00001736 // Emit the compile units header.
1737 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1738 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001739
Devang Patel1a0df9a2010-05-10 22:49:55 +00001740 // Emit size of content not including length itself
1741 unsigned ContentSize = Die->getSize() +
1742 sizeof(int16_t) + // DWARF version number
1743 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00001744 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001745
Devang Patel1a0df9a2010-05-10 22:49:55 +00001746 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1747 Asm->EmitInt32(ContentSize);
1748 Asm->OutStreamer.AddComment("DWARF version number");
1749 Asm->EmitInt16(dwarf::DWARF_VERSION);
1750 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1751 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1752 DwarfAbbrevSectionSym);
1753 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1754 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001755
Devang Patel1a0df9a2010-05-10 22:49:55 +00001756 emitDIE(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001757 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1758 }
Bill Wendling480ff322009-05-20 23:21:38 +00001759}
1760
Devang Patel930143b2009-11-21 02:48:08 +00001761/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00001762///
Devang Patel930143b2009-11-21 02:48:08 +00001763void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00001764 // Check to see if it is worth the effort.
1765 if (!Abbreviations.empty()) {
1766 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001767 Asm->OutStreamer.SwitchSection(
1768 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001769
Chris Lattnera179b522010-04-04 19:25:43 +00001770 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00001771
1772 // For each abbrevation.
1773 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1774 // Get abbreviation data
1775 const DIEAbbrev *Abbrev = Abbreviations[i];
1776
1777 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00001778 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00001779
1780 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001781 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00001782 }
1783
1784 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00001785 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00001786
Chris Lattnera179b522010-04-04 19:25:43 +00001787 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00001788 }
1789}
1790
Devang Patel930143b2009-11-21 02:48:08 +00001791/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00001792/// the line matrix.
1793///
Devang Patel930143b2009-11-21 02:48:08 +00001794void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00001795 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00001796 Asm->OutStreamer.AddComment("Extended Op");
1797 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001798
Chris Lattner566cae92010-03-09 23:52:58 +00001799 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00001800 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00001801 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1802 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1803
1804 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00001805
Chris Lattnera179b522010-04-04 19:25:43 +00001806 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00001807 Asm->getTargetData().getPointerSize(),
1808 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00001809
1810 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00001811 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1812 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001813 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00001814 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00001815}
1816
Eric Christopher4996c702011-11-07 09:24:32 +00001817/// emitAccelNames - Emit visible names into a hashed accelerator table
1818/// section.
1819void DwarfDebug::emitAccelNames() {
1820 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1821 dwarf::DW_FORM_data4));
1822 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1823 E = CUMap.end(); I != E; ++I) {
1824 CompileUnit *TheCU = I->second;
Eric Christopherd9843b32011-11-10 19:25:34 +00001825 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1826 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00001827 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1828 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001829 const std::vector<DIE *> &Entities = GI->second;
Eric Christopherd9843b32011-11-10 19:25:34 +00001830 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1831 DE = Entities.end(); DI != DE; ++DI)
1832 AT.AddName(Name, (*DI));
Eric Christopher4996c702011-11-07 09:24:32 +00001833 }
1834 }
1835
1836 AT.FinalizeTable(Asm, "Names");
1837 Asm->OutStreamer.SwitchSection(
1838 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1839 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1840 Asm->OutStreamer.EmitLabel(SectionBegin);
1841
1842 // Emit the full data.
1843 AT.Emit(Asm, SectionBegin, this);
1844}
1845
1846/// emitAccelObjC - Emit objective C classes and categories into a hashed
1847/// accelerator table section.
1848void DwarfDebug::emitAccelObjC() {
1849 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1850 dwarf::DW_FORM_data4));
1851 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1852 E = CUMap.end(); I != E; ++I) {
1853 CompileUnit *TheCU = I->second;
1854 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1855 for (StringMap<std::vector<DIE*> >::const_iterator
1856 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1857 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001858 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher4996c702011-11-07 09:24:32 +00001859 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1860 DE = Entities.end(); DI != DE; ++DI)
1861 AT.AddName(Name, (*DI));
1862 }
1863 }
1864
1865 AT.FinalizeTable(Asm, "ObjC");
1866 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1867 .getDwarfAccelObjCSection());
1868 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1869 Asm->OutStreamer.EmitLabel(SectionBegin);
1870
1871 // Emit the full data.
1872 AT.Emit(Asm, SectionBegin, this);
1873}
1874
1875/// emitAccelNamespace - Emit namespace dies into a hashed accelerator
1876/// table.
1877void DwarfDebug::emitAccelNamespaces() {
1878 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1879 dwarf::DW_FORM_data4));
1880 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1881 E = CUMap.end(); I != E; ++I) {
1882 CompileUnit *TheCU = I->second;
Eric Christopher66b37db2011-11-10 21:47:55 +00001883 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
1884 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00001885 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1886 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001887 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher66b37db2011-11-10 21:47:55 +00001888 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1889 DE = Entities.end(); DI != DE; ++DI)
1890 AT.AddName(Name, (*DI));
Eric Christopher4996c702011-11-07 09:24:32 +00001891 }
1892 }
1893
1894 AT.FinalizeTable(Asm, "namespac");
1895 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1896 .getDwarfAccelNamespaceSection());
1897 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
1898 Asm->OutStreamer.EmitLabel(SectionBegin);
1899
1900 // Emit the full data.
1901 AT.Emit(Asm, SectionBegin, this);
1902}
1903
1904/// emitAccelTypes() - Emit type dies into a hashed accelerator table.
1905void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00001906 std::vector<DwarfAccelTable::Atom> Atoms;
1907 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1908 dwarf::DW_FORM_data4));
1909 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
1910 dwarf::DW_FORM_data2));
1911 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
1912 dwarf::DW_FORM_data1));
1913 DwarfAccelTable AT(Atoms);
Eric Christopher4996c702011-11-07 09:24:32 +00001914 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1915 E = CUMap.end(); I != E; ++I) {
1916 CompileUnit *TheCU = I->second;
Eric Christopher21bde872012-01-06 04:35:23 +00001917 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
1918 = TheCU->getAccelTypes();
1919 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00001920 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1921 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001922 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopher21bde872012-01-06 04:35:23 +00001923 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
1924 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
1925 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher4996c702011-11-07 09:24:32 +00001926 }
1927 }
1928
1929 AT.FinalizeTable(Asm, "types");
1930 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1931 .getDwarfAccelTypesSection());
1932 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
1933 Asm->OutStreamer.EmitLabel(SectionBegin);
1934
1935 // Emit the full data.
1936 AT.Emit(Asm, SectionBegin, this);
1937}
1938
Devang Patel04d2f2d2009-11-24 01:14:22 +00001939void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001940 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1941 E = CUMap.end(); I != E; ++I) {
1942 CompileUnit *TheCU = I->second;
Eric Christopher6abc9c52011-11-07 09:18:35 +00001943 // Start the dwarf pubtypes section.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001944 Asm->OutStreamer.SwitchSection(
1945 Asm->getObjFileLowering().getDwarfPubTypesSection());
1946 Asm->OutStreamer.AddComment("Length of Public Types Info");
1947 Asm->EmitLabelDifference(
1948 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1949 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001950
Devang Patel1a0df9a2010-05-10 22:49:55 +00001951 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1952 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001953
Devang Patel1a0df9a2010-05-10 22:49:55 +00001954 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1955 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001956
Devang Patel1a0df9a2010-05-10 22:49:55 +00001957 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1958 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1959 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001960
Devang Patel1a0df9a2010-05-10 22:49:55 +00001961 Asm->OutStreamer.AddComment("Compilation Unit Length");
1962 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1963 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1964 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001965
Devang Patel1a0df9a2010-05-10 22:49:55 +00001966 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1967 for (StringMap<DIE*>::const_iterator
1968 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1969 const char *Name = GI->getKeyData();
Nick Lewycky019d2552011-07-29 03:49:23 +00001970 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001971
Devang Patel1a0df9a2010-05-10 22:49:55 +00001972 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1973 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001974
Devang Patel1a0df9a2010-05-10 22:49:55 +00001975 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00001976 // Emit the name with a terminating null byte.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001977 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1978 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001979
Devang Patel1a0df9a2010-05-10 22:49:55 +00001980 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001981 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001982 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1983 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00001984 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001985}
1986
Devang Patel930143b2009-11-21 02:48:08 +00001987/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00001988///
Devang Patel930143b2009-11-21 02:48:08 +00001989void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00001990 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00001991 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001992
Chris Lattner3d72a672010-03-09 23:38:23 +00001993 // Start the dwarf str section.
1994 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001995 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001996
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001997 // Get all of the string pool entries and put them in an array by their ID so
1998 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001999 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002000 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002001
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002002 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2003 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2004 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002005
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002006 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002007
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002008 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002009 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002010 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002011
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002012 // Emit the string itself with a terminating null byte.
Benjamin Kramer148db362011-11-09 12:12:04 +00002013 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
2014 Entries[i].second->getKeyLength()+1),
2015 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00002016 }
2017}
2018
Devang Patel930143b2009-11-21 02:48:08 +00002019/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00002020///
Devang Patel930143b2009-11-21 02:48:08 +00002021void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002022 if (DotDebugLocEntries.empty())
2023 return;
2024
Devang Patel116a9d72011-02-04 22:57:18 +00002025 for (SmallVector<DotDebugLocEntry, 4>::iterator
2026 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2027 I != E; ++I) {
2028 DotDebugLocEntry &Entry = *I;
2029 if (I + 1 != DotDebugLocEntries.end())
2030 Entry.Merge(I+1);
2031 }
2032
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002033 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002034 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00002035 Asm->getObjFileLowering().getDwarfLocSection());
2036 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002037 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2038 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002039 for (SmallVector<DotDebugLocEntry, 4>::iterator
2040 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002041 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002042 DotDebugLocEntry &Entry = *I;
2043 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002044 if (Entry.isEmpty()) {
2045 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2046 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002047 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002048 } else {
2049 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2050 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patel3e021532011-04-28 02:22:40 +00002051 DIVariable DV(Entry.Variable);
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002052 Asm->OutStreamer.AddComment("Loc expr size");
2053 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2054 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2055 Asm->EmitLabelDifference(end, begin, 2);
2056 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002057 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002058 DIBasicType BTy(DV.getType());
2059 if (BTy.Verify() &&
2060 (BTy.getEncoding() == dwarf::DW_ATE_signed
2061 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2062 Asm->OutStreamer.AddComment("DW_OP_consts");
2063 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002064 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002065 } else {
2066 Asm->OutStreamer.AddComment("DW_OP_constu");
2067 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002068 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002069 }
Devang Pateled9fd452011-07-08 16:49:43 +00002070 } else if (Entry.isLocation()) {
2071 if (!DV.hasComplexAddress())
2072 // Regular entry.
Devang Patel3e021532011-04-28 02:22:40 +00002073 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Pateled9fd452011-07-08 16:49:43 +00002074 else {
2075 // Complex address entry.
2076 unsigned N = DV.getNumAddrElements();
2077 unsigned i = 0;
2078 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2079 if (Entry.Loc.getOffset()) {
2080 i = 2;
2081 Asm->EmitDwarfRegOp(Entry.Loc);
2082 Asm->OutStreamer.AddComment("DW_OP_deref");
2083 Asm->EmitInt8(dwarf::DW_OP_deref);
2084 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2085 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2086 Asm->EmitSLEB128(DV.getAddrElement(1));
2087 } else {
2088 // If first address element is OpPlus then emit
2089 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2090 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2091 Asm->EmitDwarfRegOp(Loc);
2092 i = 2;
2093 }
2094 } else {
2095 Asm->EmitDwarfRegOp(Entry.Loc);
2096 }
2097
2098 // Emit remaining complex address elements.
2099 for (; i < N; ++i) {
2100 uint64_t Element = DV.getAddrElement(i);
2101 if (Element == DIBuilder::OpPlus) {
2102 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2103 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002104 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher8d2a77d2012-05-08 21:24:39 +00002105 if (!Entry.Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002106 Asm->EmitInt8(dwarf::DW_OP_deref);
2107 } else
2108 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002109 }
Devang Patel3e021532011-04-28 02:22:40 +00002110 }
Devang Patel3e021532011-04-28 02:22:40 +00002111 }
Devang Pateled9fd452011-07-08 16:49:43 +00002112 // else ... ignore constant fp. There is not any good way to
2113 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002114 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002115 }
2116 }
Bill Wendling480ff322009-05-20 23:21:38 +00002117}
2118
2119/// EmitDebugARanges - Emit visible names into a debug aranges section.
2120///
2121void DwarfDebug::EmitDebugARanges() {
2122 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002123 Asm->OutStreamer.SwitchSection(
2124 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002125}
2126
Devang Patel930143b2009-11-21 02:48:08 +00002127/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00002128///
Devang Patel930143b2009-11-21 02:48:08 +00002129void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002130 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002131 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00002132 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00002133 unsigned char Size = Asm->getTargetData().getPointerSize();
2134 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002135 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00002136 I != E; ++I) {
2137 if (*I)
2138 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00002139 else
Devang Patel6c74a872010-04-27 19:46:33 +00002140 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00002141 }
Bill Wendling480ff322009-05-20 23:21:38 +00002142}
2143
Devang Patel930143b2009-11-21 02:48:08 +00002144/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00002145///
Devang Patel930143b2009-11-21 02:48:08 +00002146void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002147 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002148 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002149 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002150 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002151 }
2152}
2153
Devang Patel930143b2009-11-21 02:48:08 +00002154/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00002155/// Section Header:
2156/// 1. length of section
2157/// 2. Dwarf version number
2158/// 3. address size.
2159///
2160/// Entries (one "entry" for each function that was inlined):
2161///
2162/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2163/// otherwise offset into __debug_str for regular function name.
2164/// 2. offset into __debug_str section for regular function name.
2165/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2166/// instances for the function.
2167///
2168/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2169/// inlined instance; the die_offset points to the inlined_subroutine die in the
2170/// __debug_info section, and the low_pc is the starting address for the
2171/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002172void DwarfDebug::emitDebugInlineInfo() {
Eric Christopher1df94bf2012-03-02 02:11:47 +00002173 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002174 return;
2175
Devang Patel1a0df9a2010-05-10 22:49:55 +00002176 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002177 return;
2178
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002179 Asm->OutStreamer.SwitchSection(
2180 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002181
Chris Lattner566cae92010-03-09 23:52:58 +00002182 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002183 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2184 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002185
Chris Lattnera179b522010-04-04 19:25:43 +00002186 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002187
Chris Lattner566cae92010-03-09 23:52:58 +00002188 Asm->OutStreamer.AddComment("Dwarf Version");
2189 Asm->EmitInt16(dwarf::DWARF_VERSION);
2190 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002191 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002192
Devang Patel32cc43c2010-05-07 20:54:48 +00002193 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002194 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002195
Devang Patel32cc43c2010-05-07 20:54:48 +00002196 const MDNode *Node = *I;
2197 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00002198 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002199 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002200 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002201 StringRef LName = SP.getLinkageName();
2202 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002203
Chris Lattner566cae92010-03-09 23:52:58 +00002204 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher77725312012-03-02 01:57:52 +00002205 if (LName.empty())
2206 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2207 else
Chris Lattner70a4fce2010-04-04 23:25:33 +00002208 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2209 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002210
Chris Lattner566cae92010-03-09 23:52:58 +00002211 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00002212 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00002213 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002214
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002215 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002216 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002217 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002218 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002219
Chris Lattner7bde8c02010-04-04 18:52:31 +00002220 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002221 Asm->OutStreamer.EmitSymbolValue(LI->first,
2222 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00002223 }
2224 }
2225
Chris Lattnera179b522010-04-04 19:25:43 +00002226 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002227}