blob: 7ce9a06983b3570fb3cbb050b98db3be609097c6 [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
Chris Lattnerbc733f52010-03-13 02:17:42 +0000139MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
140 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
141 if (Entry.first) return Entry.first;
142
143 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000144 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000145}
146
147
Devang Patel2c4ceb12009-11-21 02:48:08 +0000148/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000149///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000150void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000151 // Profile the node so that we can make it unique.
152 FoldingSetNodeID ID;
153 Abbrev.Profile(ID);
154
155 // Check the set for priors.
156 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
157
158 // If it's newly added.
159 if (InSet == &Abbrev) {
160 // Add to abbreviation list.
161 Abbreviations.push_back(&Abbrev);
162
163 // Assign the vector position + 1 as its number.
164 Abbrev.setNumber(Abbreviations.size());
165 } else {
166 // Assign existing abbreviation number.
167 Abbrev.setNumber(InSet->getNumber());
168 }
169}
170
Jim Grosbach1e20b962010-07-21 21:21:52 +0000171/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000172/// printer to not emit usual symbol prefix before the symbol name is used then
173/// return linkage name after skipping this special LLVM prefix.
174static StringRef getRealLinkageName(StringRef LinkageName) {
175 char One = '\1';
176 if (LinkageName.startswith(StringRef(&One, 1)))
177 return LinkageName.substr(1);
178 return LinkageName;
179}
180
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000181/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000182/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
183/// If there are global variables in this scope then create and insert
184/// DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000185DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
186 const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000187 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000188
Chris Lattnerd38fee82010-04-05 00:13:49 +0000189 assert(SPDie && "Unable to find subprogram DIE!");
190 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000191
Devang Patel5e06bb82011-04-22 23:10:17 +0000192 DISubprogram SPDecl = SP.getFunctionDeclaration();
193 if (SPDecl.isSubprogram())
194 // Refer function declaration directly.
Devang Patel3cbee302011-04-12 22:53:02 +0000195 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Devang Pateldbc64af2011-08-15 17:24:54 +0000196 SPCU->getOrCreateSubprogramDIE(SPDecl));
Devang Patel5e06bb82011-04-22 23:10:17 +0000197 else {
198 // There is not any need to generate specification DIE for a function
199 // defined at compile unit level. If a function is defined inside another
200 // function then gdb prefers the definition at top level and but does not
201 // expect specification DIE in parent function. So avoid creating
202 // specification DIE for a function defined inside a function.
203 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
204 !SP.getContext().isFile() &&
205 !isSubprogramContext(SP.getContext())) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000206 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel5e06bb82011-04-22 23:10:17 +0000207
208 // Add arguments.
209 DICompositeType SPTy = SP.getType();
210 DIArray Args = SPTy.getTypeArray();
211 unsigned SPTag = SPTy.getTag();
212 if (SPTag == dwarf::DW_TAG_subroutine_type)
213 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
214 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
215 DIType ATy = DIType(DIType(Args.getElement(i)));
216 SPCU->addType(Arg, ATy);
217 if (ATy.isArtificial())
218 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
219 SPDie->addChild(Arg);
220 }
221 DIE *SPDeclDie = SPDie;
222 SPDie = new DIE(dwarf::DW_TAG_subprogram);
223 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
224 SPDeclDie);
225 SPCU->addDie(SPDie);
226 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000227 }
Devang Patel8aa61472010-07-07 22:20:57 +0000228 // Pick up abstract subprogram DIE.
229 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
230 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000231 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
232 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000233 SPCU->addDie(SPDie);
234 }
235
Devang Patel3cbee302011-04-12 22:53:02 +0000236 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
237 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
238 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
239 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000240 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
241 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000242 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000243
Chris Lattnerd38fee82010-04-05 00:13:49 +0000244 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000245}
246
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000247/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000248/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld3024342011-08-15 22:24:32 +0000249DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
250 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000251 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
252 if (Scope->isAbstractScope())
253 return ScopeDIE;
254
Devang Patelbf47fdb2011-08-10 20:55:27 +0000255 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000256 if (Ranges.empty())
257 return 0;
258
Devang Patelbf47fdb2011-08-10 20:55:27 +0000259 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000260 if (Ranges.size() > 1) {
261 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000262 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000263 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000264 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000265 DebugRangeSymbols.size()
266 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000267 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000268 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000269 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
270 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000271 }
272 DebugRangeSymbols.push_back(NULL);
273 DebugRangeSymbols.push_back(NULL);
274 return ScopeDIE;
275 }
276
Devang Patelc3f5f782010-05-25 23:40:22 +0000277 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
278 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000279
Devang Patelc3f5f782010-05-25 23:40:22 +0000280 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000281
Chris Lattnerb7db7332010-03-09 01:58:53 +0000282 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
283 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000284
Devang Patel3cbee302011-04-12 22:53:02 +0000285 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
286 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000287
288 return ScopeDIE;
289}
290
Devang Patel2c4ceb12009-11-21 02:48:08 +0000291/// constructInlinedScopeDIE - This scope represents inlined body of
292/// a function. Construct DIE to represent this concrete inlined copy
293/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000294DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
295 LexicalScope *Scope) {
Devang Patelbf47fdb2011-08-10 20:55:27 +0000296 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000297 assert(Ranges.empty() == false &&
298 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000299
Devang Patel26a92002011-07-27 00:34:13 +0000300 if (!Scope->getScopeNode())
301 return NULL;
302 DIScope DS(Scope->getScopeNode());
303 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000304 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
305 if (!OriginDIE) {
306 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
307 return NULL;
308 }
309
Devang Patelbf47fdb2011-08-10 20:55:27 +0000310 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000311 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
312 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000313
Devang Patel0afbf232010-07-08 22:39:20 +0000314 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000315 assert(0 && "Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000316 return 0;
317 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000318 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000319 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000320 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000321 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000322
Devang Pateld96efb82011-05-05 17:54:26 +0000323 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000324 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
325 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000326
Devang Patel26a92002011-07-27 00:34:13 +0000327 if (Ranges.size() > 1) {
328 // .debug_range section has not been laid out yet. Emit offset in
329 // .debug_range as a uint, size 4, for now. emitDIE will handle
330 // DW_AT_ranges appropriately.
331 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000332 DebugRangeSymbols.size()
333 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000334 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000335 RE = Ranges.end(); RI != RE; ++RI) {
336 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
337 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
338 }
339 DebugRangeSymbols.push_back(NULL);
340 DebugRangeSymbols.push_back(NULL);
341 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000342 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
343 StartLabel);
344 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
345 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000346 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000347
348 InlinedSubprogramDIEs.insert(OriginDIE);
349
350 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000351 //.debug_inlined section specification does not clearly state how
352 // to emit inlined scope that is split into multiple instruction ranges.
353 // For now, use first instruction range and emit low_pc/high_pc pair and
354 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000355 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000356 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000357
358 if (I == InlineInfo.end()) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000359 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000360 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000361 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000362 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000363
Devang Patel53bb5c92009-11-10 23:06:00 +0000364 DILocation DL(Scope->getInlinedAt());
Devang Patel3cbee302011-04-12 22:53:02 +0000365 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
366 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000367
368 return ScopeDIE;
369}
370
Devang Patel2c4ceb12009-11-21 02:48:08 +0000371/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000372DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000373 if (!Scope || !Scope->getScopeNode())
374 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000375
Nick Lewycky746cb672011-10-26 22:55:33 +0000376 SmallVector<DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000377
378 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000379 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000380 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
381 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000382 if (DIE *Arg =
383 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel0478c152011-03-01 22:58:55 +0000384 Children.push_back(Arg);
385
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000386 // Collect lexical scope children first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000387 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000388 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000389 if (DIE *Variable =
390 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000391 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000392 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000393 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000394 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000395 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000396 DIScope DS(Scope->getScopeNode());
397 DIE *ScopeDIE = NULL;
398 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000399 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000400 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000401 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000402 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000403 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000404 // Note down abstract DIE.
405 if (ScopeDIE)
406 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
407 }
Devang Patel3c91b052010-03-08 20:52:55 +0000408 else
Devang Pateld3024342011-08-15 22:24:32 +0000409 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000410 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000411 else {
412 // There is no need to emit empty lexical block DIE.
413 if (Children.empty())
414 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000415 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000416 }
417
Devang Patelaead63c2010-03-29 22:59:58 +0000418 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000419
Devang Patel5bc9fec2011-02-19 01:31:27 +0000420 // Add children
421 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
422 E = Children.end(); I != E; ++I)
423 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000424
Jim Grosbach1e20b962010-07-21 21:21:52 +0000425 if (DS.isSubprogram())
Devang Pateld3024342011-08-15 22:24:32 +0000426 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000427
Devang Patel193f7202009-11-24 01:14:22 +0000428 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000429}
430
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000431/// GetOrCreateSourceID - Look up the source id with the given directory and
432/// source file names. If none currently exists, create a new id and insert it
433/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
434/// maps as well.
Devang Patel23670e52011-03-24 20:30:50 +0000435unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
436 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000437 // If FE did not provide a file name, then assume stdin.
438 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000439 return GetOrCreateSourceID("<stdin>", StringRef());
440
Nick Lewycky44d798d2011-10-17 23:05:28 +0000441 unsigned SrcId = SourceIdMap.size()+1;
442 std::pair<std::string, std::string> SourceName =
443 std::make_pair(FileName, DirName);
444 std::pair<std::pair<std::string, std::string>, unsigned> Entry =
445 make_pair(SourceName, SrcId);
Devang Patel1905a182010-09-16 20:57:49 +0000446
Nick Lewycky44d798d2011-10-17 23:05:28 +0000447 std::map<std::pair<std::string, std::string>, unsigned>::iterator I;
448 bool NewlyInserted;
449 tie(I, NewlyInserted) = SourceIdMap.insert(Entry);
450 if (!NewlyInserted)
451 return I->second;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000452
Rafael Espindola5c055632010-11-18 02:04:25 +0000453 // Print out a .file directive to specify files for .loc directives.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000454 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.first.second,
455 Entry.first.first);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000456
457 return SrcId;
458}
459
Jim Grosbach1e20b962010-07-21 21:21:52 +0000460/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000461/// metadata node with tag DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000462CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000463 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000464 StringRef FN = DIUnit.getFilename();
465 StringRef Dir = DIUnit.getDirectory();
Devang Patel23670e52011-03-24 20:30:50 +0000466 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000467
468 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel3cbee302011-04-12 22:53:02 +0000469 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
470 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
471 DIUnit.getProducer());
472 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
473 DIUnit.getLanguage());
474 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +0000475 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
476 // simplifies debug range entries.
Devang Patel3cbee302011-04-12 22:53:02 +0000477 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000478 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000479 // compile unit in debug_line section.
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000480 if (Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindola597a7662011-05-04 17:44:06 +0000481 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000482 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000483 else
Devang Patel3cbee302011-04-12 22:53:02 +0000484 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000485
Devang Patel65dbc902009-11-25 17:36:49 +0000486 if (!Dir.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000487 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000488 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000489 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000490
Devang Patel65dbc902009-11-25 17:36:49 +0000491 StringRef Flags = DIUnit.getFlags();
492 if (!Flags.empty())
Devang Patel5bc942c2011-08-10 23:58:09 +0000493 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string,
494 Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000495
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000496 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000497 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000498 dwarf::DW_FORM_data1, RVer);
499
Devang Patel163a9f72010-05-10 22:49:55 +0000500 if (!FirstCU)
501 FirstCU = NewCU;
502 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000503 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000504}
505
Devang Patel163a9f72010-05-10 22:49:55 +0000506/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000507void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
508 const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000509 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000510 if (!SP.isDefinition())
511 // This is a method declaration which will be handled while constructing
512 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000513 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000514
Devang Pateldbc64af2011-08-15 17:24:54 +0000515 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000516
517 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000518 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000519
520 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000521 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000522
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000523 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +0000524 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +0000525
Devang Patel94c7ddb2011-08-16 22:09:43 +0000526 SPMap[N] = TheCU;
Devang Patel13e16b62009-06-26 01:49:18 +0000527 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000528}
529
Devang Patel02e603f2011-08-15 23:47:24 +0000530/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
531/// as llvm.dbg.enum and llvm.dbg.ty
532void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000533 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
534 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
535 const MDNode *N = NMD->getOperand(i);
536 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
537 constructSubprogramDIE(CU, N);
538 }
539
540 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
541 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
542 const MDNode *N = NMD->getOperand(i);
543 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000544 CU->createGlobalVariableDIE(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000545 }
546
Devang Patel02e603f2011-08-15 23:47:24 +0000547 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
548 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
549 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000550 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
551 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000552 }
553
554 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
555 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
556 DIType Ty(NMD->getOperand(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000557 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
558 CU->getOrCreateTypeDIE(Ty);
Devang Patel02e603f2011-08-15 23:47:24 +0000559 }
560}
561
562/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
563/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
564bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
565 DebugInfoFinder DbgFinder;
566 DbgFinder.processModule(*M);
567
568 bool HasDebugInfo = false;
569 // Scan all the compile-units to see if there are any marked as the main
570 // unit. If not, we do not generate debug info.
571 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
572 E = DbgFinder.compile_unit_end(); I != E; ++I) {
573 if (DICompileUnit(*I).isMain()) {
574 HasDebugInfo = true;
575 break;
576 }
577 }
578 if (!HasDebugInfo) return false;
579
580 // Create all the compile unit DIEs.
581 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
582 E = DbgFinder.compile_unit_end(); I != E; ++I)
583 constructCompileUnit(*I);
584
585 // Create DIEs for each global variable.
586 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
587 E = DbgFinder.global_variable_end(); I != E; ++I) {
588 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000589 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel28bea082011-08-18 23:17:55 +0000590 CU->createGlobalVariableDIE(N);
Devang Patel02e603f2011-08-15 23:47:24 +0000591 }
Devang Patel94c7ddb2011-08-16 22:09:43 +0000592
Devang Patel02e603f2011-08-15 23:47:24 +0000593 // Create DIEs for each subprogram.
594 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
595 E = DbgFinder.subprogram_end(); I != E; ++I) {
596 const MDNode *N = *I;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000597 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
598 constructSubprogramDIE(CU, N);
Devang Patel02e603f2011-08-15 23:47:24 +0000599 }
600
601 return HasDebugInfo;
602}
603
Devang Patel2c4ceb12009-11-21 02:48:08 +0000604/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000605/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000606/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000607void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000608 if (DisableDebugInfoPrinting)
609 return;
610
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000611 // If module has named metadata anchors then use them, otherwise scan the
612 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000613 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
614 if (CU_Nodes) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000615 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
616 DICompileUnit CUNode(CU_Nodes->getOperand(i));
617 CompileUnit *CU = constructCompileUnit(CUNode);
618 DIArray GVs = CUNode.getGlobalVariables();
619 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel28bea082011-08-18 23:17:55 +0000620 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000621 DIArray SPs = CUNode.getSubprograms();
622 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
623 constructSubprogramDIE(CU, SPs.getElement(i));
624 DIArray EnumTypes = CUNode.getEnumTypes();
625 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
626 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
627 DIArray RetainedTypes = CUNode.getRetainedTypes();
628 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
629 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
630 }
Devang Patel02e603f2011-08-15 23:47:24 +0000631 } else if (!collectLegacyDebugInfo(M))
632 return;
Devang Patel30692ab2011-05-03 16:45:22 +0000633
Devang Patel02e603f2011-08-15 23:47:24 +0000634 collectInfoFromNamedMDNodes(M);
Devang Patel30692ab2011-05-03 16:45:22 +0000635
Chris Lattnerd850ac72010-04-05 02:19:28 +0000636 // Tell MMI that we have debug info.
637 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000638
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000639 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000640 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000641
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000642 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000643 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000644}
645
Devang Patel2c4ceb12009-11-21 02:48:08 +0000646/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000647///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000648void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000649 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000650 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000651 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000652
Devang Patel94c7ddb2011-08-16 22:09:43 +0000653 // Collect info for variables that were optimized out.
654 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
655 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
656 DICompileUnit TheCU(CU_Nodes->getOperand(i));
657 DIArray Subprograms = TheCU.getSubprograms();
658 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
659 DISubprogram SP(Subprograms.getElement(i));
660 if (ProcessedSPNodes.count(SP) != 0) continue;
661 if (!SP.Verify()) continue;
662 if (!SP.isDefinition()) continue;
Devang Patel93d39be2011-08-19 23:28:12 +0000663 DIArray Variables = SP.getVariables();
664 if (Variables.getNumElements() == 0) continue;
665
Devang Patel94c7ddb2011-08-16 22:09:43 +0000666 LexicalScope *Scope =
667 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
668 DeadFnScopeMap[SP] = Scope;
669
670 // Construct subprogram DIE and add variables DIEs.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000671 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky746cb672011-10-26 22:55:33 +0000672 assert(SPCU && "Unable to find Compile Unit!");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000673 constructSubprogramDIE(SPCU, SP);
674 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel93d39be2011-08-19 23:28:12 +0000675 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
676 DIVariable DV(Variables.getElement(vi));
677 if (!DV.Verify()) continue;
678 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000679 if (DIE *VariableDIE =
Devang Patel93d39be2011-08-19 23:28:12 +0000680 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Patel94c7ddb2011-08-16 22:09:43 +0000681 ScopeDIE->addChild(VariableDIE);
682 }
Devang Patel4a1cad62010-06-28 18:25:03 +0000683 }
684 }
685 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000686
Devang Patel53bb5c92009-11-10 23:06:00 +0000687 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
688 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
689 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
690 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000691 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000692 }
693
Devang Pateldbc64af2011-08-15 17:24:54 +0000694 // Emit DW_AT_containing_type attribute to connect types with their
695 // vtable holding type.
696 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
697 CUE = CUMap.end(); CUI != CUE; ++CUI) {
698 CompileUnit *TheCU = CUI->second;
699 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000700 }
701
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000702 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000703 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000704 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000705 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000706 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000707
708 // End text sections.
709 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000710 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000711 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000712 }
713
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000714 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000715 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000716
717 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000718 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000719
720 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000721 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000722
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000723 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000724 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000725
Devang Patel193f7202009-11-24 01:14:22 +0000726 // Emit info into a debug pubtypes section.
727 emitDebugPubTypes();
728
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000729 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000730 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000731
732 // Emit info into a debug aranges section.
733 EmitDebugARanges();
734
735 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000736 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000737
738 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000739 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000740
741 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000742 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000743
Chris Lattnerbc733f52010-03-13 02:17:42 +0000744 // Emit info into a debug str section.
745 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000746
Devang Patele9a1cca2010-08-02 17:32:15 +0000747 // clean up.
748 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000749 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +0000750 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
751 E = CUMap.end(); I != E; ++I)
752 delete I->second;
753 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000754}
755
Devang Patel53bb5c92009-11-10 23:06:00 +0000756/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000757DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000758 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000759 LLVMContext &Ctx = DV->getContext();
760 // More then one inlined variable corresponds to one abstract variable.
761 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000762 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000763 if (AbsDbgVariable)
764 return AbsDbgVariable;
765
Devang Patelbf47fdb2011-08-10 20:55:27 +0000766 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000767 if (!Scope)
768 return NULL;
769
Devang Patel5a1a67c2011-08-15 19:01:20 +0000770 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000771 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000772 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000773 return AbsDbgVariable;
774}
775
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000776/// addCurrentFnArgument - If Var is a current function argument then add
777/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000778bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000779 DbgVariable *Var, LexicalScope *Scope) {
780 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000781 return false;
782 DIVariable DV = Var->getVariable();
783 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
784 return false;
785 unsigned ArgNo = DV.getArgNumber();
786 if (ArgNo == 0)
787 return false;
788
Devang Patelcb3a6572011-03-03 20:02:02 +0000789 size_t Size = CurrentFnArguments.size();
790 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000791 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000792 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000793 // arguments does the function have at source level.
794 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000795 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000796 CurrentFnArguments[ArgNo - 1] = Var;
797 return true;
798}
799
Devang Patelee432862010-05-20 19:57:06 +0000800/// collectVariableInfoFromMMITable - Collect variable information from
801/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000802void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000803DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000804 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000805 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
806 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
807 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000808 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000809 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000810 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000811 DIVariable DV(Var);
812 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000813
Devang Patelbf47fdb2011-08-10 20:55:27 +0000814 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000815
Devang Patelfb0ee432009-11-10 23:20:04 +0000816 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000817 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000818 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000819
Devang Patel26c1e562010-05-20 16:36:41 +0000820 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000821 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000822 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000823 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000824 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000825 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000826 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000827 }
Devang Patelee432862010-05-20 19:57:06 +0000828}
Devang Patel90a48ad2010-03-15 18:33:46 +0000829
Jim Grosbach1e20b962010-07-21 21:21:52 +0000830/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000831/// DBG_VALUE instruction, is in a defined reg.
832static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000833 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000834 return MI->getNumOperands() == 3 &&
835 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
836 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000837}
838
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000839/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000840/// at MI.
841static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
842 const MCSymbol *FLabel,
843 const MCSymbol *SLabel,
844 const MachineInstr *MI) {
845 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
846
847 if (MI->getNumOperands() != 3) {
848 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
849 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
850 }
851 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
852 MachineLocation MLoc;
853 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
854 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
855 }
856 if (MI->getOperand(0).isImm())
857 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
858 if (MI->getOperand(0).isFPImm())
859 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
860 if (MI->getOperand(0).isCImm())
861 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
862
Nick Lewycky746cb672011-10-26 22:55:33 +0000863 assert(0 && "Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +0000864 return DotDebugLocEntry();
865}
866
Devang Patelbf47fdb2011-08-10 20:55:27 +0000867/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000868void
Devang Patel78e127d2010-06-25 22:07:34 +0000869DwarfDebug::collectVariableInfo(const MachineFunction *MF,
870 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000871
Devang Patelee432862010-05-20 19:57:06 +0000872 /// collection info from MMI table.
873 collectVariableInfoFromMMITable(MF, Processed);
874
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000875 for (SmallVectorImpl<const MDNode*>::const_iterator
876 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
877 ++UVI) {
878 const MDNode *Var = *UVI;
879 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +0000880 continue;
881
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000882 // History contains relevant DBG_VALUE instructions for Var and instructions
883 // clobbering it.
884 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
885 if (History.empty())
886 continue;
887 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +0000888
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000889 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000890 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +0000891 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
892 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000893 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +0000894 else {
895 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +0000896 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +0000897 else {
898 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +0000899 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +0000900 else
Devang Patelbf47fdb2011-08-10 20:55:27 +0000901 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +0000902 }
903 }
Devang Patelee432862010-05-20 19:57:06 +0000904 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +0000905 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +0000906 continue;
907
908 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000909 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +0000910 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
911 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +0000912 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000913 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000914 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000915 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000916
917 // Simple ranges that are fully coalesced.
918 if (History.size() <= 1 || (History.size() == 2 &&
919 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +0000920 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +0000921 continue;
922 }
923
924 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000925 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000926
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000927 for (SmallVectorImpl<const MachineInstr*>::const_iterator
928 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
929 const MachineInstr *Begin = *HI;
930 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +0000931
Devang Patel4ada1d72011-06-01 23:00:17 +0000932 // Check if DBG_VALUE is truncating a range.
933 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
934 && !Begin->getOperand(0).getReg())
935 continue;
936
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000937 // Compute the range for a register location.
938 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
939 const MCSymbol *SLabel = 0;
940
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000941 if (HI + 1 == HE)
942 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000943 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000944 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000945 else {
946 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +0000947 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
948 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000949 if (End->isDebugValue())
950 SLabel = getLabelBeforeInsn(End);
951 else {
952 // End is a normal instruction clobbering the range.
953 SLabel = getLabelAfterInsn(End);
954 assert(SLabel && "Forgot label after clobber instruction");
955 ++HI;
956 }
957 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000958
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000959 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel90b40412011-07-08 17:09:57 +0000960 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +0000961 }
962 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +0000963 }
Devang Patel98e1cac2010-05-14 21:01:35 +0000964
965 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +0000966 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
967 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
968 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
969 DIVariable DV(Variables.getElement(i));
970 if (!DV || !DV.Verify() || !Processed.insert(DV))
971 continue;
972 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
973 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +0000974 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000975}
Devang Patel98e1cac2010-05-14 21:01:35 +0000976
Devang Patelc3f5f782010-05-25 23:40:22 +0000977/// getLabelBeforeInsn - Return Label preceding the instruction.
978const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000979 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
980 assert(Label && "Didn't insert label before instruction");
981 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +0000982}
983
984/// getLabelAfterInsn - Return Label immediately following the instruction.
985const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000986 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +0000987}
988
Devang Patelcbbe2872010-10-26 17:49:02 +0000989/// beginInstruction - Process beginning of an instruction.
990void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +0000991 // Check if source location changes, but ignore DBG_VALUE locations.
992 if (!MI->isDebugValue()) {
993 DebugLoc DL = MI->getDebugLoc();
994 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel4243e672011-05-11 19:22:19 +0000995 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +0000996 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +0000997 if (DL == PrologEndLoc) {
998 Flags |= DWARF2_FLAG_PROLOGUE_END;
999 PrologEndLoc = DebugLoc();
1000 }
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001001 if (!DL.isUnknown()) {
1002 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001003 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001004 } else
Devang Patel4243e672011-05-11 19:22:19 +00001005 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001006 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001007 }
Devang Patelaead63c2010-03-29 22:59:58 +00001008
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001009 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001010 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1011 LabelsBeforeInsn.find(MI);
1012
1013 // No label needed.
1014 if (I == LabelsBeforeInsn.end())
1015 return;
1016
1017 // Label already assigned.
1018 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001019 return;
Devang Patel553881b2010-03-29 17:20:31 +00001020
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001021 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001022 PrevLabel = MMI->getContext().CreateTempSymbol();
1023 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001024 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001025 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001026}
1027
Devang Patelcbbe2872010-10-26 17:49:02 +00001028/// endInstruction - Process end of an instruction.
1029void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001030 // Don't create a new label after DBG_VALUE instructions.
1031 // They don't generate code.
1032 if (!MI->isDebugValue())
1033 PrevLabel = 0;
1034
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001035 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1036 LabelsAfterInsn.find(MI);
1037
1038 // No label needed.
1039 if (I == LabelsAfterInsn.end())
1040 return;
1041
1042 // Label already assigned.
1043 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001044 return;
1045
1046 // We need a label after this instruction.
1047 if (!PrevLabel) {
1048 PrevLabel = MMI->getContext().CreateTempSymbol();
1049 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001050 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001051 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001052}
1053
Jim Grosbach1e20b962010-07-21 21:21:52 +00001054/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001055/// Each LexicalScope has first instruction and last instruction to mark
1056/// beginning and end of a scope respectively. Create an inverse map that list
1057/// scopes starts (and ends) with an instruction. One instruction may start (or
1058/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001059void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001060 SmallVector<LexicalScope *, 4> WorkList;
1061 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001062 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001063 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001064
Devang Patelbf47fdb2011-08-10 20:55:27 +00001065 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001066 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001067 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001068 SE = Children.end(); SI != SE; ++SI)
1069 WorkList.push_back(*SI);
1070
Devang Patel53bb5c92009-11-10 23:06:00 +00001071 if (S->isAbstractScope())
1072 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001073
Devang Patelbf47fdb2011-08-10 20:55:27 +00001074 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001075 if (Ranges.empty())
1076 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001077 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001078 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001079 assert(RI->first && "InsnRange does not have first instruction!");
1080 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001081 requestLabelBeforeInsn(RI->first);
1082 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001083 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001084 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001085}
1086
Devang Patela3f48672011-05-09 22:14:49 +00001087/// getScopeNode - Get MDNode for DebugLoc's scope.
1088static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1089 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1090 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1091 return DL.getScope(Ctx);
1092}
1093
Devang Patel4243e672011-05-11 19:22:19 +00001094/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1095/// line number info for the function.
1096static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1097 const MDNode *Scope = getScopeNode(DL, Ctx);
1098 DISubprogram SP = getDISubprogram(Scope);
1099 if (SP.Verify())
1100 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1101 return DebugLoc();
1102}
1103
Devang Patel2c4ceb12009-11-21 02:48:08 +00001104/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001105/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001106void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001107 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001108 LScopes.initialize(*MF);
1109 if (LScopes.empty()) return;
1110 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001111
Devang Pateleac9c072010-04-27 19:46:33 +00001112 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1113 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001114 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001115 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001116
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001117 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1118
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001119 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001120 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1121 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1122
Devang Patelb2b31a62010-05-26 19:37:24 +00001123 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001124 I != E; ++I) {
1125 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001126 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1127 II != IE; ++II) {
1128 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001129
Devang Patelb2b31a62010-05-26 19:37:24 +00001130 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001131 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001132
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001133 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001134 const MDNode *Var =
1135 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001136
1137 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001138 if (isDbgValueInDefinedReg(MI))
1139 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1140
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001141 // Check the history of this variable.
1142 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1143 if (History.empty()) {
1144 UserVariables.push_back(Var);
1145 // The first mention of a function argument gets the FunctionBeginSym
1146 // label, so arguments are visible when breaking at function entry.
1147 DIVariable DV(Var);
1148 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1149 DISubprogram(getDISubprogram(DV.getContext()))
1150 .describes(MF->getFunction()))
1151 LabelsBeforeInsn[MI] = FunctionBeginSym;
1152 } else {
1153 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1154 const MachineInstr *Prev = History.back();
1155 if (Prev->isDebugValue()) {
1156 // Coalesce identical entries at the end of History.
1157 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001158 Prev->isIdenticalTo(History[History.size() - 2])) {
1159 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1160 << "\t" << *Prev
1161 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001162 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001163 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001164
1165 // Terminate old register assignments that don't reach MI;
1166 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1167 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1168 isDbgValueInDefinedReg(Prev)) {
1169 // Previous register assignment needs to terminate at the end of
1170 // its basic block.
1171 MachineBasicBlock::const_iterator LastMI =
1172 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001173 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001174 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001175 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1176 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001177 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001178 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001179 else {
1180 // Terminate after LastMI.
1181 History.push_back(LastMI);
1182 }
1183 }
1184 }
1185 }
1186 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001187 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001188 // Not a DBG_VALUE instruction.
1189 if (!MI->isLabel())
1190 AtBlockEntry = false;
1191
Devang Patel4243e672011-05-11 19:22:19 +00001192 // First known non DBG_VALUE location marks beginning of function
1193 // body.
1194 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1195 PrologEndLoc = MI->getDebugLoc();
1196
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001197 // Check if the instruction clobbers any registers with debug vars.
1198 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1199 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1200 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1201 continue;
1202 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1203 unsigned Reg = *AI; ++AI) {
1204 const MDNode *Var = LiveUserVar[Reg];
1205 if (!Var)
1206 continue;
1207 // Reg is now clobbered.
1208 LiveUserVar[Reg] = 0;
1209
1210 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001211 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1212 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001213 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001214 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1215 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001216 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001217 const MachineInstr *Prev = History.back();
1218 // Sanity-check: Register assignments are terminated at the end of
1219 // their block.
1220 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1221 continue;
1222 // Is the variable still in Reg?
1223 if (!isDbgValueInDefinedReg(Prev) ||
1224 Prev->getOperand(0).getReg() != Reg)
1225 continue;
1226 // Var is clobbered. Make sure the next instruction gets a label.
1227 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001228 }
1229 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001230 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001231 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001232 }
1233
1234 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1235 I != E; ++I) {
1236 SmallVectorImpl<const MachineInstr*> &History = I->second;
1237 if (History.empty())
1238 continue;
1239
1240 // Make sure the final register assignments are terminated.
1241 const MachineInstr *Prev = History.back();
1242 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1243 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001244 MachineBasicBlock::const_iterator LastMI =
1245 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001246 if (LastMI == PrevMBB->end())
1247 // Drop DBG_VALUE for empty range.
1248 History.pop_back();
1249 else {
1250 // Terminate after LastMI.
1251 History.push_back(LastMI);
1252 }
1253 }
1254 // Request labels for the full history.
1255 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1256 const MachineInstr *MI = History[i];
1257 if (MI->isDebugValue())
1258 requestLabelBeforeInsn(MI);
1259 else
1260 requestLabelAfterInsn(MI);
1261 }
1262 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001263
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001264 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001265 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001266
1267 // Record beginning of function.
1268 if (!PrologEndLoc.isUnknown()) {
1269 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1270 MF->getFunction()->getContext());
1271 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1272 FnStartDL.getScope(MF->getFunction()->getContext()),
1273 DWARF2_FLAG_IS_STMT);
1274 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001275}
1276
Devang Patelbf47fdb2011-08-10 20:55:27 +00001277void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1278// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1279 ScopeVariables[LS].push_back(Var);
1280// Vars.push_back(Var);
1281}
1282
Devang Patel2c4ceb12009-11-21 02:48:08 +00001283/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001284///
Chris Lattnereec791a2010-01-26 23:18:02 +00001285void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001286 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001287
Devang Patelbf47fdb2011-08-10 20:55:27 +00001288 // Define end label for subprogram.
1289 FunctionEndSym = Asm->GetTempSymbol("func_end",
1290 Asm->getFunctionNumber());
1291 // Assumes in correct section after the entry point.
1292 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1293
1294 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1295 collectVariableInfo(MF, ProcessedVars);
1296
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001297 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001298 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001299 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001300
Devang Patelbf47fdb2011-08-10 20:55:27 +00001301 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001302 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1303 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1304 LexicalScope *AScope = AList[i];
1305 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001306 if (SP.Verify()) {
1307 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001308 DIArray Variables = SP.getVariables();
1309 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1310 DIVariable DV(Variables.getElement(i));
1311 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1312 continue;
1313 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1314 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001315 }
1316 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001317 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001318 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001319 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001320
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001321 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001322
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001323 if (!DisableFramePointerElim(*MF))
Devang Patel5bc942c2011-08-10 23:58:09 +00001324 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1325 dwarf::DW_FORM_flag, 1);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001326
Devang Patelbf47fdb2011-08-10 20:55:27 +00001327 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1328 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001329
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001330 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001331 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1332 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1333 DeleteContainerPointers(I->second);
1334 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001335 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001336 UserVariables.clear();
1337 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001338 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001339 LabelsBeforeInsn.clear();
1340 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001341 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001342}
1343
Chris Lattnerc6087842010-03-09 04:54:43 +00001344/// recordSourceLine - Register a source line with debug info. Returns the
1345/// unique label that was emitted and which provides correspondence to
1346/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001347void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1348 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001349 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001350 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001351 unsigned Src = 1;
1352 if (S) {
1353 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001354
Dan Gohman1cc0d622010-05-05 23:41:32 +00001355 if (Scope.isCompileUnit()) {
1356 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001357 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001358 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001359 } else if (Scope.isFile()) {
1360 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001361 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001362 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001363 } else if (Scope.isSubprogram()) {
1364 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001365 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001366 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001367 } else if (Scope.isLexicalBlockFile()) {
1368 DILexicalBlockFile DBF(S);
1369 Fn = DBF.getFilename();
1370 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001371 } else if (Scope.isLexicalBlock()) {
1372 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001373 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001374 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001375 } else
1376 assert(0 && "Unexpected scope info");
1377
Devang Patel23670e52011-03-24 20:30:50 +00001378 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001379 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001380 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001381}
1382
Bill Wendling829e67b2009-05-20 23:22:40 +00001383//===----------------------------------------------------------------------===//
1384// Emit Methods
1385//===----------------------------------------------------------------------===//
1386
Devang Patel2c4ceb12009-11-21 02:48:08 +00001387/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001388///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001389unsigned
1390DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001391 // Get the children.
1392 const std::vector<DIE *> &Children = Die->getChildren();
1393
1394 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001395 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00001396 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00001397
1398 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001399 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001400
1401 // Get the abbreviation for this DIE.
1402 unsigned AbbrevNumber = Die->getAbbrevNumber();
1403 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1404
1405 // Set DIE offset
1406 Die->setOffset(Offset);
1407
1408 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001409 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001410
1411 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1412 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1413
1414 // Size the DIE attribute values.
1415 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1416 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001417 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001418
1419 // Size the DIE children if any.
1420 if (!Children.empty()) {
1421 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1422 "Children flag not set");
1423
1424 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001425 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001426
1427 // End of children marker.
1428 Offset += sizeof(int8_t);
1429 }
1430
1431 Die->setSize(Offset - Die->getOffset());
1432 return Offset;
1433}
1434
Devang Patel2c4ceb12009-11-21 02:48:08 +00001435/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001436///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001437void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001438 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1439 E = CUMap.end(); I != E; ++I) {
1440 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001441 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001442 sizeof(int32_t) + // Length of Compilation Unit Info
1443 sizeof(int16_t) + // DWARF version number
1444 sizeof(int32_t) + // Offset Into Abbrev. Section
1445 sizeof(int8_t); // Pointer Size (in bytes)
1446 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001447 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001448}
1449
Chris Lattner11b8f302010-04-04 23:02:02 +00001450/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1451/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00001452static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00001453 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00001454 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00001455 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001456
Chris Lattner9c69e285532010-04-04 22:59:04 +00001457 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1458 Asm->OutStreamer.EmitLabel(TmpSym);
1459 return TmpSym;
1460}
1461
1462/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1463/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001464void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001465 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001466
Bill Wendling94d04b82009-05-20 23:21:38 +00001467 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001468 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001469 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001470 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001471 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001472 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001473
Chris Lattner9c69e285532010-04-04 22:59:04 +00001474 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001475 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001476
Devang Patelaf608bd2010-08-24 00:06:12 +00001477 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001478 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1479 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1480 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001481 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001482 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001483 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1484 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001485
Devang Patelc3f5f782010-05-25 23:40:22 +00001486 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1487 "section_debug_loc");
1488
Chris Lattner9c69e285532010-04-04 22:59:04 +00001489 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001490 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001491}
1492
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001493/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001494///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001495void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001496 // Get the abbreviation for this DIE.
1497 unsigned AbbrevNumber = Die->getAbbrevNumber();
1498 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1499
Bill Wendling94d04b82009-05-20 23:21:38 +00001500 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001501 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001502 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1503 Twine::utohexstr(Die->getOffset()) + ":0x" +
1504 Twine::utohexstr(Die->getSize()) + " " +
1505 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001506 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001507
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001508 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001509 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1510
1511 // Emit the DIE attribute values.
1512 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1513 unsigned Attr = AbbrevData[i].getAttribute();
1514 unsigned Form = AbbrevData[i].getForm();
1515 assert(Form && "Too many attributes for DIE (check abbreviation)");
1516
Chris Lattner3f53c832010-04-04 18:52:31 +00001517 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001518 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001519
Bill Wendling94d04b82009-05-20 23:21:38 +00001520 switch (Attr) {
1521 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00001522 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00001523 break;
1524 case dwarf::DW_AT_abstract_origin: {
1525 DIEEntry *E = cast<DIEEntry>(Values[i]);
1526 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001527 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001528 Asm->EmitInt32(Addr);
1529 break;
1530 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001531 case dwarf::DW_AT_ranges: {
1532 // DW_AT_range Value encodes offset in debug_range section.
1533 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001534
1535 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1536 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1537 V->getValue(),
1538 4);
1539 } else {
1540 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1541 V->getValue(),
1542 DwarfDebugRangeSectionSym,
1543 4);
1544 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001545 break;
1546 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001547 case dwarf::DW_AT_location: {
Devang Patel7a328272011-08-15 21:43:21 +00001548 if (DIELabel *L = dyn_cast<DIELabel>(Values[i]))
Daniel Dunbar83320a02011-03-16 22:16:39 +00001549 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patel7a328272011-08-15 21:43:21 +00001550 else
Devang Patelc3f5f782010-05-25 23:40:22 +00001551 Values[i]->EmitValue(Asm, Form);
1552 break;
1553 }
Devang Patel2a361602010-09-29 19:08:08 +00001554 case dwarf::DW_AT_accessibility: {
1555 if (Asm->isVerbose()) {
1556 DIEInteger *V = cast<DIEInteger>(Values[i]);
1557 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1558 }
1559 Values[i]->EmitValue(Asm, Form);
1560 break;
1561 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001562 default:
1563 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001564 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001565 break;
1566 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001567 }
1568
1569 // Emit the DIE children if any.
1570 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1571 const std::vector<DIE *> &Children = Die->getChildren();
1572
1573 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001574 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001575
Chris Lattner3f53c832010-04-04 18:52:31 +00001576 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001577 Asm->OutStreamer.AddComment("End Of Children Mark");
1578 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001579 }
1580}
1581
Devang Patel8a241142009-12-09 18:24:21 +00001582/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001583///
Devang Patel8a241142009-12-09 18:24:21 +00001584void DwarfDebug::emitDebugInfo() {
1585 // Start debug info section.
1586 Asm->OutStreamer.SwitchSection(
1587 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001588 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1589 E = CUMap.end(); I != E; ++I) {
1590 CompileUnit *TheCU = I->second;
1591 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001592
Devang Patel163a9f72010-05-10 22:49:55 +00001593 // Emit the compile units header.
1594 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1595 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001596
Devang Patel163a9f72010-05-10 22:49:55 +00001597 // Emit size of content not including length itself
1598 unsigned ContentSize = Die->getSize() +
1599 sizeof(int16_t) + // DWARF version number
1600 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001601 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001602
Devang Patel163a9f72010-05-10 22:49:55 +00001603 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1604 Asm->EmitInt32(ContentSize);
1605 Asm->OutStreamer.AddComment("DWARF version number");
1606 Asm->EmitInt16(dwarf::DWARF_VERSION);
1607 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1608 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1609 DwarfAbbrevSectionSym);
1610 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1611 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001612
Devang Patel163a9f72010-05-10 22:49:55 +00001613 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001614 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1615 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001616}
1617
Devang Patel2c4ceb12009-11-21 02:48:08 +00001618/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001619///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001620void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001621 // Check to see if it is worth the effort.
1622 if (!Abbreviations.empty()) {
1623 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001624 Asm->OutStreamer.SwitchSection(
1625 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001626
Chris Lattnerc0215722010-04-04 19:25:43 +00001627 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001628
1629 // For each abbrevation.
1630 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1631 // Get abbreviation data
1632 const DIEAbbrev *Abbrev = Abbreviations[i];
1633
1634 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001635 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001636
1637 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001638 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001639 }
1640
1641 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001642 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001643
Chris Lattnerc0215722010-04-04 19:25:43 +00001644 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001645 }
1646}
1647
Devang Patel2c4ceb12009-11-21 02:48:08 +00001648/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001649/// the line matrix.
1650///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001651void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001652 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001653 Asm->OutStreamer.AddComment("Extended Op");
1654 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001655
Chris Lattner233f52b2010-03-09 23:52:58 +00001656 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001657 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001658 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1659 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1660
1661 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001662
Chris Lattnerc0215722010-04-04 19:25:43 +00001663 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001664 Asm->getTargetData().getPointerSize(),
1665 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001666
1667 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001668 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1669 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001670 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001671 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001672}
1673
Devang Patel8a241142009-12-09 18:24:21 +00001674/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1675///
1676void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00001677 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1678 E = CUMap.end(); I != E; ++I) {
1679 CompileUnit *TheCU = I->second;
1680 // Start the dwarf pubnames section.
1681 Asm->OutStreamer.SwitchSection(
1682 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001683
Devang Patel163a9f72010-05-10 22:49:55 +00001684 Asm->OutStreamer.AddComment("Length of Public Names Info");
1685 Asm->EmitLabelDifference(
1686 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1687 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001688
Devang Patel163a9f72010-05-10 22:49:55 +00001689 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1690 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001691
Devang Patel163a9f72010-05-10 22:49:55 +00001692 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001693 Asm->EmitInt16(dwarf::DWARF_VERSION);
1694
Devang Patel163a9f72010-05-10 22:49:55 +00001695 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001696 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00001697 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001698
Devang Patel163a9f72010-05-10 22:49:55 +00001699 Asm->OutStreamer.AddComment("Compilation Unit Length");
1700 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1701 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1702 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001703
Devang Patel163a9f72010-05-10 22:49:55 +00001704 const StringMap<DIE*> &Globals = TheCU->getGlobals();
1705 for (StringMap<DIE*>::const_iterator
1706 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1707 const char *Name = GI->getKeyData();
1708 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001709
Devang Patel163a9f72010-05-10 22:49:55 +00001710 Asm->OutStreamer.AddComment("DIE offset");
1711 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001712
Devang Patel163a9f72010-05-10 22:49:55 +00001713 if (Asm->isVerbose())
1714 Asm->OutStreamer.AddComment("External Name");
1715 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
1716 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001717
Devang Patel163a9f72010-05-10 22:49:55 +00001718 Asm->OutStreamer.AddComment("End Mark");
1719 Asm->EmitInt32(0);
1720 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001721 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00001722 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001723}
1724
Devang Patel193f7202009-11-24 01:14:22 +00001725void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001726 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1727 E = CUMap.end(); I != E; ++I) {
1728 CompileUnit *TheCU = I->second;
1729 // Start the dwarf pubnames section.
1730 Asm->OutStreamer.SwitchSection(
1731 Asm->getObjFileLowering().getDwarfPubTypesSection());
1732 Asm->OutStreamer.AddComment("Length of Public Types Info");
1733 Asm->EmitLabelDifference(
1734 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1735 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001736
Devang Patel163a9f72010-05-10 22:49:55 +00001737 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1738 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001739
Devang Patel163a9f72010-05-10 22:49:55 +00001740 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1741 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001742
Devang Patel163a9f72010-05-10 22:49:55 +00001743 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1744 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1745 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001746
Devang Patel163a9f72010-05-10 22:49:55 +00001747 Asm->OutStreamer.AddComment("Compilation Unit Length");
1748 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1749 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1750 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001751
Devang Patel163a9f72010-05-10 22:49:55 +00001752 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1753 for (StringMap<DIE*>::const_iterator
1754 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1755 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001756 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001757
Devang Patel163a9f72010-05-10 22:49:55 +00001758 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1759 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001760
Devang Patel163a9f72010-05-10 22:49:55 +00001761 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
1762 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1763 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001764
Devang Patel163a9f72010-05-10 22:49:55 +00001765 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001766 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001767 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1768 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001769 }
Devang Patel193f7202009-11-24 01:14:22 +00001770}
1771
Devang Patel2c4ceb12009-11-21 02:48:08 +00001772/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001773///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001774void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001775 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001776 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001777
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001778 // Start the dwarf str section.
1779 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001780 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001781
Chris Lattnerbc733f52010-03-13 02:17:42 +00001782 // Get all of the string pool entries and put them in an array by their ID so
1783 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001784 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00001785 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001786
Chris Lattnerbc733f52010-03-13 02:17:42 +00001787 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1788 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1789 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001790
Chris Lattnerbc733f52010-03-13 02:17:42 +00001791 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001792
Chris Lattnerbc733f52010-03-13 02:17:42 +00001793 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001794 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001795 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001796
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001797 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001798 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001799 }
1800}
1801
Devang Patel2c4ceb12009-11-21 02:48:08 +00001802/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001803///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001804void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00001805 if (DotDebugLocEntries.empty())
1806 return;
1807
Devang Patel6c3ea902011-02-04 22:57:18 +00001808 for (SmallVector<DotDebugLocEntry, 4>::iterator
1809 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
1810 I != E; ++I) {
1811 DotDebugLocEntry &Entry = *I;
1812 if (I + 1 != DotDebugLocEntries.end())
1813 Entry.Merge(I+1);
1814 }
1815
Daniel Dunbar83320a02011-03-16 22:16:39 +00001816 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001817 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00001818 Asm->getObjFileLowering().getDwarfLocSection());
1819 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00001820 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
1821 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001822 for (SmallVector<DotDebugLocEntry, 4>::iterator
1823 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00001824 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00001825 DotDebugLocEntry &Entry = *I;
1826 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00001827 if (Entry.isEmpty()) {
1828 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
1829 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00001830 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00001831 } else {
1832 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
1833 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00001834 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00001835 Asm->OutStreamer.AddComment("Loc expr size");
1836 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
1837 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
1838 Asm->EmitLabelDifference(end, begin, 2);
1839 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00001840 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00001841 DIBasicType BTy(DV.getType());
1842 if (BTy.Verify() &&
1843 (BTy.getEncoding() == dwarf::DW_ATE_signed
1844 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
1845 Asm->OutStreamer.AddComment("DW_OP_consts");
1846 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00001847 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00001848 } else {
1849 Asm->OutStreamer.AddComment("DW_OP_constu");
1850 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00001851 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00001852 }
Devang Patel80efd4e2011-07-08 16:49:43 +00001853 } else if (Entry.isLocation()) {
1854 if (!DV.hasComplexAddress())
1855 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00001856 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00001857 else {
1858 // Complex address entry.
1859 unsigned N = DV.getNumAddrElements();
1860 unsigned i = 0;
1861 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
1862 if (Entry.Loc.getOffset()) {
1863 i = 2;
1864 Asm->EmitDwarfRegOp(Entry.Loc);
1865 Asm->OutStreamer.AddComment("DW_OP_deref");
1866 Asm->EmitInt8(dwarf::DW_OP_deref);
1867 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
1868 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1869 Asm->EmitSLEB128(DV.getAddrElement(1));
1870 } else {
1871 // If first address element is OpPlus then emit
1872 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
1873 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
1874 Asm->EmitDwarfRegOp(Loc);
1875 i = 2;
1876 }
1877 } else {
1878 Asm->EmitDwarfRegOp(Entry.Loc);
1879 }
1880
1881 // Emit remaining complex address elements.
1882 for (; i < N; ++i) {
1883 uint64_t Element = DV.getAddrElement(i);
1884 if (Element == DIBuilder::OpPlus) {
1885 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1886 Asm->EmitULEB128(DV.getAddrElement(++i));
1887 } else if (Element == DIBuilder::OpDeref)
1888 Asm->EmitInt8(dwarf::DW_OP_deref);
1889 else llvm_unreachable("unknown Opcode found in complex address");
1890 }
Devang Patelc26f5442011-04-28 02:22:40 +00001891 }
Devang Patelc26f5442011-04-28 02:22:40 +00001892 }
Devang Patel80efd4e2011-07-08 16:49:43 +00001893 // else ... ignore constant fp. There is not any good way to
1894 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00001895 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00001896 }
1897 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001898}
1899
1900/// EmitDebugARanges - Emit visible names into a debug aranges section.
1901///
1902void DwarfDebug::EmitDebugARanges() {
1903 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001904 Asm->OutStreamer.SwitchSection(
1905 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001906}
1907
Devang Patel2c4ceb12009-11-21 02:48:08 +00001908/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001909///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001910void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001911 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001912 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00001913 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00001914 unsigned char Size = Asm->getTargetData().getPointerSize();
1915 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00001916 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00001917 I != E; ++I) {
1918 if (*I)
1919 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00001920 else
Devang Pateleac9c072010-04-27 19:46:33 +00001921 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00001922 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001923}
1924
Devang Patel2c4ceb12009-11-21 02:48:08 +00001925/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001926///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001927void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001928 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00001929 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001930 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001931 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001932 }
1933}
1934
Devang Patel2c4ceb12009-11-21 02:48:08 +00001935/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00001936/// Section Header:
1937/// 1. length of section
1938/// 2. Dwarf version number
1939/// 3. address size.
1940///
1941/// Entries (one "entry" for each function that was inlined):
1942///
1943/// 1. offset into __debug_str section for MIPS linkage name, if exists;
1944/// otherwise offset into __debug_str for regular function name.
1945/// 2. offset into __debug_str section for regular function name.
1946/// 3. an unsigned LEB128 number indicating the number of distinct inlining
1947/// instances for the function.
1948///
1949/// The rest of the entry consists of a {die_offset, low_pc} pair for each
1950/// inlined instance; the die_offset points to the inlined_subroutine die in the
1951/// __debug_info section, and the low_pc is the starting address for the
1952/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001953void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001954 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00001955 return;
1956
Devang Patel163a9f72010-05-10 22:49:55 +00001957 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00001958 return;
1959
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001960 Asm->OutStreamer.SwitchSection(
1961 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00001962
Chris Lattner233f52b2010-03-09 23:52:58 +00001963 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00001964 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
1965 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00001966
Chris Lattnerc0215722010-04-04 19:25:43 +00001967 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00001968
Chris Lattner233f52b2010-03-09 23:52:58 +00001969 Asm->OutStreamer.AddComment("Dwarf Version");
1970 Asm->EmitInt16(dwarf::DWARF_VERSION);
1971 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001972 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00001973
Devang Patele9f8f5e2010-05-07 20:54:48 +00001974 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00001975 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001976
Devang Patele9f8f5e2010-05-07 20:54:48 +00001977 const MDNode *Node = *I;
1978 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001979 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00001980 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00001981 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00001982 StringRef LName = SP.getLinkageName();
1983 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00001984
Chris Lattner233f52b2010-03-09 23:52:58 +00001985 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00001986 if (LName.empty()) {
1987 Asm->OutStreamer.EmitBytes(Name, 0);
1988 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001989 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00001990 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
1991 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00001992
Chris Lattner233f52b2010-03-09 23:52:58 +00001993 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00001994 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001995 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00001996
Devang Patel53bb5c92009-11-10 23:06:00 +00001997 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00001998 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00001999 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002000 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002001
Chris Lattner3f53c832010-04-04 18:52:31 +00002002 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002003 Asm->OutStreamer.EmitSymbolValue(LI->first,
2004 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002005 }
2006 }
2007
Chris Lattnerc0215722010-04-04 19:25:43 +00002008 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002009}