blob: f4344eb58fdc83be1d8339b322c9b3783a3c7e18 [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())) {
206 SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
207
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
252 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
253 if (Scope->isAbstractScope())
254 return ScopeDIE;
255
Devang Patelbf47fdb2011-08-10 20:55:27 +0000256 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000257 if (Ranges.empty())
258 return 0;
259
Devang Patelbf47fdb2011-08-10 20:55:27 +0000260 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000261 if (Ranges.size() > 1) {
262 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000263 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000264 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000265 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000266 DebugRangeSymbols.size()
267 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000268 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000269 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000270 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
271 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000272 }
273 DebugRangeSymbols.push_back(NULL);
274 DebugRangeSymbols.push_back(NULL);
275 return ScopeDIE;
276 }
277
Devang Patelc3f5f782010-05-25 23:40:22 +0000278 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
279 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000280
Devang Patelc3f5f782010-05-25 23:40:22 +0000281 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000282
Chris Lattnerb7db7332010-03-09 01:58:53 +0000283 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
284 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000285
Devang Patel3cbee302011-04-12 22:53:02 +0000286 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
287 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000288
289 return ScopeDIE;
290}
291
Devang Patel2c4ceb12009-11-21 02:48:08 +0000292/// constructInlinedScopeDIE - This scope represents inlined body of
293/// a function. Construct DIE to represent this concrete inlined copy
294/// of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000295DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
296 LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000297
Devang Patelbf47fdb2011-08-10 20:55:27 +0000298 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000299 assert (Ranges.empty() == false
Devang Patelbf47fdb2011-08-10 20:55:27 +0000300 && "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000301
Devang Patel26a92002011-07-27 00:34:13 +0000302 if (!Scope->getScopeNode())
303 return NULL;
304 DIScope DS(Scope->getScopeNode());
305 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel26a92002011-07-27 00:34:13 +0000306 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
307 if (!OriginDIE) {
308 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
309 return NULL;
310 }
311
Devang Patelbf47fdb2011-08-10 20:55:27 +0000312 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000313 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
314 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000315
Devang Patel0afbf232010-07-08 22:39:20 +0000316 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000317 assert (0 && "Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000318 return 0;
319 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000320 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000321 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000322 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000323 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000324
Devang Pateld96efb82011-05-05 17:54:26 +0000325 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000326 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
327 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000328
Devang Patel26a92002011-07-27 00:34:13 +0000329 if (Ranges.size() > 1) {
330 // .debug_range section has not been laid out yet. Emit offset in
331 // .debug_range as a uint, size 4, for now. emitDIE will handle
332 // DW_AT_ranges appropriately.
333 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000334 DebugRangeSymbols.size()
335 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000336 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000337 RE = Ranges.end(); RI != RE; ++RI) {
338 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
339 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
340 }
341 DebugRangeSymbols.push_back(NULL);
342 DebugRangeSymbols.push_back(NULL);
343 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000344 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
345 StartLabel);
346 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
347 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000348 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000349
350 InlinedSubprogramDIEs.insert(OriginDIE);
351
352 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000353 //.debug_inlined section specification does not clearly state how
354 // to emit inlined scope that is split into multiple instruction ranges.
355 // For now, use first instruction range and emit low_pc/high_pc pair and
356 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000357 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000358 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000359
360 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000361 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000362 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000363 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000364 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000365 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000366
Devang Patel53bb5c92009-11-10 23:06:00 +0000367 DILocation DL(Scope->getInlinedAt());
Devang Patel3cbee302011-04-12 22:53:02 +0000368 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
369 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000370
371 return ScopeDIE;
372}
373
Devang Patel2c4ceb12009-11-21 02:48:08 +0000374/// constructScopeDIE - Construct a DIE for this scope.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000375DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000376 if (!Scope || !Scope->getScopeNode())
377 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000378
Devang Patel5bc9fec2011-02-19 01:31:27 +0000379 SmallVector <DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000380
381 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000382 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000383 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
384 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000385 if (DIE *Arg =
386 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel0478c152011-03-01 22:58:55 +0000387 Children.push_back(Arg);
388
Devang Patel5bc9fec2011-02-19 01:31:27 +0000389 // Collect lexical scope childrens first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000390 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000391 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000392 if (DIE *Variable =
393 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000394 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000395 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000396 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000397 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000398 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000399 DIScope DS(Scope->getScopeNode());
400 DIE *ScopeDIE = NULL;
401 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000402 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000403 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000404 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000405 if (Scope->isAbstractScope()) {
Devang Pateld3024342011-08-15 22:24:32 +0000406 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000407 // Note down abstract DIE.
408 if (ScopeDIE)
409 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
410 }
Devang Patel3c91b052010-03-08 20:52:55 +0000411 else
Devang Pateld3024342011-08-15 22:24:32 +0000412 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000413 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000414 else {
415 // There is no need to emit empty lexical block DIE.
416 if (Children.empty())
417 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000418 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000419 }
420
Devang Patelaead63c2010-03-29 22:59:58 +0000421 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000422
Devang Patel5bc9fec2011-02-19 01:31:27 +0000423 // Add children
424 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
425 E = Children.end(); I != E; ++I)
426 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000427
Jim Grosbach1e20b962010-07-21 21:21:52 +0000428 if (DS.isSubprogram())
Devang Pateld3024342011-08-15 22:24:32 +0000429 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000430
Devang Patel193f7202009-11-24 01:14:22 +0000431 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000432}
433
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000434/// GetOrCreateSourceID - Look up the source id with the given directory and
435/// source file names. If none currently exists, create a new id and insert it
436/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
437/// maps as well.
Devang Patel2f584852010-07-24 00:53:22 +0000438
Devang Patel23670e52011-03-24 20:30:50 +0000439unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
440 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000441 // If FE did not provide a file name, then assume stdin.
442 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000443 return GetOrCreateSourceID("<stdin>", StringRef());
444
445 // MCStream expects full path name as filename.
Benjamin Kramerab5f7882011-06-05 14:36:47 +0000446 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
447 SmallString<128> FullPathName = DirName;
448 sys::path::append(FullPathName, FileName);
Devang Patel23670e52011-03-24 20:30:50 +0000449 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
450 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
451 }
Devang Patel1905a182010-09-16 20:57:49 +0000452
Rafael Espindola5c055632010-11-18 02:04:25 +0000453 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
454 if (Entry.getValue())
455 return Entry.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000456
Rafael Espindola5c055632010-11-18 02:04:25 +0000457 unsigned SrcId = SourceIdMap.size();
458 Entry.setValue(SrcId);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000459
Rafael Espindola5c055632010-11-18 02:04:25 +0000460 // Print out a .file directive to specify files for .loc directives.
Devang Patel23670e52011-03-24 20:30:50 +0000461 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000462
463 return SrcId;
464}
465
Jim Grosbach1e20b962010-07-21 21:21:52 +0000466/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000467/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000468void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000469 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000470 StringRef FN = DIUnit.getFilename();
471 StringRef Dir = DIUnit.getDirectory();
Devang Patel23670e52011-03-24 20:30:50 +0000472 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000473
474 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel3cbee302011-04-12 22:53:02 +0000475 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
476 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
477 DIUnit.getProducer());
478 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
479 DIUnit.getLanguage());
480 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +0000481 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
482 // simplifies debug range entries.
Devang Patel3cbee302011-04-12 22:53:02 +0000483 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000484 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000485 // compile unit in debug_line section.
Rafael Espindoladc52ecf2011-05-10 20:35:05 +0000486 if(Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindola597a7662011-05-04 17:44:06 +0000487 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000488 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000489 else
Devang Patel3cbee302011-04-12 22:53:02 +0000490 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000491
Devang Patel65dbc902009-11-25 17:36:49 +0000492 if (!Dir.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000493 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000494 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000495 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000496
Devang Patel65dbc902009-11-25 17:36:49 +0000497 StringRef Flags = DIUnit.getFlags();
498 if (!Flags.empty())
Devang Patel5bc942c2011-08-10 23:58:09 +0000499 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string,
500 Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000501
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000502 unsigned RVer = DIUnit.getRunTimeVersion();
503 if (RVer)
Devang Patel3cbee302011-04-12 22:53:02 +0000504 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000505 dwarf::DW_FORM_data1, RVer);
506
Devang Patel163a9f72010-05-10 22:49:55 +0000507 if (!FirstCU)
508 FirstCU = NewCU;
509 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000510}
511
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000512/// getCompileUnit - Get CompileUnit DIE.
Devang Patel163a9f72010-05-10 22:49:55 +0000513CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
514 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
515 DIDescriptor D(N);
516 const MDNode *CUNode = NULL;
517 if (D.isCompileUnit())
518 CUNode = N;
519 else if (D.isSubprogram())
520 CUNode = DISubprogram(N).getCompileUnit();
521 else if (D.isType())
522 CUNode = DIType(N).getCompileUnit();
523 else if (D.isGlobalVariable())
524 CUNode = DIGlobalVariable(N).getCompileUnit();
525 else if (D.isVariable())
526 CUNode = DIVariable(N).getCompileUnit();
527 else if (D.isNameSpace())
528 CUNode = DINameSpace(N).getCompileUnit();
529 else if (D.isFile())
530 CUNode = DIFile(N).getCompileUnit();
531 else
532 return FirstCU;
533
534 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
535 = CUMap.find(CUNode);
536 if (I == CUMap.end())
537 return FirstCU;
538 return I->second;
539}
540
Devang Patel163a9f72010-05-10 22:49:55 +0000541/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000542void DwarfDebug::constructGlobalVariableDIE(CompileUnit *TheCU,
543 const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +0000544 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000545
Devang Patel905cf5e2009-09-04 23:59:07 +0000546 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +0000547 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +0000548 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000549
Devang Patel6f9d8ff2011-08-15 17:57:41 +0000550 TheCU->createGlobalVariableDIE(N);
Devang Patel13e16b62009-06-26 01:49:18 +0000551 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000552}
553
Devang Patel163a9f72010-05-10 22:49:55 +0000554/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel3655a212011-08-15 23:36:40 +0000555void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
556 const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000557 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000558 if (!SP.isDefinition())
559 // This is a method declaration which will be handled while constructing
560 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000561 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000562
Devang Pateldbc64af2011-08-15 17:24:54 +0000563 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000564
565 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000566 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000567
568 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000569 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000570
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000571 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +0000572 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +0000573
Devang Patel13e16b62009-06-26 01:49:18 +0000574 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000575}
576
Devang Patel2c4ceb12009-11-21 02:48:08 +0000577/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000578/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000579/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000580void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000581 if (DisableDebugInfoPrinting)
582 return;
583
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000584 // If module has named metadata anchors then use them, otherwise scan the
585 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000586 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
587 if (CU_Nodes) {
Devang Patela938dd02011-05-04 16:34:02 +0000588
589 NamedMDNode *GV_Nodes = M->getNamedMetadata("llvm.dbg.gv");
590 NamedMDNode *SP_Nodes = M->getNamedMetadata("llvm.dbg.sp");
591 if (!GV_Nodes && !SP_Nodes)
592 // If there are not any global variables or any functions then
593 // there is not any debug info in this module.
Devang Patel30692ab2011-05-03 16:45:22 +0000594 return;
595
Devang Patela938dd02011-05-04 16:34:02 +0000596 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
597 constructCompileUnit(CU_Nodes->getOperand(i));
Devang Patel30692ab2011-05-03 16:45:22 +0000598
Devang Patela938dd02011-05-04 16:34:02 +0000599 if (GV_Nodes)
Devang Patel3655a212011-08-15 23:36:40 +0000600 for (unsigned i = 0, e = GV_Nodes->getNumOperands(); i != e; ++i) {
601 const MDNode *N = GV_Nodes->getOperand(i);
602 constructGlobalVariableDIE(getCompileUnit(N), N);
603 }
Devang Patela938dd02011-05-04 16:34:02 +0000604
605 if (SP_Nodes)
Devang Patel3655a212011-08-15 23:36:40 +0000606 for (unsigned i = 0, e = SP_Nodes->getNumOperands(); i != e; ++i) {
607 const MDNode *N = SP_Nodes->getOperand(i);
608 constructSubprogramDIE(getCompileUnit(N), N);
609 }
Devang Patel30692ab2011-05-03 16:45:22 +0000610
611 } else {
612
613 DebugInfoFinder DbgFinder;
614 DbgFinder.processModule(*M);
615
Devang Patela938dd02011-05-04 16:34:02 +0000616 bool HasDebugInfo = false;
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000617 // Scan all the compile-units to see if there are any marked as the main
618 // unit. If not, we do not generate debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000619 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
620 E = DbgFinder.compile_unit_end(); I != E; ++I) {
621 if (DICompileUnit(*I).isMain()) {
622 HasDebugInfo = true;
623 break;
624 }
625 }
Devang Patela938dd02011-05-04 16:34:02 +0000626 if (!HasDebugInfo) return;
Devang Patel30692ab2011-05-03 16:45:22 +0000627
628 // Create all the compile unit DIEs.
629 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
630 E = DbgFinder.compile_unit_end(); I != E; ++I)
631 constructCompileUnit(*I);
632
633 // Create DIEs for each global variable.
634 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
Devang Patel3655a212011-08-15 23:36:40 +0000635 E = DbgFinder.global_variable_end(); I != E; ++I) {
636 const MDNode *N = *I;
637 if (DIGlobalVariable(N).getVersion() <= LLVMDebugVersion9)
638 constructGlobalVariableDIE(getCompileUnit(N), N);
639 }
Devang Patel30692ab2011-05-03 16:45:22 +0000640
641 // Create DIEs for each subprogram.
642 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
Devang Patel3655a212011-08-15 23:36:40 +0000643 E = DbgFinder.subprogram_end(); I != E; ++I) {
644 const MDNode *N = *I;
645 constructSubprogramDIE(getCompileUnit(N), N);
646 }
Chris Lattnerd850ac72010-04-05 02:19:28 +0000647 }
Devang Patel30692ab2011-05-03 16:45:22 +0000648
Chris Lattnerd850ac72010-04-05 02:19:28 +0000649 // Tell MMI that we have debug info.
650 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000651
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000652 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000653 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000654
Devang Patele7e5a0f2010-08-10 20:01:20 +0000655 //getOrCreateTypeDIE
656 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
Devang Patel3cbee302011-04-12 22:53:02 +0000657 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
658 DIType Ty(NMD->getOperand(i));
659 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
660 }
Devang Patele7e5a0f2010-08-10 20:01:20 +0000661
Devang Patel1a7ca032010-09-28 18:08:20 +0000662 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
Devang Patel3cbee302011-04-12 22:53:02 +0000663 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
664 DIType Ty(NMD->getOperand(i));
665 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
666 }
Devang Patel1a7ca032010-09-28 18:08:20 +0000667
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000668 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000669 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000670}
671
Devang Patel2c4ceb12009-11-21 02:48:08 +0000672/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000673///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000674void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000675 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000676 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000677 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000678 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
679 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
680 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
681 DISubprogram SP(AllSPs->getOperand(SI));
682 if (!SP.Verify()) continue;
683
684 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +0000685 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +0000686 StringRef FName = SP.getLinkageName();
687 if (FName.empty())
688 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +0000689 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patel4a1cad62010-06-28 18:25:03 +0000690 if (!NMD) continue;
691 unsigned E = NMD->getNumOperands();
692 if (!E) continue;
Devang Patel5bc942c2011-08-10 23:58:09 +0000693 LexicalScope *Scope = new LexicalScope(NULL, DIDescriptor(SP), NULL,
694 false);
Devang Patele9a1cca2010-08-02 17:32:15 +0000695 DeadFnScopeMap[SP] = Scope;
Benjamin Kramer0fa5e052011-08-11 18:39:28 +0000696 SmallVector<DbgVariable, 8> Variables;
Devang Patel4a1cad62010-06-28 18:25:03 +0000697 for (unsigned I = 0; I != E; ++I) {
698 DIVariable DV(NMD->getOperand(I));
699 if (!DV.Verify()) continue;
Devang Patel5a1a67c2011-08-15 19:01:20 +0000700 Variables.push_back(DbgVariable(DV, NULL));
Devang Patel4a1cad62010-06-28 18:25:03 +0000701 }
Jim Grosbach1e20b962010-07-21 21:21:52 +0000702
Devang Patel4a1cad62010-06-28 18:25:03 +0000703 // Construct subprogram DIE and add variables DIEs.
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000704 CompileUnit *SPCU = getCompileUnit(SP);
Devang Patel3655a212011-08-15 23:36:40 +0000705 constructSubprogramDIE(SPCU, SP);
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000706 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel4a1cad62010-06-28 18:25:03 +0000707 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000708 if (DIE *VariableDIE =
709 SPCU->constructVariableDIE(&Variables[i], Scope->isAbstractScope()))
Devang Patel4a1cad62010-06-28 18:25:03 +0000710 ScopeDIE->addChild(VariableDIE);
711 }
712 }
713 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000714
Devang Patel53bb5c92009-11-10 23:06:00 +0000715 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
716 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
717 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
718 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000719 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000720 }
721
Devang Pateldbc64af2011-08-15 17:24:54 +0000722 // Emit DW_AT_containing_type attribute to connect types with their
723 // vtable holding type.
724 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
725 CUE = CUMap.end(); CUI != CUE; ++CUI) {
726 CompileUnit *TheCU = CUI->second;
727 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000728 }
729
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000730 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000731 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000732 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000733 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000734 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000735
736 // End text sections.
737 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000738 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000739 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000740 }
741
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000742 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000743 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000744
745 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +0000746 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000747
748 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000749 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000750
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000751 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000752 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000753
Devang Patel193f7202009-11-24 01:14:22 +0000754 // Emit info into a debug pubtypes section.
755 emitDebugPubTypes();
756
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000757 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000758 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000759
760 // Emit info into a debug aranges section.
761 EmitDebugARanges();
762
763 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000764 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000765
766 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000767 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000768
769 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000770 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000771
Chris Lattnerbc733f52010-03-13 02:17:42 +0000772 // Emit info into a debug str section.
773 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000774
Devang Patele9a1cca2010-08-02 17:32:15 +0000775 // clean up.
776 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel163a9f72010-05-10 22:49:55 +0000777 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
778 E = CUMap.end(); I != E; ++I)
779 delete I->second;
780 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000781}
782
Devang Patel53bb5c92009-11-10 23:06:00 +0000783/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +0000784DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +0000785 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000786 LLVMContext &Ctx = DV->getContext();
787 // More then one inlined variable corresponds to one abstract variable.
788 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +0000789 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +0000790 if (AbsDbgVariable)
791 return AbsDbgVariable;
792
Devang Patelbf47fdb2011-08-10 20:55:27 +0000793 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +0000794 if (!Scope)
795 return NULL;
796
Devang Patel5a1a67c2011-08-15 19:01:20 +0000797 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000798 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +0000799 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +0000800 return AbsDbgVariable;
801}
802
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000803/// addCurrentFnArgument - If Var is a current function argument then add
804/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +0000805bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +0000806 DbgVariable *Var, LexicalScope *Scope) {
807 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000808 return false;
809 DIVariable DV = Var->getVariable();
810 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
811 return false;
812 unsigned ArgNo = DV.getArgNumber();
813 if (ArgNo == 0)
814 return false;
815
Devang Patelcb3a6572011-03-03 20:02:02 +0000816 size_t Size = CurrentFnArguments.size();
817 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +0000818 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +0000819 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +0000820 // arguments does the function have at source level.
821 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +0000822 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +0000823 CurrentFnArguments[ArgNo - 1] = Var;
824 return true;
825}
826
Devang Patelee432862010-05-20 19:57:06 +0000827/// collectVariableInfoFromMMITable - Collect variable information from
828/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000829void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000830DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +0000831 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +0000832 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
833 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
834 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000835 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +0000836 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +0000837 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +0000838 DIVariable DV(Var);
839 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000840
Devang Patelbf47fdb2011-08-10 20:55:27 +0000841 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000842
Devang Patelfb0ee432009-11-10 23:20:04 +0000843 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +0000844 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +0000845 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000846
Devang Patel26c1e562010-05-20 16:36:41 +0000847 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000848 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patelff9dd0a2011-08-15 21:24:36 +0000849 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +0000850 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000851 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000852 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000853 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +0000854 }
Devang Patelee432862010-05-20 19:57:06 +0000855}
Devang Patel90a48ad2010-03-15 18:33:46 +0000856
Jim Grosbach1e20b962010-07-21 21:21:52 +0000857/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +0000858/// DBG_VALUE instruction, is in a defined reg.
859static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
860 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000861 return MI->getNumOperands() == 3 &&
862 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
863 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000864}
865
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000866/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +0000867/// at MI.
868static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
869 const MCSymbol *FLabel,
870 const MCSymbol *SLabel,
871 const MachineInstr *MI) {
872 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
873
874 if (MI->getNumOperands() != 3) {
875 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
876 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
877 }
878 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
879 MachineLocation MLoc;
880 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
881 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
882 }
883 if (MI->getOperand(0).isImm())
884 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
885 if (MI->getOperand(0).isFPImm())
886 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
887 if (MI->getOperand(0).isCImm())
888 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
889
890 assert (0 && "Unexpected 3 operand DBG_VALUE instruction!");
891 return DotDebugLocEntry();
892}
893
Devang Patelbf47fdb2011-08-10 20:55:27 +0000894/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000895void
Devang Patel78e127d2010-06-25 22:07:34 +0000896DwarfDebug::collectVariableInfo(const MachineFunction *MF,
897 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000898
Devang Patelee432862010-05-20 19:57:06 +0000899 /// collection info from MMI table.
900 collectVariableInfoFromMMITable(MF, Processed);
901
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000902 for (SmallVectorImpl<const MDNode*>::const_iterator
903 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
904 ++UVI) {
905 const MDNode *Var = *UVI;
906 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +0000907 continue;
908
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000909 // History contains relevant DBG_VALUE instructions for Var and instructions
910 // clobbering it.
911 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
912 if (History.empty())
913 continue;
914 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +0000915
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000916 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000917 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +0000918 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
919 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000920 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +0000921 else {
922 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +0000923 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +0000924 else {
925 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +0000926 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +0000927 else
Devang Patelbf47fdb2011-08-10 20:55:27 +0000928 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +0000929 }
930 }
Devang Patelee432862010-05-20 19:57:06 +0000931 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +0000932 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +0000933 continue;
934
935 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000936 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +0000937 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
938 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel0478c152011-03-01 22:58:55 +0000939 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +0000940 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +0000941 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +0000942 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000943
944 // Simple ranges that are fully coalesced.
945 if (History.size() <= 1 || (History.size() == 2 &&
946 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +0000947 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +0000948 continue;
949 }
950
951 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000952 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000953
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000954 for (SmallVectorImpl<const MachineInstr*>::const_iterator
955 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
956 const MachineInstr *Begin = *HI;
957 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +0000958
Devang Patel4ada1d72011-06-01 23:00:17 +0000959 // Check if DBG_VALUE is truncating a range.
960 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
961 && !Begin->getOperand(0).getReg())
962 continue;
963
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000964 // Compute the range for a register location.
965 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
966 const MCSymbol *SLabel = 0;
967
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000968 if (HI + 1 == HE)
969 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000970 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000971 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000972 else {
973 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +0000974 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
975 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000976 if (End->isDebugValue())
977 SLabel = getLabelBeforeInsn(End);
978 else {
979 // End is a normal instruction clobbering the range.
980 SLabel = getLabelAfterInsn(End);
981 assert(SLabel && "Forgot label after clobber instruction");
982 ++HI;
983 }
984 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000985
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000986 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel90b40412011-07-08 17:09:57 +0000987 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +0000988 }
989 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +0000990 }
Devang Patel98e1cac2010-05-14 21:01:35 +0000991
992 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +0000993 const Function *F = MF->getFunction();
Devang Patel62367042010-11-10 22:19:21 +0000994 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patel98e1cac2010-05-14 21:01:35 +0000995 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +0000996 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +0000997 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +0000998 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +0000999 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Devang Patel5a1a67c2011-08-15 19:01:20 +00001000 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel98e1cac2010-05-14 21:01:35 +00001001 }
1002 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001003}
Devang Patel98e1cac2010-05-14 21:01:35 +00001004
Devang Patelc3f5f782010-05-25 23:40:22 +00001005/// getLabelBeforeInsn - Return Label preceding the instruction.
1006const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001007 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1008 assert(Label && "Didn't insert label before instruction");
1009 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001010}
1011
1012/// getLabelAfterInsn - Return Label immediately following the instruction.
1013const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001014 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001015}
1016
Devang Patelcbbe2872010-10-26 17:49:02 +00001017/// beginInstruction - Process beginning of an instruction.
1018void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001019 // Check if source location changes, but ignore DBG_VALUE locations.
1020 if (!MI->isDebugValue()) {
1021 DebugLoc DL = MI->getDebugLoc();
1022 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel4243e672011-05-11 19:22:19 +00001023 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001024 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001025 if (DL == PrologEndLoc) {
1026 Flags |= DWARF2_FLAG_PROLOGUE_END;
1027 PrologEndLoc = DebugLoc();
1028 }
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001029 if (!DL.isUnknown()) {
1030 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001031 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001032 } else
Devang Patel4243e672011-05-11 19:22:19 +00001033 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001034 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001035 }
Devang Patelaead63c2010-03-29 22:59:58 +00001036
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001037 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001038 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1039 LabelsBeforeInsn.find(MI);
1040
1041 // No label needed.
1042 if (I == LabelsBeforeInsn.end())
1043 return;
1044
1045 // Label already assigned.
1046 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001047 return;
Devang Patel553881b2010-03-29 17:20:31 +00001048
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001049 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001050 PrevLabel = MMI->getContext().CreateTempSymbol();
1051 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001052 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001053 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001054}
1055
Devang Patelcbbe2872010-10-26 17:49:02 +00001056/// endInstruction - Process end of an instruction.
1057void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001058 // Don't create a new label after DBG_VALUE instructions.
1059 // They don't generate code.
1060 if (!MI->isDebugValue())
1061 PrevLabel = 0;
1062
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001063 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1064 LabelsAfterInsn.find(MI);
1065
1066 // No label needed.
1067 if (I == LabelsAfterInsn.end())
1068 return;
1069
1070 // Label already assigned.
1071 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001072 return;
1073
1074 // We need a label after this instruction.
1075 if (!PrevLabel) {
1076 PrevLabel = MMI->getContext().CreateTempSymbol();
1077 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001078 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001079 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001080}
1081
Jim Grosbach1e20b962010-07-21 21:21:52 +00001082/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001083/// Each LexicalScope has first instruction and last instruction to mark
1084/// beginning and end of a scope respectively. Create an inverse map that list
1085/// scopes starts (and ends) with an instruction. One instruction may start (or
1086/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001087void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001088 SmallVector<LexicalScope *, 4> WorkList;
1089 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001090 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001091 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001092
Devang Patelbf47fdb2011-08-10 20:55:27 +00001093 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001094 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001095 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001096 SE = Children.end(); SI != SE; ++SI)
1097 WorkList.push_back(*SI);
1098
Devang Patel53bb5c92009-11-10 23:06:00 +00001099 if (S->isAbstractScope())
1100 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001101
Devang Patelbf47fdb2011-08-10 20:55:27 +00001102 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001103 if (Ranges.empty())
1104 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001105 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001106 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001107 assert(RI->first && "InsnRange does not have first instruction!");
1108 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001109 requestLabelBeforeInsn(RI->first);
1110 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001111 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001112 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001113}
1114
Devang Patela3f48672011-05-09 22:14:49 +00001115/// getScopeNode - Get MDNode for DebugLoc's scope.
1116static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1117 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1118 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1119 return DL.getScope(Ctx);
1120}
1121
Devang Patel4243e672011-05-11 19:22:19 +00001122/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1123/// line number info for the function.
1124static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1125 const MDNode *Scope = getScopeNode(DL, Ctx);
1126 DISubprogram SP = getDISubprogram(Scope);
1127 if (SP.Verify())
1128 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1129 return DebugLoc();
1130}
1131
Devang Patel2c4ceb12009-11-21 02:48:08 +00001132/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001133/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001134void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001135 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001136 LScopes.initialize(*MF);
1137 if (LScopes.empty()) return;
1138 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001139
Devang Pateleac9c072010-04-27 19:46:33 +00001140 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1141 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001142 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001143 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001144
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001145 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1146
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001147 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001148 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1149 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1150
Devang Patelb2b31a62010-05-26 19:37:24 +00001151 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001152 I != E; ++I) {
1153 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001154 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1155 II != IE; ++II) {
1156 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001157
Devang Patelb2b31a62010-05-26 19:37:24 +00001158 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00001159 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001160
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001161 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001162 const MDNode *Var =
1163 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001164
1165 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001166 if (isDbgValueInDefinedReg(MI))
1167 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1168
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001169 // Check the history of this variable.
1170 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1171 if (History.empty()) {
1172 UserVariables.push_back(Var);
1173 // The first mention of a function argument gets the FunctionBeginSym
1174 // label, so arguments are visible when breaking at function entry.
1175 DIVariable DV(Var);
1176 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1177 DISubprogram(getDISubprogram(DV.getContext()))
1178 .describes(MF->getFunction()))
1179 LabelsBeforeInsn[MI] = FunctionBeginSym;
1180 } else {
1181 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1182 const MachineInstr *Prev = History.back();
1183 if (Prev->isDebugValue()) {
1184 // Coalesce identical entries at the end of History.
1185 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001186 Prev->isIdenticalTo(History[History.size() - 2])) {
1187 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1188 << "\t" << *Prev
1189 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001190 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001191 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001192
1193 // Terminate old register assignments that don't reach MI;
1194 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1195 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1196 isDbgValueInDefinedReg(Prev)) {
1197 // Previous register assignment needs to terminate at the end of
1198 // its basic block.
1199 MachineBasicBlock::const_iterator LastMI =
1200 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001201 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001202 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001203 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1204 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001205 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001206 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001207 else {
1208 // Terminate after LastMI.
1209 History.push_back(LastMI);
1210 }
1211 }
1212 }
1213 }
1214 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001215 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001216 // Not a DBG_VALUE instruction.
1217 if (!MI->isLabel())
1218 AtBlockEntry = false;
1219
Devang Patel4243e672011-05-11 19:22:19 +00001220 // First known non DBG_VALUE location marks beginning of function
1221 // body.
1222 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1223 PrologEndLoc = MI->getDebugLoc();
1224
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001225 // Check if the instruction clobbers any registers with debug vars.
1226 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1227 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1228 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1229 continue;
1230 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1231 unsigned Reg = *AI; ++AI) {
1232 const MDNode *Var = LiveUserVar[Reg];
1233 if (!Var)
1234 continue;
1235 // Reg is now clobbered.
1236 LiveUserVar[Reg] = 0;
1237
1238 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001239 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1240 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001241 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001242 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1243 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001244 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001245 const MachineInstr *Prev = History.back();
1246 // Sanity-check: Register assignments are terminated at the end of
1247 // their block.
1248 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1249 continue;
1250 // Is the variable still in Reg?
1251 if (!isDbgValueInDefinedReg(Prev) ||
1252 Prev->getOperand(0).getReg() != Reg)
1253 continue;
1254 // Var is clobbered. Make sure the next instruction gets a label.
1255 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001256 }
1257 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001258 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001259 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001260 }
1261
1262 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1263 I != E; ++I) {
1264 SmallVectorImpl<const MachineInstr*> &History = I->second;
1265 if (History.empty())
1266 continue;
1267
1268 // Make sure the final register assignments are terminated.
1269 const MachineInstr *Prev = History.back();
1270 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1271 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001272 MachineBasicBlock::const_iterator LastMI =
1273 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001274 if (LastMI == PrevMBB->end())
1275 // Drop DBG_VALUE for empty range.
1276 History.pop_back();
1277 else {
1278 // Terminate after LastMI.
1279 History.push_back(LastMI);
1280 }
1281 }
1282 // Request labels for the full history.
1283 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1284 const MachineInstr *MI = History[i];
1285 if (MI->isDebugValue())
1286 requestLabelBeforeInsn(MI);
1287 else
1288 requestLabelAfterInsn(MI);
1289 }
1290 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001291
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001292 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001293 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001294
1295 // Record beginning of function.
1296 if (!PrologEndLoc.isUnknown()) {
1297 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1298 MF->getFunction()->getContext());
1299 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1300 FnStartDL.getScope(MF->getFunction()->getContext()),
1301 DWARF2_FLAG_IS_STMT);
1302 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001303}
1304
Devang Patelbf47fdb2011-08-10 20:55:27 +00001305void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1306// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1307 ScopeVariables[LS].push_back(Var);
1308// Vars.push_back(Var);
1309}
1310
Devang Patel2c4ceb12009-11-21 02:48:08 +00001311/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001312///
Chris Lattnereec791a2010-01-26 23:18:02 +00001313void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001314 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001315
Devang Patelbf47fdb2011-08-10 20:55:27 +00001316 // Define end label for subprogram.
1317 FunctionEndSym = Asm->GetTempSymbol("func_end",
1318 Asm->getFunctionNumber());
1319 // Assumes in correct section after the entry point.
1320 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1321
1322 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1323 collectVariableInfo(MF, ProcessedVars);
1324
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001325 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1326 CompileUnit *TheCU = getCompileUnit(FnScope->getScopeNode());
1327
Devang Patelbf47fdb2011-08-10 20:55:27 +00001328 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001329 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1330 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1331 LexicalScope *AScope = AList[i];
1332 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001333 if (SP.Verify()) {
1334 // Collect info for variables that were optimized out.
1335 StringRef FName = SP.getLinkageName();
1336 if (FName.empty())
1337 FName = SP.getName();
1338 if (NamedMDNode *NMD =
1339 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
1340 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001341 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00001342 if (!DV || !ProcessedVars.insert(DV))
1343 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001344 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Devang Patel5a1a67c2011-08-15 19:01:20 +00001345 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel78e127d2010-06-25 22:07:34 +00001346 }
1347 }
1348 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001349 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001350 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001351 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001352
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001353 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001354
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001355 if (!DisableFramePointerElim(*MF))
Devang Patel5bc942c2011-08-10 23:58:09 +00001356 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1357 dwarf::DW_FORM_flag, 1);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001358
Devang Patelbf47fdb2011-08-10 20:55:27 +00001359 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1360 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001361
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001362 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001363 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1364 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1365 DeleteContainerPointers(I->second);
1366 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001367 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001368 UserVariables.clear();
1369 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001370 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001371 LabelsBeforeInsn.clear();
1372 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001373 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001374}
1375
Chris Lattnerc6087842010-03-09 04:54:43 +00001376/// recordSourceLine - Register a source line with debug info. Returns the
1377/// unique label that was emitted and which provides correspondence to
1378/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001379void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1380 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001381 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001382 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001383 unsigned Src = 1;
1384 if (S) {
1385 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001386
Dan Gohman1cc0d622010-05-05 23:41:32 +00001387 if (Scope.isCompileUnit()) {
1388 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001389 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001390 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001391 } else if (Scope.isFile()) {
1392 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001393 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001394 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001395 } else if (Scope.isSubprogram()) {
1396 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001397 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001398 Dir = SP.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001399 } else if (Scope.isLexicalBlock()) {
1400 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001401 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001402 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001403 } else
1404 assert(0 && "Unexpected scope info");
1405
Devang Patel23670e52011-03-24 20:30:50 +00001406 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001407 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001408 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001409}
1410
Bill Wendling829e67b2009-05-20 23:22:40 +00001411//===----------------------------------------------------------------------===//
1412// Emit Methods
1413//===----------------------------------------------------------------------===//
1414
Devang Patel2c4ceb12009-11-21 02:48:08 +00001415/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001416///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001417unsigned
1418DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001419 // Get the children.
1420 const std::vector<DIE *> &Children = Die->getChildren();
1421
1422 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001423 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00001424 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00001425
1426 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001427 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001428
1429 // Get the abbreviation for this DIE.
1430 unsigned AbbrevNumber = Die->getAbbrevNumber();
1431 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1432
1433 // Set DIE offset
1434 Die->setOffset(Offset);
1435
1436 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001437 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001438
1439 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1440 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1441
1442 // Size the DIE attribute values.
1443 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1444 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001445 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001446
1447 // Size the DIE children if any.
1448 if (!Children.empty()) {
1449 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1450 "Children flag not set");
1451
1452 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001453 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001454
1455 // End of children marker.
1456 Offset += sizeof(int8_t);
1457 }
1458
1459 Die->setSize(Offset - Die->getOffset());
1460 return Offset;
1461}
1462
Devang Patel2c4ceb12009-11-21 02:48:08 +00001463/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001464///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001465void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001466 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1467 E = CUMap.end(); I != E; ++I) {
1468 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001469 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001470 sizeof(int32_t) + // Length of Compilation Unit Info
1471 sizeof(int16_t) + // DWARF version number
1472 sizeof(int32_t) + // Offset Into Abbrev. Section
1473 sizeof(int8_t); // Pointer Size (in bytes)
1474 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001475 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001476}
1477
Chris Lattner11b8f302010-04-04 23:02:02 +00001478/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1479/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00001480static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00001481 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00001482 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00001483 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001484
Chris Lattner9c69e285532010-04-04 22:59:04 +00001485 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1486 Asm->OutStreamer.EmitLabel(TmpSym);
1487 return TmpSym;
1488}
1489
1490/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1491/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001492void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001493 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001494
Bill Wendling94d04b82009-05-20 23:21:38 +00001495 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001496 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001497 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001498 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001499 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001500 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001501
Chris Lattner9c69e285532010-04-04 22:59:04 +00001502 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001503 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001504
Devang Patelaf608bd2010-08-24 00:06:12 +00001505 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001506 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1507 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1508 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001509 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001510 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001511 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1512 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001513
Devang Patelc3f5f782010-05-25 23:40:22 +00001514 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1515 "section_debug_loc");
1516
Chris Lattner9c69e285532010-04-04 22:59:04 +00001517 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001518 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001519}
1520
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001521/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001522///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001523void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001524 // Get the abbreviation for this DIE.
1525 unsigned AbbrevNumber = Die->getAbbrevNumber();
1526 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1527
Bill Wendling94d04b82009-05-20 23:21:38 +00001528 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001529 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001530 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1531 Twine::utohexstr(Die->getOffset()) + ":0x" +
1532 Twine::utohexstr(Die->getSize()) + " " +
1533 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001534 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001535
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001536 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001537 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1538
1539 // Emit the DIE attribute values.
1540 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1541 unsigned Attr = AbbrevData[i].getAttribute();
1542 unsigned Form = AbbrevData[i].getForm();
1543 assert(Form && "Too many attributes for DIE (check abbreviation)");
1544
Chris Lattner3f53c832010-04-04 18:52:31 +00001545 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001546 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001547
Bill Wendling94d04b82009-05-20 23:21:38 +00001548 switch (Attr) {
1549 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00001550 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00001551 break;
1552 case dwarf::DW_AT_abstract_origin: {
1553 DIEEntry *E = cast<DIEEntry>(Values[i]);
1554 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001555 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001556 Asm->EmitInt32(Addr);
1557 break;
1558 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001559 case dwarf::DW_AT_ranges: {
1560 // DW_AT_range Value encodes offset in debug_range section.
1561 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001562
1563 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1564 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1565 V->getValue(),
1566 4);
1567 } else {
1568 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1569 V->getValue(),
1570 DwarfDebugRangeSectionSym,
1571 4);
1572 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001573 break;
1574 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001575 case dwarf::DW_AT_location: {
Devang Patel7a328272011-08-15 21:43:21 +00001576 if (DIELabel *L = dyn_cast<DIELabel>(Values[i]))
Daniel Dunbar83320a02011-03-16 22:16:39 +00001577 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patel7a328272011-08-15 21:43:21 +00001578 else
Devang Patelc3f5f782010-05-25 23:40:22 +00001579 Values[i]->EmitValue(Asm, Form);
1580 break;
1581 }
Devang Patel2a361602010-09-29 19:08:08 +00001582 case dwarf::DW_AT_accessibility: {
1583 if (Asm->isVerbose()) {
1584 DIEInteger *V = cast<DIEInteger>(Values[i]);
1585 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1586 }
1587 Values[i]->EmitValue(Asm, Form);
1588 break;
1589 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001590 default:
1591 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001592 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001593 break;
1594 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001595 }
1596
1597 // Emit the DIE children if any.
1598 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1599 const std::vector<DIE *> &Children = Die->getChildren();
1600
1601 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001602 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001603
Chris Lattner3f53c832010-04-04 18:52:31 +00001604 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001605 Asm->OutStreamer.AddComment("End Of Children Mark");
1606 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001607 }
1608}
1609
Devang Patel8a241142009-12-09 18:24:21 +00001610/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001611///
Devang Patel8a241142009-12-09 18:24:21 +00001612void DwarfDebug::emitDebugInfo() {
1613 // Start debug info section.
1614 Asm->OutStreamer.SwitchSection(
1615 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001616 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1617 E = CUMap.end(); I != E; ++I) {
1618 CompileUnit *TheCU = I->second;
1619 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001620
Devang Patel163a9f72010-05-10 22:49:55 +00001621 // Emit the compile units header.
1622 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1623 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001624
Devang Patel163a9f72010-05-10 22:49:55 +00001625 // Emit size of content not including length itself
1626 unsigned ContentSize = Die->getSize() +
1627 sizeof(int16_t) + // DWARF version number
1628 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001629 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001630
Devang Patel163a9f72010-05-10 22:49:55 +00001631 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1632 Asm->EmitInt32(ContentSize);
1633 Asm->OutStreamer.AddComment("DWARF version number");
1634 Asm->EmitInt16(dwarf::DWARF_VERSION);
1635 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1636 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1637 DwarfAbbrevSectionSym);
1638 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1639 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001640
Devang Patel163a9f72010-05-10 22:49:55 +00001641 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001642 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1643 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001644}
1645
Devang Patel2c4ceb12009-11-21 02:48:08 +00001646/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001647///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001648void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001649 // Check to see if it is worth the effort.
1650 if (!Abbreviations.empty()) {
1651 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001652 Asm->OutStreamer.SwitchSection(
1653 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001654
Chris Lattnerc0215722010-04-04 19:25:43 +00001655 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001656
1657 // For each abbrevation.
1658 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1659 // Get abbreviation data
1660 const DIEAbbrev *Abbrev = Abbreviations[i];
1661
1662 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001663 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001664
1665 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001666 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001667 }
1668
1669 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001670 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001671
Chris Lattnerc0215722010-04-04 19:25:43 +00001672 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001673 }
1674}
1675
Devang Patel2c4ceb12009-11-21 02:48:08 +00001676/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001677/// the line matrix.
1678///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001679void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001680 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001681 Asm->OutStreamer.AddComment("Extended Op");
1682 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001683
Chris Lattner233f52b2010-03-09 23:52:58 +00001684 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001685 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001686 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1687 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1688
1689 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001690
Chris Lattnerc0215722010-04-04 19:25:43 +00001691 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001692 Asm->getTargetData().getPointerSize(),
1693 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001694
1695 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001696 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1697 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001698 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001699 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001700}
1701
Devang Patel8a241142009-12-09 18:24:21 +00001702/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1703///
1704void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00001705 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1706 E = CUMap.end(); I != E; ++I) {
1707 CompileUnit *TheCU = I->second;
1708 // Start the dwarf pubnames section.
1709 Asm->OutStreamer.SwitchSection(
1710 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001711
Devang Patel163a9f72010-05-10 22:49:55 +00001712 Asm->OutStreamer.AddComment("Length of Public Names Info");
1713 Asm->EmitLabelDifference(
1714 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1715 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001716
Devang Patel163a9f72010-05-10 22:49:55 +00001717 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1718 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001719
Devang Patel163a9f72010-05-10 22:49:55 +00001720 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001721 Asm->EmitInt16(dwarf::DWARF_VERSION);
1722
Devang Patel163a9f72010-05-10 22:49:55 +00001723 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001724 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00001725 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001726
Devang Patel163a9f72010-05-10 22:49:55 +00001727 Asm->OutStreamer.AddComment("Compilation Unit Length");
1728 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1729 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1730 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001731
Devang Patel163a9f72010-05-10 22:49:55 +00001732 const StringMap<DIE*> &Globals = TheCU->getGlobals();
1733 for (StringMap<DIE*>::const_iterator
1734 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1735 const char *Name = GI->getKeyData();
1736 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001737
Devang Patel163a9f72010-05-10 22:49:55 +00001738 Asm->OutStreamer.AddComment("DIE offset");
1739 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001740
Devang Patel163a9f72010-05-10 22:49:55 +00001741 if (Asm->isVerbose())
1742 Asm->OutStreamer.AddComment("External Name");
1743 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
1744 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001745
Devang Patel163a9f72010-05-10 22:49:55 +00001746 Asm->OutStreamer.AddComment("End Mark");
1747 Asm->EmitInt32(0);
1748 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001749 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00001750 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001751}
1752
Devang Patel193f7202009-11-24 01:14:22 +00001753void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00001754 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1755 E = CUMap.end(); I != E; ++I) {
1756 CompileUnit *TheCU = I->second;
1757 // Start the dwarf pubnames section.
1758 Asm->OutStreamer.SwitchSection(
1759 Asm->getObjFileLowering().getDwarfPubTypesSection());
1760 Asm->OutStreamer.AddComment("Length of Public Types Info");
1761 Asm->EmitLabelDifference(
1762 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1763 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001764
Devang Patel163a9f72010-05-10 22:49:55 +00001765 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1766 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001767
Devang Patel163a9f72010-05-10 22:49:55 +00001768 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1769 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001770
Devang Patel163a9f72010-05-10 22:49:55 +00001771 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1772 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1773 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001774
Devang Patel163a9f72010-05-10 22:49:55 +00001775 Asm->OutStreamer.AddComment("Compilation Unit Length");
1776 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1777 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1778 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001779
Devang Patel163a9f72010-05-10 22:49:55 +00001780 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1781 for (StringMap<DIE*>::const_iterator
1782 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1783 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001784 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001785
Devang Patel163a9f72010-05-10 22:49:55 +00001786 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1787 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001788
Devang Patel163a9f72010-05-10 22:49:55 +00001789 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
1790 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1791 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001792
Devang Patel163a9f72010-05-10 22:49:55 +00001793 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001794 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00001795 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1796 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00001797 }
Devang Patel193f7202009-11-24 01:14:22 +00001798}
1799
Devang Patel2c4ceb12009-11-21 02:48:08 +00001800/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001801///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001802void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001803 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001804 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001805
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001806 // Start the dwarf str section.
1807 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001808 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001809
Chris Lattnerbc733f52010-03-13 02:17:42 +00001810 // Get all of the string pool entries and put them in an array by their ID so
1811 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001812 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00001813 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001814
Chris Lattnerbc733f52010-03-13 02:17:42 +00001815 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1816 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1817 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001818
Chris Lattnerbc733f52010-03-13 02:17:42 +00001819 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001820
Chris Lattnerbc733f52010-03-13 02:17:42 +00001821 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001822 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001823 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001824
Chris Lattner0d9d70f2010-03-09 23:38:23 +00001825 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00001826 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001827 }
1828}
1829
Devang Patel2c4ceb12009-11-21 02:48:08 +00001830/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001831///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001832void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00001833 if (DotDebugLocEntries.empty())
1834 return;
1835
Devang Patel6c3ea902011-02-04 22:57:18 +00001836 for (SmallVector<DotDebugLocEntry, 4>::iterator
1837 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
1838 I != E; ++I) {
1839 DotDebugLocEntry &Entry = *I;
1840 if (I + 1 != DotDebugLocEntries.end())
1841 Entry.Merge(I+1);
1842 }
1843
Daniel Dunbar83320a02011-03-16 22:16:39 +00001844 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001845 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00001846 Asm->getObjFileLowering().getDwarfLocSection());
1847 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00001848 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
1849 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001850 for (SmallVector<DotDebugLocEntry, 4>::iterator
1851 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00001852 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00001853 DotDebugLocEntry &Entry = *I;
1854 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00001855 if (Entry.isEmpty()) {
1856 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
1857 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00001858 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00001859 } else {
1860 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
1861 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00001862 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00001863 Asm->OutStreamer.AddComment("Loc expr size");
1864 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
1865 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
1866 Asm->EmitLabelDifference(end, begin, 2);
1867 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00001868 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00001869 DIBasicType BTy(DV.getType());
1870 if (BTy.Verify() &&
1871 (BTy.getEncoding() == dwarf::DW_ATE_signed
1872 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
1873 Asm->OutStreamer.AddComment("DW_OP_consts");
1874 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00001875 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00001876 } else {
1877 Asm->OutStreamer.AddComment("DW_OP_constu");
1878 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00001879 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00001880 }
Devang Patel80efd4e2011-07-08 16:49:43 +00001881 } else if (Entry.isLocation()) {
1882 if (!DV.hasComplexAddress())
1883 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00001884 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00001885 else {
1886 // Complex address entry.
1887 unsigned N = DV.getNumAddrElements();
1888 unsigned i = 0;
1889 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
1890 if (Entry.Loc.getOffset()) {
1891 i = 2;
1892 Asm->EmitDwarfRegOp(Entry.Loc);
1893 Asm->OutStreamer.AddComment("DW_OP_deref");
1894 Asm->EmitInt8(dwarf::DW_OP_deref);
1895 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
1896 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1897 Asm->EmitSLEB128(DV.getAddrElement(1));
1898 } else {
1899 // If first address element is OpPlus then emit
1900 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
1901 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
1902 Asm->EmitDwarfRegOp(Loc);
1903 i = 2;
1904 }
1905 } else {
1906 Asm->EmitDwarfRegOp(Entry.Loc);
1907 }
1908
1909 // Emit remaining complex address elements.
1910 for (; i < N; ++i) {
1911 uint64_t Element = DV.getAddrElement(i);
1912 if (Element == DIBuilder::OpPlus) {
1913 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1914 Asm->EmitULEB128(DV.getAddrElement(++i));
1915 } else if (Element == DIBuilder::OpDeref)
1916 Asm->EmitInt8(dwarf::DW_OP_deref);
1917 else llvm_unreachable("unknown Opcode found in complex address");
1918 }
Devang Patelc26f5442011-04-28 02:22:40 +00001919 }
Devang Patelc26f5442011-04-28 02:22:40 +00001920 }
Devang Patel80efd4e2011-07-08 16:49:43 +00001921 // else ... ignore constant fp. There is not any good way to
1922 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00001923 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00001924 }
1925 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001926}
1927
1928/// EmitDebugARanges - Emit visible names into a debug aranges section.
1929///
1930void DwarfDebug::EmitDebugARanges() {
1931 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001932 Asm->OutStreamer.SwitchSection(
1933 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001934}
1935
Devang Patel2c4ceb12009-11-21 02:48:08 +00001936/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001937///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001938void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001939 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001940 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00001941 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00001942 unsigned char Size = Asm->getTargetData().getPointerSize();
1943 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00001944 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00001945 I != E; ++I) {
1946 if (*I)
1947 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00001948 else
Devang Pateleac9c072010-04-27 19:46:33 +00001949 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00001950 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001951}
1952
Devang Patel2c4ceb12009-11-21 02:48:08 +00001953/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001954///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001955void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001956 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00001957 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001958 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001959 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001960 }
1961}
1962
Devang Patel2c4ceb12009-11-21 02:48:08 +00001963/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00001964/// Section Header:
1965/// 1. length of section
1966/// 2. Dwarf version number
1967/// 3. address size.
1968///
1969/// Entries (one "entry" for each function that was inlined):
1970///
1971/// 1. offset into __debug_str section for MIPS linkage name, if exists;
1972/// otherwise offset into __debug_str for regular function name.
1973/// 2. offset into __debug_str section for regular function name.
1974/// 3. an unsigned LEB128 number indicating the number of distinct inlining
1975/// instances for the function.
1976///
1977/// The rest of the entry consists of a {die_offset, low_pc} pair for each
1978/// inlined instance; the die_offset points to the inlined_subroutine die in the
1979/// __debug_info section, and the low_pc is the starting address for the
1980/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001981void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001982 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00001983 return;
1984
Devang Patel163a9f72010-05-10 22:49:55 +00001985 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00001986 return;
1987
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001988 Asm->OutStreamer.SwitchSection(
1989 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00001990
Chris Lattner233f52b2010-03-09 23:52:58 +00001991 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00001992 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
1993 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00001994
Chris Lattnerc0215722010-04-04 19:25:43 +00001995 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00001996
Chris Lattner233f52b2010-03-09 23:52:58 +00001997 Asm->OutStreamer.AddComment("Dwarf Version");
1998 Asm->EmitInt16(dwarf::DWARF_VERSION);
1999 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002000 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002001
Devang Patele9f8f5e2010-05-07 20:54:48 +00002002 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002003 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002004
Devang Patele9f8f5e2010-05-07 20:54:48 +00002005 const MDNode *Node = *I;
2006 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002007 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002008 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002009 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002010 StringRef LName = SP.getLinkageName();
2011 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002012
Chris Lattner233f52b2010-03-09 23:52:58 +00002013 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002014 if (LName.empty()) {
2015 Asm->OutStreamer.EmitBytes(Name, 0);
2016 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002017 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00002018 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2019 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002020
Chris Lattner233f52b2010-03-09 23:52:58 +00002021 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002022 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002023 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002024
Devang Patel53bb5c92009-11-10 23:06:00 +00002025 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002026 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002027 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002028 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002029
Chris Lattner3f53c832010-04-04 18:52:31 +00002030 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002031 Asm->OutStreamer.EmitSymbolValue(LI->first,
2032 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002033 }
2034 }
2035
Chris Lattnerc0215722010-04-04 19:25:43 +00002036 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002037}