blob: 52fae53fe84c630e75e69b4f82341c4796cca4ce [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattner6cde3e62010-03-09 00:39:24 +000013
Devang Patele4b27562009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000016#include "DIE.h"
Eric Christopher09ac3d82011-11-07 09:24:32 +000017#include "DwarfAccelTable.h"
Devang Patel8b9df622011-04-12 17:40:32 +000018#include "DwarfCompileUnit.h"
Bill Wendling57fbba42010-04-05 22:59:21 +000019#include "llvm/Constants.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000020#include "llvm/DebugInfo.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000021#include "llvm/DIBuilder.h"
Bill Wendling0310d762009-05-15 09:23:25 +000022#include "llvm/Module.h"
Devang Patele449d1f2011-01-20 00:02:16 +000023#include "llvm/Instructions.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000026#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000027#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000028#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000029#include "llvm/MC/MCSymbol.h"
Bill Wendling0310d762009-05-15 09:23:25 +000030#include "llvm/Target/TargetData.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000031#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000033#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000035#include "llvm/Target/TargetOptions.h"
Devang Patel9a31f0f2010-10-25 20:45:32 +000036#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000037#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000038#include "llvm/ADT/StringExtras.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000039#include "llvm/ADT/Triple.h"
Devang Pateleac9c072010-04-27 19:46:33 +000040#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000041#include "llvm/Support/Debug.h"
42#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000043#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000044#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000045#include "llvm/Support/Timer.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000046#include "llvm/Support/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000047using namespace llvm;
48
Jim Grosbach1e20b962010-07-21 21:21:52 +000049static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel61409622010-07-07 20:12:52 +000050 cl::Hidden,
Devang Pateleac9c072010-04-27 19:46:33 +000051 cl::desc("Disable debug info printing"));
52
Dan Gohman281d65d2010-05-07 01:08:53 +000053static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner7a2bdde2011-04-15 05:18:47 +000054 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman281d65d2010-05-07 01:08:53 +000055 cl::init(false));
56
Eric Christopher20f47ab2012-08-23 22:36:40 +000057namespace {
58 enum DefaultOnOff {
59 Default, Enable, Disable
60 };
61}
Eric Christopher09ac3d82011-11-07 09:24:32 +000062
Eric Christopher20f47ab2012-08-23 22:36:40 +000063static cl::opt<DefaultOnOff> DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
64 cl::desc("Output prototype dwarf accelerator tables."),
65 cl::values(
66 clEnumVal(Default, "Default for platform"),
67 clEnumVal(Enable, "Enabled"),
68 clEnumVal(Disable, "Disabled"),
69 clEnumValEnd),
70 cl::init(Default));
71
72static cl::opt<DefaultOnOff> DarwinGDBCompat("darwin-gdb-compat", cl::Hidden,
Eric Christopher10cb7442012-08-23 07:10:46 +000073 cl::desc("Compatibility with Darwin gdb."),
Eric Christopher20f47ab2012-08-23 22:36:40 +000074 cl::values(
75 clEnumVal(Default, "Default for platform"),
76 clEnumVal(Enable, "Enabled"),
77 clEnumVal(Disable, "Disabled"),
78 clEnumValEnd),
79 cl::init(Default));
Eric Christopher10cb7442012-08-23 07:10:46 +000080
Bill Wendling5f017e82010-04-07 09:28:04 +000081namespace {
82 const char *DWARFGroupName = "DWARF Emission";
83 const char *DbgTimerName = "DWARF Debug Writer";
84} // end anonymous namespace
85
Bill Wendling0310d762009-05-15 09:23:25 +000086//===----------------------------------------------------------------------===//
87
88/// Configuration values for initial hash set sizes (log2).
89///
Bill Wendling0310d762009-05-15 09:23:25 +000090static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000091
92namespace llvm {
93
Nick Lewycky3bbb6f72011-07-29 03:49:23 +000094DIType DbgVariable::getType() const {
Devang Patel3cbee302011-04-12 22:53:02 +000095 DIType Ty = Var.getType();
96 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
97 // addresses instead.
98 if (Var.isBlockByrefVariable()) {
99 /* Byref variables, in Blocks, are declared by the programmer as
100 "SomeType VarName;", but the compiler creates a
101 __Block_byref_x_VarName struct, and gives the variable VarName
102 either the struct, or a pointer to the struct, as its type. This
103 is necessary for various behind-the-scenes things the compiler
104 needs to do with by-reference variables in blocks.
105
106 However, as far as the original *programmer* is concerned, the
107 variable should still have type 'SomeType', as originally declared.
108
109 The following function dives into the __Block_byref_x_VarName
110 struct to find the original type of the variable. This will be
111 passed back to the code generating the type for the Debug
112 Information Entry for the variable 'VarName'. 'VarName' will then
113 have the original type 'SomeType' in its debug information.
114
115 The original type 'SomeType' will be the type of the field named
116 'VarName' inside the __Block_byref_x_VarName struct.
117
118 NOTE: In order for this to not completely fail on the debugger
119 side, the Debug Information Entry for the variable VarName needs to
120 have a DW_AT_location that tells the debugger how to unwind through
121 the pointers and __Block_byref_x_VarName struct to find the actual
122 value of the variable. The function addBlockByrefType does this. */
123 DIType subType = Ty;
124 unsigned tag = Ty.getTag();
125
126 if (tag == dwarf::DW_TAG_pointer_type) {
127 DIDerivedType DTy = DIDerivedType(Ty);
128 subType = DTy.getTypeDerivedFrom();
129 }
130
131 DICompositeType blockStruct = DICompositeType(subType);
132 DIArray Elements = blockStruct.getTypeArray();
133
134 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
135 DIDescriptor Element = Elements.getElement(i);
136 DIDerivedType DT = DIDerivedType(Element);
137 if (getName() == DT.getName())
138 return (DT.getTypeDerivedFrom());
Devang Patel8bd11de2010-08-09 21:01:39 +0000139 }
Devang Patel8bd11de2010-08-09 21:01:39 +0000140 }
Devang Patel3cbee302011-04-12 22:53:02 +0000141 return Ty;
142}
Bill Wendling0310d762009-05-15 09:23:25 +0000143
Chris Lattnerea761862010-04-05 04:09:20 +0000144} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000145
Chris Lattner49cd6642010-04-05 05:11:15 +0000146DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000147 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000148 AbbreviationsSet(InitAbbreviationsSetSize),
Benjamin Kramerf33a79c2012-06-09 10:34:15 +0000149 SourceIdMap(DIEValueAllocator), StringPool(DIEValueAllocator),
Eric Christopher6635cad2012-08-01 18:19:01 +0000150 PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000151 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000152
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000153 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000154 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000155 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000156 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopher60777d82012-04-02 17:58:52 +0000157
Eric Christopher10cb7442012-08-23 07:10:46 +0000158 // Turn on accelerator tables and older gdb compatibility
159 // for Darwin.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000160 bool isDarwin = Triple(M->getTargetTriple()).isOSDarwin();
161 if (DarwinGDBCompat == Default) {
162 if (isDarwin)
163 isDarwinGDBCompat = true;
164 else
165 isDarwinGDBCompat = false;
166 } else
167 isDarwinGDBCompat = DarwinGDBCompat == Enable ? true : false;
Eric Christopherc1610fa2012-08-23 22:36:36 +0000168
Eric Christopher20f47ab2012-08-23 22:36:40 +0000169 if (DwarfAccelTables == Default) {
170 if (isDarwin)
171 hasDwarfAccelTables = true;
172 else
173 hasDwarfAccelTables = false;
174 } else
175 hasDwarfAccelTables = DwarfAccelTables == Enable ? true : false;
176
Dan Gohman03c3dc72010-06-18 15:56:31 +0000177 {
178 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
179 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000180 }
Bill Wendling0310d762009-05-15 09:23:25 +0000181}
182DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000183}
184
Eric Christopherd8a87522011-11-07 09:18:38 +0000185/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
186/// temporary label to it if SymbolStem is specified.
187static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
188 const char *SymbolStem = 0) {
189 Asm->OutStreamer.SwitchSection(Section);
190 if (!SymbolStem) return 0;
191
192 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
193 Asm->OutStreamer.EmitLabel(TmpSym);
194 return TmpSym;
195}
196
Nick Lewycky390c40d2011-10-27 06:44:11 +0000197MCSymbol *DwarfDebug::getStringPool() {
198 return Asm->GetTempSymbol("section_str");
199}
200
Chris Lattnerbc733f52010-03-13 02:17:42 +0000201MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
202 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
203 if (Entry.first) return Entry.first;
204
205 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000206 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000207}
208
Devang Patel2c4ceb12009-11-21 02:48:08 +0000209/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000210///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000211void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000212 // Profile the node so that we can make it unique.
213 FoldingSetNodeID ID;
214 Abbrev.Profile(ID);
215
216 // Check the set for priors.
217 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
218
219 // If it's newly added.
220 if (InSet == &Abbrev) {
221 // Add to abbreviation list.
222 Abbreviations.push_back(&Abbrev);
223
224 // Assign the vector position + 1 as its number.
225 Abbrev.setNumber(Abbreviations.size());
226 } else {
227 // Assign existing abbreviation number.
228 Abbrev.setNumber(InSet->getNumber());
229 }
230}
231
Jim Grosbach1e20b962010-07-21 21:21:52 +0000232/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000233/// printer to not emit usual symbol prefix before the symbol name is used then
234/// return linkage name after skipping this special LLVM prefix.
235static StringRef getRealLinkageName(StringRef LinkageName) {
236 char One = '\1';
237 if (LinkageName.startswith(StringRef(&One, 1)))
238 return LinkageName.substr(1);
239 return LinkageName;
240}
241
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000242static bool isObjCClass(StringRef Name) {
243 return Name.startswith("+") || Name.startswith("-");
244}
245
246static bool hasObjCCategory(StringRef Name) {
247 if (!isObjCClass(Name)) return false;
248
249 size_t pos = Name.find(')');
250 if (pos != std::string::npos) {
251 if (Name[pos+1] != ' ') return false;
252 return true;
253 }
254 return false;
255}
256
257static void getObjCClassCategory(StringRef In, StringRef &Class,
258 StringRef &Category) {
259 if (!hasObjCCategory(In)) {
260 Class = In.slice(In.find('[') + 1, In.find(' '));
261 Category = "";
262 return;
263 }
264
265 Class = In.slice(In.find('[') + 1, In.find('('));
266 Category = In.slice(In.find('[') + 1, In.find(' '));
267 return;
268}
269
270static StringRef getObjCMethodName(StringRef In) {
271 return In.slice(In.find(' ') + 1, In.find(']'));
272}
273
274// Add the various names to the Dwarf accelerator table names.
275static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
276 DIE* Die) {
277 if (!SP.isDefinition()) return;
278
279 TheCU->addAccelName(SP.getName(), Die);
280
281 // If the linkage name is different than the name, go ahead and output
282 // that as well into the name table.
283 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
284 TheCU->addAccelName(SP.getLinkageName(), Die);
285
286 // If this is an Objective-C selector name add it to the ObjC accelerator
287 // too.
288 if (isObjCClass(SP.getName())) {
289 StringRef Class, Category;
290 getObjCClassCategory(SP.getName(), Class, Category);
291 TheCU->addAccelObjC(Class, Die);
292 if (Category != "")
293 TheCU->addAccelObjC(Category, Die);
294 // Also add the base method name to the name table.
295 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
296 }
297}
298
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000299/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000300/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
301/// If there are global variables in this scope then create and insert
302/// DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000303DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
304 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000305 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000306
Chris Lattnerd38fee82010-04-05 00:13:49 +0000307 assert(SPDie && "Unable to find subprogram DIE!");
308 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000309
Devang Patel5e06bb82011-04-22 23:10:17 +0000310 DISubprogram SPDecl = SP.getFunctionDeclaration();
Rafael Espindolab0527282011-11-04 19:00:29 +0000311 if (!SPDecl.isSubprogram()) {
Devang Patel5e06bb82011-04-22 23:10:17 +0000312 // There is not any need to generate specification DIE for a function
313 // defined at compile unit level. If a function is defined inside another
314 // function then gdb prefers the definition at top level and but does not
315 // expect specification DIE in parent function. So avoid creating
316 // specification DIE for a function defined inside a function.
317 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
318 !SP.getContext().isFile() &&
319 !isSubprogramContext(SP.getContext())) {
Eric Christopher873cf0a2012-08-24 01:14:27 +0000320 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
Devang Patel5e06bb82011-04-22 23:10:17 +0000321
322 // Add arguments.
323 DICompositeType SPTy = SP.getType();
324 DIArray Args = SPTy.getTypeArray();
325 unsigned SPTag = SPTy.getTag();
326 if (SPTag == dwarf::DW_TAG_subroutine_type)
327 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
328 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Eric Christopher5c38de92012-09-10 23:33:57 +0000329 DIType ATy = DIType(Args.getElement(i));
Devang Patel5e06bb82011-04-22 23:10:17 +0000330 SPCU->addType(Arg, ATy);
331 if (ATy.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000332 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
Eric Christophere5212782012-09-12 23:36:19 +0000333 if (ATy.isObjectPointer())
334 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer, dwarf::DW_FORM_ref4,
335 Arg);
Devang Patel5e06bb82011-04-22 23:10:17 +0000336 SPDie->addChild(Arg);
337 }
338 DIE *SPDeclDie = SPDie;
339 SPDie = new DIE(dwarf::DW_TAG_subprogram);
340 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
341 SPDeclDie);
342 SPCU->addDie(SPDie);
343 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000344 }
Devang Patel8aa61472010-07-07 22:20:57 +0000345 // Pick up abstract subprogram DIE.
346 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
347 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000348 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
349 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000350 SPCU->addDie(SPDie);
351 }
352
Devang Patel3cbee302011-04-12 22:53:02 +0000353 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
354 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
355 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
356 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000357 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
358 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000359 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000360
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000361 // Add name to the name table, we do this here because we're guaranteed
362 // to have concrete versions of our DW_TAG_subprogram nodes.
363 addSubprogramNames(SPCU, SP, SPDie);
364
Chris Lattnerd38fee82010-04-05 00:13:49 +0000365 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000366}
367
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000368/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000369/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000370DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
371 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000372 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
373 if (Scope->isAbstractScope())
374 return ScopeDIE;
375
Devang Patelbf47fdb2011-08-10 20:55:27 +0000376 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000377 if (Ranges.empty())
378 return 0;
379
Devang Patelbf47fdb2011-08-10 20:55:27 +0000380 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000381 if (Ranges.size() > 1) {
382 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000383 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000384 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000385 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000386 DebugRangeSymbols.size()
387 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000388 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000389 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000390 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
391 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000392 }
393 DebugRangeSymbols.push_back(NULL);
394 DebugRangeSymbols.push_back(NULL);
395 return ScopeDIE;
396 }
397
Devang Patelc3f5f782010-05-25 23:40:22 +0000398 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
399 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000400
Devang Patelc3f5f782010-05-25 23:40:22 +0000401 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000402
Chris Lattnerb7db7332010-03-09 01:58:53 +0000403 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
404 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000405
Devang Patel3cbee302011-04-12 22:53:02 +0000406 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
407 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000408
409 return ScopeDIE;
410}
411
Devang Patel2c4ceb12009-11-21 02:48:08 +0000412/// constructInlinedScopeDIE - This scope represents inlined body of
413/// a function. Construct DIE to represent this concrete inlined copy
414/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000415DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
416 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000417 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000418 assert(Ranges.empty() == false &&
419 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000420
Devang Patel26a92002011-07-27 00:34:13 +0000421 if (!Scope->getScopeNode())
422 return NULL;
423 DIScope DS(Scope->getScopeNode());
424 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000425 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
426 if (!OriginDIE) {
427 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
428 return NULL;
429 }
430
Devang Patelbf47fdb2011-08-10 20:55:27 +0000431 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000432 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
433 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000434
Devang Patel0afbf232010-07-08 22:39:20 +0000435 if (StartLabel == 0 || EndLabel == 0) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000436 llvm_unreachable("Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000437 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000438 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000439 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000440 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000441 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000442
Devang Pateld96efb82011-05-05 17:54:26 +0000443 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000444 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
445 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000446
Devang Patel26a92002011-07-27 00:34:13 +0000447 if (Ranges.size() > 1) {
448 // .debug_range section has not been laid out yet. Emit offset in
449 // .debug_range as a uint, size 4, for now. emitDIE will handle
450 // DW_AT_ranges appropriately.
451 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000452 DebugRangeSymbols.size()
453 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000454 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000455 RE = Ranges.end(); RI != RE; ++RI) {
456 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
457 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
458 }
459 DebugRangeSymbols.push_back(NULL);
460 DebugRangeSymbols.push_back(NULL);
461 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000462 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
463 StartLabel);
464 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
465 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000466 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000467
468 InlinedSubprogramDIEs.insert(OriginDIE);
469
470 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000471 //.debug_inlined section specification does not clearly state how
472 // to emit inlined scope that is split into multiple instruction ranges.
473 // For now, use first instruction range and emit low_pc/high_pc pair and
474 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000475 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000476 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000477
478 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000479 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000480 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000481 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000482 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000483
Devang Patel53bb5c92009-11-10 23:06:00 +0000484 DILocation DL(Scope->getInlinedAt());
Eric Christopher08212002012-03-26 21:38:38 +0000485 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
486 GetOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patel3cbee302011-04-12 22:53:02 +0000487 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000488
Eric Christopher309bedd2011-12-04 06:02:38 +0000489 // Add name to the name table, we do this here because we're guaranteed
490 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
491 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
492
Devang Patel53bb5c92009-11-10 23:06:00 +0000493 return ScopeDIE;
494}
495
Devang Patel2c4ceb12009-11-21 02:48:08 +0000496/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000497DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000498 if (!Scope || !Scope->getScopeNode())
499 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000500
Nick Lewycky746cb672011-10-26 22:55:33 +0000501 SmallVector<DIE *, 8> Children;
Eric Christophere5212782012-09-12 23:36:19 +0000502 DIE *ObjectPointer = NULL;
Devang Patel0478c152011-03-01 22:58:55 +0000503
504 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000505 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000506 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
507 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000508 if (DIE *Arg =
Eric Christophere5212782012-09-12 23:36:19 +0000509 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope())) {
Devang Patel0478c152011-03-01 22:58:55 +0000510 Children.push_back(Arg);
Eric Christophere5212782012-09-12 23:36:19 +0000511 if (ArgDV->isObjectPointer()) ObjectPointer = Arg;
512 }
Devang Patel0478c152011-03-01 22:58:55 +0000513
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000514 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000515 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000516 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000517 if (DIE *Variable =
518 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000519 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000520 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000521 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000522 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000523 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000524 DIScope DS(Scope->getScopeNode());
525 DIE *ScopeDIE = NULL;
526 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000527 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000528 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000529 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000530 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000531 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000532 // Note down abstract DIE.
533 if (ScopeDIE)
534 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
535 }
Devang Patel3c91b052010-03-08 20:52:55 +0000536 else
Devang Pateld3024342011-08-15 22:24:32 +0000537 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000538 }
Devang Patel5bc9fec2011-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 Pateld3024342011-08-15 22:24:32 +0000543 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000544 }
545
Devang Patelaead63c2010-03-29 22:59:58 +0000546 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000547
Devang Patel5bc9fec2011-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 Patel193f7202009-11-24 01:14:22 +0000552
Eric Christophere5212782012-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 Grosbach1e20b962010-07-21 21:21:52 +0000557 if (DS.isSubprogram())
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000558 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000559
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000560 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000561}
562
Bill Wendlingf0fb9872009-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 Patel23670e52011-03-24 20:30:50 +0000567unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
568 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000569 // If FE did not provide a file name, then assume stdin.
570 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000571 return GetOrCreateSourceID("<stdin>", StringRef());
572
Nick Lewycky6c1a7032011-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 Lewycky44d798d2011-10-17 23:05:28 +0000577 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel1905a182010-09-16 20:57:49 +0000578
Benjamin Kramer74612c22012-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 Wendlingf0fb9872009-05-20 23:19:06 +0000588
Rafael Espindola5c055632010-11-18 02:04:25 +0000589 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer74612c22012-03-11 14:56:26 +0000590 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000591
592 return SrcId;
593}
594
Jim Grosbach1e20b962010-07-21 21:21:52 +0000595/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000596/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000597CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000598 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000599 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000600 CompilationDir = DIUnit.getDirectory();
601 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000602
603 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopheredf8bc82012-09-10 23:34:00 +0000604 CompileUnit *NewCU = new CompileUnit(ID, DIUnit.getLanguage(), Die,
605 Asm, this);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000606 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000607 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
608 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000609 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher6635cad2012-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 Patel4a602ca2010-03-22 23:11:36 +0000613 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000614 // compile unit in debug_line section.
Rafael Espindola2241e512012-06-22 13:24:07 +0000615 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindola597a7662011-05-04 17:44:06 +0000616 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000617 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000618 else
Devang Patel3cbee302011-04-12 22:53:02 +0000619 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000620
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000621 if (!CompilationDir.empty())
622 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000623 if (DIUnit.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000624 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000625
Devang Patel65dbc902009-11-25 17:36:49 +0000626 StringRef Flags = DIUnit.getFlags();
627 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000628 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000629
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000630 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000631 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000632 dwarf::DW_FORM_data1, RVer);
633
Devang Patel163a9f72010-05-10 22:49:55 +0000634 if (!FirstCU)
635 FirstCU = NewCU;
636 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000637 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000638}
639
Devang Patel163a9f72010-05-10 22:49:55 +0000640/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000641void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
642 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000643 CompileUnit *&CURef = SPMap[N];
644 if (CURef)
645 return;
646 CURef = TheCU;
647
Devang Patele4b27562009-08-28 23:24:31 +0000648 DISubprogram SP(N);
Bill Wendlingf0fb9872009-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 Patel13e16b62009-06-26 01:49:18 +0000652 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000653
Devang Pateldbc64af2011-08-15 17:24:54 +0000654 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000655
656 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000657 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000658
659 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000660 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000661
Devang Patel13e16b62009-06-26 01:49:18 +0000662 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000663}
664
Devang Patel02e603f2011-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 Patel94c7ddb2011-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 Patel28bea082011-08-18 23:17:55 +0000679 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000680 }
681
Devang Patel02e603f2011-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 Patel94c7ddb2011-08-16 22:09:43 +0000685 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
686 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-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 Patel94c7ddb2011-08-16 22:09:43 +0000692 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
693 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-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 Patel94c7ddb2011-08-16 22:09:43 +0000724 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000725 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000726 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000727
Devang Patel02e603f2011-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 Patel94c7ddb2011-08-16 22:09:43 +0000732 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
733 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000734 }
735
736 return HasDebugInfo;
737}
738
Devang Patel2c4ceb12009-11-21 02:48:08 +0000739/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000740/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000741/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000742void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000743 if (DisableDebugInfoPrinting)
744 return;
745
Nick Lewycky3bbb6f72011-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 Patel30692ab2011-05-03 16:45:22 +0000748 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
749 if (CU_Nodes) {
Devang Patel94c7ddb2011-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 Patel28bea082011-08-18 23:17:55 +0000755 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-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 Patel02e603f2011-08-15 23:47:24 +0000766 } else if (!collectLegacyDebugInfo(M))
767 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000768
Devang Patel02e603f2011-08-15 23:47:24 +0000769 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000770
Chris Lattnerd850ac72010-04-05 02:19:28 +0000771 // Tell MMI that we have debug info.
772 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000773
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000774 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000775 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000776
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000777 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000778 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000779}
780
Devang Patel2c4ceb12009-11-21 02:48:08 +0000781/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000782///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000783void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000784 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000785 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000786 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000787
Devang Patel94c7ddb2011-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 Patel93d39be2011-08-19 23:28:12 +0000798 DIArray Variables = SP.getVariables();
799 if (Variables.getNumElements() == 0) continue;
800
Devang Patel94c7ddb2011-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 Patel94c7ddb2011-08-16 22:09:43 +0000806 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky746cb672011-10-26 22:55:33 +0000807 assert(SPCU && "Unable to find Compile Unit!");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000808 constructSubprogramDIE(SPCU, SP);
809 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel93d39be2011-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 Patel94c7ddb2011-08-16 22:09:43 +0000814 if (DIE *VariableDIE =
Devang Patel93d39be2011-08-19 23:28:12 +0000815 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Patel94c7ddb2011-08-16 22:09:43 +0000816 ScopeDIE->addChild(VariableDIE);
817 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000818 }
819 }
820 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000821
Devang Patel53bb5c92009-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 Patel3cbee302011-04-12 22:53:02 +0000826 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000827 }
Rafael Espindolad1ac3a42011-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 Patel53bb5c92009-11-10 23:06:00 +0000835
Eric Christopher6635cad2012-08-01 18:19:01 +0000836 // Emit DW_AT_containing_type attribute to connect types with their
837 // vtable holding type.
Devang Pateldbc64af2011-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 Patel5d11eb02009-12-03 19:11:07 +0000842 }
843
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000844 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000845 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000846 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000847 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000848 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000849
850 // End text sections.
851 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000852 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000853 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000854 }
855
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000856 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000857 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000858
859 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000860 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000861
862 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000863 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000864
Eric Christopher9d9f5a52012-08-23 07:32:06 +0000865 // Emit info into the dwarf accelerator table sections.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000866 if (useDwarfAccelTables()) {
Eric Christopher09ac3d82011-11-07 09:24:32 +0000867 emitAccelNames();
868 emitAccelObjC();
869 emitAccelNamespaces();
870 emitAccelTypes();
871 }
872
Devang Patel193f7202009-11-24 01:14:22 +0000873 // Emit info into a debug pubtypes section.
Eric Christopher360f0062012-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 Christopher20f47ab2012-08-23 22:36:40 +0000876 if (useDarwinGDBCompat())
Eric Christopher360f0062012-08-23 07:10:56 +0000877 emitDebugPubTypes();
Devang Patel193f7202009-11-24 01:14:22 +0000878
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000879 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000880 emitDebugLoc();
Bill Wendlingf0fb9872009-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 Patel2c4ceb12009-11-21 02:48:08 +0000886 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000887
888 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000889 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000890
891 // Emit inline info.
Eric Christopher9eb1a942012-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 Christopher20f47ab2012-08-23 22:36:40 +0000895 if (useDarwinGDBCompat())
Eric Christopher9eb1a942012-08-23 07:32:02 +0000896 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000897
Eric Christopher7550f7d2012-03-02 00:30:24 +0000898 // Emit info into a debug str section.
899 emitDebugStr();
900
Devang Patele9a1cca2010-08-02 17:32:15 +0000901 // clean up.
902 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000903 SPMap.clear();
Devang Patel163a9f72010-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 Wendlingf0fb9872009-05-20 23:19:06 +0000908}
909
Devang Patel53bb5c92009-11-10 23:06:00 +0000910/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000911DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000912 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-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 Patel2db49d72010-05-07 18:11:54 +0000916 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000917 if (AbsDbgVariable)
918 return AbsDbgVariable;
919
Devang Patelbf47fdb2011-08-10 20:55:27 +0000920 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000921 if (!Scope)
922 return NULL;
923
Devang Patel5a1a67c2011-08-15 19:01:20 +0000924 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000925 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000926 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000927 return AbsDbgVariable;
928}
929
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000930/// addCurrentFnArgument - If Var is a current function argument then add
931/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000932bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000933 DbgVariable *Var, LexicalScope *Scope) {
934 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-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 Patelcb3a6572011-03-03 20:02:02 +0000943 size_t Size = CurrentFnArguments.size();
944 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000945 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000946 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000947 // arguments does the function have at source level.
948 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000949 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000950 CurrentFnArguments[ArgNo - 1] = Var;
951 return true;
952}
953
Devang Patelee432862010-05-20 19:57:06 +0000954/// collectVariableInfoFromMMITable - Collect variable information from
955/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000956void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000957DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000958 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-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 Patele9f8f5e2010-05-07 20:54:48 +0000962 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000963 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000964 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000965 DIVariable DV(Var);
966 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000967
Devang Patelbf47fdb2011-08-10 20:55:27 +0000968 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000969
Devang Patelfb0ee432009-11-10 23:20:04 +0000970 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000971 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000972 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000973
Devang Patel26c1e562010-05-20 16:36:41 +0000974 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000975 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000976 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000977 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000978 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000979 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000980 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000981 }
Devang Patelee432862010-05-20 19:57:06 +0000982}
Devang Patel90a48ad2010-03-15 18:33:46 +0000983
Jim Grosbach1e20b962010-07-21 21:21:52 +0000984/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000985/// DBG_VALUE instruction, is in a defined reg.
986static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000987 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-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 Patelc3f5f782010-05-25 23:40:22 +0000991}
992
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000993/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-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 Topper5e25ee82012-02-05 08:31:47 +00001017 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +00001018}
1019
Devang Patelbf47fdb2011-08-10 20:55:27 +00001020/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001021void
Devang Patel78e127d2010-06-25 22:07:34 +00001022DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1023 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001024
Devang Patelee432862010-05-20 19:57:06 +00001025 /// collection info from MMI table.
1026 collectVariableInfoFromMMITable(MF, Processed);
1027
Jakob Stoklund Olesenadb877d2011-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 Patelee432862010-05-20 19:57:06 +00001033 continue;
1034
Jakob Stoklund Olesenadb877d2011-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 Patelc3f5f782010-05-25 23:40:22 +00001041
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001042 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001043 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001044 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1045 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001046 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001047 else {
1048 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001049 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001050 else {
1051 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001052 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001053 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001054 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001055 }
1056 }
Devang Patelee432862010-05-20 19:57:06 +00001057 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001058 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001059 continue;
1060
1061 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001062 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001063 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1064 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001065 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001066 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001067 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001068 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-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 Patelff9dd0a2011-08-15 21:24:36 +00001073 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001074 continue;
1075 }
1076
1077 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001078 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001079
Jakob Stoklund Olesenadb877d2011-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 Olesene17232e2011-03-22 00:21:41 +00001084
Devang Patel4ada1d72011-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 Olesen28cf1152011-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 Olesenadb877d2011-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 Lattner7a2bdde2011-04-15 05:18:47 +00001096 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001097 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001098 else {
1099 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001100 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1101 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-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 Olesen28cf1152011-03-22 22:33:08 +00001111
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001112 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001113 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1114 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001115 }
1116 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001117 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001118
1119 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-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 Patel98e1cac2010-05-14 21:01:35 +00001128 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001129}
Devang Patel98e1cac2010-05-14 21:01:35 +00001130
Devang Patelc3f5f782010-05-25 23:40:22 +00001131/// getLabelBeforeInsn - Return Label preceding the instruction.
1132const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001133 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1134 assert(Label && "Didn't insert label before instruction");
1135 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001136}
1137
1138/// getLabelAfterInsn - Return Label immediately following the instruction.
1139const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001140 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001141}
1142
Devang Patelcbbe2872010-10-26 17:49:02 +00001143/// beginInstruction - Process beginning of an instruction.
1144void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-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 Christopher60b35f42012-04-05 20:39:05 +00001149 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001150 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001151 if (DL == PrologEndLoc) {
1152 Flags |= DWARF2_FLAG_PROLOGUE_END;
1153 PrologEndLoc = DebugLoc();
1154 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001155 if (PrologEndLoc.isUnknown())
1156 Flags |= DWARF2_FLAG_IS_STMT;
1157
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001158 if (!DL.isUnknown()) {
1159 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001160 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001161 } else
Devang Patel4243e672011-05-11 19:22:19 +00001162 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001163 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001164 }
Devang Patelaead63c2010-03-29 22:59:58 +00001165
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001166 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-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 Patelb2b31a62010-05-26 19:37:24 +00001176 return;
Devang Patel553881b2010-03-29 17:20:31 +00001177
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001178 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001179 PrevLabel = MMI->getContext().CreateTempSymbol();
1180 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001181 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001182 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001183}
1184
Devang Patelcbbe2872010-10-26 17:49:02 +00001185/// endInstruction - Process end of an instruction.
1186void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-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 Olesenadb877d2011-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 Olesen15a3ea02011-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 Patel1c246352010-04-08 16:50:29 +00001207 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001208 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001209}
1210
Jim Grosbach1e20b962010-07-21 21:21:52 +00001211/// identifyScopeMarkers() -
Devang Patel5bc942c2011-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 Patele37b0c62010-04-08 18:43:56 +00001216void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001217 SmallVector<LexicalScope *, 4> WorkList;
1218 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001219 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001220 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001221
Devang Patelbf47fdb2011-08-10 20:55:27 +00001222 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001223 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001224 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001225 SE = Children.end(); SI != SE; ++SI)
1226 WorkList.push_back(*SI);
1227
Devang Patel53bb5c92009-11-10 23:06:00 +00001228 if (S->isAbstractScope())
1229 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001230
Devang Patelbf47fdb2011-08-10 20:55:27 +00001231 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001232 if (Ranges.empty())
1233 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001234 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001235 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-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 Olesenadb877d2011-03-26 02:19:36 +00001238 requestLabelBeforeInsn(RI->first);
1239 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001240 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001241 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001242}
1243
Devang Patela3f48672011-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 Patel4243e672011-05-11 19:22:19 +00001251/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
Eric Christopher6126a1e2012-04-03 00:43:49 +00001252/// line number info for the function.
Devang Patel4243e672011-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 Christopher6126a1e2012-04-03 00:43:49 +00001256 if (SP.Verify()) {
1257 // Check for number of operands since the compatibility is
1258 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001259 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-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 Patel4243e672011-05-11 19:22:19 +00001265 return DebugLoc();
1266}
1267
Devang Patel2c4ceb12009-11-21 02:48:08 +00001268/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001269/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001270void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001271 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001272 LScopes.initialize(*MF);
1273 if (LScopes.empty()) return;
1274 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001275
Devang Pateleac9c072010-04-27 19:46:33 +00001276 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1277 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001278 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001279 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001280
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001281 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1282
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001283 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-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 Patelb2b31a62010-05-26 19:37:24 +00001287 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001288 I != E; ++I) {
1289 bool AtBlockEntry = true;
Devang Patelb2b31a62010-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 Olesenadb877d2011-03-26 02:19:36 +00001293
Devang Patelb2b31a62010-05-26 19:37:24 +00001294 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001295 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001296
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001297 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001298 const MDNode *Var =
1299 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001300
1301 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001302 if (isDbgValueInDefinedReg(MI))
1303 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1304
Jakob Stoklund Olesenadb877d2011-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 Patel79862892011-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 Olesenadb877d2011-03-26 02:19:36 +00001326 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001327 }
Jakob Stoklund Olesenadb877d2011-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 Patel79862892011-07-07 00:14:27 +00001337 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001338 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001339 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1340 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001341 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001342 }
Jakob Stoklund Olesenadb877d2011-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 Patelb2b31a62010-05-26 19:37:24 +00001351 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001352 // Not a DBG_VALUE instruction.
1353 if (!MI->isLabel())
1354 AtBlockEntry = false;
1355
Devang Patel4243e672011-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 Olesen28cf1152011-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 Olesen396618b2012-06-01 23:28:30 +00001366 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1367 AI.isValid(); ++AI) {
1368 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-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 Olesenadb877d2011-03-26 02:19:36 +00001376 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1377 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001378 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001379 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1380 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001381 continue;
Jakob Stoklund Olesenadb877d2011-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 Olesen28cf1152011-03-22 22:33:08 +00001393 }
1394 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001395 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001396 }
Jakob Stoklund Olesenadb877d2011-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 Patel5bc942c2011-08-10 23:58:09 +00001409 MachineBasicBlock::const_iterator LastMI =
1410 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-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 Patelb2b31a62010-05-26 19:37:24 +00001428
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001429 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001430 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-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 Christopher09e47502012-09-10 23:34:06 +00001438 0);
Devang Patel4243e672011-05-11 19:22:19 +00001439 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001440}
1441
Devang Patelbf47fdb2011-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 Patel2c4ceb12009-11-21 02:48:08 +00001448/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001449///
Chris Lattnereec791a2010-01-26 23:18:02 +00001450void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001451 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001452
Devang Patelbf47fdb2011-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 Pateld0b5a5e2011-08-15 22:04:40 +00001462 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001463 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001464 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001465
Devang Patelbf47fdb2011-08-10 20:55:27 +00001466 // Construct abstract scopes.
Devang Patelcd9f6c52011-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 Patelbf47fdb2011-08-10 20:55:27 +00001471 if (SP.Verify()) {
1472 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-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 Samsonovb67bd332012-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 Patel93d39be2011-08-19 23:28:12 +00001484 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1485 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001486 }
1487 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001488 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001489 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001490 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001491
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001492 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001493
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001494 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001495 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001496
Devang Patelbf47fdb2011-08-10 20:55:27 +00001497 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1498 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001499
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001500 // Clear debug info
Devang Patelbf47fdb2011-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 Pateleac0c9d2011-04-22 18:09:57 +00001505 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001506 UserVariables.clear();
1507 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001508 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001509 LabelsBeforeInsn.clear();
1510 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001511 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001512}
1513
Chris Lattnerc6087842010-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 Patel4243e672011-05-11 19:22:19 +00001517void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1518 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001519 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001520 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001521 unsigned Src = 1;
1522 if (S) {
1523 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001524
Dan Gohman1cc0d622010-05-05 23:41:32 +00001525 if (Scope.isCompileUnit()) {
1526 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001527 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001528 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001529 } else if (Scope.isFile()) {
1530 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001531 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001532 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001533 } else if (Scope.isSubprogram()) {
1534 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001535 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001536 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001537 } else if (Scope.isLexicalBlockFile()) {
1538 DILexicalBlockFile DBF(S);
1539 Fn = DBF.getFilename();
1540 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001541 } else if (Scope.isLexicalBlock()) {
1542 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001543 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001544 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001545 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001546 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001547
Devang Patel23670e52011-03-24 20:30:50 +00001548 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001549 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001550 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001551}
1552
Bill Wendling829e67b2009-05-20 23:22:40 +00001553//===----------------------------------------------------------------------===//
1554// Emit Methods
1555//===----------------------------------------------------------------------===//
1556
Devang Patel2c4ceb12009-11-21 02:48:08 +00001557/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001558///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001559unsigned
1560DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001561 // Get the children.
1562 const std::vector<DIE *> &Children = Die->getChildren();
1563
Bill Wendling94d04b82009-05-20 23:21:38 +00001564 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001565 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-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 Lattneraf76e592009-08-22 20:48:53 +00001575 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-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 Lattnera37d5382010-04-05 00:18:22 +00001583 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-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 Patel2c4ceb12009-11-21 02:48:08 +00001591 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-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 Patel2c4ceb12009-11-21 02:48:08 +00001601/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001602///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001603void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-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 Patel513edf62011-04-12 23:10:47 +00001607 unsigned Offset =
Devang Patel163a9f72010-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 Patel163a9f72010-05-10 22:49:55 +00001613 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001614}
1615
Chris Lattner9c69e285532010-04-04 22:59:04 +00001616/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1617/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001618void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001619 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001620
Bill Wendling94d04b82009-05-20 23:21:38 +00001621 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001622 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001623 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001624 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001625 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001626 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001627
Chris Lattner9c69e285532010-04-04 22:59:04 +00001628 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001629 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001630
Devang Patelaf608bd2010-08-24 00:06:12 +00001631 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001632 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
Chris Lattner11b8f302010-04-04 23:02:02 +00001633 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001634 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001635 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001636 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1637 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001638
Devang Patelc3f5f782010-05-25 23:40:22 +00001639 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1640 "section_debug_loc");
1641
Chris Lattner9c69e285532010-04-04 22:59:04 +00001642 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001643 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001644}
1645
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001646/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001647///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001648void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-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 Wendling94d04b82009-05-20 23:21:38 +00001653 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001654 if (Asm->isVerbose())
Chris Lattner894d75a2010-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 Lattner7e1a8f82010-04-04 19:09:29 +00001659 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001660
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001661 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-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 Lattner3f53c832010-04-04 18:52:31 +00001670 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001671 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001672
Bill Wendling94d04b82009-05-20 23:21:38 +00001673 switch (Attr) {
Bill Wendling94d04b82009-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 Patel53bb5c92009-11-10 23:06:00 +00001677 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001678 Asm->EmitInt32(Addr);
1679 break;
1680 }
Devang Patelf2548ca2010-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 Patelb1fcfbe2010-09-02 16:43:44 +00001684
Nick Lewyckyffccd922012-06-22 01:25:12 +00001685 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-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 Patelf2548ca2010-04-16 23:33:45 +00001695 break;
1696 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001697 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-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 Patelc3f5f782010-05-25 23:40:22 +00001704 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00001705 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001706 break;
1707 }
Devang Patel2a361602010-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 Wendling94d04b82009-05-20 23:21:38 +00001716 default:
1717 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001718 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001719 break;
1720 }
Bill Wendling94d04b82009-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 Patel2c4ceb12009-11-21 02:48:08 +00001728 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001729
Chris Lattner3f53c832010-04-04 18:52:31 +00001730 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001731 Asm->OutStreamer.AddComment("End Of Children Mark");
1732 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001733 }
1734}
1735
Devang Patel8a241142009-12-09 18:24:21 +00001736/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001737///
Devang Patel8a241142009-12-09 18:24:21 +00001738void DwarfDebug::emitDebugInfo() {
1739 // Start debug info section.
1740 Asm->OutStreamer.SwitchSection(
1741 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-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 Grosbach1e20b962010-07-21 21:21:52 +00001746
Devang Patel163a9f72010-05-10 22:49:55 +00001747 // Emit the compile units header.
1748 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1749 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001750
Devang Patel163a9f72010-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 Patel65705d52011-04-13 19:41:17 +00001755 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001756
Devang Patel163a9f72010-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 Grosbach1e20b962010-07-21 21:21:52 +00001766
Devang Patel163a9f72010-05-10 22:49:55 +00001767 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001768 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1769 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001770}
1771
Devang Patel2c4ceb12009-11-21 02:48:08 +00001772/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001773///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001774void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-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 Lattner6c2f9e12009-08-19 05:49:37 +00001778 Asm->OutStreamer.SwitchSection(
1779 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001780
Chris Lattnerc0215722010-04-04 19:25:43 +00001781 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-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 Lattner7e1a8f82010-04-04 19:09:29 +00001789 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001790
1791 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001792 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001793 }
1794
1795 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001796 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001797
Chris Lattnerc0215722010-04-04 19:25:43 +00001798 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001799 }
1800}
1801
Devang Patel2c4ceb12009-11-21 02:48:08 +00001802/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001803/// the line matrix.
1804///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001805void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001806 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001807 Asm->OutStreamer.AddComment("Extended Op");
1808 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001809
Chris Lattner233f52b2010-03-09 23:52:58 +00001810 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001811 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-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 Lattnerd85fc6e2010-03-10 01:17:49 +00001816
Chris Lattnerc0215722010-04-04 19:25:43 +00001817 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001818 Asm->getTargetData().getPointerSize(),
1819 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001820
1821 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001822 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1823 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001824 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001825 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001826}
1827
Eric Christopher09ac3d82011-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 Christopher0ffe2b42011-11-10 19:25:34 +00001836 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1837 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001838 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1839 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001840 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-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 Christopher09ac3d82011-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 Christopherfa03db02012-01-06 23:03:34 +00001869 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-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 Christopher8bd36ea2011-11-10 21:47:55 +00001894 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
1895 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001896 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1897 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001898 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-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 Christopher09ac3d82011-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 Christopherc36145f2012-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 Christopher09ac3d82011-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 Christopherc36145f2012-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 Christopher09ac3d82011-11-07 09:24:32 +00001931 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1932 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001933 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-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 Christopher09ac3d82011-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 Patel193f7202009-11-24 01:14:22 +00001950void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-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 Christopher63701182011-11-07 09:18:35 +00001954 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-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 Grosbach1e20b962010-07-21 21:21:52 +00001961
Devang Patel163a9f72010-05-10 22:49:55 +00001962 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1963 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001964
Devang Patel163a9f72010-05-10 22:49:55 +00001965 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1966 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001967
Devang Patel163a9f72010-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 Grosbach1e20b962010-07-21 21:21:52 +00001971
Devang Patel163a9f72010-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 Grosbach1e20b962010-07-21 21:21:52 +00001976
Devang Patel163a9f72010-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 Lewycky3bbb6f72011-07-29 03:49:23 +00001981 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001982
Devang Patel163a9f72010-05-10 22:49:55 +00001983 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1984 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001985
Devang Patel163a9f72010-05-10 22:49:55 +00001986 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00001987 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00001988 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1989 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001990
Devang Patel163a9f72010-05-10 22:49:55 +00001991 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001992 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001993 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1994 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001995 }
Devang Patel193f7202009-11-24 01:14:22 +00001996}
1997
Devang Patel2c4ceb12009-11-21 02:48:08 +00001998/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001999///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002000void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002001 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002002 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002003
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002004 // Start the dwarf str section.
2005 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002006 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002007
Chris Lattnerbc733f52010-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 Grosbach1e20b962010-07-21 21:21:52 +00002010 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00002011 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002012
Chris Lattnerbc733f52010-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 Grosbach1e20b962010-07-21 21:21:52 +00002016
Chris Lattnerbc733f52010-03-13 02:17:42 +00002017 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002018
Chris Lattnerbc733f52010-03-13 02:17:42 +00002019 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002020 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002021 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002022
Benjamin Kramer983c4572011-11-09 18:16:11 +00002023 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00002024 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
2025 Entries[i].second->getKeyLength()+1),
2026 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002027 }
2028}
2029
Devang Patel2c4ceb12009-11-21 02:48:08 +00002030/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002031///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002032void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002033 if (DotDebugLocEntries.empty())
2034 return;
2035
Devang Patel6c3ea902011-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 Dunbar83320a02011-03-16 22:16:39 +00002044 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002045 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002046 Asm->getObjFileLowering().getDwarfLocSection());
2047 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002048 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2049 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002050 for (SmallVector<DotDebugLocEntry, 4>::iterator
2051 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002052 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002053 DotDebugLocEntry &Entry = *I;
2054 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-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 Patel80250682010-05-26 23:55:23 +00002058 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002059 } else {
2060 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2061 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002062 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-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 Patel80efd4e2011-07-08 16:49:43 +00002068 if (Entry.isInt()) {
Devang Patelc4329072011-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 Patel80efd4e2011-07-08 16:49:43 +00002075 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002076 } else {
2077 Asm->OutStreamer.AddComment("DW_OP_constu");
2078 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002079 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002080 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002081 } else if (Entry.isLocation()) {
2082 if (!DV.hasComplexAddress())
2083 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002084 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-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 Christopher50120762012-05-08 18:56:00 +00002115 } else if (Element == DIBuilder::OpDeref) {
Eric Christophera80f2d12012-05-08 21:24:39 +00002116 if (!Entry.Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002117 Asm->EmitInt8(dwarf::DW_OP_deref);
2118 } else
2119 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002120 }
Devang Patelc26f5442011-04-28 02:22:40 +00002121 }
Devang Patelc26f5442011-04-28 02:22:40 +00002122 }
Devang Patel80efd4e2011-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 Espindola5b23b7f2011-05-27 22:05:41 +00002125 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002126 }
2127 }
Bill Wendling94d04b82009-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 Lattner6c2f9e12009-08-19 05:49:37 +00002134 Asm->OutStreamer.SwitchSection(
2135 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002136}
2137
Devang Patel2c4ceb12009-11-21 02:48:08 +00002138/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002139///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002140void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002141 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002142 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002143 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002144 unsigned char Size = Asm->getTargetData().getPointerSize();
2145 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002146 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002147 I != E; ++I) {
2148 if (*I)
2149 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002150 else
Devang Pateleac9c072010-04-27 19:46:33 +00002151 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002152 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002153}
2154
Devang Patel2c4ceb12009-11-21 02:48:08 +00002155/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002156///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002157void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002158 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002159 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002160 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002161 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002162 }
2163}
2164
Devang Patel2c4ceb12009-11-21 02:48:08 +00002165/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-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 Patel2c4ceb12009-11-21 02:48:08 +00002183void DwarfDebug::emitDebugInlineInfo() {
Eric Christopherb83e2bb2012-03-02 02:11:47 +00002184 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002185 return;
2186
Devang Patel163a9f72010-05-10 22:49:55 +00002187 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002188 return;
2189
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002190 Asm->OutStreamer.SwitchSection(
2191 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002192
Chris Lattner233f52b2010-03-09 23:52:58 +00002193 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002194 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2195 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002196
Chris Lattnerc0215722010-04-04 19:25:43 +00002197 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002198
Chris Lattner233f52b2010-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 Lattnerd38fee82010-04-05 00:13:49 +00002202 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002203
Devang Patele9f8f5e2010-05-07 20:54:48 +00002204 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002205 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002206
Devang Patele9f8f5e2010-05-07 20:54:48 +00002207 const MDNode *Node = *I;
2208 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002209 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002210 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002211 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002212 StringRef LName = SP.getLinkageName();
2213 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002214
Chris Lattner233f52b2010-03-09 23:52:58 +00002215 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher50e26612012-03-02 01:57:52 +00002216 if (LName.empty())
2217 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2218 else
Chris Lattner6189ed12010-04-04 23:25:33 +00002219 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2220 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002221
Chris Lattner233f52b2010-03-09 23:52:58 +00002222 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002223 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002224 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002225
Devang Patel53bb5c92009-11-10 23:06:00 +00002226 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002227 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002228 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002229 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002230
Chris Lattner3f53c832010-04-04 18:52:31 +00002231 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002232 Asm->OutStreamer.EmitSymbolValue(LI->first,
2233 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002234 }
2235 }
2236
Chris Lattnerc0215722010-04-04 19:25:43 +00002237 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002238}