blob: 1d5e05d8fdaa8cae58c019548e960988fd4d3a1b [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"
Devang Patel8b9df622011-04-12 17:40:32 +000017#include "DwarfCompileUnit.h"
Bill Wendling57fbba42010-04-05 22:59:21 +000018#include "llvm/Constants.h"
Bill Wendling0310d762009-05-15 09:23:25 +000019#include "llvm/Module.h"
Devang Patele449d1f2011-01-20 00:02:16 +000020#include "llvm/Instructions.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000021#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000023#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000024#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000025#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000026#include "llvm/MC/MCSymbol.h"
Bill Wendling0310d762009-05-15 09:23:25 +000027#include "llvm/Target/TargetData.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000028#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000029#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000033#include "llvm/Analysis/DebugInfo.h"
Devang Patel0eea95d2011-02-24 18:49:30 +000034#include "llvm/Analysis/DIBuilder.h"
Devang Patel9a31f0f2010-10-25 20:45:32 +000035#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000036#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000037#include "llvm/ADT/StringExtras.h"
Devang Pateleac9c072010-04-27 19:46:33 +000038#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000039#include "llvm/Support/Debug.h"
40#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000041#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000042#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000043#include "llvm/Support/Timer.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000044#include "llvm/Support/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000045using namespace llvm;
46
Jim Grosbach1e20b962010-07-21 21:21:52 +000047static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel61409622010-07-07 20:12:52 +000048 cl::Hidden,
Devang Pateleac9c072010-04-27 19:46:33 +000049 cl::desc("Disable debug info printing"));
50
Dan Gohman281d65d2010-05-07 01:08:53 +000051static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner7a2bdde2011-04-15 05:18:47 +000052 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman281d65d2010-05-07 01:08:53 +000053 cl::init(false));
54
Bill Wendling5f017e82010-04-07 09:28:04 +000055namespace {
56 const char *DWARFGroupName = "DWARF Emission";
57 const char *DbgTimerName = "DWARF Debug Writer";
58} // end anonymous namespace
59
Bill Wendling0310d762009-05-15 09:23:25 +000060//===----------------------------------------------------------------------===//
61
62/// Configuration values for initial hash set sizes (log2).
63///
Bill Wendling0310d762009-05-15 09:23:25 +000064static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000065
66namespace llvm {
67
Nick Lewycky3bbb6f72011-07-29 03:49:23 +000068DIType DbgVariable::getType() const {
Devang Patel3cbee302011-04-12 22:53:02 +000069 DIType Ty = Var.getType();
70 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
71 // addresses instead.
72 if (Var.isBlockByrefVariable()) {
73 /* Byref variables, in Blocks, are declared by the programmer as
74 "SomeType VarName;", but the compiler creates a
75 __Block_byref_x_VarName struct, and gives the variable VarName
76 either the struct, or a pointer to the struct, as its type. This
77 is necessary for various behind-the-scenes things the compiler
78 needs to do with by-reference variables in blocks.
79
80 However, as far as the original *programmer* is concerned, the
81 variable should still have type 'SomeType', as originally declared.
82
83 The following function dives into the __Block_byref_x_VarName
84 struct to find the original type of the variable. This will be
85 passed back to the code generating the type for the Debug
86 Information Entry for the variable 'VarName'. 'VarName' will then
87 have the original type 'SomeType' in its debug information.
88
89 The original type 'SomeType' will be the type of the field named
90 'VarName' inside the __Block_byref_x_VarName struct.
91
92 NOTE: In order for this to not completely fail on the debugger
93 side, the Debug Information Entry for the variable VarName needs to
94 have a DW_AT_location that tells the debugger how to unwind through
95 the pointers and __Block_byref_x_VarName struct to find the actual
96 value of the variable. The function addBlockByrefType does this. */
97 DIType subType = Ty;
98 unsigned tag = Ty.getTag();
99
100 if (tag == dwarf::DW_TAG_pointer_type) {
101 DIDerivedType DTy = DIDerivedType(Ty);
102 subType = DTy.getTypeDerivedFrom();
103 }
104
105 DICompositeType blockStruct = DICompositeType(subType);
106 DIArray Elements = blockStruct.getTypeArray();
107
108 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
109 DIDescriptor Element = Elements.getElement(i);
110 DIDerivedType DT = DIDerivedType(Element);
111 if (getName() == DT.getName())
112 return (DT.getTypeDerivedFrom());
Devang Patel8bd11de2010-08-09 21:01:39 +0000113 }
114 return Ty;
115 }
Devang Patel3cbee302011-04-12 22:53:02 +0000116 return Ty;
117}
Bill Wendling0310d762009-05-15 09:23:25 +0000118
Chris Lattnerea761862010-04-05 04:09:20 +0000119} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000120
Chris Lattner49cd6642010-04-05 05:11:15 +0000121DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000122 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000123 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelbf47fdb2011-08-10 20:55:27 +0000124 PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000125 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000126
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000127 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000128 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000129 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000130 FunctionBeginSym = FunctionEndSym = 0;
Dan Gohman03c3dc72010-06-18 15:56:31 +0000131 {
132 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
133 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000134 }
Bill Wendling0310d762009-05-15 09:23:25 +0000135}
136DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000137}
138
Nick Lewycky390c40d2011-10-27 06:44:11 +0000139MCSymbol *DwarfDebug::getStringPool() {
140 return Asm->GetTempSymbol("section_str");
141}
142
Chris Lattnerbc733f52010-03-13 02:17:42 +0000143MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
144 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
145 if (Entry.first) return Entry.first;
146
147 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000148 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000149}
150
151
Devang Patel2c4ceb12009-11-21 02:48:08 +0000152/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000153///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000154void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000155 // Profile the node so that we can make it unique.
156 FoldingSetNodeID ID;
157 Abbrev.Profile(ID);
158
159 // Check the set for priors.
160 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
161
162 // If it's newly added.
163 if (InSet == &Abbrev) {
164 // Add to abbreviation list.
165 Abbreviations.push_back(&Abbrev);
166
167 // Assign the vector position + 1 as its number.
168 Abbrev.setNumber(Abbreviations.size());
169 } else {
170 // Assign existing abbreviation number.
171 Abbrev.setNumber(InSet->getNumber());
172 }
173}
174
Jim Grosbach1e20b962010-07-21 21:21:52 +0000175/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000176/// printer to not emit usual symbol prefix before the symbol name is used then
177/// return linkage name after skipping this special LLVM prefix.
178static StringRef getRealLinkageName(StringRef LinkageName) {
179 char One = '\1';
180 if (LinkageName.startswith(StringRef(&One, 1)))
181 return LinkageName.substr(1);
182 return LinkageName;
183}
184
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000185/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000186/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
187/// If there are global variables in this scope then create and insert
188/// DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000189DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
190 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000191 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000192
Chris Lattnerd38fee82010-04-05 00:13:49 +0000193 assert(SPDie && "Unable to find subprogram DIE!");
194 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000195
Devang Patel5e06bb82011-04-22 23:10:17 +0000196 DISubprogram SPDecl = SP.getFunctionDeclaration();
197 if (SPDecl.isSubprogram())
198 // Refer function declaration directly.
Devang Patel3cbee302011-04-12 22:53:02 +0000199 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Devang Pateldbc64af2011-08-15 17:24:54 +0000200 SPCU->getOrCreateSubprogramDIE(SPDecl));
Devang Patel5e06bb82011-04-22 23:10:17 +0000201 else {
202 // There is not any need to generate specification DIE for a function
203 // defined at compile unit level. If a function is defined inside another
204 // function then gdb prefers the definition at top level and but does not
205 // expect specification DIE in parent function. So avoid creating
206 // specification DIE for a function defined inside a function.
207 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
208 !SP.getContext().isFile() &&
209 !isSubprogramContext(SP.getContext())) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000210 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel5e06bb82011-04-22 23:10:17 +0000211
212 // Add arguments.
213 DICompositeType SPTy = SP.getType();
214 DIArray Args = SPTy.getTypeArray();
215 unsigned SPTag = SPTy.getTag();
216 if (SPTag == dwarf::DW_TAG_subroutine_type)
217 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
218 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
219 DIType ATy = DIType(DIType(Args.getElement(i)));
220 SPCU->addType(Arg, ATy);
221 if (ATy.isArtificial())
222 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
223 SPDie->addChild(Arg);
224 }
225 DIE *SPDeclDie = SPDie;
226 SPDie = new DIE(dwarf::DW_TAG_subprogram);
227 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
228 SPDeclDie);
229 SPCU->addDie(SPDie);
230 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000231 }
Devang Patel8aa61472010-07-07 22:20:57 +0000232 // Pick up abstract subprogram DIE.
233 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
234 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000235 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
236 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000237 SPCU->addDie(SPDie);
238 }
239
Devang Patel3cbee302011-04-12 22:53:02 +0000240 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
241 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
242 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
243 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000244 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
245 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000246 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000247
Chris Lattnerd38fee82010-04-05 00:13:49 +0000248 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000249}
250
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000251/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000252/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000253DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
254 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000255 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
256 if (Scope->isAbstractScope())
257 return ScopeDIE;
258
Devang Patelbf47fdb2011-08-10 20:55:27 +0000259 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000260 if (Ranges.empty())
261 return 0;
262
Devang Patelbf47fdb2011-08-10 20:55:27 +0000263 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000264 if (Ranges.size() > 1) {
265 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000266 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000267 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000268 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000269 DebugRangeSymbols.size()
270 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000271 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000272 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000273 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
274 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000275 }
276 DebugRangeSymbols.push_back(NULL);
277 DebugRangeSymbols.push_back(NULL);
278 return ScopeDIE;
279 }
280
Devang Patelc3f5f782010-05-25 23:40:22 +0000281 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
282 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000283
Devang Patelc3f5f782010-05-25 23:40:22 +0000284 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000285
Chris Lattnerb7db7332010-03-09 01:58:53 +0000286 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
287 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000288
Devang Patel3cbee302011-04-12 22:53:02 +0000289 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
290 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000291
292 return ScopeDIE;
293}
294
Devang Patel2c4ceb12009-11-21 02:48:08 +0000295/// constructInlinedScopeDIE - This scope represents inlined body of
296/// a function. Construct DIE to represent this concrete inlined copy
297/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000298DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
299 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000300 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000301 assert(Ranges.empty() == false &&
302 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000303
Devang Patel26a92002011-07-27 00:34:13 +0000304 if (!Scope->getScopeNode())
305 return NULL;
306 DIScope DS(Scope->getScopeNode());
307 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000308 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
309 if (!OriginDIE) {
310 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
311 return NULL;
312 }
313
Devang Patelbf47fdb2011-08-10 20:55:27 +0000314 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000315 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
316 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000317
Devang Patel0afbf232010-07-08 22:39:20 +0000318 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000319 assert(0 && "Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000320 return 0;
321 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000322 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000323 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000324 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000325 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000326
Devang Pateld96efb82011-05-05 17:54:26 +0000327 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000328 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
329 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000330
Devang Patel26a92002011-07-27 00:34:13 +0000331 if (Ranges.size() > 1) {
332 // .debug_range section has not been laid out yet. Emit offset in
333 // .debug_range as a uint, size 4, for now. emitDIE will handle
334 // DW_AT_ranges appropriately.
335 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000336 DebugRangeSymbols.size()
337 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000338 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000339 RE = Ranges.end(); RI != RE; ++RI) {
340 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
341 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
342 }
343 DebugRangeSymbols.push_back(NULL);
344 DebugRangeSymbols.push_back(NULL);
345 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000346 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
347 StartLabel);
348 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
349 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000350 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000351
352 InlinedSubprogramDIEs.insert(OriginDIE);
353
354 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000355 //.debug_inlined section specification does not clearly state how
356 // to emit inlined scope that is split into multiple instruction ranges.
357 // For now, use first instruction range and emit low_pc/high_pc pair and
358 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000359 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000360 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000361
362 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000363 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000364 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000365 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000366 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000367
Devang Patel53bb5c92009-11-10 23:06:00 +0000368 DILocation DL(Scope->getInlinedAt());
Devang Patel3cbee302011-04-12 22:53:02 +0000369 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
370 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000371
372 return ScopeDIE;
373}
374
Devang Patel2c4ceb12009-11-21 02:48:08 +0000375/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000376DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000377 if (!Scope || !Scope->getScopeNode())
378 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000379
Nick Lewycky746cb672011-10-26 22:55:33 +0000380 SmallVector<DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000381
382 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000383 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000384 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
385 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000386 if (DIE *Arg =
387 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel0478c152011-03-01 22:58:55 +0000388 Children.push_back(Arg);
389
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000390 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000391 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000392 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000393 if (DIE *Variable =
394 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000395 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000396 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000397 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000398 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000399 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000400 DIScope DS(Scope->getScopeNode());
401 DIE *ScopeDIE = NULL;
402 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000403 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000404 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000405 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000406 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000407 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000408 // Note down abstract DIE.
409 if (ScopeDIE)
410 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
411 }
Devang Patel3c91b052010-03-08 20:52:55 +0000412 else
Devang Pateld3024342011-08-15 22:24:32 +0000413 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000414 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000415 else {
416 // There is no need to emit empty lexical block DIE.
417 if (Children.empty())
418 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000419 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000420 }
421
Devang Patelaead63c2010-03-29 22:59:58 +0000422 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000423
Devang Patel5bc9fec2011-02-19 01:31:27 +0000424 // Add children
425 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
426 E = Children.end(); I != E; ++I)
427 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000428
Jim Grosbach1e20b962010-07-21 21:21:52 +0000429 if (DS.isSubprogram())
Devang Pateld3024342011-08-15 22:24:32 +0000430 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000431
Devang Patel193f7202009-11-24 01:14:22 +0000432 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000433}
434
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000435/// GetOrCreateSourceID - Look up the source id with the given directory and
436/// source file names. If none currently exists, create a new id and insert it
437/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
438/// maps as well.
Devang Patel23670e52011-03-24 20:30:50 +0000439unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
440 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000441 // If FE did not provide a file name, then assume stdin.
442 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000443 return GetOrCreateSourceID("<stdin>", StringRef());
444
Nick Lewycky44d798d2011-10-17 23:05:28 +0000445 unsigned SrcId = SourceIdMap.size()+1;
446 std::pair<std::string, std::string> SourceName =
447 std::make_pair(FileName, DirName);
448 std::pair<std::pair<std::string, std::string>, unsigned> Entry =
449 make_pair(SourceName, SrcId);
Devang Patel1905a182010-09-16 20:57:49 +0000450
Nick Lewycky44d798d2011-10-17 23:05:28 +0000451 std::map<std::pair<std::string, std::string>, unsigned>::iterator I;
452 bool NewlyInserted;
453 tie(I, NewlyInserted) = SourceIdMap.insert(Entry);
454 if (!NewlyInserted)
455 return I->second;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000456
Rafael Espindola5c055632010-11-18 02:04:25 +0000457 // Print out a .file directive to specify files for .loc directives.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000458 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.first.second,
459 Entry.first.first);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000460
461 return SrcId;
462}
463
Jim Grosbach1e20b962010-07-21 21:21:52 +0000464/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000465/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000466CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000467 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000468 StringRef FN = DIUnit.getFilename();
469 StringRef Dir = DIUnit.getDirectory();
Devang Patel23670e52011-03-24 20:30:50 +0000470 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000471
472 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel3cbee302011-04-12 22:53:02 +0000473 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000474 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000475 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
476 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000477 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Devang Patel5098da02010-04-26 22:54:28 +0000478 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
479 // simplifies debug range entries.
Devang Patel3cbee302011-04-12 22:53:02 +0000480 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000481 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000482 // compile unit in debug_line section.
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000483 if (Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindola597a7662011-05-04 17:44:06 +0000484 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000485 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000486 else
Devang Patel3cbee302011-04-12 22:53:02 +0000487 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000488
Devang Patel65dbc902009-11-25 17:36:49 +0000489 if (!Dir.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000490 NewCU->addString(Die, dwarf::DW_AT_comp_dir, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000491 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000492 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000493
Devang Patel65dbc902009-11-25 17:36:49 +0000494 StringRef Flags = DIUnit.getFlags();
495 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000496 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000497
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000498 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000499 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000500 dwarf::DW_FORM_data1, RVer);
501
Devang Patel163a9f72010-05-10 22:49:55 +0000502 if (!FirstCU)
503 FirstCU = NewCU;
504 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000505 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000506}
507
Devang Patel163a9f72010-05-10 22:49:55 +0000508/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000509void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
510 const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000511 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000512 if (!SP.isDefinition())
513 // This is a method declaration which will be handled while constructing
514 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000515 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000516
Devang Pateldbc64af2011-08-15 17:24:54 +0000517 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000518
519 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000520 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000521
522 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000523 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000524
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000525 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +0000526 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +0000527
Devang Patel94c7ddb2011-08-16 22:09:43 +0000528 SPMap[N] = TheCU;
Devang Patel13e16b62009-06-26 01:49:18 +0000529 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000530}
531
Devang Patel02e603f2011-08-15 23:47:24 +0000532/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
533/// as llvm.dbg.enum and llvm.dbg.ty
534void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000535 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
536 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
537 const MDNode *N = NMD->getOperand(i);
538 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
539 constructSubprogramDIE(CU, N);
540 }
541
542 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
543 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
544 const MDNode *N = NMD->getOperand(i);
545 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000546 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000547 }
548
Devang Patel02e603f2011-08-15 23:47:24 +0000549 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
550 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
551 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000552 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
553 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000554 }
555
556 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
557 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
558 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000559 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
560 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000561 }
562}
563
564/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
565/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
566bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
567 DebugInfoFinder DbgFinder;
568 DbgFinder.processModule(*M);
569
570 bool HasDebugInfo = false;
571 // Scan all the compile-units to see if there are any marked as the main
572 // unit. If not, we do not generate debug info.
573 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
574 E = DbgFinder.compile_unit_end(); I != E; ++I) {
575 if (DICompileUnit(*I).isMain()) {
576 HasDebugInfo = true;
577 break;
578 }
579 }
580 if (!HasDebugInfo) return false;
581
582 // Create all the compile unit DIEs.
583 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
584 E = DbgFinder.compile_unit_end(); I != E; ++I)
585 constructCompileUnit(*I);
586
587 // Create DIEs for each global variable.
588 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
589 E = DbgFinder.global_variable_end(); I != E; ++I) {
590 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000591 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000592 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000593 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000594
Devang Patel02e603f2011-08-15 23:47:24 +0000595 // Create DIEs for each subprogram.
596 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
597 E = DbgFinder.subprogram_end(); I != E; ++I) {
598 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000599 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
600 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000601 }
602
603 return HasDebugInfo;
604}
605
Devang Patel2c4ceb12009-11-21 02:48:08 +0000606/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000607/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000608/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000609void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000610 if (DisableDebugInfoPrinting)
611 return;
612
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000613 // If module has named metadata anchors then use them, otherwise scan the
614 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000615 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
616 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000617 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
618 DICompileUnit CUNode(CU_Nodes->getOperand(i));
619 CompileUnit *CU = constructCompileUnit(CUNode);
620 DIArray GVs = CUNode.getGlobalVariables();
621 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000622 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000623 DIArray SPs = CUNode.getSubprograms();
624 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
625 constructSubprogramDIE(CU, SPs.getElement(i));
626 DIArray EnumTypes = CUNode.getEnumTypes();
627 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
628 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
629 DIArray RetainedTypes = CUNode.getRetainedTypes();
630 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
631 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
632 }
Devang Patel02e603f2011-08-15 23:47:24 +0000633 } else if (!collectLegacyDebugInfo(M))
634 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000635
Devang Patel02e603f2011-08-15 23:47:24 +0000636 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000637
Chris Lattnerd850ac72010-04-05 02:19:28 +0000638 // Tell MMI that we have debug info.
639 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000640
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000641 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000642 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000643
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000644 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000645 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000646}
647
Devang Patel2c4ceb12009-11-21 02:48:08 +0000648/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000649///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000650void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000651 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000652 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000653 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000654
Devang Patel94c7ddb2011-08-16 22:09:43 +0000655 // Collect info for variables that were optimized out.
656 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
657 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
658 DICompileUnit TheCU(CU_Nodes->getOperand(i));
659 DIArray Subprograms = TheCU.getSubprograms();
660 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
661 DISubprogram SP(Subprograms.getElement(i));
662 if (ProcessedSPNodes.count(SP) != 0) continue;
663 if (!SP.Verify()) continue;
664 if (!SP.isDefinition()) continue;
Devang Patel93d39be2011-08-19 23:28:12 +0000665 DIArray Variables = SP.getVariables();
666 if (Variables.getNumElements() == 0) continue;
667
Devang Patel94c7ddb2011-08-16 22:09:43 +0000668 LexicalScope *Scope =
669 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
670 DeadFnScopeMap[SP] = Scope;
671
672 // Construct subprogram DIE and add variables DIEs.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000673 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky746cb672011-10-26 22:55:33 +0000674 assert(SPCU && "Unable to find Compile Unit!");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000675 constructSubprogramDIE(SPCU, SP);
676 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel93d39be2011-08-19 23:28:12 +0000677 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
678 DIVariable DV(Variables.getElement(vi));
679 if (!DV.Verify()) continue;
680 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000681 if (DIE *VariableDIE =
Devang Patel93d39be2011-08-19 23:28:12 +0000682 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Patel94c7ddb2011-08-16 22:09:43 +0000683 ScopeDIE->addChild(VariableDIE);
684 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000685 }
686 }
687 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000688
Devang Patel53bb5c92009-11-10 23:06:00 +0000689 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
690 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
691 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
692 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000693 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000694 }
695
Devang Pateldbc64af2011-08-15 17:24:54 +0000696 // Emit DW_AT_containing_type attribute to connect types with their
697 // vtable holding type.
698 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
699 CUE = CUMap.end(); CUI != CUE; ++CUI) {
700 CompileUnit *TheCU = CUI->second;
701 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000702 }
703
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000704 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000705 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000706 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000707 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000708 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000709
710 // End text sections.
711 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000712 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000713 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000714 }
715
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000716 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000717 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000718
719 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000720 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000721
722 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000723 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000724
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000725 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000726 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000727
Devang Patel193f7202009-11-24 01:14:22 +0000728 // Emit info into a debug pubtypes section.
729 emitDebugPubTypes();
730
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000731 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000732 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000733
734 // Emit info into a debug aranges section.
735 EmitDebugARanges();
736
737 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000738 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000739
740 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000741 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000742
743 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000744 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000745
Chris Lattnerbc733f52010-03-13 02:17:42 +0000746 // Emit info into a debug str section.
747 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000748
Devang Patele9a1cca2010-08-02 17:32:15 +0000749 // clean up.
750 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000751 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000752 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
753 E = CUMap.end(); I != E; ++I)
754 delete I->second;
755 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000756}
757
Devang Patel53bb5c92009-11-10 23:06:00 +0000758/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000759DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000760 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000761 LLVMContext &Ctx = DV->getContext();
762 // More then one inlined variable corresponds to one abstract variable.
763 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000764 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000765 if (AbsDbgVariable)
766 return AbsDbgVariable;
767
Devang Patelbf47fdb2011-08-10 20:55:27 +0000768 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000769 if (!Scope)
770 return NULL;
771
Devang Patel5a1a67c2011-08-15 19:01:20 +0000772 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000773 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000774 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000775 return AbsDbgVariable;
776}
777
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000778/// addCurrentFnArgument - If Var is a current function argument then add
779/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000780bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000781 DbgVariable *Var, LexicalScope *Scope) {
782 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000783 return false;
784 DIVariable DV = Var->getVariable();
785 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
786 return false;
787 unsigned ArgNo = DV.getArgNumber();
788 if (ArgNo == 0)
789 return false;
790
Devang Patelcb3a6572011-03-03 20:02:02 +0000791 size_t Size = CurrentFnArguments.size();
792 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000793 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000794 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000795 // arguments does the function have at source level.
796 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000797 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000798 CurrentFnArguments[ArgNo - 1] = Var;
799 return true;
800}
801
Devang Patelee432862010-05-20 19:57:06 +0000802/// collectVariableInfoFromMMITable - Collect variable information from
803/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000804void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000805DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000806 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000807 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
808 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
809 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000810 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000811 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000812 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000813 DIVariable DV(Var);
814 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000815
Devang Patelbf47fdb2011-08-10 20:55:27 +0000816 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000817
Devang Patelfb0ee432009-11-10 23:20:04 +0000818 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000819 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000820 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000821
Devang Patel26c1e562010-05-20 16:36:41 +0000822 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000823 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000824 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000825 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000826 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000827 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000828 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000829 }
Devang Patelee432862010-05-20 19:57:06 +0000830}
Devang Patel90a48ad2010-03-15 18:33:46 +0000831
Jim Grosbach1e20b962010-07-21 21:21:52 +0000832/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000833/// DBG_VALUE instruction, is in a defined reg.
834static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000835 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000836 return MI->getNumOperands() == 3 &&
837 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
838 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000839}
840
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000841/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000842/// at MI.
843static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
844 const MCSymbol *FLabel,
845 const MCSymbol *SLabel,
846 const MachineInstr *MI) {
847 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
848
849 if (MI->getNumOperands() != 3) {
850 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
851 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
852 }
853 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
854 MachineLocation MLoc;
855 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
856 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
857 }
858 if (MI->getOperand(0).isImm())
859 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
860 if (MI->getOperand(0).isFPImm())
861 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
862 if (MI->getOperand(0).isCImm())
863 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
864
Nick Lewycky746cb672011-10-26 22:55:33 +0000865 assert(0 && "Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +0000866 return DotDebugLocEntry();
867}
868
Devang Patelbf47fdb2011-08-10 20:55:27 +0000869/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000870void
Devang Patel78e127d2010-06-25 22:07:34 +0000871DwarfDebug::collectVariableInfo(const MachineFunction *MF,
872 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000873
Devang Patelee432862010-05-20 19:57:06 +0000874 /// collection info from MMI table.
875 collectVariableInfoFromMMITable(MF, Processed);
876
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000877 for (SmallVectorImpl<const MDNode*>::const_iterator
878 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
879 ++UVI) {
880 const MDNode *Var = *UVI;
881 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +0000882 continue;
883
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000884 // History contains relevant DBG_VALUE instructions for Var and instructions
885 // clobbering it.
886 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
887 if (History.empty())
888 continue;
889 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +0000890
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000891 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000892 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +0000893 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
894 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000895 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +0000896 else {
897 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +0000898 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +0000899 else {
900 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +0000901 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +0000902 else
Devang Patelbf47fdb2011-08-10 20:55:27 +0000903 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +0000904 }
905 }
Devang Patelee432862010-05-20 19:57:06 +0000906 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +0000907 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +0000908 continue;
909
910 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000911 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +0000912 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
913 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +0000914 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000915 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000916 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000917 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000918
919 // Simple ranges that are fully coalesced.
920 if (History.size() <= 1 || (History.size() == 2 &&
921 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +0000922 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +0000923 continue;
924 }
925
926 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000927 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000928
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000929 for (SmallVectorImpl<const MachineInstr*>::const_iterator
930 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
931 const MachineInstr *Begin = *HI;
932 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +0000933
Devang Patel4ada1d72011-06-01 23:00:17 +0000934 // Check if DBG_VALUE is truncating a range.
935 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
936 && !Begin->getOperand(0).getReg())
937 continue;
938
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000939 // Compute the range for a register location.
940 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
941 const MCSymbol *SLabel = 0;
942
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000943 if (HI + 1 == HE)
944 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000945 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000946 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000947 else {
948 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +0000949 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
950 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000951 if (End->isDebugValue())
952 SLabel = getLabelBeforeInsn(End);
953 else {
954 // End is a normal instruction clobbering the range.
955 SLabel = getLabelAfterInsn(End);
956 assert(SLabel && "Forgot label after clobber instruction");
957 ++HI;
958 }
959 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000960
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000961 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel90b40412011-07-08 17:09:57 +0000962 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +0000963 }
964 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +0000965 }
Devang Patel98e1cac2010-05-14 21:01:35 +0000966
967 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +0000968 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
969 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
970 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
971 DIVariable DV(Variables.getElement(i));
972 if (!DV || !DV.Verify() || !Processed.insert(DV))
973 continue;
974 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
975 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +0000976 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000977}
Devang Patel98e1cac2010-05-14 21:01:35 +0000978
Devang Patelc3f5f782010-05-25 23:40:22 +0000979/// getLabelBeforeInsn - Return Label preceding the instruction.
980const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000981 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
982 assert(Label && "Didn't insert label before instruction");
983 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +0000984}
985
986/// getLabelAfterInsn - Return Label immediately following the instruction.
987const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000988 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +0000989}
990
Devang Patelcbbe2872010-10-26 17:49:02 +0000991/// beginInstruction - Process beginning of an instruction.
992void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +0000993 // Check if source location changes, but ignore DBG_VALUE locations.
994 if (!MI->isDebugValue()) {
995 DebugLoc DL = MI->getDebugLoc();
996 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel4243e672011-05-11 19:22:19 +0000997 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +0000998 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +0000999 if (DL == PrologEndLoc) {
1000 Flags |= DWARF2_FLAG_PROLOGUE_END;
1001 PrologEndLoc = DebugLoc();
1002 }
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001003 if (!DL.isUnknown()) {
1004 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001005 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001006 } else
Devang Patel4243e672011-05-11 19:22:19 +00001007 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001008 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001009 }
Devang Patelaead63c2010-03-29 22:59:58 +00001010
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001011 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001012 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1013 LabelsBeforeInsn.find(MI);
1014
1015 // No label needed.
1016 if (I == LabelsBeforeInsn.end())
1017 return;
1018
1019 // Label already assigned.
1020 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001021 return;
Devang Patel553881b2010-03-29 17:20:31 +00001022
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001023 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001024 PrevLabel = MMI->getContext().CreateTempSymbol();
1025 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001026 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001027 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001028}
1029
Devang Patelcbbe2872010-10-26 17:49:02 +00001030/// endInstruction - Process end of an instruction.
1031void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001032 // Don't create a new label after DBG_VALUE instructions.
1033 // They don't generate code.
1034 if (!MI->isDebugValue())
1035 PrevLabel = 0;
1036
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001037 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1038 LabelsAfterInsn.find(MI);
1039
1040 // No label needed.
1041 if (I == LabelsAfterInsn.end())
1042 return;
1043
1044 // Label already assigned.
1045 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001046 return;
1047
1048 // We need a label after this instruction.
1049 if (!PrevLabel) {
1050 PrevLabel = MMI->getContext().CreateTempSymbol();
1051 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001052 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001053 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001054}
1055
Jim Grosbach1e20b962010-07-21 21:21:52 +00001056/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001057/// Each LexicalScope has first instruction and last instruction to mark
1058/// beginning and end of a scope respectively. Create an inverse map that list
1059/// scopes starts (and ends) with an instruction. One instruction may start (or
1060/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001061void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001062 SmallVector<LexicalScope *, 4> WorkList;
1063 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001064 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001065 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001066
Devang Patelbf47fdb2011-08-10 20:55:27 +00001067 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001068 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001069 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001070 SE = Children.end(); SI != SE; ++SI)
1071 WorkList.push_back(*SI);
1072
Devang Patel53bb5c92009-11-10 23:06:00 +00001073 if (S->isAbstractScope())
1074 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001075
Devang Patelbf47fdb2011-08-10 20:55:27 +00001076 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001077 if (Ranges.empty())
1078 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001079 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001080 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001081 assert(RI->first && "InsnRange does not have first instruction!");
1082 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001083 requestLabelBeforeInsn(RI->first);
1084 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001085 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001086 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001087}
1088
Devang Patela3f48672011-05-09 22:14:49 +00001089/// getScopeNode - Get MDNode for DebugLoc's scope.
1090static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1091 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1092 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1093 return DL.getScope(Ctx);
1094}
1095
Devang Patel4243e672011-05-11 19:22:19 +00001096/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1097/// line number info for the function.
1098static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1099 const MDNode *Scope = getScopeNode(DL, Ctx);
1100 DISubprogram SP = getDISubprogram(Scope);
1101 if (SP.Verify())
1102 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1103 return DebugLoc();
1104}
1105
Devang Patel2c4ceb12009-11-21 02:48:08 +00001106/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001107/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001108void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001109 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001110 LScopes.initialize(*MF);
1111 if (LScopes.empty()) return;
1112 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001113
Devang Pateleac9c072010-04-27 19:46:33 +00001114 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1115 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001116 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001117 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001118
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001119 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1120
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001121 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001122 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1123 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1124
Devang Patelb2b31a62010-05-26 19:37:24 +00001125 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001126 I != E; ++I) {
1127 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001128 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1129 II != IE; ++II) {
1130 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001131
Devang Patelb2b31a62010-05-26 19:37:24 +00001132 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001133 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001134
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001135 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001136 const MDNode *Var =
1137 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001138
1139 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001140 if (isDbgValueInDefinedReg(MI))
1141 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1142
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001143 // Check the history of this variable.
1144 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1145 if (History.empty()) {
1146 UserVariables.push_back(Var);
1147 // The first mention of a function argument gets the FunctionBeginSym
1148 // label, so arguments are visible when breaking at function entry.
1149 DIVariable DV(Var);
1150 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1151 DISubprogram(getDISubprogram(DV.getContext()))
1152 .describes(MF->getFunction()))
1153 LabelsBeforeInsn[MI] = FunctionBeginSym;
1154 } else {
1155 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1156 const MachineInstr *Prev = History.back();
1157 if (Prev->isDebugValue()) {
1158 // Coalesce identical entries at the end of History.
1159 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001160 Prev->isIdenticalTo(History[History.size() - 2])) {
1161 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1162 << "\t" << *Prev
1163 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001164 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001165 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001166
1167 // Terminate old register assignments that don't reach MI;
1168 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1169 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1170 isDbgValueInDefinedReg(Prev)) {
1171 // Previous register assignment needs to terminate at the end of
1172 // its basic block.
1173 MachineBasicBlock::const_iterator LastMI =
1174 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001175 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001176 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001177 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1178 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001179 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001180 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001181 else {
1182 // Terminate after LastMI.
1183 History.push_back(LastMI);
1184 }
1185 }
1186 }
1187 }
1188 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001189 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001190 // Not a DBG_VALUE instruction.
1191 if (!MI->isLabel())
1192 AtBlockEntry = false;
1193
Devang Patel4243e672011-05-11 19:22:19 +00001194 // First known non DBG_VALUE location marks beginning of function
1195 // body.
1196 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1197 PrologEndLoc = MI->getDebugLoc();
1198
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001199 // Check if the instruction clobbers any registers with debug vars.
1200 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1201 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1202 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1203 continue;
1204 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1205 unsigned Reg = *AI; ++AI) {
1206 const MDNode *Var = LiveUserVar[Reg];
1207 if (!Var)
1208 continue;
1209 // Reg is now clobbered.
1210 LiveUserVar[Reg] = 0;
1211
1212 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001213 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1214 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001215 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001216 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1217 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001218 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001219 const MachineInstr *Prev = History.back();
1220 // Sanity-check: Register assignments are terminated at the end of
1221 // their block.
1222 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1223 continue;
1224 // Is the variable still in Reg?
1225 if (!isDbgValueInDefinedReg(Prev) ||
1226 Prev->getOperand(0).getReg() != Reg)
1227 continue;
1228 // Var is clobbered. Make sure the next instruction gets a label.
1229 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001230 }
1231 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001232 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001233 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001234 }
1235
1236 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1237 I != E; ++I) {
1238 SmallVectorImpl<const MachineInstr*> &History = I->second;
1239 if (History.empty())
1240 continue;
1241
1242 // Make sure the final register assignments are terminated.
1243 const MachineInstr *Prev = History.back();
1244 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1245 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001246 MachineBasicBlock::const_iterator LastMI =
1247 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001248 if (LastMI == PrevMBB->end())
1249 // Drop DBG_VALUE for empty range.
1250 History.pop_back();
1251 else {
1252 // Terminate after LastMI.
1253 History.push_back(LastMI);
1254 }
1255 }
1256 // Request labels for the full history.
1257 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1258 const MachineInstr *MI = History[i];
1259 if (MI->isDebugValue())
1260 requestLabelBeforeInsn(MI);
1261 else
1262 requestLabelAfterInsn(MI);
1263 }
1264 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001265
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001266 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001267 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001268
1269 // Record beginning of function.
1270 if (!PrologEndLoc.isUnknown()) {
1271 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1272 MF->getFunction()->getContext());
1273 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1274 FnStartDL.getScope(MF->getFunction()->getContext()),
1275 DWARF2_FLAG_IS_STMT);
1276 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001277}
1278
Devang Patelbf47fdb2011-08-10 20:55:27 +00001279void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1280// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1281 ScopeVariables[LS].push_back(Var);
1282// Vars.push_back(Var);
1283}
1284
Devang Patel2c4ceb12009-11-21 02:48:08 +00001285/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001286///
Chris Lattnereec791a2010-01-26 23:18:02 +00001287void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001288 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001289
Devang Patelbf47fdb2011-08-10 20:55:27 +00001290 // Define end label for subprogram.
1291 FunctionEndSym = Asm->GetTempSymbol("func_end",
1292 Asm->getFunctionNumber());
1293 // Assumes in correct section after the entry point.
1294 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1295
1296 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1297 collectVariableInfo(MF, ProcessedVars);
1298
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001299 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001300 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001301 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001302
Devang Patelbf47fdb2011-08-10 20:55:27 +00001303 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001304 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1305 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1306 LexicalScope *AScope = AList[i];
1307 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001308 if (SP.Verify()) {
1309 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001310 DIArray Variables = SP.getVariables();
1311 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1312 DIVariable DV(Variables.getElement(i));
1313 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1314 continue;
1315 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1316 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001317 }
1318 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001319 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001320 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001321 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001322
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001323 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001324
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001325 if (!DisableFramePointerElim(*MF))
Devang Patel5bc942c2011-08-10 23:58:09 +00001326 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1327 dwarf::DW_FORM_flag, 1);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001328
Devang Patelbf47fdb2011-08-10 20:55:27 +00001329 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1330 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001331
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001332 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001333 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1334 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1335 DeleteContainerPointers(I->second);
1336 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001337 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001338 UserVariables.clear();
1339 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001340 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001341 LabelsBeforeInsn.clear();
1342 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001343 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001344}
1345
Chris Lattnerc6087842010-03-09 04:54:43 +00001346/// recordSourceLine - Register a source line with debug info. Returns the
1347/// unique label that was emitted and which provides correspondence to
1348/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001349void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1350 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001351 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001352 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001353 unsigned Src = 1;
1354 if (S) {
1355 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001356
Dan Gohman1cc0d622010-05-05 23:41:32 +00001357 if (Scope.isCompileUnit()) {
1358 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001359 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001360 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001361 } else if (Scope.isFile()) {
1362 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001363 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001364 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001365 } else if (Scope.isSubprogram()) {
1366 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001367 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001368 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001369 } else if (Scope.isLexicalBlockFile()) {
1370 DILexicalBlockFile DBF(S);
1371 Fn = DBF.getFilename();
1372 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001373 } else if (Scope.isLexicalBlock()) {
1374 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001375 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001376 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001377 } else
1378 assert(0 && "Unexpected scope info");
1379
Devang Patel23670e52011-03-24 20:30:50 +00001380 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001381 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001382 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001383}
1384
Bill Wendling829e67b2009-05-20 23:22:40 +00001385//===----------------------------------------------------------------------===//
1386// Emit Methods
1387//===----------------------------------------------------------------------===//
1388
Devang Patel2c4ceb12009-11-21 02:48:08 +00001389/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001390///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001391unsigned
1392DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001393 // Get the children.
1394 const std::vector<DIE *> &Children = Die->getChildren();
1395
1396 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001397 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00001398 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00001399
1400 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001401 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001402
1403 // Get the abbreviation for this DIE.
1404 unsigned AbbrevNumber = Die->getAbbrevNumber();
1405 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1406
1407 // Set DIE offset
1408 Die->setOffset(Offset);
1409
1410 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001411 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001412
1413 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1414 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1415
1416 // Size the DIE attribute values.
1417 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1418 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001419 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001420
1421 // Size the DIE children if any.
1422 if (!Children.empty()) {
1423 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1424 "Children flag not set");
1425
1426 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001427 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001428
1429 // End of children marker.
1430 Offset += sizeof(int8_t);
1431 }
1432
1433 Die->setSize(Offset - Die->getOffset());
1434 return Offset;
1435}
1436
Devang Patel2c4ceb12009-11-21 02:48:08 +00001437/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001438///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001439void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001440 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1441 E = CUMap.end(); I != E; ++I) {
1442 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001443 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001444 sizeof(int32_t) + // Length of Compilation Unit Info
1445 sizeof(int16_t) + // DWARF version number
1446 sizeof(int32_t) + // Offset Into Abbrev. Section
1447 sizeof(int8_t); // Pointer Size (in bytes)
1448 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001449 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001450}
1451
Chris Lattner11b8f302010-04-04 23:02:02 +00001452/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1453/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00001454static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00001455 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00001456 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00001457 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001458
Chris Lattner9c69e285532010-04-04 22:59:04 +00001459 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1460 Asm->OutStreamer.EmitLabel(TmpSym);
1461 return TmpSym;
1462}
1463
1464/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1465/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001466void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001467 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001468
Bill Wendling94d04b82009-05-20 23:21:38 +00001469 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001470 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001471 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001472 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001473 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001474 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001475
Chris Lattner9c69e285532010-04-04 22:59:04 +00001476 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001477 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001478
Devang Patelaf608bd2010-08-24 00:06:12 +00001479 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001480 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1481 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1482 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001483 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001484 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001485 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1486 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001487
Devang Patelc3f5f782010-05-25 23:40:22 +00001488 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1489 "section_debug_loc");
1490
Chris Lattner9c69e285532010-04-04 22:59:04 +00001491 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001492 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001493}
1494
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001495/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001496///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001497void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001498 // Get the abbreviation for this DIE.
1499 unsigned AbbrevNumber = Die->getAbbrevNumber();
1500 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1501
Bill Wendling94d04b82009-05-20 23:21:38 +00001502 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001503 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001504 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1505 Twine::utohexstr(Die->getOffset()) + ":0x" +
1506 Twine::utohexstr(Die->getSize()) + " " +
1507 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001508 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001509
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001510 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001511 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1512
1513 // Emit the DIE attribute values.
1514 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1515 unsigned Attr = AbbrevData[i].getAttribute();
1516 unsigned Form = AbbrevData[i].getForm();
1517 assert(Form && "Too many attributes for DIE (check abbreviation)");
1518
Chris Lattner3f53c832010-04-04 18:52:31 +00001519 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001520 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001521
Bill Wendling94d04b82009-05-20 23:21:38 +00001522 switch (Attr) {
1523 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00001524 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00001525 break;
1526 case dwarf::DW_AT_abstract_origin: {
1527 DIEEntry *E = cast<DIEEntry>(Values[i]);
1528 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001529 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001530 Asm->EmitInt32(Addr);
1531 break;
1532 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001533 case dwarf::DW_AT_ranges: {
1534 // DW_AT_range Value encodes offset in debug_range section.
1535 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001536
1537 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1538 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1539 V->getValue(),
1540 4);
1541 } else {
1542 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1543 V->getValue(),
1544 DwarfDebugRangeSectionSym,
1545 4);
1546 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001547 break;
1548 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001549 case dwarf::DW_AT_location: {
Devang Patel7a328272011-08-15 21:43:21 +00001550 if (DIELabel *L = dyn_cast<DIELabel>(Values[i]))
Daniel Dunbar83320a02011-03-16 22:16:39 +00001551 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patel7a328272011-08-15 21:43:21 +00001552 else
Devang Patelc3f5f782010-05-25 23:40:22 +00001553 Values[i]->EmitValue(Asm, Form);
1554 break;
1555 }
Devang Patel2a361602010-09-29 19:08:08 +00001556 case dwarf::DW_AT_accessibility: {
1557 if (Asm->isVerbose()) {
1558 DIEInteger *V = cast<DIEInteger>(Values[i]);
1559 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1560 }
1561 Values[i]->EmitValue(Asm, Form);
1562 break;
1563 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001564 default:
1565 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001566 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001567 break;
1568 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001569 }
1570
1571 // Emit the DIE children if any.
1572 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1573 const std::vector<DIE *> &Children = Die->getChildren();
1574
1575 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001576 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001577
Chris Lattner3f53c832010-04-04 18:52:31 +00001578 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001579 Asm->OutStreamer.AddComment("End Of Children Mark");
1580 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001581 }
1582}
1583
Devang Patel8a241142009-12-09 18:24:21 +00001584/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001585///
Devang Patel8a241142009-12-09 18:24:21 +00001586void DwarfDebug::emitDebugInfo() {
1587 // Start debug info section.
1588 Asm->OutStreamer.SwitchSection(
1589 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001590 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1591 E = CUMap.end(); I != E; ++I) {
1592 CompileUnit *TheCU = I->second;
1593 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001594
Devang Patel163a9f72010-05-10 22:49:55 +00001595 // Emit the compile units header.
1596 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1597 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001598
Devang Patel163a9f72010-05-10 22:49:55 +00001599 // Emit size of content not including length itself
1600 unsigned ContentSize = Die->getSize() +
1601 sizeof(int16_t) + // DWARF version number
1602 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001603 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001604
Devang Patel163a9f72010-05-10 22:49:55 +00001605 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1606 Asm->EmitInt32(ContentSize);
1607 Asm->OutStreamer.AddComment("DWARF version number");
1608 Asm->EmitInt16(dwarf::DWARF_VERSION);
1609 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1610 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1611 DwarfAbbrevSectionSym);
1612 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1613 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001614
Devang Patel163a9f72010-05-10 22:49:55 +00001615 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001616 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1617 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001618}
1619
Devang Patel2c4ceb12009-11-21 02:48:08 +00001620/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001621///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001622void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001623 // Check to see if it is worth the effort.
1624 if (!Abbreviations.empty()) {
1625 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001626 Asm->OutStreamer.SwitchSection(
1627 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001628
Chris Lattnerc0215722010-04-04 19:25:43 +00001629 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001630
1631 // For each abbrevation.
1632 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1633 // Get abbreviation data
1634 const DIEAbbrev *Abbrev = Abbreviations[i];
1635
1636 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001637 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001638
1639 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001640 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001641 }
1642
1643 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001644 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001645
Chris Lattnerc0215722010-04-04 19:25:43 +00001646 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001647 }
1648}
1649
Devang Patel2c4ceb12009-11-21 02:48:08 +00001650/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001651/// the line matrix.
1652///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001653void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001654 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001655 Asm->OutStreamer.AddComment("Extended Op");
1656 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001657
Chris Lattner233f52b2010-03-09 23:52:58 +00001658 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001659 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001660 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1661 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1662
1663 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001664
Chris Lattnerc0215722010-04-04 19:25:43 +00001665 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001666 Asm->getTargetData().getPointerSize(),
1667 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001668
1669 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001670 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1671 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001672 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001673 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001674}
1675
Devang Patel8a241142009-12-09 18:24:21 +00001676/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1677///
1678void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00001679 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1680 E = CUMap.end(); I != E; ++I) {
1681 CompileUnit *TheCU = I->second;
1682 // Start the dwarf pubnames section.
1683 Asm->OutStreamer.SwitchSection(
1684 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001685
Devang Patel163a9f72010-05-10 22:49:55 +00001686 Asm->OutStreamer.AddComment("Length of Public Names Info");
1687 Asm->EmitLabelDifference(
1688 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1689 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001690
Devang Patel163a9f72010-05-10 22:49:55 +00001691 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1692 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001693
Devang Patel163a9f72010-05-10 22:49:55 +00001694 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001695 Asm->EmitInt16(dwarf::DWARF_VERSION);
1696
Devang Patel163a9f72010-05-10 22:49:55 +00001697 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001698 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00001699 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001700
Devang Patel163a9f72010-05-10 22:49:55 +00001701 Asm->OutStreamer.AddComment("Compilation Unit Length");
1702 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1703 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1704 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001705
Devang Patel163a9f72010-05-10 22:49:55 +00001706 const StringMap<DIE*> &Globals = TheCU->getGlobals();
1707 for (StringMap<DIE*>::const_iterator
1708 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1709 const char *Name = GI->getKeyData();
1710 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001711
Devang Patel163a9f72010-05-10 22:49:55 +00001712 Asm->OutStreamer.AddComment("DIE offset");
1713 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001714
Devang Patel163a9f72010-05-10 22:49:55 +00001715 if (Asm->isVerbose())
1716 Asm->OutStreamer.AddComment("External Name");
1717 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
1718 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001719
Devang Patel163a9f72010-05-10 22:49:55 +00001720 Asm->OutStreamer.AddComment("End Mark");
1721 Asm->EmitInt32(0);
1722 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001723 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00001724 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001725}
1726
Devang Patel193f7202009-11-24 01:14:22 +00001727void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001728 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1729 E = CUMap.end(); I != E; ++I) {
1730 CompileUnit *TheCU = I->second;
1731 // Start the dwarf pubnames section.
1732 Asm->OutStreamer.SwitchSection(
1733 Asm->getObjFileLowering().getDwarfPubTypesSection());
1734 Asm->OutStreamer.AddComment("Length of Public Types Info");
1735 Asm->EmitLabelDifference(
1736 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1737 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001738
Devang Patel163a9f72010-05-10 22:49:55 +00001739 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1740 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001741
Devang Patel163a9f72010-05-10 22:49:55 +00001742 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1743 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001744
Devang Patel163a9f72010-05-10 22:49:55 +00001745 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1746 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1747 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001748
Devang Patel163a9f72010-05-10 22:49:55 +00001749 Asm->OutStreamer.AddComment("Compilation Unit Length");
1750 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1751 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1752 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001753
Devang Patel163a9f72010-05-10 22:49:55 +00001754 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1755 for (StringMap<DIE*>::const_iterator
1756 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1757 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001758 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001759
Devang Patel163a9f72010-05-10 22:49:55 +00001760 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1761 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001762
Devang Patel163a9f72010-05-10 22:49:55 +00001763 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
1764 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1765 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001766
Devang Patel163a9f72010-05-10 22:49:55 +00001767 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001768 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001769 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1770 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001771 }
Devang Patel193f7202009-11-24 01:14:22 +00001772}
1773
Devang Patel2c4ceb12009-11-21 02:48:08 +00001774/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001775///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001776void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001777 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001778 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001779
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001780 // Start the dwarf str section.
1781 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001782 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001783
Chris Lattnerbc733f52010-03-13 02:17:42 +00001784 // Get all of the string pool entries and put them in an array by their ID so
1785 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001786 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00001787 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001788
Chris Lattnerbc733f52010-03-13 02:17:42 +00001789 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1790 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1791 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001792
Chris Lattnerbc733f52010-03-13 02:17:42 +00001793 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001794
Chris Lattnerbc733f52010-03-13 02:17:42 +00001795 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001796 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001797 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001798
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001799 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001800 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Nick Lewycky390c40d2011-10-27 06:44:11 +00001801 Asm->OutStreamer.EmitZeros(1, 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001802 }
1803}
1804
Devang Patel2c4ceb12009-11-21 02:48:08 +00001805/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001806///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001807void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00001808 if (DotDebugLocEntries.empty())
1809 return;
1810
Devang Patel6c3ea902011-02-04 22:57:18 +00001811 for (SmallVector<DotDebugLocEntry, 4>::iterator
1812 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
1813 I != E; ++I) {
1814 DotDebugLocEntry &Entry = *I;
1815 if (I + 1 != DotDebugLocEntries.end())
1816 Entry.Merge(I+1);
1817 }
1818
Daniel Dunbar83320a02011-03-16 22:16:39 +00001819 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001820 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00001821 Asm->getObjFileLowering().getDwarfLocSection());
1822 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00001823 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
1824 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001825 for (SmallVector<DotDebugLocEntry, 4>::iterator
1826 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00001827 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00001828 DotDebugLocEntry &Entry = *I;
1829 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00001830 if (Entry.isEmpty()) {
1831 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
1832 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00001833 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00001834 } else {
1835 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
1836 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00001837 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00001838 Asm->OutStreamer.AddComment("Loc expr size");
1839 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
1840 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
1841 Asm->EmitLabelDifference(end, begin, 2);
1842 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00001843 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00001844 DIBasicType BTy(DV.getType());
1845 if (BTy.Verify() &&
1846 (BTy.getEncoding() == dwarf::DW_ATE_signed
1847 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
1848 Asm->OutStreamer.AddComment("DW_OP_consts");
1849 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00001850 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00001851 } else {
1852 Asm->OutStreamer.AddComment("DW_OP_constu");
1853 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00001854 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00001855 }
Devang Patel80efd4e2011-07-08 16:49:43 +00001856 } else if (Entry.isLocation()) {
1857 if (!DV.hasComplexAddress())
1858 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00001859 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00001860 else {
1861 // Complex address entry.
1862 unsigned N = DV.getNumAddrElements();
1863 unsigned i = 0;
1864 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
1865 if (Entry.Loc.getOffset()) {
1866 i = 2;
1867 Asm->EmitDwarfRegOp(Entry.Loc);
1868 Asm->OutStreamer.AddComment("DW_OP_deref");
1869 Asm->EmitInt8(dwarf::DW_OP_deref);
1870 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
1871 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1872 Asm->EmitSLEB128(DV.getAddrElement(1));
1873 } else {
1874 // If first address element is OpPlus then emit
1875 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
1876 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
1877 Asm->EmitDwarfRegOp(Loc);
1878 i = 2;
1879 }
1880 } else {
1881 Asm->EmitDwarfRegOp(Entry.Loc);
1882 }
1883
1884 // Emit remaining complex address elements.
1885 for (; i < N; ++i) {
1886 uint64_t Element = DV.getAddrElement(i);
1887 if (Element == DIBuilder::OpPlus) {
1888 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1889 Asm->EmitULEB128(DV.getAddrElement(++i));
1890 } else if (Element == DIBuilder::OpDeref)
1891 Asm->EmitInt8(dwarf::DW_OP_deref);
1892 else llvm_unreachable("unknown Opcode found in complex address");
1893 }
Devang Patelc26f5442011-04-28 02:22:40 +00001894 }
Devang Patelc26f5442011-04-28 02:22:40 +00001895 }
Devang Patel80efd4e2011-07-08 16:49:43 +00001896 // else ... ignore constant fp. There is not any good way to
1897 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00001898 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00001899 }
1900 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001901}
1902
1903/// EmitDebugARanges - Emit visible names into a debug aranges section.
1904///
1905void DwarfDebug::EmitDebugARanges() {
1906 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001907 Asm->OutStreamer.SwitchSection(
1908 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001909}
1910
Devang Patel2c4ceb12009-11-21 02:48:08 +00001911/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001912///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001913void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001914 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001915 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00001916 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00001917 unsigned char Size = Asm->getTargetData().getPointerSize();
1918 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00001919 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00001920 I != E; ++I) {
1921 if (*I)
1922 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00001923 else
Devang Pateleac9c072010-04-27 19:46:33 +00001924 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00001925 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001926}
1927
Devang Patel2c4ceb12009-11-21 02:48:08 +00001928/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001929///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001930void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001931 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00001932 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001933 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001934 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001935 }
1936}
1937
Devang Patel2c4ceb12009-11-21 02:48:08 +00001938/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00001939/// Section Header:
1940/// 1. length of section
1941/// 2. Dwarf version number
1942/// 3. address size.
1943///
1944/// Entries (one "entry" for each function that was inlined):
1945///
1946/// 1. offset into __debug_str section for MIPS linkage name, if exists;
1947/// otherwise offset into __debug_str for regular function name.
1948/// 2. offset into __debug_str section for regular function name.
1949/// 3. an unsigned LEB128 number indicating the number of distinct inlining
1950/// instances for the function.
1951///
1952/// The rest of the entry consists of a {die_offset, low_pc} pair for each
1953/// inlined instance; the die_offset points to the inlined_subroutine die in the
1954/// __debug_info section, and the low_pc is the starting address for the
1955/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001956void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001957 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00001958 return;
1959
Devang Patel163a9f72010-05-10 22:49:55 +00001960 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00001961 return;
1962
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001963 Asm->OutStreamer.SwitchSection(
1964 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00001965
Chris Lattner233f52b2010-03-09 23:52:58 +00001966 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00001967 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
1968 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00001969
Chris Lattnerc0215722010-04-04 19:25:43 +00001970 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00001971
Chris Lattner233f52b2010-03-09 23:52:58 +00001972 Asm->OutStreamer.AddComment("Dwarf Version");
1973 Asm->EmitInt16(dwarf::DWARF_VERSION);
1974 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001975 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00001976
Devang Patele9f8f5e2010-05-07 20:54:48 +00001977 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00001978 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001979
Devang Patele9f8f5e2010-05-07 20:54:48 +00001980 const MDNode *Node = *I;
1981 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001982 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00001983 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00001984 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00001985 StringRef LName = SP.getLinkageName();
1986 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00001987
Chris Lattner233f52b2010-03-09 23:52:58 +00001988 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00001989 if (LName.empty()) {
1990 Asm->OutStreamer.EmitBytes(Name, 0);
1991 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001992 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00001993 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
1994 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00001995
Chris Lattner233f52b2010-03-09 23:52:58 +00001996 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00001997 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001998 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00001999
Devang Patel53bb5c92009-11-10 23:06:00 +00002000 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002001 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002002 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002003 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002004
Chris Lattner3f53c832010-04-04 18:52:31 +00002005 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002006 Asm->OutStreamer.EmitSymbolValue(LI->first,
2007 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002008 }
2009 }
2010
Chris Lattnerc0215722010-04-04 19:25:43 +00002011 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002012}