blob: 1991785b119028f8216180f3d1ddc1b22e9c6b47 [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 Christopher09ac3d82011-11-07 09:24:32 +000057static cl::opt<bool> DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
58 cl::desc("Output prototype dwarf accelerator tables."),
59 cl::init(false));
60
Eric Christopher10cb7442012-08-23 07:10:46 +000061static cl::opt<bool> DarwinGDBCompat("darwin-gdb-compat", cl::Hidden,
62 cl::desc("Compatibility with Darwin gdb."),
63 cl::init(false));
64
Bill Wendling5f017e82010-04-07 09:28:04 +000065namespace {
66 const char *DWARFGroupName = "DWARF Emission";
67 const char *DbgTimerName = "DWARF Debug Writer";
68} // end anonymous namespace
69
Bill Wendling0310d762009-05-15 09:23:25 +000070//===----------------------------------------------------------------------===//
71
72/// Configuration values for initial hash set sizes (log2).
73///
Bill Wendling0310d762009-05-15 09:23:25 +000074static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000075
76namespace llvm {
77
Nick Lewycky3bbb6f72011-07-29 03:49:23 +000078DIType DbgVariable::getType() const {
Devang Patel3cbee302011-04-12 22:53:02 +000079 DIType Ty = Var.getType();
80 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
81 // addresses instead.
82 if (Var.isBlockByrefVariable()) {
83 /* Byref variables, in Blocks, are declared by the programmer as
84 "SomeType VarName;", but the compiler creates a
85 __Block_byref_x_VarName struct, and gives the variable VarName
86 either the struct, or a pointer to the struct, as its type. This
87 is necessary for various behind-the-scenes things the compiler
88 needs to do with by-reference variables in blocks.
89
90 However, as far as the original *programmer* is concerned, the
91 variable should still have type 'SomeType', as originally declared.
92
93 The following function dives into the __Block_byref_x_VarName
94 struct to find the original type of the variable. This will be
95 passed back to the code generating the type for the Debug
96 Information Entry for the variable 'VarName'. 'VarName' will then
97 have the original type 'SomeType' in its debug information.
98
99 The original type 'SomeType' will be the type of the field named
100 'VarName' inside the __Block_byref_x_VarName struct.
101
102 NOTE: In order for this to not completely fail on the debugger
103 side, the Debug Information Entry for the variable VarName needs to
104 have a DW_AT_location that tells the debugger how to unwind through
105 the pointers and __Block_byref_x_VarName struct to find the actual
106 value of the variable. The function addBlockByrefType does this. */
107 DIType subType = Ty;
108 unsigned tag = Ty.getTag();
109
110 if (tag == dwarf::DW_TAG_pointer_type) {
111 DIDerivedType DTy = DIDerivedType(Ty);
112 subType = DTy.getTypeDerivedFrom();
113 }
114
115 DICompositeType blockStruct = DICompositeType(subType);
116 DIArray Elements = blockStruct.getTypeArray();
117
118 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
119 DIDescriptor Element = Elements.getElement(i);
120 DIDerivedType DT = DIDerivedType(Element);
121 if (getName() == DT.getName())
122 return (DT.getTypeDerivedFrom());
Devang Patel8bd11de2010-08-09 21:01:39 +0000123 }
Devang Patel8bd11de2010-08-09 21:01:39 +0000124 }
Devang Patel3cbee302011-04-12 22:53:02 +0000125 return Ty;
126}
Bill Wendling0310d762009-05-15 09:23:25 +0000127
Chris Lattnerea761862010-04-05 04:09:20 +0000128} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000129
Chris Lattner49cd6642010-04-05 05:11:15 +0000130DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000131 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000132 AbbreviationsSet(InitAbbreviationsSetSize),
Benjamin Kramerf33a79c2012-06-09 10:34:15 +0000133 SourceIdMap(DIEValueAllocator), StringPool(DIEValueAllocator),
Eric Christopher6635cad2012-08-01 18:19:01 +0000134 PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000135 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000136
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000137 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000138 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000139 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000140 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopher60777d82012-04-02 17:58:52 +0000141
Eric Christopher10cb7442012-08-23 07:10:46 +0000142 // Turn on accelerator tables and older gdb compatibility
143 // for Darwin.
144 if (Triple(M->getTargetTriple()).isOSDarwin()) {
Eric Christopher60777d82012-04-02 17:58:52 +0000145 DwarfAccelTables = true;
Eric Christopher10cb7442012-08-23 07:10:46 +0000146 DarwinGDBCompat = true;
147 }
Eric Christopherc1610fa2012-08-23 22:36:36 +0000148
149 isDarwinGDBCompat = DarwinGDBCompat;
Eric Christopher60777d82012-04-02 17:58:52 +0000150
Dan Gohman03c3dc72010-06-18 15:56:31 +0000151 {
152 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
153 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000154 }
Bill Wendling0310d762009-05-15 09:23:25 +0000155}
156DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000157}
158
Eric Christopherd8a87522011-11-07 09:18:38 +0000159/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
160/// temporary label to it if SymbolStem is specified.
161static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
162 const char *SymbolStem = 0) {
163 Asm->OutStreamer.SwitchSection(Section);
164 if (!SymbolStem) return 0;
165
166 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
167 Asm->OutStreamer.EmitLabel(TmpSym);
168 return TmpSym;
169}
170
Nick Lewycky390c40d2011-10-27 06:44:11 +0000171MCSymbol *DwarfDebug::getStringPool() {
172 return Asm->GetTempSymbol("section_str");
173}
174
Chris Lattnerbc733f52010-03-13 02:17:42 +0000175MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
176 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
177 if (Entry.first) return Entry.first;
178
179 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000180 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000181}
182
Devang Patel2c4ceb12009-11-21 02:48:08 +0000183/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000184///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000185void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000186 // Profile the node so that we can make it unique.
187 FoldingSetNodeID ID;
188 Abbrev.Profile(ID);
189
190 // Check the set for priors.
191 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
192
193 // If it's newly added.
194 if (InSet == &Abbrev) {
195 // Add to abbreviation list.
196 Abbreviations.push_back(&Abbrev);
197
198 // Assign the vector position + 1 as its number.
199 Abbrev.setNumber(Abbreviations.size());
200 } else {
201 // Assign existing abbreviation number.
202 Abbrev.setNumber(InSet->getNumber());
203 }
204}
205
Jim Grosbach1e20b962010-07-21 21:21:52 +0000206/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000207/// printer to not emit usual symbol prefix before the symbol name is used then
208/// return linkage name after skipping this special LLVM prefix.
209static StringRef getRealLinkageName(StringRef LinkageName) {
210 char One = '\1';
211 if (LinkageName.startswith(StringRef(&One, 1)))
212 return LinkageName.substr(1);
213 return LinkageName;
214}
215
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000216static bool isObjCClass(StringRef Name) {
217 return Name.startswith("+") || Name.startswith("-");
218}
219
220static bool hasObjCCategory(StringRef Name) {
221 if (!isObjCClass(Name)) return false;
222
223 size_t pos = Name.find(')');
224 if (pos != std::string::npos) {
225 if (Name[pos+1] != ' ') return false;
226 return true;
227 }
228 return false;
229}
230
231static void getObjCClassCategory(StringRef In, StringRef &Class,
232 StringRef &Category) {
233 if (!hasObjCCategory(In)) {
234 Class = In.slice(In.find('[') + 1, In.find(' '));
235 Category = "";
236 return;
237 }
238
239 Class = In.slice(In.find('[') + 1, In.find('('));
240 Category = In.slice(In.find('[') + 1, In.find(' '));
241 return;
242}
243
244static StringRef getObjCMethodName(StringRef In) {
245 return In.slice(In.find(' ') + 1, In.find(']'));
246}
247
248// Add the various names to the Dwarf accelerator table names.
249static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
250 DIE* Die) {
251 if (!SP.isDefinition()) return;
252
253 TheCU->addAccelName(SP.getName(), Die);
254
255 // If the linkage name is different than the name, go ahead and output
256 // that as well into the name table.
257 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
258 TheCU->addAccelName(SP.getLinkageName(), Die);
259
260 // If this is an Objective-C selector name add it to the ObjC accelerator
261 // too.
262 if (isObjCClass(SP.getName())) {
263 StringRef Class, Category;
264 getObjCClassCategory(SP.getName(), Class, Category);
265 TheCU->addAccelObjC(Class, Die);
266 if (Category != "")
267 TheCU->addAccelObjC(Category, Die);
268 // Also add the base method name to the name table.
269 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
270 }
271}
272
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000273/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000274/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
275/// If there are global variables in this scope then create and insert
276/// DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000277DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
278 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000279 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000280
Chris Lattnerd38fee82010-04-05 00:13:49 +0000281 assert(SPDie && "Unable to find subprogram DIE!");
282 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000283
Devang Patel5e06bb82011-04-22 23:10:17 +0000284 DISubprogram SPDecl = SP.getFunctionDeclaration();
Rafael Espindolab0527282011-11-04 19:00:29 +0000285 if (!SPDecl.isSubprogram()) {
Devang Patel5e06bb82011-04-22 23:10:17 +0000286 // There is not any need to generate specification DIE for a function
287 // defined at compile unit level. If a function is defined inside another
288 // function then gdb prefers the definition at top level and but does not
289 // expect specification DIE in parent function. So avoid creating
290 // specification DIE for a function defined inside a function.
291 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
292 !SP.getContext().isFile() &&
293 !isSubprogramContext(SP.getContext())) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000294 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel5e06bb82011-04-22 23:10:17 +0000295
296 // Add arguments.
297 DICompositeType SPTy = SP.getType();
298 DIArray Args = SPTy.getTypeArray();
299 unsigned SPTag = SPTy.getTag();
300 if (SPTag == dwarf::DW_TAG_subroutine_type)
301 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
302 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
303 DIType ATy = DIType(DIType(Args.getElement(i)));
304 SPCU->addType(Arg, ATy);
305 if (ATy.isArtificial())
306 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
307 SPDie->addChild(Arg);
308 }
309 DIE *SPDeclDie = SPDie;
310 SPDie = new DIE(dwarf::DW_TAG_subprogram);
311 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
312 SPDeclDie);
313 SPCU->addDie(SPDie);
314 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000315 }
Devang Patel8aa61472010-07-07 22:20:57 +0000316 // Pick up abstract subprogram DIE.
317 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
318 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000319 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
320 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000321 SPCU->addDie(SPDie);
322 }
323
Devang Patel3cbee302011-04-12 22:53:02 +0000324 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
325 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
326 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
327 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000328 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
329 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000330 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000331
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000332 // Add name to the name table, we do this here because we're guaranteed
333 // to have concrete versions of our DW_TAG_subprogram nodes.
334 addSubprogramNames(SPCU, SP, SPDie);
335
Chris Lattnerd38fee82010-04-05 00:13:49 +0000336 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000337}
338
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000339/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000340/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000341DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
342 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000343 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
344 if (Scope->isAbstractScope())
345 return ScopeDIE;
346
Devang Patelbf47fdb2011-08-10 20:55:27 +0000347 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000348 if (Ranges.empty())
349 return 0;
350
Devang Patelbf47fdb2011-08-10 20:55:27 +0000351 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000352 if (Ranges.size() > 1) {
353 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000354 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000355 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000356 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000357 DebugRangeSymbols.size()
358 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000359 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000360 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000361 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
362 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000363 }
364 DebugRangeSymbols.push_back(NULL);
365 DebugRangeSymbols.push_back(NULL);
366 return ScopeDIE;
367 }
368
Devang Patelc3f5f782010-05-25 23:40:22 +0000369 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
370 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000371
Devang Patelc3f5f782010-05-25 23:40:22 +0000372 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000373
Chris Lattnerb7db7332010-03-09 01:58:53 +0000374 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
375 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000376
Devang Patel3cbee302011-04-12 22:53:02 +0000377 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
378 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000379
380 return ScopeDIE;
381}
382
Devang Patel2c4ceb12009-11-21 02:48:08 +0000383/// constructInlinedScopeDIE - This scope represents inlined body of
384/// a function. Construct DIE to represent this concrete inlined copy
385/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000386DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
387 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000388 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000389 assert(Ranges.empty() == false &&
390 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000391
Devang Patel26a92002011-07-27 00:34:13 +0000392 if (!Scope->getScopeNode())
393 return NULL;
394 DIScope DS(Scope->getScopeNode());
395 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000396 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
397 if (!OriginDIE) {
398 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
399 return NULL;
400 }
401
Devang Patelbf47fdb2011-08-10 20:55:27 +0000402 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000403 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
404 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000405
Devang Patel0afbf232010-07-08 22:39:20 +0000406 if (StartLabel == 0 || EndLabel == 0) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000407 llvm_unreachable("Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000408 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000409 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000410 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000411 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000412 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000413
Devang Pateld96efb82011-05-05 17:54:26 +0000414 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000415 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
416 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000417
Devang Patel26a92002011-07-27 00:34:13 +0000418 if (Ranges.size() > 1) {
419 // .debug_range section has not been laid out yet. Emit offset in
420 // .debug_range as a uint, size 4, for now. emitDIE will handle
421 // DW_AT_ranges appropriately.
422 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000423 DebugRangeSymbols.size()
424 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000425 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000426 RE = Ranges.end(); RI != RE; ++RI) {
427 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
428 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
429 }
430 DebugRangeSymbols.push_back(NULL);
431 DebugRangeSymbols.push_back(NULL);
432 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000433 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
434 StartLabel);
435 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
436 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000437 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000438
439 InlinedSubprogramDIEs.insert(OriginDIE);
440
441 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000442 //.debug_inlined section specification does not clearly state how
443 // to emit inlined scope that is split into multiple instruction ranges.
444 // For now, use first instruction range and emit low_pc/high_pc pair and
445 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000446 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000447 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000448
449 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000450 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000451 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000452 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000453 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000454
Devang Patel53bb5c92009-11-10 23:06:00 +0000455 DILocation DL(Scope->getInlinedAt());
Eric Christopher08212002012-03-26 21:38:38 +0000456 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
457 GetOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patel3cbee302011-04-12 22:53:02 +0000458 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000459
Eric Christopher309bedd2011-12-04 06:02:38 +0000460 // Add name to the name table, we do this here because we're guaranteed
461 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
462 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
463
Devang Patel53bb5c92009-11-10 23:06:00 +0000464 return ScopeDIE;
465}
466
Devang Patel2c4ceb12009-11-21 02:48:08 +0000467/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000468DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000469 if (!Scope || !Scope->getScopeNode())
470 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000471
Nick Lewycky746cb672011-10-26 22:55:33 +0000472 SmallVector<DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000473
474 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000475 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000476 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
477 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000478 if (DIE *Arg =
479 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel0478c152011-03-01 22:58:55 +0000480 Children.push_back(Arg);
481
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000482 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000483 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000484 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000485 if (DIE *Variable =
486 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000487 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000488 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000489 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000490 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000491 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000492 DIScope DS(Scope->getScopeNode());
493 DIE *ScopeDIE = NULL;
494 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000495 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000496 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000497 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000498 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000499 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000500 // Note down abstract DIE.
501 if (ScopeDIE)
502 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
503 }
Devang Patel3c91b052010-03-08 20:52:55 +0000504 else
Devang Pateld3024342011-08-15 22:24:32 +0000505 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000506 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000507 else {
508 // There is no need to emit empty lexical block DIE.
509 if (Children.empty())
510 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000511 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000512 }
513
Devang Patelaead63c2010-03-29 22:59:58 +0000514 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000515
Devang Patel5bc9fec2011-02-19 01:31:27 +0000516 // Add children
517 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
518 E = Children.end(); I != E; ++I)
519 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000520
Jim Grosbach1e20b962010-07-21 21:21:52 +0000521 if (DS.isSubprogram())
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000522 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000523
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000524 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000525}
526
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000527/// GetOrCreateSourceID - Look up the source id with the given directory and
528/// source file names. If none currently exists, create a new id and insert it
529/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
530/// maps as well.
Devang Patel23670e52011-03-24 20:30:50 +0000531unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
532 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000533 // If FE did not provide a file name, then assume stdin.
534 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000535 return GetOrCreateSourceID("<stdin>", StringRef());
536
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000537 // TODO: this might not belong here. See if we can factor this better.
538 if (DirName == CompilationDir)
539 DirName = "";
540
Nick Lewycky44d798d2011-10-17 23:05:28 +0000541 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel1905a182010-09-16 20:57:49 +0000542
Benjamin Kramer74612c22012-03-11 14:56:26 +0000543 // We look up the file/dir pair by concatenating them with a zero byte.
544 SmallString<128> NamePair;
545 NamePair += DirName;
546 NamePair += '\0'; // Zero bytes are not allowed in paths.
547 NamePair += FileName;
548
549 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
550 if (Ent.getValue() != SrcId)
551 return Ent.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000552
Rafael Espindola5c055632010-11-18 02:04:25 +0000553 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer74612c22012-03-11 14:56:26 +0000554 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000555
556 return SrcId;
557}
558
Jim Grosbach1e20b962010-07-21 21:21:52 +0000559/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000560/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000561CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000562 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000563 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000564 CompilationDir = DIUnit.getDirectory();
565 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000566
567 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher438b0922012-02-22 08:46:13 +0000568 CompileUnit *NewCU = new CompileUnit(ID, DIUnit.getLanguage(), Die, Asm, this);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000569 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000570 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
571 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000572 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher6635cad2012-08-01 18:19:01 +0000573 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
574 // into an entity.
575 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000576 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000577 // compile unit in debug_line section.
Rafael Espindola2241e512012-06-22 13:24:07 +0000578 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindola597a7662011-05-04 17:44:06 +0000579 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000580 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000581 else
Devang Patel3cbee302011-04-12 22:53:02 +0000582 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000583
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000584 if (!CompilationDir.empty())
585 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000586 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000587 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000588
Devang Patel65dbc902009-11-25 17:36:49 +0000589 StringRef Flags = DIUnit.getFlags();
590 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000591 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000592
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000593 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000594 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000595 dwarf::DW_FORM_data1, RVer);
596
Devang Patel163a9f72010-05-10 22:49:55 +0000597 if (!FirstCU)
598 FirstCU = NewCU;
599 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000600 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000601}
602
Devang Patel163a9f72010-05-10 22:49:55 +0000603/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000604void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
605 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000606 CompileUnit *&CURef = SPMap[N];
607 if (CURef)
608 return;
609 CURef = TheCU;
610
Devang Patele4b27562009-08-28 23:24:31 +0000611 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000612 if (!SP.isDefinition())
613 // This is a method declaration which will be handled while constructing
614 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000615 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000616
Devang Pateldbc64af2011-08-15 17:24:54 +0000617 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000618
619 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000620 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000621
622 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000623 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000624
Devang Patel13e16b62009-06-26 01:49:18 +0000625 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000626}
627
Devang Patel02e603f2011-08-15 23:47:24 +0000628/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
629/// as llvm.dbg.enum and llvm.dbg.ty
630void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000631 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
632 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
633 const MDNode *N = NMD->getOperand(i);
634 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
635 constructSubprogramDIE(CU, N);
636 }
637
638 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
639 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
640 const MDNode *N = NMD->getOperand(i);
641 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000642 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000643 }
644
Devang Patel02e603f2011-08-15 23:47:24 +0000645 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
646 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
647 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000648 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
649 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000650 }
651
652 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
653 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
654 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000655 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
656 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000657 }
658}
659
660/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
661/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
662bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
663 DebugInfoFinder DbgFinder;
664 DbgFinder.processModule(*M);
665
666 bool HasDebugInfo = false;
667 // Scan all the compile-units to see if there are any marked as the main
668 // unit. If not, we do not generate debug info.
669 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
670 E = DbgFinder.compile_unit_end(); I != E; ++I) {
671 if (DICompileUnit(*I).isMain()) {
672 HasDebugInfo = true;
673 break;
674 }
675 }
676 if (!HasDebugInfo) return false;
677
678 // Create all the compile unit DIEs.
679 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
680 E = DbgFinder.compile_unit_end(); I != E; ++I)
681 constructCompileUnit(*I);
682
683 // Create DIEs for each global variable.
684 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
685 E = DbgFinder.global_variable_end(); I != E; ++I) {
686 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000687 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000688 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000689 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000690
Devang Patel02e603f2011-08-15 23:47:24 +0000691 // Create DIEs for each subprogram.
692 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
693 E = DbgFinder.subprogram_end(); I != E; ++I) {
694 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000695 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
696 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000697 }
698
699 return HasDebugInfo;
700}
701
Devang Patel2c4ceb12009-11-21 02:48:08 +0000702/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000703/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000704/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000705void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000706 if (DisableDebugInfoPrinting)
707 return;
708
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000709 // If module has named metadata anchors then use them, otherwise scan the
710 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000711 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
712 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000713 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
714 DICompileUnit CUNode(CU_Nodes->getOperand(i));
715 CompileUnit *CU = constructCompileUnit(CUNode);
716 DIArray GVs = CUNode.getGlobalVariables();
717 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000718 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000719 DIArray SPs = CUNode.getSubprograms();
720 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
721 constructSubprogramDIE(CU, SPs.getElement(i));
722 DIArray EnumTypes = CUNode.getEnumTypes();
723 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
724 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
725 DIArray RetainedTypes = CUNode.getRetainedTypes();
726 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
727 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
728 }
Devang Patel02e603f2011-08-15 23:47:24 +0000729 } else if (!collectLegacyDebugInfo(M))
730 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000731
Devang Patel02e603f2011-08-15 23:47:24 +0000732 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000733
Chris Lattnerd850ac72010-04-05 02:19:28 +0000734 // Tell MMI that we have debug info.
735 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000736
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000737 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000738 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000739
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000740 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000741 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000742}
743
Devang Patel2c4ceb12009-11-21 02:48:08 +0000744/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000745///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000746void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000747 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000748 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000749 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000750
Devang Patel94c7ddb2011-08-16 22:09:43 +0000751 // Collect info for variables that were optimized out.
752 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
753 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
754 DICompileUnit TheCU(CU_Nodes->getOperand(i));
755 DIArray Subprograms = TheCU.getSubprograms();
756 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
757 DISubprogram SP(Subprograms.getElement(i));
758 if (ProcessedSPNodes.count(SP) != 0) continue;
759 if (!SP.Verify()) continue;
760 if (!SP.isDefinition()) continue;
Devang Patel93d39be2011-08-19 23:28:12 +0000761 DIArray Variables = SP.getVariables();
762 if (Variables.getNumElements() == 0) continue;
763
Devang Patel94c7ddb2011-08-16 22:09:43 +0000764 LexicalScope *Scope =
765 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
766 DeadFnScopeMap[SP] = Scope;
767
768 // Construct subprogram DIE and add variables DIEs.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000769 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky746cb672011-10-26 22:55:33 +0000770 assert(SPCU && "Unable to find Compile Unit!");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000771 constructSubprogramDIE(SPCU, SP);
772 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel93d39be2011-08-19 23:28:12 +0000773 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
774 DIVariable DV(Variables.getElement(vi));
775 if (!DV.Verify()) continue;
776 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000777 if (DIE *VariableDIE =
Devang Patel93d39be2011-08-19 23:28:12 +0000778 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Patel94c7ddb2011-08-16 22:09:43 +0000779 ScopeDIE->addChild(VariableDIE);
780 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000781 }
782 }
783 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000784
Devang Patel53bb5c92009-11-10 23:06:00 +0000785 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
786 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
787 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
788 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000789 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000790 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000791 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
792 AE = AbstractSPDies.end(); AI != AE; ++AI) {
793 DIE *ISP = AI->second;
794 if (InlinedSubprogramDIEs.count(ISP))
795 continue;
796 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
797 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000798
Eric Christopher6635cad2012-08-01 18:19:01 +0000799 // Emit DW_AT_containing_type attribute to connect types with their
800 // vtable holding type.
Devang Pateldbc64af2011-08-15 17:24:54 +0000801 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
802 CUE = CUMap.end(); CUI != CUE; ++CUI) {
803 CompileUnit *TheCU = CUI->second;
804 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000805 }
806
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000807 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000808 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000809 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000810 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000811 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000812
813 // End text sections.
814 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000815 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000816 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000817 }
818
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000819 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000820 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000821
822 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000823 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000824
825 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000826 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000827
Eric Christopher9d9f5a52012-08-23 07:32:06 +0000828 // Emit info into the dwarf accelerator table sections.
Eric Christopher09ac3d82011-11-07 09:24:32 +0000829 if (DwarfAccelTables) {
830 emitAccelNames();
831 emitAccelObjC();
832 emitAccelNamespaces();
833 emitAccelTypes();
834 }
835
Devang Patel193f7202009-11-24 01:14:22 +0000836 // Emit info into a debug pubtypes section.
Eric Christopher360f0062012-08-23 07:10:56 +0000837 // TODO: When we don't need the option anymore we can
838 // remove all of the code that adds to the table.
839 if (DarwinGDBCompat)
840 emitDebugPubTypes();
Devang Patel193f7202009-11-24 01:14:22 +0000841
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000842 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000843 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000844
845 // Emit info into a debug aranges section.
846 EmitDebugARanges();
847
848 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000849 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000850
851 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000852 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000853
854 // Emit inline info.
Eric Christopher9eb1a942012-08-23 07:32:02 +0000855 // TODO: When we don't need the option anymore we
856 // can remove all of the code that this section
857 // depends upon.
858 if (DarwinGDBCompat)
859 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000860
Eric Christopher7550f7d2012-03-02 00:30:24 +0000861 // Emit info into a debug str section.
862 emitDebugStr();
863
Devang Patele9a1cca2010-08-02 17:32:15 +0000864 // clean up.
865 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000866 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000867 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
868 E = CUMap.end(); I != E; ++I)
869 delete I->second;
870 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000871}
872
Devang Patel53bb5c92009-11-10 23:06:00 +0000873/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000874DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000875 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000876 LLVMContext &Ctx = DV->getContext();
877 // More then one inlined variable corresponds to one abstract variable.
878 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000879 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000880 if (AbsDbgVariable)
881 return AbsDbgVariable;
882
Devang Patelbf47fdb2011-08-10 20:55:27 +0000883 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000884 if (!Scope)
885 return NULL;
886
Devang Patel5a1a67c2011-08-15 19:01:20 +0000887 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000888 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000889 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000890 return AbsDbgVariable;
891}
892
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000893/// addCurrentFnArgument - If Var is a current function argument then add
894/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000895bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000896 DbgVariable *Var, LexicalScope *Scope) {
897 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000898 return false;
899 DIVariable DV = Var->getVariable();
900 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
901 return false;
902 unsigned ArgNo = DV.getArgNumber();
903 if (ArgNo == 0)
904 return false;
905
Devang Patelcb3a6572011-03-03 20:02:02 +0000906 size_t Size = CurrentFnArguments.size();
907 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000908 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000909 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000910 // arguments does the function have at source level.
911 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000912 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000913 CurrentFnArguments[ArgNo - 1] = Var;
914 return true;
915}
916
Devang Patelee432862010-05-20 19:57:06 +0000917/// collectVariableInfoFromMMITable - Collect variable information from
918/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000919void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000920DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000921 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000922 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
923 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
924 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000925 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000926 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000927 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000928 DIVariable DV(Var);
929 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000930
Devang Patelbf47fdb2011-08-10 20:55:27 +0000931 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000932
Devang Patelfb0ee432009-11-10 23:20:04 +0000933 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000934 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000935 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000936
Devang Patel26c1e562010-05-20 16:36:41 +0000937 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000938 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000939 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000940 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000941 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000942 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000943 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000944 }
Devang Patelee432862010-05-20 19:57:06 +0000945}
Devang Patel90a48ad2010-03-15 18:33:46 +0000946
Jim Grosbach1e20b962010-07-21 21:21:52 +0000947/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000948/// DBG_VALUE instruction, is in a defined reg.
949static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000950 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000951 return MI->getNumOperands() == 3 &&
952 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
953 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000954}
955
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000956/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000957/// at MI.
958static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
959 const MCSymbol *FLabel,
960 const MCSymbol *SLabel,
961 const MachineInstr *MI) {
962 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
963
964 if (MI->getNumOperands() != 3) {
965 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
966 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
967 }
968 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
969 MachineLocation MLoc;
970 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
971 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
972 }
973 if (MI->getOperand(0).isImm())
974 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
975 if (MI->getOperand(0).isFPImm())
976 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
977 if (MI->getOperand(0).isCImm())
978 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
979
Craig Topper5e25ee82012-02-05 08:31:47 +0000980 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +0000981}
982
Devang Patelbf47fdb2011-08-10 20:55:27 +0000983/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000984void
Devang Patel78e127d2010-06-25 22:07:34 +0000985DwarfDebug::collectVariableInfo(const MachineFunction *MF,
986 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000987
Devang Patelee432862010-05-20 19:57:06 +0000988 /// collection info from MMI table.
989 collectVariableInfoFromMMITable(MF, Processed);
990
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000991 for (SmallVectorImpl<const MDNode*>::const_iterator
992 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
993 ++UVI) {
994 const MDNode *Var = *UVI;
995 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +0000996 continue;
997
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000998 // History contains relevant DBG_VALUE instructions for Var and instructions
999 // clobbering it.
1000 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1001 if (History.empty())
1002 continue;
1003 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001004
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001005 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001006 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001007 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1008 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001009 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001010 else {
1011 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001012 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001013 else {
1014 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001015 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001016 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001017 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001018 }
1019 }
Devang Patelee432862010-05-20 19:57:06 +00001020 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001021 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001022 continue;
1023
1024 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001025 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001026 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1027 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001028 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001029 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001030 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001031 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001032
1033 // Simple ranges that are fully coalesced.
1034 if (History.size() <= 1 || (History.size() == 2 &&
1035 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001036 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001037 continue;
1038 }
1039
1040 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001041 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001042
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001043 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1044 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1045 const MachineInstr *Begin = *HI;
1046 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001047
Devang Patel4ada1d72011-06-01 23:00:17 +00001048 // Check if DBG_VALUE is truncating a range.
1049 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1050 && !Begin->getOperand(0).getReg())
1051 continue;
1052
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001053 // Compute the range for a register location.
1054 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1055 const MCSymbol *SLabel = 0;
1056
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001057 if (HI + 1 == HE)
1058 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001059 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001060 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001061 else {
1062 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001063 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1064 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001065 if (End->isDebugValue())
1066 SLabel = getLabelBeforeInsn(End);
1067 else {
1068 // End is a normal instruction clobbering the range.
1069 SLabel = getLabelAfterInsn(End);
1070 assert(SLabel && "Forgot label after clobber instruction");
1071 ++HI;
1072 }
1073 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001074
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001075 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001076 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1077 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001078 }
1079 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001080 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001081
1082 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001083 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1084 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1085 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1086 DIVariable DV(Variables.getElement(i));
1087 if (!DV || !DV.Verify() || !Processed.insert(DV))
1088 continue;
1089 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1090 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001091 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001092}
Devang Patel98e1cac2010-05-14 21:01:35 +00001093
Devang Patelc3f5f782010-05-25 23:40:22 +00001094/// getLabelBeforeInsn - Return Label preceding the instruction.
1095const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001096 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1097 assert(Label && "Didn't insert label before instruction");
1098 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001099}
1100
1101/// getLabelAfterInsn - Return Label immediately following the instruction.
1102const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001103 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001104}
1105
Devang Patelcbbe2872010-10-26 17:49:02 +00001106/// beginInstruction - Process beginning of an instruction.
1107void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001108 // Check if source location changes, but ignore DBG_VALUE locations.
1109 if (!MI->isDebugValue()) {
1110 DebugLoc DL = MI->getDebugLoc();
1111 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001112 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001113 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001114 if (DL == PrologEndLoc) {
1115 Flags |= DWARF2_FLAG_PROLOGUE_END;
1116 PrologEndLoc = DebugLoc();
1117 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001118 if (PrologEndLoc.isUnknown())
1119 Flags |= DWARF2_FLAG_IS_STMT;
1120
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001121 if (!DL.isUnknown()) {
1122 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001123 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001124 } else
Devang Patel4243e672011-05-11 19:22:19 +00001125 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001126 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001127 }
Devang Patelaead63c2010-03-29 22:59:58 +00001128
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001129 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001130 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1131 LabelsBeforeInsn.find(MI);
1132
1133 // No label needed.
1134 if (I == LabelsBeforeInsn.end())
1135 return;
1136
1137 // Label already assigned.
1138 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001139 return;
Devang Patel553881b2010-03-29 17:20:31 +00001140
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001141 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001142 PrevLabel = MMI->getContext().CreateTempSymbol();
1143 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001144 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001145 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001146}
1147
Devang Patelcbbe2872010-10-26 17:49:02 +00001148/// endInstruction - Process end of an instruction.
1149void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001150 // Don't create a new label after DBG_VALUE instructions.
1151 // They don't generate code.
1152 if (!MI->isDebugValue())
1153 PrevLabel = 0;
1154
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001155 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1156 LabelsAfterInsn.find(MI);
1157
1158 // No label needed.
1159 if (I == LabelsAfterInsn.end())
1160 return;
1161
1162 // Label already assigned.
1163 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001164 return;
1165
1166 // We need a label after this instruction.
1167 if (!PrevLabel) {
1168 PrevLabel = MMI->getContext().CreateTempSymbol();
1169 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001170 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001171 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001172}
1173
Jim Grosbach1e20b962010-07-21 21:21:52 +00001174/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001175/// Each LexicalScope has first instruction and last instruction to mark
1176/// beginning and end of a scope respectively. Create an inverse map that list
1177/// scopes starts (and ends) with an instruction. One instruction may start (or
1178/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001179void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001180 SmallVector<LexicalScope *, 4> WorkList;
1181 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001182 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001183 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001184
Devang Patelbf47fdb2011-08-10 20:55:27 +00001185 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001186 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001187 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001188 SE = Children.end(); SI != SE; ++SI)
1189 WorkList.push_back(*SI);
1190
Devang Patel53bb5c92009-11-10 23:06:00 +00001191 if (S->isAbstractScope())
1192 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001193
Devang Patelbf47fdb2011-08-10 20:55:27 +00001194 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001195 if (Ranges.empty())
1196 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001197 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001198 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001199 assert(RI->first && "InsnRange does not have first instruction!");
1200 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001201 requestLabelBeforeInsn(RI->first);
1202 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001203 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001204 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001205}
1206
Devang Patela3f48672011-05-09 22:14:49 +00001207/// getScopeNode - Get MDNode for DebugLoc's scope.
1208static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1209 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1210 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1211 return DL.getScope(Ctx);
1212}
1213
Devang Patel4243e672011-05-11 19:22:19 +00001214/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
Eric Christopher6126a1e2012-04-03 00:43:49 +00001215/// line number info for the function.
Devang Patel4243e672011-05-11 19:22:19 +00001216static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1217 const MDNode *Scope = getScopeNode(DL, Ctx);
1218 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher6126a1e2012-04-03 00:43:49 +00001219 if (SP.Verify()) {
1220 // Check for number of operands since the compatibility is
1221 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001222 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-04-03 00:43:49 +00001223 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1224 else
1225 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1226 }
1227
Devang Patel4243e672011-05-11 19:22:19 +00001228 return DebugLoc();
1229}
1230
Devang Patel2c4ceb12009-11-21 02:48:08 +00001231/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001232/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001233void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001234 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001235 LScopes.initialize(*MF);
1236 if (LScopes.empty()) return;
1237 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001238
Devang Pateleac9c072010-04-27 19:46:33 +00001239 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1240 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001241 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001242 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001243
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001244 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1245
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001246 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001247 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1248 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1249
Devang Patelb2b31a62010-05-26 19:37:24 +00001250 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001251 I != E; ++I) {
1252 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001253 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1254 II != IE; ++II) {
1255 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001256
Devang Patelb2b31a62010-05-26 19:37:24 +00001257 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001258 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001259
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001260 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001261 const MDNode *Var =
1262 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001263
1264 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001265 if (isDbgValueInDefinedReg(MI))
1266 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1267
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001268 // Check the history of this variable.
1269 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1270 if (History.empty()) {
1271 UserVariables.push_back(Var);
1272 // The first mention of a function argument gets the FunctionBeginSym
1273 // label, so arguments are visible when breaking at function entry.
1274 DIVariable DV(Var);
1275 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1276 DISubprogram(getDISubprogram(DV.getContext()))
1277 .describes(MF->getFunction()))
1278 LabelsBeforeInsn[MI] = FunctionBeginSym;
1279 } else {
1280 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1281 const MachineInstr *Prev = History.back();
1282 if (Prev->isDebugValue()) {
1283 // Coalesce identical entries at the end of History.
1284 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001285 Prev->isIdenticalTo(History[History.size() - 2])) {
1286 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1287 << "\t" << *Prev
1288 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001289 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001290 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001291
1292 // Terminate old register assignments that don't reach MI;
1293 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1294 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1295 isDbgValueInDefinedReg(Prev)) {
1296 // Previous register assignment needs to terminate at the end of
1297 // its basic block.
1298 MachineBasicBlock::const_iterator LastMI =
1299 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001300 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001301 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001302 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1303 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001304 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001305 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001306 else {
1307 // Terminate after LastMI.
1308 History.push_back(LastMI);
1309 }
1310 }
1311 }
1312 }
1313 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001314 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001315 // Not a DBG_VALUE instruction.
1316 if (!MI->isLabel())
1317 AtBlockEntry = false;
1318
Devang Patel4243e672011-05-11 19:22:19 +00001319 // First known non DBG_VALUE location marks beginning of function
1320 // body.
1321 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1322 PrologEndLoc = MI->getDebugLoc();
1323
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001324 // Check if the instruction clobbers any registers with debug vars.
1325 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1326 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1327 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1328 continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001329 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1330 AI.isValid(); ++AI) {
1331 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001332 const MDNode *Var = LiveUserVar[Reg];
1333 if (!Var)
1334 continue;
1335 // Reg is now clobbered.
1336 LiveUserVar[Reg] = 0;
1337
1338 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001339 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1340 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001341 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001342 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1343 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001344 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001345 const MachineInstr *Prev = History.back();
1346 // Sanity-check: Register assignments are terminated at the end of
1347 // their block.
1348 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1349 continue;
1350 // Is the variable still in Reg?
1351 if (!isDbgValueInDefinedReg(Prev) ||
1352 Prev->getOperand(0).getReg() != Reg)
1353 continue;
1354 // Var is clobbered. Make sure the next instruction gets a label.
1355 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001356 }
1357 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001358 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001359 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001360 }
1361
1362 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1363 I != E; ++I) {
1364 SmallVectorImpl<const MachineInstr*> &History = I->second;
1365 if (History.empty())
1366 continue;
1367
1368 // Make sure the final register assignments are terminated.
1369 const MachineInstr *Prev = History.back();
1370 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1371 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001372 MachineBasicBlock::const_iterator LastMI =
1373 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001374 if (LastMI == PrevMBB->end())
1375 // Drop DBG_VALUE for empty range.
1376 History.pop_back();
1377 else {
1378 // Terminate after LastMI.
1379 History.push_back(LastMI);
1380 }
1381 }
1382 // Request labels for the full history.
1383 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1384 const MachineInstr *MI = History[i];
1385 if (MI->isDebugValue())
1386 requestLabelBeforeInsn(MI);
1387 else
1388 requestLabelAfterInsn(MI);
1389 }
1390 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001391
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001392 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001393 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001394
1395 // Record beginning of function.
1396 if (!PrologEndLoc.isUnknown()) {
1397 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1398 MF->getFunction()->getContext());
1399 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1400 FnStartDL.getScope(MF->getFunction()->getContext()),
Eric Christopher5cf55e12012-07-12 23:30:25 +00001401 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0);
Devang Patel4243e672011-05-11 19:22:19 +00001402 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001403}
1404
Devang Patelbf47fdb2011-08-10 20:55:27 +00001405void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1406// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1407 ScopeVariables[LS].push_back(Var);
1408// Vars.push_back(Var);
1409}
1410
Devang Patel2c4ceb12009-11-21 02:48:08 +00001411/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001412///
Chris Lattnereec791a2010-01-26 23:18:02 +00001413void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001414 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001415
Devang Patelbf47fdb2011-08-10 20:55:27 +00001416 // Define end label for subprogram.
1417 FunctionEndSym = Asm->GetTempSymbol("func_end",
1418 Asm->getFunctionNumber());
1419 // Assumes in correct section after the entry point.
1420 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1421
1422 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1423 collectVariableInfo(MF, ProcessedVars);
1424
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001425 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001426 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001427 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001428
Devang Patelbf47fdb2011-08-10 20:55:27 +00001429 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001430 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1431 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1432 LexicalScope *AScope = AList[i];
1433 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001434 if (SP.Verify()) {
1435 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001436 DIArray Variables = SP.getVariables();
1437 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1438 DIVariable DV(Variables.getElement(i));
1439 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1440 continue;
Alexey Samsonovb67bd332012-07-06 08:45:08 +00001441 // Check that DbgVariable for DV wasn't created earlier, when
1442 // findAbstractVariable() was called for inlined instance of DV.
1443 LLVMContext &Ctx = DV->getContext();
1444 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1445 if (AbstractVariables.lookup(CleanDV))
1446 continue;
Devang Patel93d39be2011-08-19 23:28:12 +00001447 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1448 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001449 }
1450 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001451 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001452 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001453 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001454
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001455 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001456
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001457 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Devang Patel5bc942c2011-08-10 23:58:09 +00001458 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1459 dwarf::DW_FORM_flag, 1);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001460
Devang Patelbf47fdb2011-08-10 20:55:27 +00001461 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1462 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001463
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001464 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001465 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1466 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1467 DeleteContainerPointers(I->second);
1468 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001469 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001470 UserVariables.clear();
1471 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001472 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001473 LabelsBeforeInsn.clear();
1474 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001475 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001476}
1477
Chris Lattnerc6087842010-03-09 04:54:43 +00001478/// recordSourceLine - Register a source line with debug info. Returns the
1479/// unique label that was emitted and which provides correspondence to
1480/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001481void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1482 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001483 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001484 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001485 unsigned Src = 1;
1486 if (S) {
1487 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001488
Dan Gohman1cc0d622010-05-05 23:41:32 +00001489 if (Scope.isCompileUnit()) {
1490 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001491 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001492 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001493 } else if (Scope.isFile()) {
1494 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001495 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001496 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001497 } else if (Scope.isSubprogram()) {
1498 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001499 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001500 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001501 } else if (Scope.isLexicalBlockFile()) {
1502 DILexicalBlockFile DBF(S);
1503 Fn = DBF.getFilename();
1504 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001505 } else if (Scope.isLexicalBlock()) {
1506 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001507 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001508 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001509 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001510 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001511
Devang Patel23670e52011-03-24 20:30:50 +00001512 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001513 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001514 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001515}
1516
Bill Wendling829e67b2009-05-20 23:22:40 +00001517//===----------------------------------------------------------------------===//
1518// Emit Methods
1519//===----------------------------------------------------------------------===//
1520
Devang Patel2c4ceb12009-11-21 02:48:08 +00001521/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001522///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001523unsigned
1524DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001525 // Get the children.
1526 const std::vector<DIE *> &Children = Die->getChildren();
1527
Bill Wendling94d04b82009-05-20 23:21:38 +00001528 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001529 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001530
1531 // Get the abbreviation for this DIE.
1532 unsigned AbbrevNumber = Die->getAbbrevNumber();
1533 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1534
1535 // Set DIE offset
1536 Die->setOffset(Offset);
1537
1538 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001539 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001540
1541 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1542 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1543
1544 // Size the DIE attribute values.
1545 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1546 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001547 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001548
1549 // Size the DIE children if any.
1550 if (!Children.empty()) {
1551 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1552 "Children flag not set");
1553
1554 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001555 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001556
1557 // End of children marker.
1558 Offset += sizeof(int8_t);
1559 }
1560
1561 Die->setSize(Offset - Die->getOffset());
1562 return Offset;
1563}
1564
Devang Patel2c4ceb12009-11-21 02:48:08 +00001565/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001566///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001567void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001568 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1569 E = CUMap.end(); I != E; ++I) {
1570 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001571 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001572 sizeof(int32_t) + // Length of Compilation Unit Info
1573 sizeof(int16_t) + // DWARF version number
1574 sizeof(int32_t) + // Offset Into Abbrev. Section
1575 sizeof(int8_t); // Pointer Size (in bytes)
1576 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001577 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001578}
1579
Chris Lattner9c69e285532010-04-04 22:59:04 +00001580/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1581/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001582void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001583 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001584
Bill Wendling94d04b82009-05-20 23:21:38 +00001585 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001586 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001587 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001588 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001589 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001590 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001591
Chris Lattner9c69e285532010-04-04 22:59:04 +00001592 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001593 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001594
Devang Patelaf608bd2010-08-24 00:06:12 +00001595 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001596 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
Chris Lattner11b8f302010-04-04 23:02:02 +00001597 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001598 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001599 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001600 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1601 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001602
Devang Patelc3f5f782010-05-25 23:40:22 +00001603 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1604 "section_debug_loc");
1605
Chris Lattner9c69e285532010-04-04 22:59:04 +00001606 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001607 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001608}
1609
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001610/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001611///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001612void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001613 // Get the abbreviation for this DIE.
1614 unsigned AbbrevNumber = Die->getAbbrevNumber();
1615 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1616
Bill Wendling94d04b82009-05-20 23:21:38 +00001617 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001618 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001619 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1620 Twine::utohexstr(Die->getOffset()) + ":0x" +
1621 Twine::utohexstr(Die->getSize()) + " " +
1622 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001623 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001624
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001625 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001626 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1627
1628 // Emit the DIE attribute values.
1629 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1630 unsigned Attr = AbbrevData[i].getAttribute();
1631 unsigned Form = AbbrevData[i].getForm();
1632 assert(Form && "Too many attributes for DIE (check abbreviation)");
1633
Chris Lattner3f53c832010-04-04 18:52:31 +00001634 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001635 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001636
Bill Wendling94d04b82009-05-20 23:21:38 +00001637 switch (Attr) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001638 case dwarf::DW_AT_abstract_origin: {
1639 DIEEntry *E = cast<DIEEntry>(Values[i]);
1640 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001641 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001642 Asm->EmitInt32(Addr);
1643 break;
1644 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001645 case dwarf::DW_AT_ranges: {
1646 // DW_AT_range Value encodes offset in debug_range section.
1647 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001648
Nick Lewyckyffccd922012-06-22 01:25:12 +00001649 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001650 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1651 V->getValue(),
1652 4);
1653 } else {
1654 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1655 V->getValue(),
1656 DwarfDebugRangeSectionSym,
1657 4);
1658 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001659 break;
1660 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001661 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-06-22 01:25:12 +00001662 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1663 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1664 Asm->EmitLabelReference(L->getValue(), 4);
1665 else
1666 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1667 } else {
Devang Patelc3f5f782010-05-25 23:40:22 +00001668 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00001669 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001670 break;
1671 }
Devang Patel2a361602010-09-29 19:08:08 +00001672 case dwarf::DW_AT_accessibility: {
1673 if (Asm->isVerbose()) {
1674 DIEInteger *V = cast<DIEInteger>(Values[i]);
1675 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1676 }
1677 Values[i]->EmitValue(Asm, Form);
1678 break;
1679 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001680 default:
1681 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001682 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001683 break;
1684 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001685 }
1686
1687 // Emit the DIE children if any.
1688 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1689 const std::vector<DIE *> &Children = Die->getChildren();
1690
1691 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001692 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001693
Chris Lattner3f53c832010-04-04 18:52:31 +00001694 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001695 Asm->OutStreamer.AddComment("End Of Children Mark");
1696 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001697 }
1698}
1699
Devang Patel8a241142009-12-09 18:24:21 +00001700/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001701///
Devang Patel8a241142009-12-09 18:24:21 +00001702void DwarfDebug::emitDebugInfo() {
1703 // Start debug info section.
1704 Asm->OutStreamer.SwitchSection(
1705 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001706 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1707 E = CUMap.end(); I != E; ++I) {
1708 CompileUnit *TheCU = I->second;
1709 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001710
Devang Patel163a9f72010-05-10 22:49:55 +00001711 // Emit the compile units header.
1712 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1713 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001714
Devang Patel163a9f72010-05-10 22:49:55 +00001715 // Emit size of content not including length itself
1716 unsigned ContentSize = Die->getSize() +
1717 sizeof(int16_t) + // DWARF version number
1718 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001719 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001720
Devang Patel163a9f72010-05-10 22:49:55 +00001721 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1722 Asm->EmitInt32(ContentSize);
1723 Asm->OutStreamer.AddComment("DWARF version number");
1724 Asm->EmitInt16(dwarf::DWARF_VERSION);
1725 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1726 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1727 DwarfAbbrevSectionSym);
1728 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1729 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001730
Devang Patel163a9f72010-05-10 22:49:55 +00001731 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001732 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1733 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001734}
1735
Devang Patel2c4ceb12009-11-21 02:48:08 +00001736/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001737///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001738void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001739 // Check to see if it is worth the effort.
1740 if (!Abbreviations.empty()) {
1741 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001742 Asm->OutStreamer.SwitchSection(
1743 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001744
Chris Lattnerc0215722010-04-04 19:25:43 +00001745 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001746
1747 // For each abbrevation.
1748 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1749 // Get abbreviation data
1750 const DIEAbbrev *Abbrev = Abbreviations[i];
1751
1752 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001753 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001754
1755 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001756 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001757 }
1758
1759 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001760 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001761
Chris Lattnerc0215722010-04-04 19:25:43 +00001762 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001763 }
1764}
1765
Devang Patel2c4ceb12009-11-21 02:48:08 +00001766/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001767/// the line matrix.
1768///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001769void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001770 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001771 Asm->OutStreamer.AddComment("Extended Op");
1772 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001773
Chris Lattner233f52b2010-03-09 23:52:58 +00001774 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001775 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001776 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1777 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1778
1779 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001780
Chris Lattnerc0215722010-04-04 19:25:43 +00001781 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001782 Asm->getTargetData().getPointerSize(),
1783 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001784
1785 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001786 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1787 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001788 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001789 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001790}
1791
Eric Christopher09ac3d82011-11-07 09:24:32 +00001792/// emitAccelNames - Emit visible names into a hashed accelerator table
1793/// section.
1794void DwarfDebug::emitAccelNames() {
1795 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1796 dwarf::DW_FORM_data4));
1797 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1798 E = CUMap.end(); I != E; ++I) {
1799 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001800 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1801 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001802 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1803 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001804 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001805 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1806 DE = Entities.end(); DI != DE; ++DI)
1807 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001808 }
1809 }
1810
1811 AT.FinalizeTable(Asm, "Names");
1812 Asm->OutStreamer.SwitchSection(
1813 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1814 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1815 Asm->OutStreamer.EmitLabel(SectionBegin);
1816
1817 // Emit the full data.
1818 AT.Emit(Asm, SectionBegin, this);
1819}
1820
1821/// emitAccelObjC - Emit objective C classes and categories into a hashed
1822/// accelerator table section.
1823void DwarfDebug::emitAccelObjC() {
1824 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1825 dwarf::DW_FORM_data4));
1826 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1827 E = CUMap.end(); I != E; ++I) {
1828 CompileUnit *TheCU = I->second;
1829 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1830 for (StringMap<std::vector<DIE*> >::const_iterator
1831 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1832 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001833 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-11-07 09:24:32 +00001834 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1835 DE = Entities.end(); DI != DE; ++DI)
1836 AT.AddName(Name, (*DI));
1837 }
1838 }
1839
1840 AT.FinalizeTable(Asm, "ObjC");
1841 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1842 .getDwarfAccelObjCSection());
1843 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1844 Asm->OutStreamer.EmitLabel(SectionBegin);
1845
1846 // Emit the full data.
1847 AT.Emit(Asm, SectionBegin, this);
1848}
1849
1850/// emitAccelNamespace - Emit namespace dies into a hashed accelerator
1851/// table.
1852void DwarfDebug::emitAccelNamespaces() {
1853 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1854 dwarf::DW_FORM_data4));
1855 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1856 E = CUMap.end(); I != E; ++I) {
1857 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001858 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
1859 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001860 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1861 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001862 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001863 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1864 DE = Entities.end(); DI != DE; ++DI)
1865 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001866 }
1867 }
1868
1869 AT.FinalizeTable(Asm, "namespac");
1870 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1871 .getDwarfAccelNamespaceSection());
1872 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
1873 Asm->OutStreamer.EmitLabel(SectionBegin);
1874
1875 // Emit the full data.
1876 AT.Emit(Asm, SectionBegin, this);
1877}
1878
1879/// emitAccelTypes() - Emit type dies into a hashed accelerator table.
1880void DwarfDebug::emitAccelTypes() {
Eric Christopherc36145f2012-01-06 04:35:23 +00001881 std::vector<DwarfAccelTable::Atom> Atoms;
1882 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1883 dwarf::DW_FORM_data4));
1884 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
1885 dwarf::DW_FORM_data2));
1886 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
1887 dwarf::DW_FORM_data1));
1888 DwarfAccelTable AT(Atoms);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001889 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1890 E = CUMap.end(); I != E; ++I) {
1891 CompileUnit *TheCU = I->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001892 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
1893 = TheCU->getAccelTypes();
1894 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001895 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1896 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001897 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001898 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
1899 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
1900 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001901 }
1902 }
1903
1904 AT.FinalizeTable(Asm, "types");
1905 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1906 .getDwarfAccelTypesSection());
1907 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
1908 Asm->OutStreamer.EmitLabel(SectionBegin);
1909
1910 // Emit the full data.
1911 AT.Emit(Asm, SectionBegin, this);
1912}
1913
Devang Patel193f7202009-11-24 01:14:22 +00001914void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001915 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1916 E = CUMap.end(); I != E; ++I) {
1917 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00001918 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-05-10 22:49:55 +00001919 Asm->OutStreamer.SwitchSection(
1920 Asm->getObjFileLowering().getDwarfPubTypesSection());
1921 Asm->OutStreamer.AddComment("Length of Public Types Info");
1922 Asm->EmitLabelDifference(
1923 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1924 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001925
Devang Patel163a9f72010-05-10 22:49:55 +00001926 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1927 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001928
Devang Patel163a9f72010-05-10 22:49:55 +00001929 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1930 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001931
Devang Patel163a9f72010-05-10 22:49:55 +00001932 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1933 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1934 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001935
Devang Patel163a9f72010-05-10 22:49:55 +00001936 Asm->OutStreamer.AddComment("Compilation Unit Length");
1937 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1938 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1939 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001940
Devang Patel163a9f72010-05-10 22:49:55 +00001941 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1942 for (StringMap<DIE*>::const_iterator
1943 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1944 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001945 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001946
Devang Patel163a9f72010-05-10 22:49:55 +00001947 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1948 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001949
Devang Patel163a9f72010-05-10 22:49:55 +00001950 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00001951 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00001952 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1953 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001954
Devang Patel163a9f72010-05-10 22:49:55 +00001955 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001956 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001957 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1958 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001959 }
Devang Patel193f7202009-11-24 01:14:22 +00001960}
1961
Devang Patel2c4ceb12009-11-21 02:48:08 +00001962/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001963///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001964void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001965 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001966 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001967
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001968 // Start the dwarf str section.
1969 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001970 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001971
Chris Lattnerbc733f52010-03-13 02:17:42 +00001972 // Get all of the string pool entries and put them in an array by their ID so
1973 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001974 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00001975 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001976
Chris Lattnerbc733f52010-03-13 02:17:42 +00001977 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1978 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1979 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001980
Chris Lattnerbc733f52010-03-13 02:17:42 +00001981 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001982
Chris Lattnerbc733f52010-03-13 02:17:42 +00001983 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001984 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001985 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001986
Benjamin Kramer983c4572011-11-09 18:16:11 +00001987 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00001988 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
1989 Entries[i].second->getKeyLength()+1),
1990 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001991 }
1992}
1993
Devang Patel2c4ceb12009-11-21 02:48:08 +00001994/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001995///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001996void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00001997 if (DotDebugLocEntries.empty())
1998 return;
1999
Devang Patel6c3ea902011-02-04 22:57:18 +00002000 for (SmallVector<DotDebugLocEntry, 4>::iterator
2001 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2002 I != E; ++I) {
2003 DotDebugLocEntry &Entry = *I;
2004 if (I + 1 != DotDebugLocEntries.end())
2005 Entry.Merge(I+1);
2006 }
2007
Daniel Dunbar83320a02011-03-16 22:16:39 +00002008 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002009 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002010 Asm->getObjFileLowering().getDwarfLocSection());
2011 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002012 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2013 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002014 for (SmallVector<DotDebugLocEntry, 4>::iterator
2015 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002016 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002017 DotDebugLocEntry &Entry = *I;
2018 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002019 if (Entry.isEmpty()) {
2020 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2021 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002022 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002023 } else {
2024 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2025 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002026 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002027 Asm->OutStreamer.AddComment("Loc expr size");
2028 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2029 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2030 Asm->EmitLabelDifference(end, begin, 2);
2031 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002032 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002033 DIBasicType BTy(DV.getType());
2034 if (BTy.Verify() &&
2035 (BTy.getEncoding() == dwarf::DW_ATE_signed
2036 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2037 Asm->OutStreamer.AddComment("DW_OP_consts");
2038 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002039 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002040 } else {
2041 Asm->OutStreamer.AddComment("DW_OP_constu");
2042 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002043 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002044 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002045 } else if (Entry.isLocation()) {
2046 if (!DV.hasComplexAddress())
2047 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002048 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002049 else {
2050 // Complex address entry.
2051 unsigned N = DV.getNumAddrElements();
2052 unsigned i = 0;
2053 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2054 if (Entry.Loc.getOffset()) {
2055 i = 2;
2056 Asm->EmitDwarfRegOp(Entry.Loc);
2057 Asm->OutStreamer.AddComment("DW_OP_deref");
2058 Asm->EmitInt8(dwarf::DW_OP_deref);
2059 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2060 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2061 Asm->EmitSLEB128(DV.getAddrElement(1));
2062 } else {
2063 // If first address element is OpPlus then emit
2064 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2065 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2066 Asm->EmitDwarfRegOp(Loc);
2067 i = 2;
2068 }
2069 } else {
2070 Asm->EmitDwarfRegOp(Entry.Loc);
2071 }
2072
2073 // Emit remaining complex address elements.
2074 for (; i < N; ++i) {
2075 uint64_t Element = DV.getAddrElement(i);
2076 if (Element == DIBuilder::OpPlus) {
2077 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2078 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher50120762012-05-08 18:56:00 +00002079 } else if (Element == DIBuilder::OpDeref) {
Eric Christophera80f2d12012-05-08 21:24:39 +00002080 if (!Entry.Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002081 Asm->EmitInt8(dwarf::DW_OP_deref);
2082 } else
2083 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002084 }
Devang Patelc26f5442011-04-28 02:22:40 +00002085 }
Devang Patelc26f5442011-04-28 02:22:40 +00002086 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002087 // else ... ignore constant fp. There is not any good way to
2088 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002089 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002090 }
2091 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002092}
2093
2094/// EmitDebugARanges - Emit visible names into a debug aranges section.
2095///
2096void DwarfDebug::EmitDebugARanges() {
2097 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002098 Asm->OutStreamer.SwitchSection(
2099 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002100}
2101
Devang Patel2c4ceb12009-11-21 02:48:08 +00002102/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002103///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002104void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002105 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002106 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002107 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002108 unsigned char Size = Asm->getTargetData().getPointerSize();
2109 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002110 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002111 I != E; ++I) {
2112 if (*I)
2113 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002114 else
Devang Pateleac9c072010-04-27 19:46:33 +00002115 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002116 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002117}
2118
Devang Patel2c4ceb12009-11-21 02:48:08 +00002119/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002120///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002121void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002122 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002123 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002124 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002125 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002126 }
2127}
2128
Devang Patel2c4ceb12009-11-21 02:48:08 +00002129/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002130/// Section Header:
2131/// 1. length of section
2132/// 2. Dwarf version number
2133/// 3. address size.
2134///
2135/// Entries (one "entry" for each function that was inlined):
2136///
2137/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2138/// otherwise offset into __debug_str for regular function name.
2139/// 2. offset into __debug_str section for regular function name.
2140/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2141/// instances for the function.
2142///
2143/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2144/// inlined instance; the die_offset points to the inlined_subroutine die in the
2145/// __debug_info section, and the low_pc is the starting address for the
2146/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002147void DwarfDebug::emitDebugInlineInfo() {
Eric Christopherb83e2bb2012-03-02 02:11:47 +00002148 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002149 return;
2150
Devang Patel163a9f72010-05-10 22:49:55 +00002151 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002152 return;
2153
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002154 Asm->OutStreamer.SwitchSection(
2155 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002156
Chris Lattner233f52b2010-03-09 23:52:58 +00002157 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002158 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2159 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002160
Chris Lattnerc0215722010-04-04 19:25:43 +00002161 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002162
Chris Lattner233f52b2010-03-09 23:52:58 +00002163 Asm->OutStreamer.AddComment("Dwarf Version");
2164 Asm->EmitInt16(dwarf::DWARF_VERSION);
2165 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002166 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002167
Devang Patele9f8f5e2010-05-07 20:54:48 +00002168 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002169 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002170
Devang Patele9f8f5e2010-05-07 20:54:48 +00002171 const MDNode *Node = *I;
2172 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002173 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002174 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002175 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002176 StringRef LName = SP.getLinkageName();
2177 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002178
Chris Lattner233f52b2010-03-09 23:52:58 +00002179 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher50e26612012-03-02 01:57:52 +00002180 if (LName.empty())
2181 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2182 else
Chris Lattner6189ed12010-04-04 23:25:33 +00002183 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2184 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002185
Chris Lattner233f52b2010-03-09 23:52:58 +00002186 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002187 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002188 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002189
Devang Patel53bb5c92009-11-10 23:06:00 +00002190 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002191 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002192 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002193 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002194
Chris Lattner3f53c832010-04-04 18:52:31 +00002195 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002196 Asm->OutStreamer.EmitSymbolValue(LI->first,
2197 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002198 }
2199 }
2200
Chris Lattnerc0215722010-04-04 19:25:43 +00002201 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002202}