blob: 52fae53fe84c630e75e69b4f82341c4796cca4ce [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);
Eric Christophera47d0962012-09-10 23:33:57 +0000329 DIType ATy = DIType(Args.getElement(i));
Devang Patel1d6bbd42011-04-22 23:10:17 +0000330 SPCU->addType(Arg, ATy);
331 if (ATy.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +0000332 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
Eric Christophere3417762012-09-12 23:36:19 +0000333 if (ATy.isObjectPointer())
334 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer, dwarf::DW_FORM_ref4,
335 Arg);
Devang Patel1d6bbd42011-04-22 23:10:17 +0000336 SPDie->addChild(Arg);
337 }
338 DIE *SPDeclDie = SPDie;
339 SPDie = new DIE(dwarf::DW_TAG_subprogram);
340 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
341 SPDeclDie);
342 SPCU->addDie(SPDie);
343 }
Chris Lattner3a383cb2010-04-05 00:13:49 +0000344 }
Devang Patela37a95e2010-07-07 22:20:57 +0000345 // Pick up abstract subprogram DIE.
346 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
347 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf20c4f72011-04-12 22:53:02 +0000348 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
349 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patela37a95e2010-07-07 22:20:57 +0000350 SPCU->addDie(SPDie);
351 }
352
Devang Patelf20c4f72011-04-12 22:53:02 +0000353 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
354 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
355 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
356 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattner3a383cb2010-04-05 00:13:49 +0000357 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
358 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000359 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000360
Eric Christopherd9843b32011-11-10 19:25:34 +0000361 // Add name to the name table, we do this here because we're guaranteed
362 // to have concrete versions of our DW_TAG_subprogram nodes.
363 addSubprogramNames(SPCU, SP, SPDie);
364
Chris Lattner3a383cb2010-04-05 00:13:49 +0000365 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000366}
367
Jim Grosbach042483e2009-11-21 23:12:12 +0000368/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +0000369/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000370DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
371 LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000372 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
373 if (Scope->isAbstractScope())
374 return ScopeDIE;
375
Devang Patel7e623022011-08-10 20:55:27 +0000376 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +0000377 if (Ranges.empty())
378 return 0;
379
Devang Patel7e623022011-08-10 20:55:27 +0000380 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel6c74a872010-04-27 19:46:33 +0000381 if (Ranges.size() > 1) {
382 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000383 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000384 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000385 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000386 DebugRangeSymbols.size()
387 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000388 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +0000389 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000390 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
391 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000392 }
393 DebugRangeSymbols.push_back(NULL);
394 DebugRangeSymbols.push_back(NULL);
395 return ScopeDIE;
396 }
397
Devang Patel9fc11702010-05-25 23:40:22 +0000398 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
399 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000400
Devang Patel9fc11702010-05-25 23:40:22 +0000401 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000402
Chris Lattnere13c3722010-03-09 01:58:53 +0000403 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
404 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000405
Devang Patelf20c4f72011-04-12 22:53:02 +0000406 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
407 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000408
409 return ScopeDIE;
410}
411
Devang Patel930143b2009-11-21 02:48:08 +0000412/// constructInlinedScopeDIE - This scope represents inlined body of
413/// a function. Construct DIE to represent this concrete inlined copy
414/// of the function.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000415DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
416 LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +0000417 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000418 assert(Ranges.empty() == false &&
419 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000420
Devang Patelf098ce22011-07-27 00:34:13 +0000421 if (!Scope->getScopeNode())
422 return NULL;
423 DIScope DS(Scope->getScopeNode());
424 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patelf098ce22011-07-27 00:34:13 +0000425 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
426 if (!OriginDIE) {
427 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
428 return NULL;
429 }
430
Devang Patel7e623022011-08-10 20:55:27 +0000431 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +0000432 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
433 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000434
Devang Patel4c6bd662010-07-08 22:39:20 +0000435 if (StartLabel == 0 || EndLabel == 0) {
Craig Topperee4dab52012-02-05 08:31:47 +0000436 llvm_unreachable("Unexpected Start and End labels for a inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000437 }
Chris Lattnere13c3722010-03-09 01:58:53 +0000438 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000439 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +0000440 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000441 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000442
Devang Patel73bc1722011-05-05 17:54:26 +0000443 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patelf20c4f72011-04-12 22:53:02 +0000444 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
445 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000446
Devang Patelf098ce22011-07-27 00:34:13 +0000447 if (Ranges.size() > 1) {
448 // .debug_range section has not been laid out yet. Emit offset in
449 // .debug_range as a uint, size 4, for now. emitDIE will handle
450 // DW_AT_ranges appropriately.
451 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000452 DebugRangeSymbols.size()
453 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000454 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patelf098ce22011-07-27 00:34:13 +0000455 RE = Ranges.end(); RI != RE; ++RI) {
456 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
457 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
458 }
459 DebugRangeSymbols.push_back(NULL);
460 DebugRangeSymbols.push_back(NULL);
461 } else {
Devang Patel784077e2011-08-10 23:58:09 +0000462 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
463 StartLabel);
464 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
465 EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000466 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000467
468 InlinedSubprogramDIEs.insert(OriginDIE);
469
470 // Track the start label for this inlined function.
Devang Patelf098ce22011-07-27 00:34:13 +0000471 //.debug_inlined section specification does not clearly state how
472 // to emit inlined scope that is split into multiple instruction ranges.
473 // For now, use first instruction range and emit low_pc/high_pc pair and
474 // corresponding .debug_inlined section entry for this pair.
Devang Patel32cc43c2010-05-07 20:54:48 +0000475 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000476 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000477
478 if (I == InlineInfo.end()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000479 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000480 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000481 } else
Chris Lattner085b6522010-03-09 00:31:02 +0000482 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000483
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000484 DILocation DL(Scope->getInlinedAt());
Eric Christopher0925c622012-03-26 21:38:38 +0000485 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
486 GetOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patelf20c4f72011-04-12 22:53:02 +0000487 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000488
Eric Christopher8dda5d02011-12-04 06:02:38 +0000489 // Add name to the name table, we do this here because we're guaranteed
490 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
491 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
492
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000493 return ScopeDIE;
494}
495
Devang Patel930143b2009-11-21 02:48:08 +0000496/// constructScopeDIE - Construct a DIE for this scope.
Devang Patel3acc70e2011-08-15 22:04:40 +0000497DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000498 if (!Scope || !Scope->getScopeNode())
499 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000500
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000501 SmallVector<DIE *, 8> Children;
Eric Christophere3417762012-09-12 23:36:19 +0000502 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000503
504 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000505 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000506 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
507 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Patel3acc70e2011-08-15 22:04:40 +0000508 if (DIE *Arg =
Eric Christophere3417762012-09-12 23:36:19 +0000509 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000510 Children.push_back(Arg);
Eric Christophere3417762012-09-12 23:36:19 +0000511 if (ArgDV->isObjectPointer()) ObjectPointer = Arg;
512 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000513
Eric Christopherf84354b2011-10-03 15:49:16 +0000514 // Collect lexical scope children first.
Devang Patel7e623022011-08-10 20:55:27 +0000515 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000516 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Patel3acc70e2011-08-15 22:04:40 +0000517 if (DIE *Variable =
518 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000519 Children.push_back(Variable);
Devang Patel7e623022011-08-10 20:55:27 +0000520 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000521 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000522 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000523 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000524 DIScope DS(Scope->getScopeNode());
525 DIE *ScopeDIE = NULL;
526 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000527 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000528 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000529 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000530 if (Scope->isAbstractScope()) {
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000531 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000532 // Note down abstract DIE.
533 if (ScopeDIE)
534 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
535 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000536 else
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000537 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000538 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000539 else {
540 // There is no need to emit empty lexical block DIE.
541 if (Children.empty())
542 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000543 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000544 }
545
Devang Patel23b2ae62010-03-29 22:59:58 +0000546 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000547
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000548 // Add children
549 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
550 E = Children.end(); I != E; ++I)
551 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000552
Eric Christophere3417762012-09-12 23:36:19 +0000553 if (DS.isSubprogram() && ObjectPointer != NULL)
554 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer,
555 dwarf::DW_FORM_ref4, ObjectPointer);
556
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000557 if (DS.isSubprogram())
Eric Christopherd9843b32011-11-10 19:25:34 +0000558 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000559
Eric Christopherd9843b32011-11-10 19:25:34 +0000560 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000561}
562
Bill Wendling2b128d72009-05-20 23:19:06 +0000563/// GetOrCreateSourceID - Look up the source id with the given directory and
564/// source file names. If none currently exists, create a new id and insert it
565/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
566/// maps as well.
Devang Patele01b75c2011-03-24 20:30:50 +0000567unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
568 StringRef DirName) {
Devang Patel871d0b12010-09-16 20:57:49 +0000569 // If FE did not provide a file name, then assume stdin.
570 if (FileName.empty())
Devang Patele01b75c2011-03-24 20:30:50 +0000571 return GetOrCreateSourceID("<stdin>", StringRef());
572
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000573 // TODO: this might not belong here. See if we can factor this better.
574 if (DirName == CompilationDir)
575 DirName = "";
576
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000577 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel871d0b12010-09-16 20:57:49 +0000578
Benjamin Kramer71b19732012-03-11 14:56:26 +0000579 // We look up the file/dir pair by concatenating them with a zero byte.
580 SmallString<128> NamePair;
581 NamePair += DirName;
582 NamePair += '\0'; // Zero bytes are not allowed in paths.
583 NamePair += FileName;
584
585 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
586 if (Ent.getValue() != SrcId)
587 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000588
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000589 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000590 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendling2b128d72009-05-20 23:19:06 +0000591
592 return SrcId;
593}
594
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000595/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel1a0df9a2010-05-10 22:49:55 +0000596/// metadata node with tag DW_TAG_compile_unit.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000597CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000598 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +0000599 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000600 CompilationDir = DIUnit.getDirectory();
601 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000602
603 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopherabb4d9e2012-09-10 23:34:00 +0000604 CompileUnit *NewCU = new CompileUnit(ID, DIUnit.getLanguage(), Die,
605 Asm, this);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000606 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000607 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
608 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000609 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopherb1b94512012-08-01 18:19:01 +0000610 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
611 // into an entity.
612 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +0000613 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +0000614 // compile unit in debug_line section.
Rafael Espindolad7bdaf52012-06-22 13:24:07 +0000615 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindolaa7558912011-05-04 17:44:06 +0000616 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patelf20c4f72011-04-12 22:53:02 +0000617 Asm->GetTempSymbol("section_line"));
Devang Patelea636392010-08-31 23:50:19 +0000618 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000619 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +0000620
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000621 if (!CompilationDir.empty())
622 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000623 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000624 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000625
Devang Patel2d9caf92009-11-25 17:36:49 +0000626 StringRef Flags = DIUnit.getFlags();
627 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000628 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patelf20c4f72011-04-12 22:53:02 +0000629
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000630 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000631 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000632 dwarf::DW_FORM_data1, RVer);
633
Devang Patel1a0df9a2010-05-10 22:49:55 +0000634 if (!FirstCU)
635 FirstCU = NewCU;
636 CUMap.insert(std::make_pair(N, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000637 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000638}
639
Devang Patel1a0df9a2010-05-10 22:49:55 +0000640/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel1f4f98d2011-08-15 23:36:40 +0000641void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
642 const MDNode *N) {
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000643 CompileUnit *&CURef = SPMap[N];
644 if (CURef)
645 return;
646 CURef = TheCU;
647
Devang Patel80ae3492009-08-28 23:24:31 +0000648 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000649 if (!SP.isDefinition())
650 // This is a method declaration which will be handled while constructing
651 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000652 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000653
Devang Patel89543712011-08-15 17:24:54 +0000654 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000655
656 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000657 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000658
659 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +0000660 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +0000661
Devang Patel0751a282009-06-26 01:49:18 +0000662 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000663}
664
Devang Patel07bb9ee2011-08-15 23:47:24 +0000665/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
666/// as llvm.dbg.enum and llvm.dbg.ty
667void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000668 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
669 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
670 const MDNode *N = NMD->getOperand(i);
671 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
672 constructSubprogramDIE(CU, N);
673 }
674
675 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
676 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
677 const MDNode *N = NMD->getOperand(i);
678 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000679 CU->createGlobalVariableDIE(N);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000680 }
681
Devang Patel07bb9ee2011-08-15 23:47:24 +0000682 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
683 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
684 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000685 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
686 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000687 }
688
689 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
690 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
691 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000692 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
693 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000694 }
695}
696
697/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
698/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
699bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
700 DebugInfoFinder DbgFinder;
701 DbgFinder.processModule(*M);
702
703 bool HasDebugInfo = false;
704 // Scan all the compile-units to see if there are any marked as the main
705 // unit. If not, we do not generate debug info.
706 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
707 E = DbgFinder.compile_unit_end(); I != E; ++I) {
708 if (DICompileUnit(*I).isMain()) {
709 HasDebugInfo = true;
710 break;
711 }
712 }
713 if (!HasDebugInfo) return false;
714
715 // Create all the compile unit DIEs.
716 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
717 E = DbgFinder.compile_unit_end(); I != E; ++I)
718 constructCompileUnit(*I);
719
720 // Create DIEs for each global variable.
721 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
722 E = DbgFinder.global_variable_end(); I != E; ++I) {
723 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000724 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000725 CU->createGlobalVariableDIE(N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000726 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000727
Devang Patel07bb9ee2011-08-15 23:47:24 +0000728 // Create DIEs for each subprogram.
729 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
730 E = DbgFinder.subprogram_end(); I != E; ++I) {
731 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000732 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
733 constructSubprogramDIE(CU, N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000734 }
735
736 return HasDebugInfo;
737}
738
Devang Patel930143b2009-11-21 02:48:08 +0000739/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000740/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky019d2552011-07-29 03:49:23 +0000741/// This is invoked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +0000742void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +0000743 if (DisableDebugInfoPrinting)
744 return;
745
Nick Lewycky019d2552011-07-29 03:49:23 +0000746 // If module has named metadata anchors then use them, otherwise scan the
747 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000748 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
749 if (CU_Nodes) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000750 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
751 DICompileUnit CUNode(CU_Nodes->getOperand(i));
752 CompileUnit *CU = constructCompileUnit(CUNode);
753 DIArray GVs = CUNode.getGlobalVariables();
754 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000755 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000756 DIArray SPs = CUNode.getSubprograms();
757 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
758 constructSubprogramDIE(CU, SPs.getElement(i));
759 DIArray EnumTypes = CUNode.getEnumTypes();
760 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
761 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
762 DIArray RetainedTypes = CUNode.getRetainedTypes();
763 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
764 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
765 }
Devang Patel07bb9ee2011-08-15 23:47:24 +0000766 } else if (!collectLegacyDebugInfo(M))
767 return;
Devang Patele02e5852011-05-03 16:45:22 +0000768
Devang Patel07bb9ee2011-08-15 23:47:24 +0000769 collectInfoFromNamedMDNodes(M);
Devang Patele02e5852011-05-03 16:45:22 +0000770
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000771 // Tell MMI that we have debug info.
772 MMI->setDebugInfoAvailability(true);
Devang Patele02e5852011-05-03 16:45:22 +0000773
Chris Lattnerd442aa32010-04-04 23:17:54 +0000774 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000775 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000776
Bill Wendling2b128d72009-05-20 23:19:06 +0000777 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +0000778 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +0000779}
780
Devang Patel930143b2009-11-21 02:48:08 +0000781/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +0000782///
Devang Patel930143b2009-11-21 02:48:08 +0000783void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000784 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +0000785 const Module *M = MMI->getModule();
Devang Patel7e623022011-08-10 20:55:27 +0000786 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +0000787
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000788 // Collect info for variables that were optimized out.
789 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
790 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
791 DICompileUnit TheCU(CU_Nodes->getOperand(i));
792 DIArray Subprograms = TheCU.getSubprograms();
793 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
794 DISubprogram SP(Subprograms.getElement(i));
795 if (ProcessedSPNodes.count(SP) != 0) continue;
796 if (!SP.Verify()) continue;
797 if (!SP.isDefinition()) continue;
Devang Patel59e27c52011-08-19 23:28:12 +0000798 DIArray Variables = SP.getVariables();
799 if (Variables.getNumElements() == 0) continue;
800
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000801 LexicalScope *Scope =
802 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
803 DeadFnScopeMap[SP] = Scope;
804
805 // Construct subprogram DIE and add variables DIEs.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000806 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000807 assert(SPCU && "Unable to find Compile Unit!");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000808 constructSubprogramDIE(SPCU, SP);
809 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel59e27c52011-08-19 23:28:12 +0000810 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
811 DIVariable DV(Variables.getElement(vi));
812 if (!DV.Verify()) continue;
813 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000814 if (DIE *VariableDIE =
Devang Patel59e27c52011-08-19 23:28:12 +0000815 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000816 ScopeDIE->addChild(VariableDIE);
817 }
Devang Patelf3b2db62010-06-28 18:25:03 +0000818 }
819 }
820 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000821
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000822 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
823 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
824 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
825 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +0000826 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000827 }
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000828 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
829 AE = AbstractSPDies.end(); AI != AE; ++AI) {
830 DIE *ISP = AI->second;
831 if (InlinedSubprogramDIEs.count(ISP))
832 continue;
833 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
834 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000835
Eric Christopherb1b94512012-08-01 18:19:01 +0000836 // Emit DW_AT_containing_type attribute to connect types with their
837 // vtable holding type.
Devang Patel89543712011-08-15 17:24:54 +0000838 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
839 CUE = CUMap.end(); CUI != CUE; ++CUI) {
840 CompileUnit *TheCU = CUI->second;
841 TheCU->constructContainingTypeDIEs();
Devang Pateleb57c592009-12-03 19:11:07 +0000842 }
843
Bill Wendling2b128d72009-05-20 23:19:06 +0000844 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000845 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000846 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000847 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000848 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +0000849
850 // End text sections.
851 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000852 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +0000853 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +0000854 }
855
Bill Wendling2b128d72009-05-20 23:19:06 +0000856 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +0000857 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +0000858
859 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +0000860 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000861
862 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +0000863 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +0000864
Eric Christophera876b822012-08-23 07:32:06 +0000865 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +0000866 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +0000867 emitAccelNames();
868 emitAccelObjC();
869 emitAccelNamespaces();
870 emitAccelTypes();
871 }
872
Devang Patel04d2f2d2009-11-24 01:14:22 +0000873 // Emit info into a debug pubtypes section.
Eric Christopher77826182012-08-23 07:10:56 +0000874 // TODO: When we don't need the option anymore we can
875 // remove all of the code that adds to the table.
Eric Christopher20b76a72012-08-23 22:36:40 +0000876 if (useDarwinGDBCompat())
Eric Christopher77826182012-08-23 07:10:56 +0000877 emitDebugPubTypes();
Devang Patel04d2f2d2009-11-24 01:14:22 +0000878
Bill Wendling2b128d72009-05-20 23:19:06 +0000879 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +0000880 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +0000881
882 // Emit info into a debug aranges section.
883 EmitDebugARanges();
884
885 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +0000886 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +0000887
888 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +0000889 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000890
891 // Emit inline info.
Eric Christopher3a47c3e2012-08-23 07:32:02 +0000892 // TODO: When we don't need the option anymore we
893 // can remove all of the code that this section
894 // depends upon.
Eric Christopher20b76a72012-08-23 22:36:40 +0000895 if (useDarwinGDBCompat())
Eric Christopher3a47c3e2012-08-23 07:32:02 +0000896 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000897
Eric Christopher7524fe42012-03-02 00:30:24 +0000898 // Emit info into a debug str section.
899 emitDebugStr();
900
Devang Pateld0701282010-08-02 17:32:15 +0000901 // clean up.
902 DeleteContainerSeconds(DeadFnScopeMap);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000903 SPMap.clear();
Devang Patel1a0df9a2010-05-10 22:49:55 +0000904 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
905 E = CUMap.end(); I != E; ++I)
906 delete I->second;
907 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +0000908}
909
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000910/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +0000911DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +0000912 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +0000913 LLVMContext &Ctx = DV->getContext();
914 // More then one inlined variable corresponds to one abstract variable.
915 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000916 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000917 if (AbsDbgVariable)
918 return AbsDbgVariable;
919
Devang Patel7e623022011-08-10 20:55:27 +0000920 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000921 if (!Scope)
922 return NULL;
923
Devang Patel99819b52011-08-15 19:01:20 +0000924 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patel7e623022011-08-10 20:55:27 +0000925 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000926 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000927 return AbsDbgVariable;
928}
929
Nick Lewycky019d2552011-07-29 03:49:23 +0000930/// addCurrentFnArgument - If Var is a current function argument then add
931/// it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +0000932bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +0000933 DbgVariable *Var, LexicalScope *Scope) {
934 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000935 return false;
936 DIVariable DV = Var->getVariable();
937 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
938 return false;
939 unsigned ArgNo = DV.getArgNumber();
940 if (ArgNo == 0)
941 return false;
942
Devang Patel4ab660b2011-03-03 20:02:02 +0000943 size_t Size = CurrentFnArguments.size();
944 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +0000945 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +0000946 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +0000947 // arguments does the function have at source level.
948 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +0000949 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +0000950 CurrentFnArguments[ArgNo - 1] = Var;
951 return true;
952}
953
Devang Patel490c8ab2010-05-20 19:57:06 +0000954/// collectVariableInfoFromMMITable - Collect variable information from
955/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000956void
Nick Lewycky019d2552011-07-29 03:49:23 +0000957DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patel490c8ab2010-05-20 19:57:06 +0000958 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +0000959 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
960 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
961 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +0000962 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000963 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +0000964 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +0000965 DIVariable DV(Var);
966 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000967
Devang Patel7e623022011-08-10 20:55:27 +0000968 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000969
Devang Patelcdb7d442009-11-10 23:20:04 +0000970 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +0000971 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +0000972 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000973
Devang Patele1c53f22010-05-20 16:36:41 +0000974 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel99819b52011-08-15 19:01:20 +0000975 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patel3e4a9652011-08-15 21:24:36 +0000976 RegVar->setFrameIndex(VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +0000977 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +0000978 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +0000979 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +0000980 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +0000981 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000982}
Devang Patela3e9c9c2010-03-15 18:33:46 +0000983
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000984/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +0000985/// DBG_VALUE instruction, is in a defined reg.
986static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000987 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000988 return MI->getNumOperands() == 3 &&
989 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
990 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000991}
992
Nick Lewycky019d2552011-07-29 03:49:23 +0000993/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel2442a892011-07-08 17:09:57 +0000994/// at MI.
995static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
996 const MCSymbol *FLabel,
997 const MCSymbol *SLabel,
998 const MachineInstr *MI) {
999 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1000
1001 if (MI->getNumOperands() != 3) {
1002 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1003 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1004 }
1005 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1006 MachineLocation MLoc;
1007 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1008 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1009 }
1010 if (MI->getOperand(0).isImm())
1011 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1012 if (MI->getOperand(0).isFPImm())
1013 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1014 if (MI->getOperand(0).isCImm())
1015 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1016
Craig Topperee4dab52012-02-05 08:31:47 +00001017 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001018}
1019
Devang Patel7e623022011-08-10 20:55:27 +00001020/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001021void
Devang Patel5c0f85c2010-06-25 22:07:34 +00001022DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1023 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001024
Devang Patel490c8ab2010-05-20 19:57:06 +00001025 /// collection info from MMI table.
1026 collectVariableInfoFromMMITable(MF, Processed);
1027
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001028 for (SmallVectorImpl<const MDNode*>::const_iterator
1029 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1030 ++UVI) {
1031 const MDNode *Var = *UVI;
1032 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001033 continue;
1034
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001035 // History contains relevant DBG_VALUE instructions for Var and instructions
1036 // clobbering it.
1037 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1038 if (History.empty())
1039 continue;
1040 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001041
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001042 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001043 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001044 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1045 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001046 Scope = LScopes.getCurrentFunctionScope();
Devang Patel8fb9fd62011-07-20 22:18:50 +00001047 else {
1048 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patel7e623022011-08-10 20:55:27 +00001049 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel8fb9fd62011-07-20 22:18:50 +00001050 else {
1051 if (MDNode *IA = DV.getInlinedAt())
Devang Patel7e623022011-08-10 20:55:27 +00001052 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001053 else
Devang Patel7e623022011-08-10 20:55:27 +00001054 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001055 }
1056 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001057 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001058 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001059 continue;
1060
1061 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001062 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001063 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1064 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel6c622ef2011-03-01 22:58:55 +00001065 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001066 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001067 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001068 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001069
1070 // Simple ranges that are fully coalesced.
1071 if (History.size() <= 1 || (History.size() == 2 &&
1072 MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001073 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001074 continue;
1075 }
1076
1077 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001078 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001079
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001080 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1081 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1082 const MachineInstr *Begin = *HI;
1083 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001084
Devang Patele7181b52011-06-01 23:00:17 +00001085 // Check if DBG_VALUE is truncating a range.
1086 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1087 && !Begin->getOperand(0).getReg())
1088 continue;
1089
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001090 // Compute the range for a register location.
1091 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1092 const MCSymbol *SLabel = 0;
1093
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001094 if (HI + 1 == HE)
1095 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001096 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001097 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001098 else {
1099 const MachineInstr *End = HI[1];
Devang Patel53b050a2011-07-07 21:44:42 +00001100 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1101 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001102 if (End->isDebugValue())
1103 SLabel = getLabelBeforeInsn(End);
1104 else {
1105 // End is a normal instruction clobbering the range.
1106 SLabel = getLabelAfterInsn(End);
1107 assert(SLabel && "Forgot label after clobber instruction");
1108 ++HI;
1109 }
1110 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001111
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001112 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopher365d0832011-12-16 23:42:31 +00001113 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1114 Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001115 }
1116 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001117 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001118
1119 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001120 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1121 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1122 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1123 DIVariable DV(Variables.getElement(i));
1124 if (!DV || !DV.Verify() || !Processed.insert(DV))
1125 continue;
1126 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1127 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patele0a94bf2010-05-14 21:01:35 +00001128 }
Devang Patel9fc11702010-05-25 23:40:22 +00001129}
Devang Patele0a94bf2010-05-14 21:01:35 +00001130
Devang Patel9fc11702010-05-25 23:40:22 +00001131/// getLabelBeforeInsn - Return Label preceding the instruction.
1132const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001133 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1134 assert(Label && "Didn't insert label before instruction");
1135 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001136}
1137
1138/// getLabelAfterInsn - Return Label immediately following the instruction.
1139const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001140 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001141}
1142
Devang Patelb5694e72010-10-26 17:49:02 +00001143/// beginInstruction - Process beginning of an instruction.
1144void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001145 // Check if source location changes, but ignore DBG_VALUE locations.
1146 if (!MI->isDebugValue()) {
1147 DebugLoc DL = MI->getDebugLoc();
1148 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001149 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001150 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001151 if (DL == PrologEndLoc) {
1152 Flags |= DWARF2_FLAG_PROLOGUE_END;
1153 PrologEndLoc = DebugLoc();
1154 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001155 if (PrologEndLoc.isUnknown())
1156 Flags |= DWARF2_FLAG_IS_STMT;
1157
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001158 if (!DL.isUnknown()) {
1159 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001160 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001161 } else
Devang Patel34a66202011-05-11 19:22:19 +00001162 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001163 }
Devang Patel9fc11702010-05-25 23:40:22 +00001164 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001165
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001166 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001167 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1168 LabelsBeforeInsn.find(MI);
1169
1170 // No label needed.
1171 if (I == LabelsBeforeInsn.end())
1172 return;
1173
1174 // Label already assigned.
1175 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001176 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001177
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001178 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001179 PrevLabel = MMI->getContext().CreateTempSymbol();
1180 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001181 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001182 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001183}
1184
Devang Patelb5694e72010-10-26 17:49:02 +00001185/// endInstruction - Process end of an instruction.
1186void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001187 // Don't create a new label after DBG_VALUE instructions.
1188 // They don't generate code.
1189 if (!MI->isDebugValue())
1190 PrevLabel = 0;
1191
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001192 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1193 LabelsAfterInsn.find(MI);
1194
1195 // No label needed.
1196 if (I == LabelsAfterInsn.end())
1197 return;
1198
1199 // Label already assigned.
1200 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001201 return;
1202
1203 // We need a label after this instruction.
1204 if (!PrevLabel) {
1205 PrevLabel = MMI->getContext().CreateTempSymbol();
1206 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001207 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001208 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001209}
1210
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001211/// identifyScopeMarkers() -
Devang Patel784077e2011-08-10 23:58:09 +00001212/// Each LexicalScope has first instruction and last instruction to mark
1213/// beginning and end of a scope respectively. Create an inverse map that list
1214/// scopes starts (and ends) with an instruction. One instruction may start (or
1215/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001216void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001217 SmallVector<LexicalScope *, 4> WorkList;
1218 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001219 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001220 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001221
Devang Patel7e623022011-08-10 20:55:27 +00001222 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001223 if (!Children.empty())
Devang Patel7e623022011-08-10 20:55:27 +00001224 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001225 SE = Children.end(); SI != SE; ++SI)
1226 WorkList.push_back(*SI);
1227
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001228 if (S->isAbstractScope())
1229 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001230
Devang Patel7e623022011-08-10 20:55:27 +00001231 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001232 if (Ranges.empty())
1233 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001234 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +00001235 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001236 assert(RI->first && "InsnRange does not have first instruction!");
1237 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001238 requestLabelBeforeInsn(RI->first);
1239 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001240 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001241 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001242}
1243
Devang Patel589845d2011-05-09 22:14:49 +00001244/// getScopeNode - Get MDNode for DebugLoc's scope.
1245static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1246 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1247 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1248 return DL.getScope(Ctx);
1249}
1250
Devang Patel34a66202011-05-11 19:22:19 +00001251/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
Eric Christopher34164192012-04-03 00:43:49 +00001252/// line number info for the function.
Devang Patel34a66202011-05-11 19:22:19 +00001253static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1254 const MDNode *Scope = getScopeNode(DL, Ctx);
1255 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher34164192012-04-03 00:43:49 +00001256 if (SP.Verify()) {
1257 // Check for number of operands since the compatibility is
1258 // cheap here.
Eric Christopherb81e2b42012-04-03 17:55:42 +00001259 if (SP->getNumOperands() > 19)
Eric Christopher34164192012-04-03 00:43:49 +00001260 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1261 else
1262 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1263 }
1264
Devang Patel34a66202011-05-11 19:22:19 +00001265 return DebugLoc();
1266}
1267
Devang Patel930143b2009-11-21 02:48:08 +00001268/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00001269/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00001270void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001271 if (!MMI->hasDebugInfo()) return;
Devang Patel7e623022011-08-10 20:55:27 +00001272 LScopes.initialize(*MF);
1273 if (LScopes.empty()) return;
1274 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001275
Devang Patel6c74a872010-04-27 19:46:33 +00001276 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1277 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001278 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001279 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001280
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001281 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1282
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001283 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001284 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1285 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1286
Devang Patel002d54d2010-05-26 19:37:24 +00001287 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001288 I != E; ++I) {
1289 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001290 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1291 II != IE; ++II) {
1292 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001293
Devang Patel002d54d2010-05-26 19:37:24 +00001294 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001295 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001296
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001297 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001298 const MDNode *Var =
1299 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001300
1301 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001302 if (isDbgValueInDefinedReg(MI))
1303 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1304
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001305 // Check the history of this variable.
1306 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1307 if (History.empty()) {
1308 UserVariables.push_back(Var);
1309 // The first mention of a function argument gets the FunctionBeginSym
1310 // label, so arguments are visible when breaking at function entry.
1311 DIVariable DV(Var);
1312 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1313 DISubprogram(getDISubprogram(DV.getContext()))
1314 .describes(MF->getFunction()))
1315 LabelsBeforeInsn[MI] = FunctionBeginSym;
1316 } else {
1317 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1318 const MachineInstr *Prev = History.back();
1319 if (Prev->isDebugValue()) {
1320 // Coalesce identical entries at the end of History.
1321 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001322 Prev->isIdenticalTo(History[History.size() - 2])) {
1323 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1324 << "\t" << *Prev
1325 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001326 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001327 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001328
1329 // Terminate old register assignments that don't reach MI;
1330 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1331 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1332 isDbgValueInDefinedReg(Prev)) {
1333 // Previous register assignment needs to terminate at the end of
1334 // its basic block.
1335 MachineBasicBlock::const_iterator LastMI =
1336 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001337 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001338 // Drop DBG_VALUE for empty range.
Devang Patelb7a328e2011-07-07 00:14:27 +00001339 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1340 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001341 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001342 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001343 else {
1344 // Terminate after LastMI.
1345 History.push_back(LastMI);
1346 }
1347 }
1348 }
1349 }
1350 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001351 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001352 // Not a DBG_VALUE instruction.
1353 if (!MI->isLabel())
1354 AtBlockEntry = false;
1355
Devang Patel34a66202011-05-11 19:22:19 +00001356 // First known non DBG_VALUE location marks beginning of function
1357 // body.
1358 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1359 PrologEndLoc = MI->getDebugLoc();
1360
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001361 // Check if the instruction clobbers any registers with debug vars.
1362 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1363 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1364 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1365 continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001366 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1367 AI.isValid(); ++AI) {
1368 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001369 const MDNode *Var = LiveUserVar[Reg];
1370 if (!Var)
1371 continue;
1372 // Reg is now clobbered.
1373 LiveUserVar[Reg] = 0;
1374
1375 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001376 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1377 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001378 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001379 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1380 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001381 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001382 const MachineInstr *Prev = History.back();
1383 // Sanity-check: Register assignments are terminated at the end of
1384 // their block.
1385 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1386 continue;
1387 // Is the variable still in Reg?
1388 if (!isDbgValueInDefinedReg(Prev) ||
1389 Prev->getOperand(0).getReg() != Reg)
1390 continue;
1391 // Var is clobbered. Make sure the next instruction gets a label.
1392 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001393 }
1394 }
Devang Patel002d54d2010-05-26 19:37:24 +00001395 }
Devang Patel002d54d2010-05-26 19:37:24 +00001396 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001397 }
1398
1399 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1400 I != E; ++I) {
1401 SmallVectorImpl<const MachineInstr*> &History = I->second;
1402 if (History.empty())
1403 continue;
1404
1405 // Make sure the final register assignments are terminated.
1406 const MachineInstr *Prev = History.back();
1407 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1408 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel784077e2011-08-10 23:58:09 +00001409 MachineBasicBlock::const_iterator LastMI =
1410 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001411 if (LastMI == PrevMBB->end())
1412 // Drop DBG_VALUE for empty range.
1413 History.pop_back();
1414 else {
1415 // Terminate after LastMI.
1416 History.push_back(LastMI);
1417 }
1418 }
1419 // Request labels for the full history.
1420 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1421 const MachineInstr *MI = History[i];
1422 if (MI->isDebugValue())
1423 requestLabelBeforeInsn(MI);
1424 else
1425 requestLabelAfterInsn(MI);
1426 }
1427 }
Devang Patel002d54d2010-05-26 19:37:24 +00001428
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001429 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001430 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001431
1432 // Record beginning of function.
1433 if (!PrologEndLoc.isUnknown()) {
1434 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1435 MF->getFunction()->getContext());
1436 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1437 FnStartDL.getScope(MF->getFunction()->getContext()),
Eric Christopher9fd70c82012-09-10 23:34:06 +00001438 0);
Devang Patel34a66202011-05-11 19:22:19 +00001439 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001440}
1441
Devang Patel7e623022011-08-10 20:55:27 +00001442void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1443// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1444 ScopeVariables[LS].push_back(Var);
1445// Vars.push_back(Var);
1446}
1447
Devang Patel930143b2009-11-21 02:48:08 +00001448/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00001449///
Chris Lattner76555b52010-01-26 23:18:02 +00001450void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patel7e623022011-08-10 20:55:27 +00001451 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00001452
Devang Patel7e623022011-08-10 20:55:27 +00001453 // Define end label for subprogram.
1454 FunctionEndSym = Asm->GetTempSymbol("func_end",
1455 Asm->getFunctionNumber());
1456 // Assumes in correct section after the entry point.
1457 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1458
1459 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1460 collectVariableInfo(MF, ProcessedVars);
1461
Devang Patel3acc70e2011-08-15 22:04:40 +00001462 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001463 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001464 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001465
Devang Patel7e623022011-08-10 20:55:27 +00001466 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001467 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1468 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1469 LexicalScope *AScope = AList[i];
1470 DISubprogram SP(AScope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +00001471 if (SP.Verify()) {
1472 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001473 DIArray Variables = SP.getVariables();
1474 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1475 DIVariable DV(Variables.getElement(i));
1476 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1477 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001478 // Check that DbgVariable for DV wasn't created earlier, when
1479 // findAbstractVariable() was called for inlined instance of DV.
1480 LLVMContext &Ctx = DV->getContext();
1481 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1482 if (AbstractVariables.lookup(CleanDV))
1483 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001484 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1485 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001486 }
1487 }
Devang Patel44403472011-08-12 18:10:19 +00001488 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Patel3acc70e2011-08-15 22:04:40 +00001489 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001490 }
Devang Patel7e623022011-08-10 20:55:27 +00001491
Devang Patel3acc70e2011-08-15 22:04:40 +00001492 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patel7e623022011-08-10 20:55:27 +00001493
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001494 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopherbb69a272012-08-24 01:14:27 +00001495 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001496
Devang Patel7e623022011-08-10 20:55:27 +00001497 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1498 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00001499
Bill Wendling2b128d72009-05-20 23:19:06 +00001500 // Clear debug info
Devang Patel7e623022011-08-10 20:55:27 +00001501 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1502 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1503 DeleteContainerPointers(I->second);
1504 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001505 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001506 UserVariables.clear();
1507 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001508 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001509 LabelsBeforeInsn.clear();
1510 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001511 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001512}
1513
Chris Lattnerba35a672010-03-09 04:54:43 +00001514/// recordSourceLine - Register a source line with debug info. Returns the
1515/// unique label that was emitted and which provides correspondence to
1516/// the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001517void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1518 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001519 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001520 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001521 unsigned Src = 1;
1522 if (S) {
1523 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001524
Dan Gohman50849c62010-05-05 23:41:32 +00001525 if (Scope.isCompileUnit()) {
1526 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001527 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001528 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001529 } else if (Scope.isFile()) {
1530 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001531 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001532 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001533 } else if (Scope.isSubprogram()) {
1534 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001535 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001536 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001537 } else if (Scope.isLexicalBlockFile()) {
1538 DILexicalBlockFile DBF(S);
1539 Fn = DBF.getFilename();
1540 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001541 } else if (Scope.isLexicalBlock()) {
1542 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001543 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001544 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001545 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001546 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001547
Devang Patele01b75c2011-03-24 20:30:50 +00001548 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00001549 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001550 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001551}
1552
Bill Wendling806535f2009-05-20 23:22:40 +00001553//===----------------------------------------------------------------------===//
1554// Emit Methods
1555//===----------------------------------------------------------------------===//
1556
Devang Patel930143b2009-11-21 02:48:08 +00001557/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00001558///
Jim Grosbach00e9c612009-11-22 19:20:36 +00001559unsigned
1560DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00001561 // Get the children.
1562 const std::vector<DIE *> &Children = Die->getChildren();
1563
Bill Wendling480ff322009-05-20 23:21:38 +00001564 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001565 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001566
1567 // Get the abbreviation for this DIE.
1568 unsigned AbbrevNumber = Die->getAbbrevNumber();
1569 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1570
1571 // Set DIE offset
1572 Die->setOffset(Offset);
1573
1574 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001575 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001576
1577 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1578 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1579
1580 // Size the DIE attribute values.
1581 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1582 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001583 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001584
1585 // Size the DIE children if any.
1586 if (!Children.empty()) {
1587 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1588 "Children flag not set");
1589
1590 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001591 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00001592
1593 // End of children marker.
1594 Offset += sizeof(int8_t);
1595 }
1596
1597 Die->setSize(Offset - Die->getOffset());
1598 return Offset;
1599}
1600
Devang Patel930143b2009-11-21 02:48:08 +00001601/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00001602///
Devang Patel930143b2009-11-21 02:48:08 +00001603void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001604 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1605 E = CUMap.end(); I != E; ++I) {
1606 // Compute size of compile unit header.
Devang Patel28dce702011-04-12 23:10:47 +00001607 unsigned Offset =
Devang Patel1a0df9a2010-05-10 22:49:55 +00001608 sizeof(int32_t) + // Length of Compilation Unit Info
1609 sizeof(int16_t) + // DWARF version number
1610 sizeof(int32_t) + // Offset Into Abbrev. Section
1611 sizeof(int8_t); // Pointer Size (in bytes)
1612 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001613 }
Bill Wendling480ff322009-05-20 23:21:38 +00001614}
1615
Chris Lattner6629ca92010-04-04 22:59:04 +00001616/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1617/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00001618void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001619 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001620
Bill Wendling480ff322009-05-20 23:21:38 +00001621 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001622 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001623 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001624 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001625 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001626 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001627
Chris Lattner6629ca92010-04-04 22:59:04 +00001628 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001629 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001630
Devang Patel4a213872010-08-24 00:06:12 +00001631 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001632 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001633 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001634 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001635 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00001636 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1637 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001638
Devang Patel9fc11702010-05-25 23:40:22 +00001639 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1640 "section_debug_loc");
1641
Chris Lattner6629ca92010-04-04 22:59:04 +00001642 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00001643 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001644}
1645
Nick Lewycky019d2552011-07-29 03:49:23 +00001646/// emitDIE - Recursively emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00001647///
Devang Patel930143b2009-11-21 02:48:08 +00001648void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001649 // Get the abbreviation for this DIE.
1650 unsigned AbbrevNumber = Die->getAbbrevNumber();
1651 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1652
Bill Wendling480ff322009-05-20 23:21:38 +00001653 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001654 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00001655 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1656 Twine::utohexstr(Die->getOffset()) + ":0x" +
1657 Twine::utohexstr(Die->getSize()) + " " +
1658 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00001659 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001660
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001661 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00001662 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1663
1664 // Emit the DIE attribute values.
1665 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1666 unsigned Attr = AbbrevData[i].getAttribute();
1667 unsigned Form = AbbrevData[i].getForm();
1668 assert(Form && "Too many attributes for DIE (check abbreviation)");
1669
Chris Lattner7bde8c02010-04-04 18:52:31 +00001670 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001671 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001672
Bill Wendling480ff322009-05-20 23:21:38 +00001673 switch (Attr) {
Bill Wendling480ff322009-05-20 23:21:38 +00001674 case dwarf::DW_AT_abstract_origin: {
1675 DIEEntry *E = cast<DIEEntry>(Values[i]);
1676 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001677 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00001678 Asm->EmitInt32(Addr);
1679 break;
1680 }
Devang Patel12563b32010-04-16 23:33:45 +00001681 case dwarf::DW_AT_ranges: {
1682 // DW_AT_range Value encodes offset in debug_range section.
1683 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00001684
Nick Lewycky33da3362012-06-22 01:25:12 +00001685 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelda3ef852010-09-02 16:43:44 +00001686 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1687 V->getValue(),
1688 4);
1689 } else {
1690 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1691 V->getValue(),
1692 DwarfDebugRangeSectionSym,
1693 4);
1694 }
Devang Patel12563b32010-04-16 23:33:45 +00001695 break;
1696 }
Devang Patel9fc11702010-05-25 23:40:22 +00001697 case dwarf::DW_AT_location: {
Nick Lewycky33da3362012-06-22 01:25:12 +00001698 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1699 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1700 Asm->EmitLabelReference(L->getValue(), 4);
1701 else
1702 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1703 } else {
Devang Patel9fc11702010-05-25 23:40:22 +00001704 Values[i]->EmitValue(Asm, Form);
Nick Lewycky33da3362012-06-22 01:25:12 +00001705 }
Devang Patel9fc11702010-05-25 23:40:22 +00001706 break;
1707 }
Devang Patela1bd5a12010-09-29 19:08:08 +00001708 case dwarf::DW_AT_accessibility: {
1709 if (Asm->isVerbose()) {
1710 DIEInteger *V = cast<DIEInteger>(Values[i]);
1711 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1712 }
1713 Values[i]->EmitValue(Asm, Form);
1714 break;
1715 }
Bill Wendling480ff322009-05-20 23:21:38 +00001716 default:
1717 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001718 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001719 break;
1720 }
Bill Wendling480ff322009-05-20 23:21:38 +00001721 }
1722
1723 // Emit the DIE children if any.
1724 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1725 const std::vector<DIE *> &Children = Die->getChildren();
1726
1727 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001728 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00001729
Chris Lattner7bde8c02010-04-04 18:52:31 +00001730 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00001731 Asm->OutStreamer.AddComment("End Of Children Mark");
1732 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001733 }
1734}
1735
Devang Patel9ccfb642009-12-09 18:24:21 +00001736/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00001737///
Devang Patel9ccfb642009-12-09 18:24:21 +00001738void DwarfDebug::emitDebugInfo() {
1739 // Start debug info section.
1740 Asm->OutStreamer.SwitchSection(
1741 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001742 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1743 E = CUMap.end(); I != E; ++I) {
1744 CompileUnit *TheCU = I->second;
1745 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001746
Devang Patel1a0df9a2010-05-10 22:49:55 +00001747 // Emit the compile units header.
1748 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1749 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001750
Devang Patel1a0df9a2010-05-10 22:49:55 +00001751 // Emit size of content not including length itself
1752 unsigned ContentSize = Die->getSize() +
1753 sizeof(int16_t) + // DWARF version number
1754 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00001755 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001756
Devang Patel1a0df9a2010-05-10 22:49:55 +00001757 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1758 Asm->EmitInt32(ContentSize);
1759 Asm->OutStreamer.AddComment("DWARF version number");
1760 Asm->EmitInt16(dwarf::DWARF_VERSION);
1761 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1762 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1763 DwarfAbbrevSectionSym);
1764 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1765 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001766
Devang Patel1a0df9a2010-05-10 22:49:55 +00001767 emitDIE(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001768 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1769 }
Bill Wendling480ff322009-05-20 23:21:38 +00001770}
1771
Devang Patel930143b2009-11-21 02:48:08 +00001772/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00001773///
Devang Patel930143b2009-11-21 02:48:08 +00001774void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00001775 // Check to see if it is worth the effort.
1776 if (!Abbreviations.empty()) {
1777 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001778 Asm->OutStreamer.SwitchSection(
1779 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001780
Chris Lattnera179b522010-04-04 19:25:43 +00001781 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00001782
1783 // For each abbrevation.
1784 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1785 // Get abbreviation data
1786 const DIEAbbrev *Abbrev = Abbreviations[i];
1787
1788 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00001789 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00001790
1791 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001792 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00001793 }
1794
1795 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00001796 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00001797
Chris Lattnera179b522010-04-04 19:25:43 +00001798 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00001799 }
1800}
1801
Devang Patel930143b2009-11-21 02:48:08 +00001802/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00001803/// the line matrix.
1804///
Devang Patel930143b2009-11-21 02:48:08 +00001805void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00001806 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00001807 Asm->OutStreamer.AddComment("Extended Op");
1808 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001809
Chris Lattner566cae92010-03-09 23:52:58 +00001810 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00001811 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00001812 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1813 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1814
1815 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00001816
Chris Lattnera179b522010-04-04 19:25:43 +00001817 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00001818 Asm->getTargetData().getPointerSize(),
1819 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00001820
1821 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00001822 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1823 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001824 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00001825 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00001826}
1827
Eric Christopher4996c702011-11-07 09:24:32 +00001828/// emitAccelNames - Emit visible names into a hashed accelerator table
1829/// section.
1830void DwarfDebug::emitAccelNames() {
1831 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1832 dwarf::DW_FORM_data4));
1833 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1834 E = CUMap.end(); I != E; ++I) {
1835 CompileUnit *TheCU = I->second;
Eric Christopherd9843b32011-11-10 19:25:34 +00001836 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1837 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00001838 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1839 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001840 const std::vector<DIE *> &Entities = GI->second;
Eric Christopherd9843b32011-11-10 19:25:34 +00001841 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1842 DE = Entities.end(); DI != DE; ++DI)
1843 AT.AddName(Name, (*DI));
Eric Christopher4996c702011-11-07 09:24:32 +00001844 }
1845 }
1846
1847 AT.FinalizeTable(Asm, "Names");
1848 Asm->OutStreamer.SwitchSection(
1849 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1850 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1851 Asm->OutStreamer.EmitLabel(SectionBegin);
1852
1853 // Emit the full data.
1854 AT.Emit(Asm, SectionBegin, this);
1855}
1856
1857/// emitAccelObjC - Emit objective C classes and categories into a hashed
1858/// accelerator table section.
1859void DwarfDebug::emitAccelObjC() {
1860 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1861 dwarf::DW_FORM_data4));
1862 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1863 E = CUMap.end(); I != E; ++I) {
1864 CompileUnit *TheCU = I->second;
1865 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1866 for (StringMap<std::vector<DIE*> >::const_iterator
1867 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1868 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001869 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher4996c702011-11-07 09:24:32 +00001870 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1871 DE = Entities.end(); DI != DE; ++DI)
1872 AT.AddName(Name, (*DI));
1873 }
1874 }
1875
1876 AT.FinalizeTable(Asm, "ObjC");
1877 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1878 .getDwarfAccelObjCSection());
1879 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1880 Asm->OutStreamer.EmitLabel(SectionBegin);
1881
1882 // Emit the full data.
1883 AT.Emit(Asm, SectionBegin, this);
1884}
1885
1886/// emitAccelNamespace - Emit namespace dies into a hashed accelerator
1887/// table.
1888void DwarfDebug::emitAccelNamespaces() {
1889 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1890 dwarf::DW_FORM_data4));
1891 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1892 E = CUMap.end(); I != E; ++I) {
1893 CompileUnit *TheCU = I->second;
Eric Christopher66b37db2011-11-10 21:47:55 +00001894 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
1895 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00001896 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1897 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001898 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher66b37db2011-11-10 21:47:55 +00001899 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1900 DE = Entities.end(); DI != DE; ++DI)
1901 AT.AddName(Name, (*DI));
Eric Christopher4996c702011-11-07 09:24:32 +00001902 }
1903 }
1904
1905 AT.FinalizeTable(Asm, "namespac");
1906 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1907 .getDwarfAccelNamespaceSection());
1908 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
1909 Asm->OutStreamer.EmitLabel(SectionBegin);
1910
1911 // Emit the full data.
1912 AT.Emit(Asm, SectionBegin, this);
1913}
1914
1915/// emitAccelTypes() - Emit type dies into a hashed accelerator table.
1916void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00001917 std::vector<DwarfAccelTable::Atom> Atoms;
1918 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1919 dwarf::DW_FORM_data4));
1920 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
1921 dwarf::DW_FORM_data2));
1922 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
1923 dwarf::DW_FORM_data1));
1924 DwarfAccelTable AT(Atoms);
Eric Christopher4996c702011-11-07 09:24:32 +00001925 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1926 E = CUMap.end(); I != E; ++I) {
1927 CompileUnit *TheCU = I->second;
Eric Christopher21bde872012-01-06 04:35:23 +00001928 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
1929 = TheCU->getAccelTypes();
1930 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher4996c702011-11-07 09:24:32 +00001931 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1932 const char *Name = GI->getKeyData();
Eric Christopher090fcc12012-01-06 23:03:34 +00001933 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopher21bde872012-01-06 04:35:23 +00001934 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
1935 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
1936 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher4996c702011-11-07 09:24:32 +00001937 }
1938 }
1939
1940 AT.FinalizeTable(Asm, "types");
1941 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1942 .getDwarfAccelTypesSection());
1943 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
1944 Asm->OutStreamer.EmitLabel(SectionBegin);
1945
1946 // Emit the full data.
1947 AT.Emit(Asm, SectionBegin, this);
1948}
1949
Devang Patel04d2f2d2009-11-24 01:14:22 +00001950void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001951 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1952 E = CUMap.end(); I != E; ++I) {
1953 CompileUnit *TheCU = I->second;
Eric Christopher6abc9c52011-11-07 09:18:35 +00001954 // Start the dwarf pubtypes section.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001955 Asm->OutStreamer.SwitchSection(
1956 Asm->getObjFileLowering().getDwarfPubTypesSection());
1957 Asm->OutStreamer.AddComment("Length of Public Types Info");
1958 Asm->EmitLabelDifference(
1959 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1960 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001961
Devang Patel1a0df9a2010-05-10 22:49:55 +00001962 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1963 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001964
Devang Patel1a0df9a2010-05-10 22:49:55 +00001965 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1966 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001967
Devang Patel1a0df9a2010-05-10 22:49:55 +00001968 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1969 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1970 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001971
Devang Patel1a0df9a2010-05-10 22:49:55 +00001972 Asm->OutStreamer.AddComment("Compilation Unit Length");
1973 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1974 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1975 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001976
Devang Patel1a0df9a2010-05-10 22:49:55 +00001977 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1978 for (StringMap<DIE*>::const_iterator
1979 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1980 const char *Name = GI->getKeyData();
Nick Lewycky019d2552011-07-29 03:49:23 +00001981 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001982
Devang Patel1a0df9a2010-05-10 22:49:55 +00001983 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1984 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001985
Devang Patel1a0df9a2010-05-10 22:49:55 +00001986 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00001987 // Emit the name with a terminating null byte.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001988 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1989 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001990
Devang Patel1a0df9a2010-05-10 22:49:55 +00001991 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001992 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001993 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1994 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00001995 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001996}
1997
Devang Patel930143b2009-11-21 02:48:08 +00001998/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00001999///
Devang Patel930143b2009-11-21 02:48:08 +00002000void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00002001 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00002002 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002003
Chris Lattner3d72a672010-03-09 23:38:23 +00002004 // Start the dwarf str section.
2005 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002006 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002007
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002008 // Get all of the string pool entries and put them in an array by their ID so
2009 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002010 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002011 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002012
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002013 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2014 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2015 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002016
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002017 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002018
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002019 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002020 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002021 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002022
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002023 // Emit the string itself with a terminating null byte.
Benjamin Kramer148db362011-11-09 12:12:04 +00002024 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
2025 Entries[i].second->getKeyLength()+1),
2026 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00002027 }
2028}
2029
Devang Patel930143b2009-11-21 02:48:08 +00002030/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00002031///
Devang Patel930143b2009-11-21 02:48:08 +00002032void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002033 if (DotDebugLocEntries.empty())
2034 return;
2035
Devang Patel116a9d72011-02-04 22:57:18 +00002036 for (SmallVector<DotDebugLocEntry, 4>::iterator
2037 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2038 I != E; ++I) {
2039 DotDebugLocEntry &Entry = *I;
2040 if (I + 1 != DotDebugLocEntries.end())
2041 Entry.Merge(I+1);
2042 }
2043
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002044 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002045 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00002046 Asm->getObjFileLowering().getDwarfLocSection());
2047 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002048 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2049 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002050 for (SmallVector<DotDebugLocEntry, 4>::iterator
2051 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002052 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002053 DotDebugLocEntry &Entry = *I;
2054 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002055 if (Entry.isEmpty()) {
2056 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2057 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002058 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002059 } else {
2060 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2061 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patel3e021532011-04-28 02:22:40 +00002062 DIVariable DV(Entry.Variable);
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002063 Asm->OutStreamer.AddComment("Loc expr size");
2064 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2065 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2066 Asm->EmitLabelDifference(end, begin, 2);
2067 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002068 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002069 DIBasicType BTy(DV.getType());
2070 if (BTy.Verify() &&
2071 (BTy.getEncoding() == dwarf::DW_ATE_signed
2072 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2073 Asm->OutStreamer.AddComment("DW_OP_consts");
2074 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002075 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002076 } else {
2077 Asm->OutStreamer.AddComment("DW_OP_constu");
2078 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002079 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002080 }
Devang Pateled9fd452011-07-08 16:49:43 +00002081 } else if (Entry.isLocation()) {
2082 if (!DV.hasComplexAddress())
2083 // Regular entry.
Devang Patel3e021532011-04-28 02:22:40 +00002084 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Pateled9fd452011-07-08 16:49:43 +00002085 else {
2086 // Complex address entry.
2087 unsigned N = DV.getNumAddrElements();
2088 unsigned i = 0;
2089 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2090 if (Entry.Loc.getOffset()) {
2091 i = 2;
2092 Asm->EmitDwarfRegOp(Entry.Loc);
2093 Asm->OutStreamer.AddComment("DW_OP_deref");
2094 Asm->EmitInt8(dwarf::DW_OP_deref);
2095 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2096 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2097 Asm->EmitSLEB128(DV.getAddrElement(1));
2098 } else {
2099 // If first address element is OpPlus then emit
2100 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2101 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2102 Asm->EmitDwarfRegOp(Loc);
2103 i = 2;
2104 }
2105 } else {
2106 Asm->EmitDwarfRegOp(Entry.Loc);
2107 }
2108
2109 // Emit remaining complex address elements.
2110 for (; i < N; ++i) {
2111 uint64_t Element = DV.getAddrElement(i);
2112 if (Element == DIBuilder::OpPlus) {
2113 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2114 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002115 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher8d2a77d2012-05-08 21:24:39 +00002116 if (!Entry.Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002117 Asm->EmitInt8(dwarf::DW_OP_deref);
2118 } else
2119 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002120 }
Devang Patel3e021532011-04-28 02:22:40 +00002121 }
Devang Patel3e021532011-04-28 02:22:40 +00002122 }
Devang Pateled9fd452011-07-08 16:49:43 +00002123 // else ... ignore constant fp. There is not any good way to
2124 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002125 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002126 }
2127 }
Bill Wendling480ff322009-05-20 23:21:38 +00002128}
2129
2130/// EmitDebugARanges - Emit visible names into a debug aranges section.
2131///
2132void DwarfDebug::EmitDebugARanges() {
2133 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002134 Asm->OutStreamer.SwitchSection(
2135 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002136}
2137
Devang Patel930143b2009-11-21 02:48:08 +00002138/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00002139///
Devang Patel930143b2009-11-21 02:48:08 +00002140void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002141 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002142 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00002143 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00002144 unsigned char Size = Asm->getTargetData().getPointerSize();
2145 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002146 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00002147 I != E; ++I) {
2148 if (*I)
2149 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00002150 else
Devang Patel6c74a872010-04-27 19:46:33 +00002151 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00002152 }
Bill Wendling480ff322009-05-20 23:21:38 +00002153}
2154
Devang Patel930143b2009-11-21 02:48:08 +00002155/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00002156///
Devang Patel930143b2009-11-21 02:48:08 +00002157void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002158 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002159 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002160 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002161 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002162 }
2163}
2164
Devang Patel930143b2009-11-21 02:48:08 +00002165/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00002166/// Section Header:
2167/// 1. length of section
2168/// 2. Dwarf version number
2169/// 3. address size.
2170///
2171/// Entries (one "entry" for each function that was inlined):
2172///
2173/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2174/// otherwise offset into __debug_str for regular function name.
2175/// 2. offset into __debug_str section for regular function name.
2176/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2177/// instances for the function.
2178///
2179/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2180/// inlined instance; the die_offset points to the inlined_subroutine die in the
2181/// __debug_info section, and the low_pc is the starting address for the
2182/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002183void DwarfDebug::emitDebugInlineInfo() {
Eric Christopher1df94bf2012-03-02 02:11:47 +00002184 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002185 return;
2186
Devang Patel1a0df9a2010-05-10 22:49:55 +00002187 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002188 return;
2189
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002190 Asm->OutStreamer.SwitchSection(
2191 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002192
Chris Lattner566cae92010-03-09 23:52:58 +00002193 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002194 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2195 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002196
Chris Lattnera179b522010-04-04 19:25:43 +00002197 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002198
Chris Lattner566cae92010-03-09 23:52:58 +00002199 Asm->OutStreamer.AddComment("Dwarf Version");
2200 Asm->EmitInt16(dwarf::DWARF_VERSION);
2201 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002202 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002203
Devang Patel32cc43c2010-05-07 20:54:48 +00002204 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002205 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002206
Devang Patel32cc43c2010-05-07 20:54:48 +00002207 const MDNode *Node = *I;
2208 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00002209 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002210 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002211 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002212 StringRef LName = SP.getLinkageName();
2213 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002214
Chris Lattner566cae92010-03-09 23:52:58 +00002215 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher77725312012-03-02 01:57:52 +00002216 if (LName.empty())
2217 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2218 else
Chris Lattner70a4fce2010-04-04 23:25:33 +00002219 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2220 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002221
Chris Lattner566cae92010-03-09 23:52:58 +00002222 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00002223 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00002224 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002225
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002226 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002227 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002228 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002229 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002230
Chris Lattner7bde8c02010-04-04 18:52:31 +00002231 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002232 Asm->OutStreamer.EmitSymbolValue(LI->first,
2233 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00002234 }
2235 }
2236
Chris Lattnera179b522010-04-04 19:25:43 +00002237 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002238}