blob: 1a8db3cf19de63727b199408649731241324dde7 [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
Bill Wendling5f017e82010-04-07 09:28:04 +000061namespace {
62 const char *DWARFGroupName = "DWARF Emission";
63 const char *DbgTimerName = "DWARF Debug Writer";
64} // end anonymous namespace
65
Bill Wendling0310d762009-05-15 09:23:25 +000066//===----------------------------------------------------------------------===//
67
68/// Configuration values for initial hash set sizes (log2).
69///
Bill Wendling0310d762009-05-15 09:23:25 +000070static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000071
72namespace llvm {
73
Nick Lewycky3bbb6f72011-07-29 03:49:23 +000074DIType DbgVariable::getType() const {
Devang Patel3cbee302011-04-12 22:53:02 +000075 DIType Ty = Var.getType();
76 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
77 // addresses instead.
78 if (Var.isBlockByrefVariable()) {
79 /* Byref variables, in Blocks, are declared by the programmer as
80 "SomeType VarName;", but the compiler creates a
81 __Block_byref_x_VarName struct, and gives the variable VarName
82 either the struct, or a pointer to the struct, as its type. This
83 is necessary for various behind-the-scenes things the compiler
84 needs to do with by-reference variables in blocks.
85
86 However, as far as the original *programmer* is concerned, the
87 variable should still have type 'SomeType', as originally declared.
88
89 The following function dives into the __Block_byref_x_VarName
90 struct to find the original type of the variable. This will be
91 passed back to the code generating the type for the Debug
92 Information Entry for the variable 'VarName'. 'VarName' will then
93 have the original type 'SomeType' in its debug information.
94
95 The original type 'SomeType' will be the type of the field named
96 'VarName' inside the __Block_byref_x_VarName struct.
97
98 NOTE: In order for this to not completely fail on the debugger
99 side, the Debug Information Entry for the variable VarName needs to
100 have a DW_AT_location that tells the debugger how to unwind through
101 the pointers and __Block_byref_x_VarName struct to find the actual
102 value of the variable. The function addBlockByrefType does this. */
103 DIType subType = Ty;
104 unsigned tag = Ty.getTag();
105
106 if (tag == dwarf::DW_TAG_pointer_type) {
107 DIDerivedType DTy = DIDerivedType(Ty);
108 subType = DTy.getTypeDerivedFrom();
109 }
110
111 DICompositeType blockStruct = DICompositeType(subType);
112 DIArray Elements = blockStruct.getTypeArray();
113
114 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
115 DIDescriptor Element = Elements.getElement(i);
116 DIDerivedType DT = DIDerivedType(Element);
117 if (getName() == DT.getName())
118 return (DT.getTypeDerivedFrom());
Devang Patel8bd11de2010-08-09 21:01:39 +0000119 }
Devang Patel8bd11de2010-08-09 21:01:39 +0000120 }
Devang Patel3cbee302011-04-12 22:53:02 +0000121 return Ty;
122}
Bill Wendling0310d762009-05-15 09:23:25 +0000123
Chris Lattnerea761862010-04-05 04:09:20 +0000124} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000125
Chris Lattner49cd6642010-04-05 05:11:15 +0000126DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000127 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000128 AbbreviationsSet(InitAbbreviationsSetSize),
Benjamin Kramerf33a79c2012-06-09 10:34:15 +0000129 SourceIdMap(DIEValueAllocator), StringPool(DIEValueAllocator),
Eric Christopherc23b9332012-07-27 22:00:05 +0000130 PrevLabel(NULL),
131 HasNonTextSection(false) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000132 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000133
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000134 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000135 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000136 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000137 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopher60777d82012-04-02 17:58:52 +0000138
139 // Turn on accelerator tables for Darwin.
140 if (Triple(M->getTargetTriple()).isOSDarwin())
141 DwarfAccelTables = true;
142
Dan Gohman03c3dc72010-06-18 15:56:31 +0000143 {
144 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
145 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000146 }
Bill Wendling0310d762009-05-15 09:23:25 +0000147}
148DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000149}
150
Eric Christopherd8a87522011-11-07 09:18:38 +0000151/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
152/// temporary label to it if SymbolStem is specified.
153static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
154 const char *SymbolStem = 0) {
155 Asm->OutStreamer.SwitchSection(Section);
156 if (!SymbolStem) return 0;
157
158 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
159 Asm->OutStreamer.EmitLabel(TmpSym);
160 return TmpSym;
161}
162
Nick Lewycky390c40d2011-10-27 06:44:11 +0000163MCSymbol *DwarfDebug::getStringPool() {
164 return Asm->GetTempSymbol("section_str");
165}
166
Chris Lattnerbc733f52010-03-13 02:17:42 +0000167MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
168 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
169 if (Entry.first) return Entry.first;
170
171 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000172 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000173}
174
Devang Patel2c4ceb12009-11-21 02:48:08 +0000175/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000176///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000177void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000178 // Profile the node so that we can make it unique.
179 FoldingSetNodeID ID;
180 Abbrev.Profile(ID);
181
182 // Check the set for priors.
183 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
184
185 // If it's newly added.
186 if (InSet == &Abbrev) {
187 // Add to abbreviation list.
188 Abbreviations.push_back(&Abbrev);
189
190 // Assign the vector position + 1 as its number.
191 Abbrev.setNumber(Abbreviations.size());
192 } else {
193 // Assign existing abbreviation number.
194 Abbrev.setNumber(InSet->getNumber());
195 }
196}
197
Jim Grosbach1e20b962010-07-21 21:21:52 +0000198/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000199/// printer to not emit usual symbol prefix before the symbol name is used then
200/// return linkage name after skipping this special LLVM prefix.
201static StringRef getRealLinkageName(StringRef LinkageName) {
202 char One = '\1';
203 if (LinkageName.startswith(StringRef(&One, 1)))
204 return LinkageName.substr(1);
205 return LinkageName;
206}
207
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000208static bool isObjCClass(StringRef Name) {
209 return Name.startswith("+") || Name.startswith("-");
210}
211
212static bool hasObjCCategory(StringRef Name) {
213 if (!isObjCClass(Name)) return false;
214
215 size_t pos = Name.find(')');
216 if (pos != std::string::npos) {
217 if (Name[pos+1] != ' ') return false;
218 return true;
219 }
220 return false;
221}
222
223static void getObjCClassCategory(StringRef In, StringRef &Class,
224 StringRef &Category) {
225 if (!hasObjCCategory(In)) {
226 Class = In.slice(In.find('[') + 1, In.find(' '));
227 Category = "";
228 return;
229 }
230
231 Class = In.slice(In.find('[') + 1, In.find('('));
232 Category = In.slice(In.find('[') + 1, In.find(' '));
233 return;
234}
235
236static StringRef getObjCMethodName(StringRef In) {
237 return In.slice(In.find(' ') + 1, In.find(']'));
238}
239
240// Add the various names to the Dwarf accelerator table names.
241static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
242 DIE* Die) {
243 if (!SP.isDefinition()) return;
244
245 TheCU->addAccelName(SP.getName(), Die);
246
247 // If the linkage name is different than the name, go ahead and output
248 // that as well into the name table.
249 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
250 TheCU->addAccelName(SP.getLinkageName(), Die);
251
252 // If this is an Objective-C selector name add it to the ObjC accelerator
253 // too.
254 if (isObjCClass(SP.getName())) {
255 StringRef Class, Category;
256 getObjCClassCategory(SP.getName(), Class, Category);
257 TheCU->addAccelObjC(Class, Die);
258 if (Category != "")
259 TheCU->addAccelObjC(Category, Die);
260 // Also add the base method name to the name table.
261 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
262 }
263}
264
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000265/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000266/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
267/// If there are global variables in this scope then create and insert
268/// DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000269DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
270 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000271 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000272
Chris Lattnerd38fee82010-04-05 00:13:49 +0000273 assert(SPDie && "Unable to find subprogram DIE!");
274 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000275
Devang Patel5e06bb82011-04-22 23:10:17 +0000276 DISubprogram SPDecl = SP.getFunctionDeclaration();
Rafael Espindolab0527282011-11-04 19:00:29 +0000277 if (!SPDecl.isSubprogram()) {
Devang Patel5e06bb82011-04-22 23:10:17 +0000278 // There is not any need to generate specification DIE for a function
279 // defined at compile unit level. If a function is defined inside another
280 // function then gdb prefers the definition at top level and but does not
281 // expect specification DIE in parent function. So avoid creating
282 // specification DIE for a function defined inside a function.
283 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
284 !SP.getContext().isFile() &&
285 !isSubprogramContext(SP.getContext())) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000286 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel5e06bb82011-04-22 23:10:17 +0000287
288 // Add arguments.
289 DICompositeType SPTy = SP.getType();
290 DIArray Args = SPTy.getTypeArray();
291 unsigned SPTag = SPTy.getTag();
292 if (SPTag == dwarf::DW_TAG_subroutine_type)
293 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
294 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
295 DIType ATy = DIType(DIType(Args.getElement(i)));
296 SPCU->addType(Arg, ATy);
297 if (ATy.isArtificial())
298 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
299 SPDie->addChild(Arg);
300 }
301 DIE *SPDeclDie = SPDie;
302 SPDie = new DIE(dwarf::DW_TAG_subprogram);
303 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
304 SPDeclDie);
305 SPCU->addDie(SPDie);
306 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000307 }
Devang Patel8aa61472010-07-07 22:20:57 +0000308 // Pick up abstract subprogram DIE.
309 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
310 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000311 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
312 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000313 SPCU->addDie(SPDie);
314 }
315
Devang Patel3cbee302011-04-12 22:53:02 +0000316 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
317 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
318 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
319 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000320 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
321 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000322 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000323
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000324 // Add name to the name table, we do this here because we're guaranteed
325 // to have concrete versions of our DW_TAG_subprogram nodes.
326 addSubprogramNames(SPCU, SP, SPDie);
327
Chris Lattnerd38fee82010-04-05 00:13:49 +0000328 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000329}
330
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000331/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000332/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000333DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
334 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000335 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
336 if (Scope->isAbstractScope())
337 return ScopeDIE;
338
Devang Patelbf47fdb2011-08-10 20:55:27 +0000339 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000340 if (Ranges.empty())
341 return 0;
342
Devang Patelbf47fdb2011-08-10 20:55:27 +0000343 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000344 if (Ranges.size() > 1) {
345 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000346 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000347 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000348 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000349 DebugRangeSymbols.size()
350 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000351 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000352 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000353 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
354 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000355 }
356 DebugRangeSymbols.push_back(NULL);
357 DebugRangeSymbols.push_back(NULL);
358 return ScopeDIE;
359 }
360
Devang Patelc3f5f782010-05-25 23:40:22 +0000361 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
362 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000363
Devang Patelc3f5f782010-05-25 23:40:22 +0000364 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000365
Chris Lattnerb7db7332010-03-09 01:58:53 +0000366 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
367 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000368
Devang Patel3cbee302011-04-12 22:53:02 +0000369 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
370 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000371
372 return ScopeDIE;
373}
374
Devang Patel2c4ceb12009-11-21 02:48:08 +0000375/// constructInlinedScopeDIE - This scope represents inlined body of
376/// a function. Construct DIE to represent this concrete inlined copy
377/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000378DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
379 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000380 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000381 assert(Ranges.empty() == false &&
382 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000383
Devang Patel26a92002011-07-27 00:34:13 +0000384 if (!Scope->getScopeNode())
385 return NULL;
386 DIScope DS(Scope->getScopeNode());
387 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000388 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
389 if (!OriginDIE) {
390 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
391 return NULL;
392 }
393
Devang Patelbf47fdb2011-08-10 20:55:27 +0000394 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000395 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
396 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000397
Devang Patel0afbf232010-07-08 22:39:20 +0000398 if (StartLabel == 0 || EndLabel == 0) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000399 llvm_unreachable("Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000400 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000401 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000402 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000403 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000404 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000405
Devang Pateld96efb82011-05-05 17:54:26 +0000406 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000407 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
408 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000409
Devang Patel26a92002011-07-27 00:34:13 +0000410 if (Ranges.size() > 1) {
411 // .debug_range section has not been laid out yet. Emit offset in
412 // .debug_range as a uint, size 4, for now. emitDIE will handle
413 // DW_AT_ranges appropriately.
414 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000415 DebugRangeSymbols.size()
416 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000417 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000418 RE = Ranges.end(); RI != RE; ++RI) {
419 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
420 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
421 }
422 DebugRangeSymbols.push_back(NULL);
423 DebugRangeSymbols.push_back(NULL);
424 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000425 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
426 StartLabel);
427 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
428 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000429 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000430
431 InlinedSubprogramDIEs.insert(OriginDIE);
432
433 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000434 //.debug_inlined section specification does not clearly state how
435 // to emit inlined scope that is split into multiple instruction ranges.
436 // For now, use first instruction range and emit low_pc/high_pc pair and
437 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000438 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000439 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000440
441 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000442 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000443 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000444 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000445 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000446
Devang Patel53bb5c92009-11-10 23:06:00 +0000447 DILocation DL(Scope->getInlinedAt());
Eric Christopher08212002012-03-26 21:38:38 +0000448 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
449 GetOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
Devang Patel3cbee302011-04-12 22:53:02 +0000450 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000451
Eric Christopher309bedd2011-12-04 06:02:38 +0000452 // Add name to the name table, we do this here because we're guaranteed
453 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
454 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
455
Devang Patel53bb5c92009-11-10 23:06:00 +0000456 return ScopeDIE;
457}
458
Devang Patel2c4ceb12009-11-21 02:48:08 +0000459/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000460DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000461 if (!Scope || !Scope->getScopeNode())
462 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000463
Nick Lewycky746cb672011-10-26 22:55:33 +0000464 SmallVector<DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000465
466 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000467 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000468 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
469 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000470 if (DIE *Arg =
471 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel0478c152011-03-01 22:58:55 +0000472 Children.push_back(Arg);
473
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000474 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000475 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000476 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000477 if (DIE *Variable =
478 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000479 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000480 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000481 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000482 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000483 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000484 DIScope DS(Scope->getScopeNode());
485 DIE *ScopeDIE = NULL;
486 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000487 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000488 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000489 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000490 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000491 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000492 // Note down abstract DIE.
493 if (ScopeDIE)
494 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
495 }
Devang Patel3c91b052010-03-08 20:52:55 +0000496 else
Devang Pateld3024342011-08-15 22:24:32 +0000497 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000498 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000499 else {
500 // There is no need to emit empty lexical block DIE.
501 if (Children.empty())
502 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000503 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000504 }
505
Devang Patelaead63c2010-03-29 22:59:58 +0000506 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000507
Devang Patel5bc9fec2011-02-19 01:31:27 +0000508 // Add children
509 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
510 E = Children.end(); I != E; ++I)
511 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000512
Jim Grosbach1e20b962010-07-21 21:21:52 +0000513 if (DS.isSubprogram())
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000514 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000515
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000516 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000517}
518
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000519/// GetOrCreateSourceID - Look up the source id with the given directory and
520/// source file names. If none currently exists, create a new id and insert it
521/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
522/// maps as well.
Devang Patel23670e52011-03-24 20:30:50 +0000523unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
524 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000525 // If FE did not provide a file name, then assume stdin.
526 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000527 return GetOrCreateSourceID("<stdin>", StringRef());
528
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000529 // TODO: this might not belong here. See if we can factor this better.
530 if (DirName == CompilationDir)
531 DirName = "";
532
Nick Lewycky44d798d2011-10-17 23:05:28 +0000533 unsigned SrcId = SourceIdMap.size()+1;
Devang Patel1905a182010-09-16 20:57:49 +0000534
Benjamin Kramer74612c22012-03-11 14:56:26 +0000535 // We look up the file/dir pair by concatenating them with a zero byte.
536 SmallString<128> NamePair;
537 NamePair += DirName;
538 NamePair += '\0'; // Zero bytes are not allowed in paths.
539 NamePair += FileName;
540
541 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
542 if (Ent.getValue() != SrcId)
543 return Ent.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000544
Rafael Espindola5c055632010-11-18 02:04:25 +0000545 // Print out a .file directive to specify files for .loc directives.
Benjamin Kramer74612c22012-03-11 14:56:26 +0000546 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000547
548 return SrcId;
549}
550
Jim Grosbach1e20b962010-07-21 21:21:52 +0000551/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000552/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000553CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000554 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000555 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000556 CompilationDir = DIUnit.getDirectory();
557 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000558
559 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher438b0922012-02-22 08:46:13 +0000560 CompileUnit *NewCU = new CompileUnit(ID, DIUnit.getLanguage(), Die, Asm, this);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000561 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000562 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
563 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000564 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Devang Patel4a602ca2010-03-22 23:11:36 +0000565 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000566 // compile unit in debug_line section.
Rafael Espindola2241e512012-06-22 13:24:07 +0000567 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Rafael Espindola597a7662011-05-04 17:44:06 +0000568 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000569 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000570 else
Devang Patel3cbee302011-04-12 22:53:02 +0000571 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000572
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000573 if (!CompilationDir.empty())
574 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000575 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000576 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000577
Devang Patel65dbc902009-11-25 17:36:49 +0000578 StringRef Flags = DIUnit.getFlags();
579 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000580 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000581
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000582 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000583 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000584 dwarf::DW_FORM_data1, RVer);
585
Devang Patel163a9f72010-05-10 22:49:55 +0000586 if (!FirstCU)
587 FirstCU = NewCU;
588 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000589 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000590}
591
Devang Patel163a9f72010-05-10 22:49:55 +0000592/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000593void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
594 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000595 CompileUnit *&CURef = SPMap[N];
596 if (CURef)
597 return;
598 CURef = TheCU;
599
Devang Patele4b27562009-08-28 23:24:31 +0000600 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000601 if (!SP.isDefinition())
602 // This is a method declaration which will be handled while constructing
603 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000604 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000605
Devang Pateldbc64af2011-08-15 17:24:54 +0000606 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000607
608 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000609 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000610
611 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000612 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000613
Devang Patel13e16b62009-06-26 01:49:18 +0000614 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000615}
616
Devang Patel02e603f2011-08-15 23:47:24 +0000617/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
618/// as llvm.dbg.enum and llvm.dbg.ty
619void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000620 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
621 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
622 const MDNode *N = NMD->getOperand(i);
623 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
624 constructSubprogramDIE(CU, N);
625 }
626
627 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
628 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
629 const MDNode *N = NMD->getOperand(i);
630 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000631 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000632 }
633
Devang Patel02e603f2011-08-15 23:47:24 +0000634 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
635 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
636 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000637 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
638 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000639 }
640
641 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
642 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
643 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000644 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
645 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000646 }
647}
648
649/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
650/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
651bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
652 DebugInfoFinder DbgFinder;
653 DbgFinder.processModule(*M);
654
655 bool HasDebugInfo = false;
656 // Scan all the compile-units to see if there are any marked as the main
657 // unit. If not, we do not generate debug info.
658 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
659 E = DbgFinder.compile_unit_end(); I != E; ++I) {
660 if (DICompileUnit(*I).isMain()) {
661 HasDebugInfo = true;
662 break;
663 }
664 }
665 if (!HasDebugInfo) return false;
666
667 // Create all the compile unit DIEs.
668 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
669 E = DbgFinder.compile_unit_end(); I != E; ++I)
670 constructCompileUnit(*I);
671
672 // Create DIEs for each global variable.
673 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
674 E = DbgFinder.global_variable_end(); I != E; ++I) {
675 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000676 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000677 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000678 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000679
Devang Patel02e603f2011-08-15 23:47:24 +0000680 // Create DIEs for each subprogram.
681 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
682 E = DbgFinder.subprogram_end(); I != E; ++I) {
683 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000684 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
685 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000686 }
687
688 return HasDebugInfo;
689}
690
Devang Patel2c4ceb12009-11-21 02:48:08 +0000691/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000692/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000693/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000694void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000695 if (DisableDebugInfoPrinting)
696 return;
697
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000698 // If module has named metadata anchors then use them, otherwise scan the
699 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000700 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
701 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000702 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
703 DICompileUnit CUNode(CU_Nodes->getOperand(i));
704 CompileUnit *CU = constructCompileUnit(CUNode);
705 DIArray GVs = CUNode.getGlobalVariables();
706 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000707 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000708 DIArray SPs = CUNode.getSubprograms();
709 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
710 constructSubprogramDIE(CU, SPs.getElement(i));
711 DIArray EnumTypes = CUNode.getEnumTypes();
712 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
713 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
714 DIArray RetainedTypes = CUNode.getRetainedTypes();
715 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
716 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
717 }
Devang Patel02e603f2011-08-15 23:47:24 +0000718 } else if (!collectLegacyDebugInfo(M))
719 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000720
Devang Patel02e603f2011-08-15 23:47:24 +0000721 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000722
Chris Lattnerd850ac72010-04-05 02:19:28 +0000723 // Tell MMI that we have debug info.
724 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000725
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000726 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000727 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000728
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000729 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000730 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000731}
732
Devang Patel2c4ceb12009-11-21 02:48:08 +0000733/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000734///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000735void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000736 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000737 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000738 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000739
Devang Patel94c7ddb2011-08-16 22:09:43 +0000740 // Collect info for variables that were optimized out.
741 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
742 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
743 DICompileUnit TheCU(CU_Nodes->getOperand(i));
744 DIArray Subprograms = TheCU.getSubprograms();
745 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
746 DISubprogram SP(Subprograms.getElement(i));
747 if (ProcessedSPNodes.count(SP) != 0) continue;
748 if (!SP.Verify()) continue;
749 if (!SP.isDefinition()) continue;
Devang Patel93d39be2011-08-19 23:28:12 +0000750 DIArray Variables = SP.getVariables();
751 if (Variables.getNumElements() == 0) continue;
752
Devang Patel94c7ddb2011-08-16 22:09:43 +0000753 LexicalScope *Scope =
754 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
755 DeadFnScopeMap[SP] = Scope;
756
757 // Construct subprogram DIE and add variables DIEs.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000758 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky746cb672011-10-26 22:55:33 +0000759 assert(SPCU && "Unable to find Compile Unit!");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000760 constructSubprogramDIE(SPCU, SP);
761 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel93d39be2011-08-19 23:28:12 +0000762 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
763 DIVariable DV(Variables.getElement(vi));
764 if (!DV.Verify()) continue;
765 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000766 if (DIE *VariableDIE =
Devang Patel93d39be2011-08-19 23:28:12 +0000767 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Patel94c7ddb2011-08-16 22:09:43 +0000768 ScopeDIE->addChild(VariableDIE);
769 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000770 }
771 }
772 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000773
Devang Patel53bb5c92009-11-10 23:06:00 +0000774 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
775 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
776 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
777 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000778 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000779 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000780 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
781 AE = AbstractSPDies.end(); AI != AE; ++AI) {
782 DIE *ISP = AI->second;
783 if (InlinedSubprogramDIEs.count(ISP))
784 continue;
785 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
786 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000787
Devang Pateldbc64af2011-08-15 17:24:54 +0000788 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
789 CUE = CUMap.end(); CUI != CUE; ++CUI) {
Eric Christopherc23b9332012-07-27 22:00:05 +0000790 // Emit DW_AT_containing_type attribute to connect types with their
791 // vtable holding type.
Devang Pateldbc64af2011-08-15 17:24:54 +0000792 CompileUnit *TheCU = CUI->second;
793 TheCU->constructContainingTypeDIEs();
Eric Christopherc23b9332012-07-27 22:00:05 +0000794
795 // Emit low_pc and high_pc for CU.
796 DIE *Die = TheCU->getCUDie();
797 if (HasNonTextSection) {
798 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
799 // into an entity.
800 TheCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
801 } else {
802 TheCU->addLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
803 Asm->GetTempSymbol("text_begin"));
804 TheCU->addLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
805 Asm->GetTempSymbol("text_end"));
806 }
Devang Patel5d11eb02009-12-03 19:11:07 +0000807 }
808
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000809 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000810 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000811 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000812 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000813 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000814
815 // End text sections.
816 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000817 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000818 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000819 }
820
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000821 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000822 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000823
824 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000825 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000826
827 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000828 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000829
Eric Christopher09ac3d82011-11-07 09:24:32 +0000830 // Emit info into a dwarf accelerator table sections.
831 if (DwarfAccelTables) {
832 emitAccelNames();
833 emitAccelObjC();
834 emitAccelNamespaces();
835 emitAccelTypes();
836 }
837
Devang Patel193f7202009-11-24 01:14:22 +0000838 // Emit info into a debug pubtypes section.
839 emitDebugPubTypes();
840
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000841 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000842 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000843
844 // Emit info into a debug aranges section.
845 EmitDebugARanges();
846
847 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000848 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000849
850 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000851 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000852
853 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000854 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000855
Eric Christopher7550f7d2012-03-02 00:30:24 +0000856 // Emit info into a debug str section.
857 emitDebugStr();
858
Devang Patele9a1cca2010-08-02 17:32:15 +0000859 // clean up.
860 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000861 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000862 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
863 E = CUMap.end(); I != E; ++I)
864 delete I->second;
865 FirstCU = NULL; // Reset for the next Module, if any.
Eric Christopherc23b9332012-07-27 22:00:05 +0000866 HasNonTextSection = false;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000867}
868
Devang Patel53bb5c92009-11-10 23:06:00 +0000869/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000870DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000871 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000872 LLVMContext &Ctx = DV->getContext();
873 // More then one inlined variable corresponds to one abstract variable.
874 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000875 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000876 if (AbsDbgVariable)
877 return AbsDbgVariable;
878
Devang Patelbf47fdb2011-08-10 20:55:27 +0000879 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000880 if (!Scope)
881 return NULL;
882
Devang Patel5a1a67c2011-08-15 19:01:20 +0000883 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000884 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000885 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000886 return AbsDbgVariable;
887}
888
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000889/// addCurrentFnArgument - If Var is a current function argument then add
890/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000891bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000892 DbgVariable *Var, LexicalScope *Scope) {
893 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000894 return false;
895 DIVariable DV = Var->getVariable();
896 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
897 return false;
898 unsigned ArgNo = DV.getArgNumber();
899 if (ArgNo == 0)
900 return false;
901
Devang Patelcb3a6572011-03-03 20:02:02 +0000902 size_t Size = CurrentFnArguments.size();
903 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000904 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000905 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000906 // arguments does the function have at source level.
907 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000908 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000909 CurrentFnArguments[ArgNo - 1] = Var;
910 return true;
911}
912
Devang Patelee432862010-05-20 19:57:06 +0000913/// collectVariableInfoFromMMITable - Collect variable information from
914/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000915void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000916DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000917 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000918 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
919 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
920 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000921 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000922 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000923 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000924 DIVariable DV(Var);
925 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000926
Devang Patelbf47fdb2011-08-10 20:55:27 +0000927 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000928
Devang Patelfb0ee432009-11-10 23:20:04 +0000929 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000930 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000931 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000932
Devang Patel26c1e562010-05-20 16:36:41 +0000933 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000934 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000935 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000936 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000937 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000938 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000939 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000940 }
Devang Patelee432862010-05-20 19:57:06 +0000941}
Devang Patel90a48ad2010-03-15 18:33:46 +0000942
Jim Grosbach1e20b962010-07-21 21:21:52 +0000943/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000944/// DBG_VALUE instruction, is in a defined reg.
945static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000946 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000947 return MI->getNumOperands() == 3 &&
948 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
949 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000950}
951
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000952/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000953/// at MI.
954static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
955 const MCSymbol *FLabel,
956 const MCSymbol *SLabel,
957 const MachineInstr *MI) {
958 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
959
960 if (MI->getNumOperands() != 3) {
961 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
962 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
963 }
964 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
965 MachineLocation MLoc;
966 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
967 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
968 }
969 if (MI->getOperand(0).isImm())
970 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
971 if (MI->getOperand(0).isFPImm())
972 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
973 if (MI->getOperand(0).isCImm())
974 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
975
Craig Topper5e25ee82012-02-05 08:31:47 +0000976 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +0000977}
978
Devang Patelbf47fdb2011-08-10 20:55:27 +0000979/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000980void
Devang Patel78e127d2010-06-25 22:07:34 +0000981DwarfDebug::collectVariableInfo(const MachineFunction *MF,
982 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000983
Devang Patelee432862010-05-20 19:57:06 +0000984 /// collection info from MMI table.
985 collectVariableInfoFromMMITable(MF, Processed);
986
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000987 for (SmallVectorImpl<const MDNode*>::const_iterator
988 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
989 ++UVI) {
990 const MDNode *Var = *UVI;
991 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +0000992 continue;
993
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000994 // History contains relevant DBG_VALUE instructions for Var and instructions
995 // clobbering it.
996 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
997 if (History.empty())
998 continue;
999 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001000
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001001 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001002 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001003 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1004 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001005 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001006 else {
1007 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001008 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001009 else {
1010 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001011 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001012 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001013 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001014 }
1015 }
Devang Patelee432862010-05-20 19:57:06 +00001016 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001017 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001018 continue;
1019
1020 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001021 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001022 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1023 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +00001024 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001025 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001026 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001027 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001028
1029 // Simple ranges that are fully coalesced.
1030 if (History.size() <= 1 || (History.size() == 2 &&
1031 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001032 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001033 continue;
1034 }
1035
1036 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001037 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001038
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001039 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1040 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1041 const MachineInstr *Begin = *HI;
1042 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001043
Devang Patel4ada1d72011-06-01 23:00:17 +00001044 // Check if DBG_VALUE is truncating a range.
1045 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1046 && !Begin->getOperand(0).getReg())
1047 continue;
1048
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001049 // Compute the range for a register location.
1050 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1051 const MCSymbol *SLabel = 0;
1052
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001053 if (HI + 1 == HE)
1054 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001055 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001056 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001057 else {
1058 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001059 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1060 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001061 if (End->isDebugValue())
1062 SLabel = getLabelBeforeInsn(End);
1063 else {
1064 // End is a normal instruction clobbering the range.
1065 SLabel = getLabelAfterInsn(End);
1066 assert(SLabel && "Forgot label after clobber instruction");
1067 ++HI;
1068 }
1069 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001070
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001071 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001072 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1073 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001074 }
1075 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001076 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001077
1078 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001079 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1080 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1081 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1082 DIVariable DV(Variables.getElement(i));
1083 if (!DV || !DV.Verify() || !Processed.insert(DV))
1084 continue;
1085 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1086 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001087 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001088}
Devang Patel98e1cac2010-05-14 21:01:35 +00001089
Devang Patelc3f5f782010-05-25 23:40:22 +00001090/// getLabelBeforeInsn - Return Label preceding the instruction.
1091const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001092 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1093 assert(Label && "Didn't insert label before instruction");
1094 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001095}
1096
1097/// getLabelAfterInsn - Return Label immediately following the instruction.
1098const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001099 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001100}
1101
Devang Patelcbbe2872010-10-26 17:49:02 +00001102/// beginInstruction - Process beginning of an instruction.
1103void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001104 // Check if source location changes, but ignore DBG_VALUE locations.
1105 if (!MI->isDebugValue()) {
1106 DebugLoc DL = MI->getDebugLoc();
1107 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001108 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001109 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001110 if (DL == PrologEndLoc) {
1111 Flags |= DWARF2_FLAG_PROLOGUE_END;
1112 PrologEndLoc = DebugLoc();
1113 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001114 if (PrologEndLoc.isUnknown())
1115 Flags |= DWARF2_FLAG_IS_STMT;
1116
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001117 if (!DL.isUnknown()) {
1118 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001119 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001120 } else
Devang Patel4243e672011-05-11 19:22:19 +00001121 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001122 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001123 }
Devang Patelaead63c2010-03-29 22:59:58 +00001124
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001125 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001126 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1127 LabelsBeforeInsn.find(MI);
1128
1129 // No label needed.
1130 if (I == LabelsBeforeInsn.end())
1131 return;
1132
1133 // Label already assigned.
1134 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001135 return;
Devang Patel553881b2010-03-29 17:20:31 +00001136
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001137 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001138 PrevLabel = MMI->getContext().CreateTempSymbol();
1139 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001140 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001141 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001142}
1143
Devang Patelcbbe2872010-10-26 17:49:02 +00001144/// endInstruction - Process end of an instruction.
1145void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001146 // Don't create a new label after DBG_VALUE instructions.
1147 // They don't generate code.
1148 if (!MI->isDebugValue())
1149 PrevLabel = 0;
1150
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001151 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1152 LabelsAfterInsn.find(MI);
1153
1154 // No label needed.
1155 if (I == LabelsAfterInsn.end())
1156 return;
1157
1158 // Label already assigned.
1159 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001160 return;
1161
1162 // We need a label after this instruction.
1163 if (!PrevLabel) {
1164 PrevLabel = MMI->getContext().CreateTempSymbol();
1165 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001166 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001167 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001168}
1169
Jim Grosbach1e20b962010-07-21 21:21:52 +00001170/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001171/// Each LexicalScope has first instruction and last instruction to mark
1172/// beginning and end of a scope respectively. Create an inverse map that list
1173/// scopes starts (and ends) with an instruction. One instruction may start (or
1174/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001175void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001176 SmallVector<LexicalScope *, 4> WorkList;
1177 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001178 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001179 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001180
Devang Patelbf47fdb2011-08-10 20:55:27 +00001181 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001182 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001183 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001184 SE = Children.end(); SI != SE; ++SI)
1185 WorkList.push_back(*SI);
1186
Devang Patel53bb5c92009-11-10 23:06:00 +00001187 if (S->isAbstractScope())
1188 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001189
Devang Patelbf47fdb2011-08-10 20:55:27 +00001190 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001191 if (Ranges.empty())
1192 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001193 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001194 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001195 assert(RI->first && "InsnRange does not have first instruction!");
1196 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001197 requestLabelBeforeInsn(RI->first);
1198 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001199 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001200 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001201}
1202
Devang Patela3f48672011-05-09 22:14:49 +00001203/// getScopeNode - Get MDNode for DebugLoc's scope.
1204static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1205 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1206 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1207 return DL.getScope(Ctx);
1208}
1209
Devang Patel4243e672011-05-11 19:22:19 +00001210/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
Eric Christopher6126a1e2012-04-03 00:43:49 +00001211/// line number info for the function.
Devang Patel4243e672011-05-11 19:22:19 +00001212static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1213 const MDNode *Scope = getScopeNode(DL, Ctx);
1214 DISubprogram SP = getDISubprogram(Scope);
Eric Christopher6126a1e2012-04-03 00:43:49 +00001215 if (SP.Verify()) {
1216 // Check for number of operands since the compatibility is
1217 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001218 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-04-03 00:43:49 +00001219 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1220 else
1221 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1222 }
1223
Devang Patel4243e672011-05-11 19:22:19 +00001224 return DebugLoc();
1225}
1226
Devang Patel2c4ceb12009-11-21 02:48:08 +00001227/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001228/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001229void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001230 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001231 LScopes.initialize(*MF);
1232 if (LScopes.empty()) return;
1233 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001234
Eric Christopherc23b9332012-07-27 22:00:05 +00001235 if (!Asm->getCurrentSection()->getKind().isText())
1236 HasNonTextSection = true;
1237
Devang Pateleac9c072010-04-27 19:46:33 +00001238 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1239 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001240 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001241 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001242
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001243 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1244
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001245 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001246 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1247 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1248
Devang Patelb2b31a62010-05-26 19:37:24 +00001249 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001250 I != E; ++I) {
1251 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001252 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1253 II != IE; ++II) {
1254 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001255
Devang Patelb2b31a62010-05-26 19:37:24 +00001256 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001257 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001258
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001259 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001260 const MDNode *Var =
1261 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001262
1263 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001264 if (isDbgValueInDefinedReg(MI))
1265 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1266
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001267 // Check the history of this variable.
1268 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1269 if (History.empty()) {
1270 UserVariables.push_back(Var);
1271 // The first mention of a function argument gets the FunctionBeginSym
1272 // label, so arguments are visible when breaking at function entry.
1273 DIVariable DV(Var);
1274 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1275 DISubprogram(getDISubprogram(DV.getContext()))
1276 .describes(MF->getFunction()))
1277 LabelsBeforeInsn[MI] = FunctionBeginSym;
1278 } else {
1279 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1280 const MachineInstr *Prev = History.back();
1281 if (Prev->isDebugValue()) {
1282 // Coalesce identical entries at the end of History.
1283 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001284 Prev->isIdenticalTo(History[History.size() - 2])) {
1285 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1286 << "\t" << *Prev
1287 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001288 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001289 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001290
1291 // Terminate old register assignments that don't reach MI;
1292 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1293 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1294 isDbgValueInDefinedReg(Prev)) {
1295 // Previous register assignment needs to terminate at the end of
1296 // its basic block.
1297 MachineBasicBlock::const_iterator LastMI =
1298 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001299 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001300 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001301 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1302 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001303 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001304 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001305 else {
1306 // Terminate after LastMI.
1307 History.push_back(LastMI);
1308 }
1309 }
1310 }
1311 }
1312 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001313 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001314 // Not a DBG_VALUE instruction.
1315 if (!MI->isLabel())
1316 AtBlockEntry = false;
1317
Devang Patel4243e672011-05-11 19:22:19 +00001318 // First known non DBG_VALUE location marks beginning of function
1319 // body.
1320 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1321 PrologEndLoc = MI->getDebugLoc();
1322
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001323 // Check if the instruction clobbers any registers with debug vars.
1324 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1325 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1326 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1327 continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001328 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1329 AI.isValid(); ++AI) {
1330 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001331 const MDNode *Var = LiveUserVar[Reg];
1332 if (!Var)
1333 continue;
1334 // Reg is now clobbered.
1335 LiveUserVar[Reg] = 0;
1336
1337 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001338 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1339 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001340 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001341 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1342 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001343 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001344 const MachineInstr *Prev = History.back();
1345 // Sanity-check: Register assignments are terminated at the end of
1346 // their block.
1347 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1348 continue;
1349 // Is the variable still in Reg?
1350 if (!isDbgValueInDefinedReg(Prev) ||
1351 Prev->getOperand(0).getReg() != Reg)
1352 continue;
1353 // Var is clobbered. Make sure the next instruction gets a label.
1354 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001355 }
1356 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001357 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001358 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001359 }
1360
1361 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1362 I != E; ++I) {
1363 SmallVectorImpl<const MachineInstr*> &History = I->second;
1364 if (History.empty())
1365 continue;
1366
1367 // Make sure the final register assignments are terminated.
1368 const MachineInstr *Prev = History.back();
1369 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1370 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001371 MachineBasicBlock::const_iterator LastMI =
1372 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001373 if (LastMI == PrevMBB->end())
1374 // Drop DBG_VALUE for empty range.
1375 History.pop_back();
1376 else {
1377 // Terminate after LastMI.
1378 History.push_back(LastMI);
1379 }
1380 }
1381 // Request labels for the full history.
1382 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1383 const MachineInstr *MI = History[i];
1384 if (MI->isDebugValue())
1385 requestLabelBeforeInsn(MI);
1386 else
1387 requestLabelAfterInsn(MI);
1388 }
1389 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001390
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001391 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001392 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001393
1394 // Record beginning of function.
1395 if (!PrologEndLoc.isUnknown()) {
1396 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1397 MF->getFunction()->getContext());
1398 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1399 FnStartDL.getScope(MF->getFunction()->getContext()),
Eric Christopher5cf55e12012-07-12 23:30:25 +00001400 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0);
Devang Patel4243e672011-05-11 19:22:19 +00001401 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001402}
1403
Devang Patelbf47fdb2011-08-10 20:55:27 +00001404void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1405// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1406 ScopeVariables[LS].push_back(Var);
1407// Vars.push_back(Var);
1408}
1409
Devang Patel2c4ceb12009-11-21 02:48:08 +00001410/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001411///
Chris Lattnereec791a2010-01-26 23:18:02 +00001412void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001413 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001414
Devang Patelbf47fdb2011-08-10 20:55:27 +00001415 // Define end label for subprogram.
1416 FunctionEndSym = Asm->GetTempSymbol("func_end",
1417 Asm->getFunctionNumber());
1418 // Assumes in correct section after the entry point.
1419 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1420
1421 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1422 collectVariableInfo(MF, ProcessedVars);
1423
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001424 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001425 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001426 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001427
Devang Patelbf47fdb2011-08-10 20:55:27 +00001428 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001429 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1430 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1431 LexicalScope *AScope = AList[i];
1432 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001433 if (SP.Verify()) {
1434 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001435 DIArray Variables = SP.getVariables();
1436 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1437 DIVariable DV(Variables.getElement(i));
1438 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1439 continue;
Alexey Samsonovb67bd332012-07-06 08:45:08 +00001440 // Check that DbgVariable for DV wasn't created earlier, when
1441 // findAbstractVariable() was called for inlined instance of DV.
1442 LLVMContext &Ctx = DV->getContext();
1443 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1444 if (AbstractVariables.lookup(CleanDV))
1445 continue;
Devang Patel93d39be2011-08-19 23:28:12 +00001446 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1447 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001448 }
1449 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001450 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001451 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001452 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001453
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001454 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001455
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001456 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Devang Patel5bc942c2011-08-10 23:58:09 +00001457 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1458 dwarf::DW_FORM_flag, 1);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001459
Devang Patelbf47fdb2011-08-10 20:55:27 +00001460 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1461 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001462
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001463 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001464 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1465 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1466 DeleteContainerPointers(I->second);
1467 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001468 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001469 UserVariables.clear();
1470 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001471 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001472 LabelsBeforeInsn.clear();
1473 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001474 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001475}
1476
Chris Lattnerc6087842010-03-09 04:54:43 +00001477/// recordSourceLine - Register a source line with debug info. Returns the
1478/// unique label that was emitted and which provides correspondence to
1479/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001480void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1481 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001482 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001483 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001484 unsigned Src = 1;
1485 if (S) {
1486 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001487
Dan Gohman1cc0d622010-05-05 23:41:32 +00001488 if (Scope.isCompileUnit()) {
1489 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001490 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001491 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001492 } else if (Scope.isFile()) {
1493 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001494 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001495 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001496 } else if (Scope.isSubprogram()) {
1497 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001498 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001499 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001500 } else if (Scope.isLexicalBlockFile()) {
1501 DILexicalBlockFile DBF(S);
1502 Fn = DBF.getFilename();
1503 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001504 } else if (Scope.isLexicalBlock()) {
1505 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001506 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001507 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001508 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001509 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001510
Devang Patel23670e52011-03-24 20:30:50 +00001511 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001512 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001513 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001514}
1515
Bill Wendling829e67b2009-05-20 23:22:40 +00001516//===----------------------------------------------------------------------===//
1517// Emit Methods
1518//===----------------------------------------------------------------------===//
1519
Devang Patel2c4ceb12009-11-21 02:48:08 +00001520/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001521///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001522unsigned
1523DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001524 // Get the children.
1525 const std::vector<DIE *> &Children = Die->getChildren();
1526
Bill Wendling94d04b82009-05-20 23:21:38 +00001527 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001528 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001529
1530 // Get the abbreviation for this DIE.
1531 unsigned AbbrevNumber = Die->getAbbrevNumber();
1532 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1533
1534 // Set DIE offset
1535 Die->setOffset(Offset);
1536
1537 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001538 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001539
1540 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1541 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1542
1543 // Size the DIE attribute values.
1544 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1545 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001546 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001547
1548 // Size the DIE children if any.
1549 if (!Children.empty()) {
1550 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1551 "Children flag not set");
1552
1553 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001554 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001555
1556 // End of children marker.
1557 Offset += sizeof(int8_t);
1558 }
1559
1560 Die->setSize(Offset - Die->getOffset());
1561 return Offset;
1562}
1563
Devang Patel2c4ceb12009-11-21 02:48:08 +00001564/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001565///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001566void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001567 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1568 E = CUMap.end(); I != E; ++I) {
1569 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001570 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001571 sizeof(int32_t) + // Length of Compilation Unit Info
1572 sizeof(int16_t) + // DWARF version number
1573 sizeof(int32_t) + // Offset Into Abbrev. Section
1574 sizeof(int8_t); // Pointer Size (in bytes)
1575 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001576 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001577}
1578
Chris Lattner9c69e285532010-04-04 22:59:04 +00001579/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1580/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001581void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001582 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001583
Bill Wendling94d04b82009-05-20 23:21:38 +00001584 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001585 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001586 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001587 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001588 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001589 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001590
Chris Lattner9c69e285532010-04-04 22:59:04 +00001591 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001592 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001593
Devang Patelaf608bd2010-08-24 00:06:12 +00001594 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001595 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
Chris Lattner11b8f302010-04-04 23:02:02 +00001596 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001597 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001598 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001599 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1600 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001601
Devang Patelc3f5f782010-05-25 23:40:22 +00001602 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1603 "section_debug_loc");
1604
Chris Lattner9c69e285532010-04-04 22:59:04 +00001605 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001606 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001607}
1608
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001609/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001610///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001611void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001612 // Get the abbreviation for this DIE.
1613 unsigned AbbrevNumber = Die->getAbbrevNumber();
1614 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1615
Bill Wendling94d04b82009-05-20 23:21:38 +00001616 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001617 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001618 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1619 Twine::utohexstr(Die->getOffset()) + ":0x" +
1620 Twine::utohexstr(Die->getSize()) + " " +
1621 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001622 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001623
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001624 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001625 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1626
1627 // Emit the DIE attribute values.
1628 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1629 unsigned Attr = AbbrevData[i].getAttribute();
1630 unsigned Form = AbbrevData[i].getForm();
1631 assert(Form && "Too many attributes for DIE (check abbreviation)");
1632
Chris Lattner3f53c832010-04-04 18:52:31 +00001633 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001634 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001635
Bill Wendling94d04b82009-05-20 23:21:38 +00001636 switch (Attr) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001637 case dwarf::DW_AT_abstract_origin: {
1638 DIEEntry *E = cast<DIEEntry>(Values[i]);
1639 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001640 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001641 Asm->EmitInt32(Addr);
1642 break;
1643 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001644 case dwarf::DW_AT_ranges: {
1645 // DW_AT_range Value encodes offset in debug_range section.
1646 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001647
Nick Lewyckyffccd922012-06-22 01:25:12 +00001648 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001649 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1650 V->getValue(),
1651 4);
1652 } else {
1653 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1654 V->getValue(),
1655 DwarfDebugRangeSectionSym,
1656 4);
1657 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001658 break;
1659 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001660 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-06-22 01:25:12 +00001661 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
1662 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1663 Asm->EmitLabelReference(L->getValue(), 4);
1664 else
1665 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
1666 } else {
Devang Patelc3f5f782010-05-25 23:40:22 +00001667 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00001668 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001669 break;
1670 }
Devang Patel2a361602010-09-29 19:08:08 +00001671 case dwarf::DW_AT_accessibility: {
1672 if (Asm->isVerbose()) {
1673 DIEInteger *V = cast<DIEInteger>(Values[i]);
1674 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1675 }
1676 Values[i]->EmitValue(Asm, Form);
1677 break;
1678 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001679 default:
1680 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001681 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001682 break;
1683 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001684 }
1685
1686 // Emit the DIE children if any.
1687 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1688 const std::vector<DIE *> &Children = Die->getChildren();
1689
1690 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001691 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001692
Chris Lattner3f53c832010-04-04 18:52:31 +00001693 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001694 Asm->OutStreamer.AddComment("End Of Children Mark");
1695 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001696 }
1697}
1698
Devang Patel8a241142009-12-09 18:24:21 +00001699/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001700///
Devang Patel8a241142009-12-09 18:24:21 +00001701void DwarfDebug::emitDebugInfo() {
1702 // Start debug info section.
1703 Asm->OutStreamer.SwitchSection(
1704 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001705 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1706 E = CUMap.end(); I != E; ++I) {
1707 CompileUnit *TheCU = I->second;
1708 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001709
Devang Patel163a9f72010-05-10 22:49:55 +00001710 // Emit the compile units header.
1711 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1712 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001713
Devang Patel163a9f72010-05-10 22:49:55 +00001714 // Emit size of content not including length itself
1715 unsigned ContentSize = Die->getSize() +
1716 sizeof(int16_t) + // DWARF version number
1717 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001718 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001719
Devang Patel163a9f72010-05-10 22:49:55 +00001720 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1721 Asm->EmitInt32(ContentSize);
1722 Asm->OutStreamer.AddComment("DWARF version number");
1723 Asm->EmitInt16(dwarf::DWARF_VERSION);
1724 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1725 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1726 DwarfAbbrevSectionSym);
1727 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1728 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001729
Devang Patel163a9f72010-05-10 22:49:55 +00001730 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001731 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1732 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001733}
1734
Devang Patel2c4ceb12009-11-21 02:48:08 +00001735/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001736///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001737void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001738 // Check to see if it is worth the effort.
1739 if (!Abbreviations.empty()) {
1740 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001741 Asm->OutStreamer.SwitchSection(
1742 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001743
Chris Lattnerc0215722010-04-04 19:25:43 +00001744 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001745
1746 // For each abbrevation.
1747 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1748 // Get abbreviation data
1749 const DIEAbbrev *Abbrev = Abbreviations[i];
1750
1751 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001752 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001753
1754 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001755 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001756 }
1757
1758 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001759 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001760
Chris Lattnerc0215722010-04-04 19:25:43 +00001761 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001762 }
1763}
1764
Devang Patel2c4ceb12009-11-21 02:48:08 +00001765/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001766/// the line matrix.
1767///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001768void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001769 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001770 Asm->OutStreamer.AddComment("Extended Op");
1771 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001772
Chris Lattner233f52b2010-03-09 23:52:58 +00001773 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001774 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001775 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1776 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1777
1778 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001779
Chris Lattnerc0215722010-04-04 19:25:43 +00001780 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001781 Asm->getTargetData().getPointerSize(),
1782 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001783
1784 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001785 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1786 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001787 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001788 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001789}
1790
Eric Christopher09ac3d82011-11-07 09:24:32 +00001791/// emitAccelNames - Emit visible names into a hashed accelerator table
1792/// section.
1793void DwarfDebug::emitAccelNames() {
1794 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1795 dwarf::DW_FORM_data4));
1796 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1797 E = CUMap.end(); I != E; ++I) {
1798 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001799 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
1800 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001801 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1802 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001803 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00001804 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1805 DE = Entities.end(); DI != DE; ++DI)
1806 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001807 }
1808 }
1809
1810 AT.FinalizeTable(Asm, "Names");
1811 Asm->OutStreamer.SwitchSection(
1812 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1813 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1814 Asm->OutStreamer.EmitLabel(SectionBegin);
1815
1816 // Emit the full data.
1817 AT.Emit(Asm, SectionBegin, this);
1818}
1819
1820/// emitAccelObjC - Emit objective C classes and categories into a hashed
1821/// accelerator table section.
1822void DwarfDebug::emitAccelObjC() {
1823 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1824 dwarf::DW_FORM_data4));
1825 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1826 E = CUMap.end(); I != E; ++I) {
1827 CompileUnit *TheCU = I->second;
1828 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1829 for (StringMap<std::vector<DIE*> >::const_iterator
1830 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1831 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001832 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-11-07 09:24:32 +00001833 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1834 DE = Entities.end(); DI != DE; ++DI)
1835 AT.AddName(Name, (*DI));
1836 }
1837 }
1838
1839 AT.FinalizeTable(Asm, "ObjC");
1840 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1841 .getDwarfAccelObjCSection());
1842 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1843 Asm->OutStreamer.EmitLabel(SectionBegin);
1844
1845 // Emit the full data.
1846 AT.Emit(Asm, SectionBegin, this);
1847}
1848
1849/// emitAccelNamespace - Emit namespace dies into a hashed accelerator
1850/// table.
1851void DwarfDebug::emitAccelNamespaces() {
1852 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1853 dwarf::DW_FORM_data4));
1854 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1855 E = CUMap.end(); I != E; ++I) {
1856 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001857 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
1858 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001859 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1860 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001861 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00001862 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1863 DE = Entities.end(); DI != DE; ++DI)
1864 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00001865 }
1866 }
1867
1868 AT.FinalizeTable(Asm, "namespac");
1869 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1870 .getDwarfAccelNamespaceSection());
1871 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
1872 Asm->OutStreamer.EmitLabel(SectionBegin);
1873
1874 // Emit the full data.
1875 AT.Emit(Asm, SectionBegin, this);
1876}
1877
1878/// emitAccelTypes() - Emit type dies into a hashed accelerator table.
1879void DwarfDebug::emitAccelTypes() {
Eric Christopherc36145f2012-01-06 04:35:23 +00001880 std::vector<DwarfAccelTable::Atom> Atoms;
1881 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1882 dwarf::DW_FORM_data4));
1883 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTag,
1884 dwarf::DW_FORM_data2));
1885 Atoms.push_back(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeTypeFlags,
1886 dwarf::DW_FORM_data1));
1887 DwarfAccelTable AT(Atoms);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001888 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1889 E = CUMap.end(); I != E; ++I) {
1890 CompileUnit *TheCU = I->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001891 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
1892 = TheCU->getAccelTypes();
1893 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00001894 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1895 const char *Name = GI->getKeyData();
Eric Christopherfa03db02012-01-06 23:03:34 +00001896 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00001897 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
1898 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
1899 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001900 }
1901 }
1902
1903 AT.FinalizeTable(Asm, "types");
1904 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1905 .getDwarfAccelTypesSection());
1906 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
1907 Asm->OutStreamer.EmitLabel(SectionBegin);
1908
1909 // Emit the full data.
1910 AT.Emit(Asm, SectionBegin, this);
1911}
1912
Devang Patel193f7202009-11-24 01:14:22 +00001913void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001914 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1915 E = CUMap.end(); I != E; ++I) {
1916 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00001917 // Start the dwarf pubtypes section.
Devang Patel163a9f72010-05-10 22:49:55 +00001918 Asm->OutStreamer.SwitchSection(
1919 Asm->getObjFileLowering().getDwarfPubTypesSection());
1920 Asm->OutStreamer.AddComment("Length of Public Types Info");
1921 Asm->EmitLabelDifference(
1922 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1923 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001924
Devang Patel163a9f72010-05-10 22:49:55 +00001925 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1926 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001927
Devang Patel163a9f72010-05-10 22:49:55 +00001928 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1929 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001930
Devang Patel163a9f72010-05-10 22:49:55 +00001931 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1932 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1933 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001934
Devang Patel163a9f72010-05-10 22:49:55 +00001935 Asm->OutStreamer.AddComment("Compilation Unit Length");
1936 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1937 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1938 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001939
Devang Patel163a9f72010-05-10 22:49:55 +00001940 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1941 for (StringMap<DIE*>::const_iterator
1942 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1943 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001944 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001945
Devang Patel163a9f72010-05-10 22:49:55 +00001946 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1947 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001948
Devang Patel163a9f72010-05-10 22:49:55 +00001949 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer983c4572011-11-09 18:16:11 +00001950 // Emit the name with a terminating null byte.
Devang Patel163a9f72010-05-10 22:49:55 +00001951 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1952 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001953
Devang Patel163a9f72010-05-10 22:49:55 +00001954 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001955 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001956 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1957 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001958 }
Devang Patel193f7202009-11-24 01:14:22 +00001959}
1960
Devang Patel2c4ceb12009-11-21 02:48:08 +00001961/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001962///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001963void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001964 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001965 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001966
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001967 // Start the dwarf str section.
1968 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001969 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001970
Chris Lattnerbc733f52010-03-13 02:17:42 +00001971 // Get all of the string pool entries and put them in an array by their ID so
1972 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001973 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00001974 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001975
Chris Lattnerbc733f52010-03-13 02:17:42 +00001976 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1977 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1978 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001979
Chris Lattnerbc733f52010-03-13 02:17:42 +00001980 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001981
Chris Lattnerbc733f52010-03-13 02:17:42 +00001982 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001983 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001984 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001985
Benjamin Kramer983c4572011-11-09 18:16:11 +00001986 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00001987 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
1988 Entries[i].second->getKeyLength()+1),
1989 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001990 }
1991}
1992
Devang Patel2c4ceb12009-11-21 02:48:08 +00001993/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001994///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001995void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00001996 if (DotDebugLocEntries.empty())
1997 return;
1998
Devang Patel6c3ea902011-02-04 22:57:18 +00001999 for (SmallVector<DotDebugLocEntry, 4>::iterator
2000 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2001 I != E; ++I) {
2002 DotDebugLocEntry &Entry = *I;
2003 if (I + 1 != DotDebugLocEntries.end())
2004 Entry.Merge(I+1);
2005 }
2006
Daniel Dunbar83320a02011-03-16 22:16:39 +00002007 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002008 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002009 Asm->getObjFileLowering().getDwarfLocSection());
2010 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002011 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2012 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002013 for (SmallVector<DotDebugLocEntry, 4>::iterator
2014 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002015 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002016 DotDebugLocEntry &Entry = *I;
2017 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002018 if (Entry.isEmpty()) {
2019 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2020 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002021 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002022 } else {
2023 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2024 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002025 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002026 Asm->OutStreamer.AddComment("Loc expr size");
2027 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2028 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2029 Asm->EmitLabelDifference(end, begin, 2);
2030 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002031 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002032 DIBasicType BTy(DV.getType());
2033 if (BTy.Verify() &&
2034 (BTy.getEncoding() == dwarf::DW_ATE_signed
2035 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2036 Asm->OutStreamer.AddComment("DW_OP_consts");
2037 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002038 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002039 } else {
2040 Asm->OutStreamer.AddComment("DW_OP_constu");
2041 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002042 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002043 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002044 } else if (Entry.isLocation()) {
2045 if (!DV.hasComplexAddress())
2046 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002047 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002048 else {
2049 // Complex address entry.
2050 unsigned N = DV.getNumAddrElements();
2051 unsigned i = 0;
2052 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2053 if (Entry.Loc.getOffset()) {
2054 i = 2;
2055 Asm->EmitDwarfRegOp(Entry.Loc);
2056 Asm->OutStreamer.AddComment("DW_OP_deref");
2057 Asm->EmitInt8(dwarf::DW_OP_deref);
2058 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2059 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2060 Asm->EmitSLEB128(DV.getAddrElement(1));
2061 } else {
2062 // If first address element is OpPlus then emit
2063 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2064 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2065 Asm->EmitDwarfRegOp(Loc);
2066 i = 2;
2067 }
2068 } else {
2069 Asm->EmitDwarfRegOp(Entry.Loc);
2070 }
2071
2072 // Emit remaining complex address elements.
2073 for (; i < N; ++i) {
2074 uint64_t Element = DV.getAddrElement(i);
2075 if (Element == DIBuilder::OpPlus) {
2076 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2077 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher50120762012-05-08 18:56:00 +00002078 } else if (Element == DIBuilder::OpDeref) {
Eric Christophera80f2d12012-05-08 21:24:39 +00002079 if (!Entry.Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002080 Asm->EmitInt8(dwarf::DW_OP_deref);
2081 } else
2082 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002083 }
Devang Patelc26f5442011-04-28 02:22:40 +00002084 }
Devang Patelc26f5442011-04-28 02:22:40 +00002085 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002086 // else ... ignore constant fp. There is not any good way to
2087 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002088 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002089 }
2090 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002091}
2092
2093/// EmitDebugARanges - Emit visible names into a debug aranges section.
2094///
2095void DwarfDebug::EmitDebugARanges() {
2096 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002097 Asm->OutStreamer.SwitchSection(
2098 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002099}
2100
Devang Patel2c4ceb12009-11-21 02:48:08 +00002101/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002102///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002103void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002104 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002105 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002106 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002107 unsigned char Size = Asm->getTargetData().getPointerSize();
2108 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002109 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002110 I != E; ++I) {
2111 if (*I)
2112 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002113 else
Devang Pateleac9c072010-04-27 19:46:33 +00002114 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002115 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002116}
2117
Devang Patel2c4ceb12009-11-21 02:48:08 +00002118/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002119///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002120void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002121 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002122 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002123 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002124 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002125 }
2126}
2127
Devang Patel2c4ceb12009-11-21 02:48:08 +00002128/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002129/// Section Header:
2130/// 1. length of section
2131/// 2. Dwarf version number
2132/// 3. address size.
2133///
2134/// Entries (one "entry" for each function that was inlined):
2135///
2136/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2137/// otherwise offset into __debug_str for regular function name.
2138/// 2. offset into __debug_str section for regular function name.
2139/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2140/// instances for the function.
2141///
2142/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2143/// inlined instance; the die_offset points to the inlined_subroutine die in the
2144/// __debug_info section, and the low_pc is the starting address for the
2145/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002146void DwarfDebug::emitDebugInlineInfo() {
Eric Christopherb83e2bb2012-03-02 02:11:47 +00002147 if (!Asm->MAI->doesDwarfUseInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002148 return;
2149
Devang Patel163a9f72010-05-10 22:49:55 +00002150 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002151 return;
2152
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002153 Asm->OutStreamer.SwitchSection(
2154 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002155
Chris Lattner233f52b2010-03-09 23:52:58 +00002156 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002157 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2158 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002159
Chris Lattnerc0215722010-04-04 19:25:43 +00002160 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002161
Chris Lattner233f52b2010-03-09 23:52:58 +00002162 Asm->OutStreamer.AddComment("Dwarf Version");
2163 Asm->EmitInt16(dwarf::DWARF_VERSION);
2164 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002165 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002166
Devang Patele9f8f5e2010-05-07 20:54:48 +00002167 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002168 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002169
Devang Patele9f8f5e2010-05-07 20:54:48 +00002170 const MDNode *Node = *I;
2171 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002172 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002173 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002174 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002175 StringRef LName = SP.getLinkageName();
2176 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002177
Chris Lattner233f52b2010-03-09 23:52:58 +00002178 Asm->OutStreamer.AddComment("MIPS linkage name");
Eric Christopher50e26612012-03-02 01:57:52 +00002179 if (LName.empty())
2180 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2181 else
Chris Lattner6189ed12010-04-04 23:25:33 +00002182 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2183 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002184
Chris Lattner233f52b2010-03-09 23:52:58 +00002185 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002186 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002187 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002188
Devang Patel53bb5c92009-11-10 23:06:00 +00002189 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002190 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002191 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002192 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002193
Chris Lattner3f53c832010-04-04 18:52:31 +00002194 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002195 Asm->OutStreamer.EmitSymbolValue(LI->first,
2196 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002197 }
2198 }
2199
Chris Lattnerc0215722010-04-04 19:25:43 +00002200 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002201}