blob: e51b30e487373e52f01c3225d4b7199cdac1357b [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"
Chris Lattner45111d12010-01-16 21:57:06 +000027#include "llvm/Target/Mangler.h"
Bill Wendling0310d762009-05-15 09:23:25 +000028#include "llvm/Target/TargetData.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000029#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000031#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000034#include "llvm/Analysis/DebugInfo.h"
Devang Patel0eea95d2011-02-24 18:49:30 +000035#include "llvm/Analysis/DIBuilder.h"
Devang Patel9a31f0f2010-10-25 20:45:32 +000036#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000037#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000038#include "llvm/ADT/StringExtras.h"
Devang Pateleac9c072010-04-27 19:46:33 +000039#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000040#include "llvm/Support/Debug.h"
41#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000042#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000043#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000044#include "llvm/Support/Timer.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000045#include "llvm/Support/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000046using namespace llvm;
47
Jim Grosbach1e20b962010-07-21 21:21:52 +000048static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel61409622010-07-07 20:12:52 +000049 cl::Hidden,
Devang Pateleac9c072010-04-27 19:46:33 +000050 cl::desc("Disable debug info printing"));
51
Dan Gohman281d65d2010-05-07 01:08:53 +000052static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner7a2bdde2011-04-15 05:18:47 +000053 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman281d65d2010-05-07 01:08:53 +000054 cl::init(false));
55
Bill Wendling5f017e82010-04-07 09:28:04 +000056namespace {
57 const char *DWARFGroupName = "DWARF Emission";
58 const char *DbgTimerName = "DWARF Debug Writer";
59} // end anonymous namespace
60
Bill Wendling0310d762009-05-15 09:23:25 +000061//===----------------------------------------------------------------------===//
62
63/// Configuration values for initial hash set sizes (log2).
64///
Bill Wendling0310d762009-05-15 09:23:25 +000065static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000066
67namespace llvm {
68
Nick Lewycky3bbb6f72011-07-29 03:49:23 +000069DIType DbgVariable::getType() const {
Devang Patel3cbee302011-04-12 22:53:02 +000070 DIType Ty = Var.getType();
71 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
72 // addresses instead.
73 if (Var.isBlockByrefVariable()) {
74 /* Byref variables, in Blocks, are declared by the programmer as
75 "SomeType VarName;", but the compiler creates a
76 __Block_byref_x_VarName struct, and gives the variable VarName
77 either the struct, or a pointer to the struct, as its type. This
78 is necessary for various behind-the-scenes things the compiler
79 needs to do with by-reference variables in blocks.
80
81 However, as far as the original *programmer* is concerned, the
82 variable should still have type 'SomeType', as originally declared.
83
84 The following function dives into the __Block_byref_x_VarName
85 struct to find the original type of the variable. This will be
86 passed back to the code generating the type for the Debug
87 Information Entry for the variable 'VarName'. 'VarName' will then
88 have the original type 'SomeType' in its debug information.
89
90 The original type 'SomeType' will be the type of the field named
91 'VarName' inside the __Block_byref_x_VarName struct.
92
93 NOTE: In order for this to not completely fail on the debugger
94 side, the Debug Information Entry for the variable VarName needs to
95 have a DW_AT_location that tells the debugger how to unwind through
96 the pointers and __Block_byref_x_VarName struct to find the actual
97 value of the variable. The function addBlockByrefType does this. */
98 DIType subType = Ty;
99 unsigned tag = Ty.getTag();
100
101 if (tag == dwarf::DW_TAG_pointer_type) {
102 DIDerivedType DTy = DIDerivedType(Ty);
103 subType = DTy.getTypeDerivedFrom();
104 }
105
106 DICompositeType blockStruct = DICompositeType(subType);
107 DIArray Elements = blockStruct.getTypeArray();
108
109 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
110 DIDescriptor Element = Elements.getElement(i);
111 DIDerivedType DT = DIDerivedType(Element);
112 if (getName() == DT.getName())
113 return (DT.getTypeDerivedFrom());
Devang Patel8bd11de2010-08-09 21:01:39 +0000114 }
115 return Ty;
116 }
Devang Patel3cbee302011-04-12 22:53:02 +0000117 return Ty;
118}
Bill Wendling0310d762009-05-15 09:23:25 +0000119
Chris Lattnerea761862010-04-05 04:09:20 +0000120} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000121
Chris Lattner49cd6642010-04-05 05:11:15 +0000122DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000123 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000124 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelbf47fdb2011-08-10 20:55:27 +0000125 PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000126 NextStringPoolNumber = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000127
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000128 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000129 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000130 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000131 FunctionBeginSym = FunctionEndSym = 0;
Dan Gohman03c3dc72010-06-18 15:56:31 +0000132 {
133 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
134 beginModule(M);
Torok Edwin9c421072010-04-07 10:44:46 +0000135 }
Bill Wendling0310d762009-05-15 09:23:25 +0000136}
137DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000138}
139
Chris Lattnerbc733f52010-03-13 02:17:42 +0000140MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
141 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
142 if (Entry.first) return Entry.first;
143
144 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000145 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000146}
147
148
Devang Patel2c4ceb12009-11-21 02:48:08 +0000149/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000150///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000151void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000152 // Profile the node so that we can make it unique.
153 FoldingSetNodeID ID;
154 Abbrev.Profile(ID);
155
156 // Check the set for priors.
157 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
158
159 // If it's newly added.
160 if (InSet == &Abbrev) {
161 // Add to abbreviation list.
162 Abbreviations.push_back(&Abbrev);
163
164 // Assign the vector position + 1 as its number.
165 Abbrev.setNumber(Abbreviations.size());
166 } else {
167 // Assign existing abbreviation number.
168 Abbrev.setNumber(InSet->getNumber());
169 }
170}
171
Jim Grosbach1e20b962010-07-21 21:21:52 +0000172/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel351ca332010-01-05 01:46:14 +0000173/// printer to not emit usual symbol prefix before the symbol name is used then
174/// return linkage name after skipping this special LLVM prefix.
175static StringRef getRealLinkageName(StringRef LinkageName) {
176 char One = '\1';
177 if (LinkageName.startswith(StringRef(&One, 1)))
178 return LinkageName.substr(1);
179 return LinkageName;
180}
181
Devang Patel5f094002010-04-06 23:53:48 +0000182/// isSubprogramContext - Return true if Context is either a subprogram
183/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000184static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +0000185 if (!Context)
186 return false;
187 DIDescriptor D(Context);
188 if (D.isSubprogram())
189 return true;
190 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +0000191 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +0000192 return false;
193}
194
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000195/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000196/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
197/// If there are global variables in this scope then create and insert
198/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000199DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000200 CompileUnit *SPCU = getCompileUnit(SPNode);
201 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000202
Chris Lattnerd38fee82010-04-05 00:13:49 +0000203 assert(SPDie && "Unable to find subprogram DIE!");
204 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000205
Devang Patel5e06bb82011-04-22 23:10:17 +0000206 DISubprogram SPDecl = SP.getFunctionDeclaration();
207 if (SPDecl.isSubprogram())
208 // Refer function declaration directly.
Devang Patel3cbee302011-04-12 22:53:02 +0000209 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Devang Pateldbc64af2011-08-15 17:24:54 +0000210 SPCU->getOrCreateSubprogramDIE(SPDecl));
Devang Patel5e06bb82011-04-22 23:10:17 +0000211 else {
212 // There is not any need to generate specification DIE for a function
213 // defined at compile unit level. If a function is defined inside another
214 // function then gdb prefers the definition at top level and but does not
215 // expect specification DIE in parent function. So avoid creating
216 // specification DIE for a function defined inside a function.
217 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
218 !SP.getContext().isFile() &&
219 !isSubprogramContext(SP.getContext())) {
220 SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
221
222 // Add arguments.
223 DICompositeType SPTy = SP.getType();
224 DIArray Args = SPTy.getTypeArray();
225 unsigned SPTag = SPTy.getTag();
226 if (SPTag == dwarf::DW_TAG_subroutine_type)
227 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
228 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
229 DIType ATy = DIType(DIType(Args.getElement(i)));
230 SPCU->addType(Arg, ATy);
231 if (ATy.isArtificial())
232 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
233 SPDie->addChild(Arg);
234 }
235 DIE *SPDeclDie = SPDie;
236 SPDie = new DIE(dwarf::DW_TAG_subprogram);
237 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
238 SPDeclDie);
239 SPCU->addDie(SPDie);
240 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000241 }
Devang Patel8aa61472010-07-07 22:20:57 +0000242 // Pick up abstract subprogram DIE.
243 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
244 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000245 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
246 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000247 SPCU->addDie(SPDie);
248 }
249
Devang Patel3cbee302011-04-12 22:53:02 +0000250 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
251 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
252 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
253 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000254 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
255 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000256 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000257
Chris Lattnerd38fee82010-04-05 00:13:49 +0000258 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000259}
260
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000261/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000262/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000263DIE *DwarfDebug::constructLexicalScopeDIE(LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000264
265 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
266 if (Scope->isAbstractScope())
267 return ScopeDIE;
268
Devang Patelbf47fdb2011-08-10 20:55:27 +0000269 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000270 if (Ranges.empty())
271 return 0;
272
Devang Patel3cbee302011-04-12 22:53:02 +0000273 CompileUnit *TheCU = getCompileUnit(Scope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000274 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000275 if (Ranges.size() > 1) {
276 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000277 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000278 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000279 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000280 DebugRangeSymbols.size()
281 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000282 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000283 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000284 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
285 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000286 }
287 DebugRangeSymbols.push_back(NULL);
288 DebugRangeSymbols.push_back(NULL);
289 return ScopeDIE;
290 }
291
Devang Patelc3f5f782010-05-25 23:40:22 +0000292 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
293 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000294
Devang Patelc3f5f782010-05-25 23:40:22 +0000295 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000296
Chris Lattnerb7db7332010-03-09 01:58:53 +0000297 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
298 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000299
Devang Patel3cbee302011-04-12 22:53:02 +0000300 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
301 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000302
303 return ScopeDIE;
304}
305
Devang Patel2c4ceb12009-11-21 02:48:08 +0000306/// constructInlinedScopeDIE - This scope represents inlined body of
307/// a function. Construct DIE to represent this concrete inlined copy
308/// of the function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000309DIE *DwarfDebug::constructInlinedScopeDIE(LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000310
Devang Patelbf47fdb2011-08-10 20:55:27 +0000311 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000312 assert (Ranges.empty() == false
Devang Patelbf47fdb2011-08-10 20:55:27 +0000313 && "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000314
Devang Patel26a92002011-07-27 00:34:13 +0000315 if (!Scope->getScopeNode())
316 return NULL;
317 DIScope DS(Scope->getScopeNode());
318 DISubprogram InlinedSP = getDISubprogram(DS);
319 CompileUnit *TheCU = getCompileUnit(InlinedSP);
320 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
321 if (!OriginDIE) {
322 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
323 return NULL;
324 }
325
Devang Patelbf47fdb2011-08-10 20:55:27 +0000326 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000327 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
328 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000329
Devang Patel0afbf232010-07-08 22:39:20 +0000330 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000331 assert (0 && "Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000332 return 0;
333 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000334 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000335 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000336 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000337 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000338
Devang Pateld96efb82011-05-05 17:54:26 +0000339 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000340 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
341 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000342
Devang Patel26a92002011-07-27 00:34:13 +0000343 if (Ranges.size() > 1) {
344 // .debug_range section has not been laid out yet. Emit offset in
345 // .debug_range as a uint, size 4, for now. emitDIE will handle
346 // DW_AT_ranges appropriately.
347 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel5bc942c2011-08-10 23:58:09 +0000348 DebugRangeSymbols.size()
349 * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000350 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000351 RE = Ranges.end(); RI != RE; ++RI) {
352 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
353 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
354 }
355 DebugRangeSymbols.push_back(NULL);
356 DebugRangeSymbols.push_back(NULL);
357 } else {
Devang Patel5bc942c2011-08-10 23:58:09 +0000358 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
359 StartLabel);
360 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
361 EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000362 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000363
364 InlinedSubprogramDIEs.insert(OriginDIE);
365
366 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000367 //.debug_inlined section specification does not clearly state how
368 // to emit inlined scope that is split into multiple instruction ranges.
369 // For now, use first instruction range and emit low_pc/high_pc pair and
370 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000371 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000372 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000373
374 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000375 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000376 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000377 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000378 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000379 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000380
Devang Patel53bb5c92009-11-10 23:06:00 +0000381 DILocation DL(Scope->getInlinedAt());
Devang Patel3cbee302011-04-12 22:53:02 +0000382 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
383 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000384
385 return ScopeDIE;
386}
387
Devang Patel8594d422011-06-24 20:46:11 +0000388/// isUnsignedDIType - Return true if type encoding is unsigned.
389static bool isUnsignedDIType(DIType Ty) {
390 DIDerivedType DTy(Ty);
391 if (DTy.Verify())
392 return isUnsignedDIType(DTy.getTypeDerivedFrom());
393
394 DIBasicType BTy(Ty);
395 if (BTy.Verify()) {
396 unsigned Encoding = BTy.getEncoding();
397 if (Encoding == dwarf::DW_ATE_unsigned ||
398 Encoding == dwarf::DW_ATE_unsigned_char)
399 return true;
400 }
401 return false;
402}
Devang Patel2c4ceb12009-11-21 02:48:08 +0000403
404/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000405DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, LexicalScope *Scope) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000406 StringRef Name = DV->getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000407 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +0000408 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +0000409
410 // Translate tag to proper Dwarf tag. The result variable is dropped for
411 // now.
412 unsigned Tag;
Devang Patel8bd11de2010-08-09 21:01:39 +0000413 switch (DV->getTag()) {
Devang Patel53bb5c92009-11-10 23:06:00 +0000414 case dwarf::DW_TAG_return_variable:
415 return NULL;
416 case dwarf::DW_TAG_arg_variable:
417 Tag = dwarf::DW_TAG_formal_parameter;
418 break;
419 case dwarf::DW_TAG_auto_variable: // fall thru
420 default:
421 Tag = dwarf::DW_TAG_variable;
422 break;
423 }
424
425 // Define variable debug information entry.
426 DIE *VariableDie = new DIE(Tag);
Devang Patel9341d102011-04-25 23:05:21 +0000427 CompileUnit *VariableCU = getCompileUnit(DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +0000428 DIE *AbsDIE = NULL;
Devang Patel26c1e562010-05-20 16:36:41 +0000429 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
430 V2AVI = VarToAbstractVarMap.find(DV);
431 if (V2AVI != VarToAbstractVarMap.end())
432 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000433
Devang Patel26c1e562010-05-20 16:36:41 +0000434 if (AbsDIE)
Devang Patel9341d102011-04-25 23:05:21 +0000435 VariableCU->addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel3cbee302011-04-12 22:53:02 +0000436 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000437 else {
Devang Patel9341d102011-04-25 23:05:21 +0000438 VariableCU->addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
439 Name);
440 VariableCU->addSourceLine(VariableDie, DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +0000441
442 // Add variable type.
Devang Patel9341d102011-04-25 23:05:21 +0000443 VariableCU->addType(VariableDie, DV->getType());
Devang Patel53bb5c92009-11-10 23:06:00 +0000444 }
445
Devang Patel8bd11de2010-08-09 21:01:39 +0000446 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patel9341d102011-04-25 23:05:21 +0000447 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
448 dwarf::DW_FORM_flag, 1);
Devang Patel3cf763d2010-09-29 23:07:21 +0000449 else if (DIVariable(DV->getVariable()).isArtificial())
Devang Patel9341d102011-04-25 23:05:21 +0000450 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
451 dwarf::DW_FORM_flag, 1);
Devang Patelc3f5f782010-05-25 23:40:22 +0000452
453 if (Scope->isAbstractScope()) {
454 DV->setDIE(VariableDie);
455 return VariableDie;
456 }
457
458 // Add variable address.
459
460 unsigned Offset = DV->getDotDebugLocOffset();
461 if (Offset != ~0U) {
Devang Patel5bc942c2011-08-10 23:58:09 +0000462 VariableCU->addLabel(VariableDie, dwarf::DW_AT_location,
463 dwarf::DW_FORM_data4,
464 Asm->GetTempSymbol("debug_loc", Offset));
Devang Patelc3f5f782010-05-25 23:40:22 +0000465 DV->setDIE(VariableDie);
466 UseDotDebugLocEntry.insert(VariableDie);
467 return VariableDie;
468 }
469
470 // Check if variable is described by a DBG_VALUE instruction.
471 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
472 DbgVariableToDbgInstMap.find(DV);
473 if (DVI != DbgVariableToDbgInstMap.end()) {
474 const MachineInstr *DVInsn = DVI->second;
Devang Patelc3f5f782010-05-25 23:40:22 +0000475 bool updated = false;
Devang Patelc3f5f782010-05-25 23:40:22 +0000476 if (DVInsn->getNumOperands() == 3) {
Devang Patel0b48ead2010-08-31 22:22:42 +0000477 if (DVInsn->getOperand(0).isReg()) {
478 const MachineOperand RegOp = DVInsn->getOperand(0);
479 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
480 if (DVInsn->getOperand(1).isImm() &&
481 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
Devang Patele1cdf842011-04-27 22:45:24 +0000482 unsigned FrameReg = 0;
483 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
484 int Offset =
485 TFI->getFrameIndexReference(*Asm->MF,
486 DVInsn->getOperand(1).getImm(),
487 FrameReg);
488 MachineLocation Location(FrameReg, Offset);
489 VariableCU->addVariableAddress(DV, VariableDie, Location);
490
491 } else if (RegOp.getReg())
492 VariableCU->addVariableAddress(DV, VariableDie,
493 MachineLocation(RegOp.getReg()));
494 updated = true;
Devang Patel0b48ead2010-08-31 22:22:42 +0000495 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000496 else if (DVInsn->getOperand(0).isImm())
Devang Patelb58128e2011-05-27 16:45:18 +0000497 updated =
498 VariableCU->addConstantValue(VariableDie, DVInsn->getOperand(0),
499 DV->getType());
Jim Grosbach1e20b962010-07-21 21:21:52 +0000500 else if (DVInsn->getOperand(0).isFPImm())
501 updated =
Devang Patel9341d102011-04-25 23:05:21 +0000502 VariableCU->addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patel8594d422011-06-24 20:46:11 +0000503 else if (DVInsn->getOperand(0).isCImm())
504 updated =
505 VariableCU->addConstantValue(VariableDie,
506 DVInsn->getOperand(0).getCImm(),
507 isUnsignedDIType(DV->getType()));
Devang Patelc3f5f782010-05-25 23:40:22 +0000508 } else {
Devang Patele1cdf842011-04-27 22:45:24 +0000509 VariableCU->addVariableAddress(DV, VariableDie,
510 Asm->getDebugValueLocation(DVInsn));
511 updated = true;
Devang Patelc3f5f782010-05-25 23:40:22 +0000512 }
513 if (!updated) {
514 // If variableDie is not updated then DBG_VALUE instruction does not
515 // have valid variable info.
516 delete VariableDie;
517 return NULL;
518 }
519 DV->setDIE(VariableDie);
520 return VariableDie;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000521 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000522
523 // .. else use frame index, if available.
Devang Patelc3f5f782010-05-25 23:40:22 +0000524 int FI = 0;
Devang Patele1cdf842011-04-27 22:45:24 +0000525 if (findVariableFrameIndex(DV, &FI)) {
526 unsigned FrameReg = 0;
527 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
528 int Offset =
529 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
530 MachineLocation Location(FrameReg, Offset);
531 VariableCU->addVariableAddress(DV, VariableDie, Location);
532 }
533
Devang Patel53bb5c92009-11-10 23:06:00 +0000534 DV->setDIE(VariableDie);
535 return VariableDie;
536
537}
Devang Patel2c4ceb12009-11-21 02:48:08 +0000538
539/// constructScopeDIE - Construct a DIE for this scope.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000540DIE *DwarfDebug::constructScopeDIE(LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000541 if (!Scope || !Scope->getScopeNode())
542 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000543
Devang Patel5bc9fec2011-02-19 01:31:27 +0000544 SmallVector <DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000545
546 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000547 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000548 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
549 if (DbgVariable *ArgDV = CurrentFnArguments[i])
550 if (DIE *Arg = constructVariableDIE(ArgDV, Scope))
551 Children.push_back(Arg);
552
Devang Patel5bc9fec2011-02-19 01:31:27 +0000553 // Collect lexical scope childrens first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000554 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000555 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
556 if (DIE *Variable = constructVariableDIE(Variables[i], Scope))
557 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000558 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000559 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
560 if (DIE *Nested = constructScopeDIE(Scopes[j]))
561 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000562 DIScope DS(Scope->getScopeNode());
563 DIE *ScopeDIE = NULL;
564 if (Scope->getInlinedAt())
565 ScopeDIE = constructInlinedScopeDIE(Scope);
566 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000567 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000568 if (Scope->isAbstractScope()) {
Devang Patel163a9f72010-05-10 22:49:55 +0000569 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000570 // Note down abstract DIE.
571 if (ScopeDIE)
572 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
573 }
Devang Patel3c91b052010-03-08 20:52:55 +0000574 else
Devang Patel2db49d72010-05-07 18:11:54 +0000575 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000576 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000577 else {
578 // There is no need to emit empty lexical block DIE.
579 if (Children.empty())
580 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +0000581 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000582 }
583
Devang Patelaead63c2010-03-29 22:59:58 +0000584 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000585
Devang Patel5bc9fec2011-02-19 01:31:27 +0000586 // Add children
587 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
588 E = Children.end(); I != E; ++I)
589 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000590
Jim Grosbach1e20b962010-07-21 21:21:52 +0000591 if (DS.isSubprogram())
Devang Patel3cbee302011-04-12 22:53:02 +0000592 getCompileUnit(DS)->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000593
Devang Patel193f7202009-11-24 01:14:22 +0000594 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000595}
596
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000597/// GetOrCreateSourceID - Look up the source id with the given directory and
598/// source file names. If none currently exists, create a new id and insert it
599/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
600/// maps as well.
Devang Patel2f584852010-07-24 00:53:22 +0000601
Devang Patel23670e52011-03-24 20:30:50 +0000602unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
603 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000604 // If FE did not provide a file name, then assume stdin.
605 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000606 return GetOrCreateSourceID("<stdin>", StringRef());
607
608 // MCStream expects full path name as filename.
Benjamin Kramerab5f7882011-06-05 14:36:47 +0000609 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
610 SmallString<128> FullPathName = DirName;
611 sys::path::append(FullPathName, FileName);
Devang Patel23670e52011-03-24 20:30:50 +0000612 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
613 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
614 }
Devang Patel1905a182010-09-16 20:57:49 +0000615
Rafael Espindola5c055632010-11-18 02:04:25 +0000616 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
617 if (Entry.getValue())
618 return Entry.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000619
Rafael Espindola5c055632010-11-18 02:04:25 +0000620 unsigned SrcId = SourceIdMap.size();
621 Entry.setValue(SrcId);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000622
Rafael Espindola5c055632010-11-18 02:04:25 +0000623 // Print out a .file directive to specify files for .loc directives.
Devang Patel23670e52011-03-24 20:30:50 +0000624 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000625
626 return SrcId;
627}
628
Jim Grosbach1e20b962010-07-21 21:21:52 +0000629/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000630/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000631void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000632 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000633 StringRef FN = DIUnit.getFilename();
634 StringRef Dir = DIUnit.getDirectory();
Devang Patel23670e52011-03-24 20:30:50 +0000635 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000636
637 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel3cbee302011-04-12 22:53:02 +0000638 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
639 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
640 DIUnit.getProducer());
641 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
642 DIUnit.getLanguage());
643 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +0000644 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
645 // simplifies debug range entries.
Devang Patel3cbee302011-04-12 22:53:02 +0000646 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000647 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000648 // compile unit in debug_line section.
Rafael Espindoladc52ecf2011-05-10 20:35:05 +0000649 if(Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindola597a7662011-05-04 17:44:06 +0000650 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000651 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000652 else
Devang Patel3cbee302011-04-12 22:53:02 +0000653 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000654
Devang Patel65dbc902009-11-25 17:36:49 +0000655 if (!Dir.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000656 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000657 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000658 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000659
Devang Patel65dbc902009-11-25 17:36:49 +0000660 StringRef Flags = DIUnit.getFlags();
661 if (!Flags.empty())
Devang Patel5bc942c2011-08-10 23:58:09 +0000662 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string,
663 Flags);
Devang Patel3cbee302011-04-12 22:53:02 +0000664
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000665 unsigned RVer = DIUnit.getRunTimeVersion();
666 if (RVer)
Devang Patel3cbee302011-04-12 22:53:02 +0000667 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000668 dwarf::DW_FORM_data1, RVer);
669
Devang Patel163a9f72010-05-10 22:49:55 +0000670 if (!FirstCU)
671 FirstCU = NewCU;
672 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000673}
674
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000675/// getCompileUnit - Get CompileUnit DIE.
Devang Patel163a9f72010-05-10 22:49:55 +0000676CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
677 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
678 DIDescriptor D(N);
679 const MDNode *CUNode = NULL;
680 if (D.isCompileUnit())
681 CUNode = N;
682 else if (D.isSubprogram())
683 CUNode = DISubprogram(N).getCompileUnit();
684 else if (D.isType())
685 CUNode = DIType(N).getCompileUnit();
686 else if (D.isGlobalVariable())
687 CUNode = DIGlobalVariable(N).getCompileUnit();
688 else if (D.isVariable())
689 CUNode = DIVariable(N).getCompileUnit();
690 else if (D.isNameSpace())
691 CUNode = DINameSpace(N).getCompileUnit();
692 else if (D.isFile())
693 CUNode = DIFile(N).getCompileUnit();
694 else
695 return FirstCU;
696
697 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
698 = CUMap.find(CUNode);
699 if (I == CUMap.end())
700 return FirstCU;
701 return I->second;
702}
703
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000704// Return const expression if value is a GEP to access merged global
Devang Patele449d1f2011-01-20 00:02:16 +0000705// constant. e.g.
706// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
707static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
708 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
709 if (!CE || CE->getNumOperands() != 3 ||
Bob Wilsoncfc55c22011-08-03 19:42:51 +0000710 CE->getOpcode() != Instruction::GetElementPtr)
Devang Patele449d1f2011-01-20 00:02:16 +0000711 return NULL;
712
Bob Wilsoncfc55c22011-08-03 19:42:51 +0000713 // First operand points to a global struct.
714 Value *Ptr = CE->getOperand(0);
715 if (!isa<GlobalValue>(Ptr) ||
716 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
Devang Patele449d1f2011-01-20 00:02:16 +0000717 return NULL;
718
719 // Second operand is zero.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000720 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
Devang Patele449d1f2011-01-20 00:02:16 +0000721 if (!CI || !CI->isZero())
722 return NULL;
723
724 // Third operand is offset.
725 if (!isa<ConstantInt>(CE->getOperand(2)))
726 return NULL;
727
728 return CE;
729}
730
Devang Patel163a9f72010-05-10 22:49:55 +0000731/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000732void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +0000733 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000734
Devang Patel905cf5e2009-09-04 23:59:07 +0000735 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +0000736 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +0000737 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000738
739 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000740 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel9fa539c2010-08-10 01:37:23 +0000741 if (TheCU->getDIE(GV))
Devang Patel13e16b62009-06-26 01:49:18 +0000742 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000743
Devang Patel9fa539c2010-08-10 01:37:23 +0000744 DIType GTy = GV.getType();
Devang Patel29368072010-08-10 07:11:13 +0000745 DIE *VariableDIE = new DIE(GV.getTag());
746
747 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000748
Devang Patel9fa539c2010-08-10 01:37:23 +0000749 // Add name.
Devang Patel3cbee302011-04-12 22:53:02 +0000750 TheCU->addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
751 GV.getDisplayName());
Devang Patel9fa539c2010-08-10 01:37:23 +0000752 StringRef LinkageName = GV.getLinkageName();
Devang Patel29368072010-08-10 07:11:13 +0000753 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel3cbee302011-04-12 22:53:02 +0000754 TheCU->addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
755 dwarf::DW_FORM_string,
756 getRealLinkageName(LinkageName));
Devang Patel9fa539c2010-08-10 01:37:23 +0000757 // Add type.
Devang Patel3cbee302011-04-12 22:53:02 +0000758 TheCU->addType(VariableDIE, GTy);
Devang Patele9ae06c2011-05-31 22:56:51 +0000759
Devang Patel9fa539c2010-08-10 01:37:23 +0000760 // Add scoping info.
761 if (!GV.isLocalToUnit()) {
Devang Patel3cbee302011-04-12 22:53:02 +0000762 TheCU->addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel9fa539c2010-08-10 01:37:23 +0000763 // Expose as global.
764 TheCU->addGlobal(GV.getName(), VariableDIE);
765 }
766 // Add line number info.
Devang Patel3cbee302011-04-12 22:53:02 +0000767 TheCU->addSourceLine(VariableDIE, GV);
Devang Patel9fa539c2010-08-10 01:37:23 +0000768 // Add to map.
769 TheCU->insertDIE(N, VariableDIE);
770 // Add to context owner.
771 DIDescriptor GVContext = GV.getContext();
Devang Patel3cbee302011-04-12 22:53:02 +0000772 TheCU->addToContextOwner(VariableDIE, GVContext);
Devang Patel9fa539c2010-08-10 01:37:23 +0000773 // Add location.
Devang Patel29368072010-08-10 07:11:13 +0000774 if (isGlobalVariable) {
775 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel3cbee302011-04-12 22:53:02 +0000776 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
777 TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patel29368072010-08-10 07:11:13 +0000778 Asm->Mang->getSymbol(GV.getGlobal()));
779 // Do not create specification DIE if context is either compile unit
780 // or a subprogram.
781 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
782 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
783 // Create specification DIE.
784 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel3cbee302011-04-12 22:53:02 +0000785 TheCU->addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
Devang Patel29368072010-08-10 07:11:13 +0000786 dwarf::DW_FORM_ref4, VariableDIE);
Devang Patel3cbee302011-04-12 22:53:02 +0000787 TheCU->addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel5bc942c2011-08-10 23:58:09 +0000788 TheCU->addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag,
789 1);
Devang Patel29368072010-08-10 07:11:13 +0000790 TheCU->addDie(VariableSpecDIE);
791 } else {
Devang Patel3cbee302011-04-12 22:53:02 +0000792 TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel29368072010-08-10 07:11:13 +0000793 }
Devang Patel8594d422011-06-24 20:46:11 +0000794 } else if (const ConstantInt *CI =
Devang Patel76a788c2011-01-06 21:39:25 +0000795 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
Devang Patel3cbee302011-04-12 22:53:02 +0000796 TheCU->addConstantValue(VariableDIE, CI, isUnsignedDIType(GTy));
Devang Patele449d1f2011-01-20 00:02:16 +0000797 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
798 // GV is a merged global.
799 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bob Wilsoncfc55c22011-08-03 19:42:51 +0000800 Value *Ptr = CE->getOperand(0);
Devang Patel3cbee302011-04-12 22:53:02 +0000801 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
802 TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
Bob Wilsoncfc55c22011-08-03 19:42:51 +0000803 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
Devang Patel3cbee302011-04-12 22:53:02 +0000804 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Bob Wilsoncfc55c22011-08-03 19:42:51 +0000805 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Devang Patel027cbf92011-08-03 01:25:46 +0000806 TheCU->addUInt(Block, 0, dwarf::DW_FORM_udata,
Bob Wilsoncfc55c22011-08-03 19:42:51 +0000807 Asm->getTargetData().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel3cbee302011-04-12 22:53:02 +0000808 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
809 TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Devang Patele449d1f2011-01-20 00:02:16 +0000810 }
Devang Patel76a788c2011-01-06 21:39:25 +0000811
Devang Patel13e16b62009-06-26 01:49:18 +0000812 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000813}
814
Devang Patel163a9f72010-05-10 22:49:55 +0000815/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000816void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000817 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000818
Stuart Hastings639336e2010-04-06 21:38:29 +0000819 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000820 CompileUnit *TheCU = getCompileUnit(N);
821 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +0000822 return;
823
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000824 if (!SP.isDefinition())
825 // This is a method declaration which will be handled while constructing
826 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000827 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000828
Devang Pateldbc64af2011-08-15 17:24:54 +0000829 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000830
831 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000832 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000833
834 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000835 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000836
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000837 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +0000838 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +0000839
Devang Patel13e16b62009-06-26 01:49:18 +0000840 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000841}
842
Devang Patel2c4ceb12009-11-21 02:48:08 +0000843/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000844/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000845/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000846void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000847 if (DisableDebugInfoPrinting)
848 return;
849
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000850 // If module has named metadata anchors then use them, otherwise scan the
851 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000852 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
853 if (CU_Nodes) {
Devang Patela938dd02011-05-04 16:34:02 +0000854
855 NamedMDNode *GV_Nodes = M->getNamedMetadata("llvm.dbg.gv");
856 NamedMDNode *SP_Nodes = M->getNamedMetadata("llvm.dbg.sp");
857 if (!GV_Nodes && !SP_Nodes)
858 // If there are not any global variables or any functions then
859 // there is not any debug info in this module.
Devang Patel30692ab2011-05-03 16:45:22 +0000860 return;
861
Devang Patela938dd02011-05-04 16:34:02 +0000862 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
863 constructCompileUnit(CU_Nodes->getOperand(i));
Devang Patel30692ab2011-05-03 16:45:22 +0000864
Devang Patela938dd02011-05-04 16:34:02 +0000865 if (GV_Nodes)
866 for (unsigned i = 0, e = GV_Nodes->getNumOperands(); i != e; ++i)
867 constructGlobalVariableDIE(GV_Nodes->getOperand(i));
868
869 if (SP_Nodes)
870 for (unsigned i = 0, e = SP_Nodes->getNumOperands(); i != e; ++i)
871 constructSubprogramDIE(SP_Nodes->getOperand(i));
Devang Patel30692ab2011-05-03 16:45:22 +0000872
873 } else {
874
875 DebugInfoFinder DbgFinder;
876 DbgFinder.processModule(*M);
877
Devang Patela938dd02011-05-04 16:34:02 +0000878 bool HasDebugInfo = false;
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000879 // Scan all the compile-units to see if there are any marked as the main
880 // unit. If not, we do not generate debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000881 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
882 E = DbgFinder.compile_unit_end(); I != E; ++I) {
883 if (DICompileUnit(*I).isMain()) {
884 HasDebugInfo = true;
885 break;
886 }
887 }
Devang Patela938dd02011-05-04 16:34:02 +0000888 if (!HasDebugInfo) return;
Devang Patel30692ab2011-05-03 16:45:22 +0000889
890 // Create all the compile unit DIEs.
891 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
892 E = DbgFinder.compile_unit_end(); I != E; ++I)
893 constructCompileUnit(*I);
894
895 // Create DIEs for each global variable.
896 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
897 E = DbgFinder.global_variable_end(); I != E; ++I)
898 constructGlobalVariableDIE(*I);
899
900 // Create DIEs for each subprogram.
901 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
902 E = DbgFinder.subprogram_end(); I != E; ++I)
903 constructSubprogramDIE(*I);
Chris Lattnerd850ac72010-04-05 02:19:28 +0000904 }
Devang Patel30692ab2011-05-03 16:45:22 +0000905
Chris Lattnerd850ac72010-04-05 02:19:28 +0000906 // Tell MMI that we have debug info.
907 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000908
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000909 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000910 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000911
Devang Patele7e5a0f2010-08-10 20:01:20 +0000912 //getOrCreateTypeDIE
913 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
Devang Patel3cbee302011-04-12 22:53:02 +0000914 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
915 DIType Ty(NMD->getOperand(i));
916 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
917 }
Devang Patele7e5a0f2010-08-10 20:01:20 +0000918
Devang Patel1a7ca032010-09-28 18:08:20 +0000919 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
Devang Patel3cbee302011-04-12 22:53:02 +0000920 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
921 DIType Ty(NMD->getOperand(i));
922 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
923 }
Devang Patel1a7ca032010-09-28 18:08:20 +0000924
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000925 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +0000926 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000927}
928
Devang Patel2c4ceb12009-11-21 02:48:08 +0000929/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000930///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000931void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +0000932 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +0000933 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +0000934 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +0000935 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
936 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
937 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
938 DISubprogram SP(AllSPs->getOperand(SI));
939 if (!SP.Verify()) continue;
940
941 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +0000942 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +0000943 StringRef FName = SP.getLinkageName();
944 if (FName.empty())
945 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +0000946 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patel4a1cad62010-06-28 18:25:03 +0000947 if (!NMD) continue;
948 unsigned E = NMD->getNumOperands();
949 if (!E) continue;
Devang Patel5bc942c2011-08-10 23:58:09 +0000950 LexicalScope *Scope = new LexicalScope(NULL, DIDescriptor(SP), NULL,
951 false);
Devang Patele9a1cca2010-08-02 17:32:15 +0000952 DeadFnScopeMap[SP] = Scope;
Benjamin Kramer0fa5e052011-08-11 18:39:28 +0000953 SmallVector<DbgVariable, 8> Variables;
Devang Patel4a1cad62010-06-28 18:25:03 +0000954 for (unsigned I = 0; I != E; ++I) {
955 DIVariable DV(NMD->getOperand(I));
956 if (!DV.Verify()) continue;
Benjamin Kramer0fa5e052011-08-11 18:39:28 +0000957 Variables.push_back(DbgVariable(DV));
Devang Patel4a1cad62010-06-28 18:25:03 +0000958 }
Jim Grosbach1e20b962010-07-21 21:21:52 +0000959
Devang Patel4a1cad62010-06-28 18:25:03 +0000960 // Construct subprogram DIE and add variables DIEs.
961 constructSubprogramDIE(SP);
962 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patel4a1cad62010-06-28 18:25:03 +0000963 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Benjamin Kramer0fa5e052011-08-11 18:39:28 +0000964 if (DIE *VariableDIE = constructVariableDIE(&Variables[i], Scope))
Devang Patel4a1cad62010-06-28 18:25:03 +0000965 ScopeDIE->addChild(VariableDIE);
966 }
967 }
968 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000969
Devang Patel53bb5c92009-11-10 23:06:00 +0000970 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
971 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
972 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
973 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000974 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000975 }
976
Devang Pateldbc64af2011-08-15 17:24:54 +0000977 // Emit DW_AT_containing_type attribute to connect types with their
978 // vtable holding type.
979 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
980 CUE = CUMap.end(); CUI != CUE; ++CUI) {
981 CompileUnit *TheCU = CUI->second;
982 TheCU->constructContainingTypeDIEs();
Devang Patel5d11eb02009-12-03 19:11:07 +0000983 }
984
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000985 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000986 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000987 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000988 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +0000989 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000990
991 // End text sections.
992 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000993 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +0000994 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000995 }
996
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000997 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000998 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000999
1000 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00001001 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001002
1003 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001004 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001005
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001006 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001007 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001008
Devang Patel193f7202009-11-24 01:14:22 +00001009 // Emit info into a debug pubtypes section.
1010 emitDebugPubTypes();
1011
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001012 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001013 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001014
1015 // Emit info into a debug aranges section.
1016 EmitDebugARanges();
1017
1018 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001019 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001020
1021 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001022 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001023
1024 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001025 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001026
Chris Lattnerbc733f52010-03-13 02:17:42 +00001027 // Emit info into a debug str section.
1028 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001029
Devang Patele9a1cca2010-08-02 17:32:15 +00001030 // clean up.
1031 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel163a9f72010-05-10 22:49:55 +00001032 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1033 E = CUMap.end(); I != E; ++I)
1034 delete I->second;
1035 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001036}
1037
Devang Patel53bb5c92009-11-10 23:06:00 +00001038/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +00001039DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +00001040 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +00001041 LLVMContext &Ctx = DV->getContext();
1042 // More then one inlined variable corresponds to one abstract variable.
1043 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +00001044 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001045 if (AbsDbgVariable)
1046 return AbsDbgVariable;
1047
Devang Patelbf47fdb2011-08-10 20:55:27 +00001048 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00001049 if (!Scope)
1050 return NULL;
1051
Devang Patel26c1e562010-05-20 16:36:41 +00001052 AbsDbgVariable = new DbgVariable(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001053 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00001054 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00001055 return AbsDbgVariable;
1056}
1057
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001058/// addCurrentFnArgument - If Var is a current function argument then add
1059/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +00001060bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +00001061 DbgVariable *Var, LexicalScope *Scope) {
1062 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +00001063 return false;
1064 DIVariable DV = Var->getVariable();
1065 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1066 return false;
1067 unsigned ArgNo = DV.getArgNumber();
1068 if (ArgNo == 0)
1069 return false;
1070
Devang Patelcb3a6572011-03-03 20:02:02 +00001071 size_t Size = CurrentFnArguments.size();
1072 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001073 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +00001074 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +00001075 // arguments does the function have at source level.
1076 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +00001077 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +00001078 CurrentFnArguments[ArgNo - 1] = Var;
1079 return true;
1080}
1081
Devang Patelee432862010-05-20 19:57:06 +00001082/// collectVariableInfoFromMMITable - Collect variable information from
1083/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001084void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001085DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +00001086 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +00001087 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1088 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1089 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00001090 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00001091 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00001092 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00001093 DIVariable DV(Var);
1094 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001095
Devang Patelbf47fdb2011-08-10 20:55:27 +00001096 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001097
Devang Patelfb0ee432009-11-10 23:20:04 +00001098 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00001099 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00001100 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001101
Devang Patel26c1e562010-05-20 16:36:41 +00001102 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
1103 DbgVariable *RegVar = new DbgVariable(DV);
1104 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel0478c152011-03-01 22:58:55 +00001105 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001106 addScopeVariable(Scope, RegVar);
Devang Patel26c1e562010-05-20 16:36:41 +00001107 if (AbsDbgVariable) {
1108 recordVariableFrameIndex(AbsDbgVariable, VP.first);
1109 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
1110 }
Devang Patele717faa2009-10-06 01:26:37 +00001111 }
Devang Patelee432862010-05-20 19:57:06 +00001112}
Devang Patel90a48ad2010-03-15 18:33:46 +00001113
Jim Grosbach1e20b962010-07-21 21:21:52 +00001114/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00001115/// DBG_VALUE instruction, is in a defined reg.
1116static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
1117 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001118 return MI->getNumOperands() == 3 &&
1119 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
1120 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +00001121}
1122
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001123/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +00001124/// at MI.
1125static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1126 const MCSymbol *FLabel,
1127 const MCSymbol *SLabel,
1128 const MachineInstr *MI) {
1129 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1130
1131 if (MI->getNumOperands() != 3) {
1132 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1133 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1134 }
1135 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1136 MachineLocation MLoc;
1137 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1138 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1139 }
1140 if (MI->getOperand(0).isImm())
1141 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1142 if (MI->getOperand(0).isFPImm())
1143 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1144 if (MI->getOperand(0).isCImm())
1145 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1146
1147 assert (0 && "Unexpected 3 operand DBG_VALUE instruction!");
1148 return DotDebugLocEntry();
1149}
1150
Devang Patelbf47fdb2011-08-10 20:55:27 +00001151/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001152void
Devang Patel78e127d2010-06-25 22:07:34 +00001153DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1154 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001155
Devang Patelee432862010-05-20 19:57:06 +00001156 /// collection info from MMI table.
1157 collectVariableInfoFromMMITable(MF, Processed);
1158
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001159 for (SmallVectorImpl<const MDNode*>::const_iterator
1160 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1161 ++UVI) {
1162 const MDNode *Var = *UVI;
1163 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001164 continue;
1165
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001166 // History contains relevant DBG_VALUE instructions for Var and instructions
1167 // clobbering it.
1168 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1169 if (History.empty())
1170 continue;
1171 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001172
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001173 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001174 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001175 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1176 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001177 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001178 else {
1179 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001180 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001181 else {
1182 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001183 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001184 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001185 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001186 }
1187 }
Devang Patelee432862010-05-20 19:57:06 +00001188 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001189 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001190 continue;
1191
1192 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001193 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patelee432862010-05-20 19:57:06 +00001194 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel0478c152011-03-01 22:58:55 +00001195 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001196 addScopeVariable(Scope, RegVar);
Devang Patelc0c5a262010-05-21 00:10:20 +00001197 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
1198 DbgVariableToDbgInstMap[AbsVar] = MInsn;
1199 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00001200 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001201
1202 // Simple ranges that are fully coalesced.
1203 if (History.size() <= 1 || (History.size() == 2 &&
1204 MInsn->isIdenticalTo(History.back()))) {
Devang Patelc3f5f782010-05-25 23:40:22 +00001205 DbgVariableToDbgInstMap[RegVar] = MInsn;
1206 continue;
1207 }
1208
1209 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001210 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001211
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001212 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1213 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1214 const MachineInstr *Begin = *HI;
1215 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001216
Devang Patel4ada1d72011-06-01 23:00:17 +00001217 // Check if DBG_VALUE is truncating a range.
1218 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1219 && !Begin->getOperand(0).getReg())
1220 continue;
1221
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001222 // Compute the range for a register location.
1223 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1224 const MCSymbol *SLabel = 0;
1225
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001226 if (HI + 1 == HE)
1227 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001228 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001229 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001230 else {
1231 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001232 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1233 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001234 if (End->isDebugValue())
1235 SLabel = getLabelBeforeInsn(End);
1236 else {
1237 // End is a normal instruction clobbering the range.
1238 SLabel = getLabelAfterInsn(End);
1239 assert(SLabel && "Forgot label after clobber instruction");
1240 ++HI;
1241 }
1242 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001243
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001244 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel90b40412011-07-08 17:09:57 +00001245 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001246 }
1247 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001248 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001249
1250 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +00001251 const Function *F = MF->getFunction();
Devang Patel62367042010-11-10 22:19:21 +00001252 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patel98e1cac2010-05-14 21:01:35 +00001253 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001254 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +00001255 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +00001256 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001257 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1258 addScopeVariable(Scope, new DbgVariable(DV));
Devang Patel98e1cac2010-05-14 21:01:35 +00001259 }
1260 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001261}
Devang Patel98e1cac2010-05-14 21:01:35 +00001262
Devang Patelc3f5f782010-05-25 23:40:22 +00001263/// getLabelBeforeInsn - Return Label preceding the instruction.
1264const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001265 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1266 assert(Label && "Didn't insert label before instruction");
1267 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001268}
1269
1270/// getLabelAfterInsn - Return Label immediately following the instruction.
1271const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001272 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001273}
1274
Devang Patelcbbe2872010-10-26 17:49:02 +00001275/// beginInstruction - Process beginning of an instruction.
1276void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001277 // Check if source location changes, but ignore DBG_VALUE locations.
1278 if (!MI->isDebugValue()) {
1279 DebugLoc DL = MI->getDebugLoc();
1280 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel4243e672011-05-11 19:22:19 +00001281 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001282 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001283 if (DL == PrologEndLoc) {
1284 Flags |= DWARF2_FLAG_PROLOGUE_END;
1285 PrologEndLoc = DebugLoc();
1286 }
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001287 if (!DL.isUnknown()) {
1288 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001289 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001290 } else
Devang Patel4243e672011-05-11 19:22:19 +00001291 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001292 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001293 }
Devang Patelaead63c2010-03-29 22:59:58 +00001294
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001295 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001296 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1297 LabelsBeforeInsn.find(MI);
1298
1299 // No label needed.
1300 if (I == LabelsBeforeInsn.end())
1301 return;
1302
1303 // Label already assigned.
1304 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001305 return;
Devang Patel553881b2010-03-29 17:20:31 +00001306
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001307 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001308 PrevLabel = MMI->getContext().CreateTempSymbol();
1309 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001310 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001311 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001312}
1313
Devang Patelcbbe2872010-10-26 17:49:02 +00001314/// endInstruction - Process end of an instruction.
1315void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001316 // Don't create a new label after DBG_VALUE instructions.
1317 // They don't generate code.
1318 if (!MI->isDebugValue())
1319 PrevLabel = 0;
1320
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001321 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1322 LabelsAfterInsn.find(MI);
1323
1324 // No label needed.
1325 if (I == LabelsAfterInsn.end())
1326 return;
1327
1328 // Label already assigned.
1329 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001330 return;
1331
1332 // We need a label after this instruction.
1333 if (!PrevLabel) {
1334 PrevLabel = MMI->getContext().CreateTempSymbol();
1335 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001336 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001337 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001338}
1339
Jim Grosbach1e20b962010-07-21 21:21:52 +00001340/// identifyScopeMarkers() -
Devang Patel5bc942c2011-08-10 23:58:09 +00001341/// Each LexicalScope has first instruction and last instruction to mark
1342/// beginning and end of a scope respectively. Create an inverse map that list
1343/// scopes starts (and ends) with an instruction. One instruction may start (or
1344/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001345void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001346 SmallVector<LexicalScope *, 4> WorkList;
1347 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001348 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001349 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001350
Devang Patelbf47fdb2011-08-10 20:55:27 +00001351 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001352 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001353 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001354 SE = Children.end(); SI != SE; ++SI)
1355 WorkList.push_back(*SI);
1356
Devang Patel53bb5c92009-11-10 23:06:00 +00001357 if (S->isAbstractScope())
1358 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001359
Devang Patelbf47fdb2011-08-10 20:55:27 +00001360 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001361 if (Ranges.empty())
1362 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001363 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001364 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001365 assert(RI->first && "InsnRange does not have first instruction!");
1366 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001367 requestLabelBeforeInsn(RI->first);
1368 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001369 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001370 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001371}
1372
Devang Patela3f48672011-05-09 22:14:49 +00001373/// getScopeNode - Get MDNode for DebugLoc's scope.
1374static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1375 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1376 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1377 return DL.getScope(Ctx);
1378}
1379
Devang Patel4243e672011-05-11 19:22:19 +00001380/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1381/// line number info for the function.
1382static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1383 const MDNode *Scope = getScopeNode(DL, Ctx);
1384 DISubprogram SP = getDISubprogram(Scope);
1385 if (SP.Verify())
1386 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1387 return DebugLoc();
1388}
1389
Devang Patel2c4ceb12009-11-21 02:48:08 +00001390/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001391/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001392void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001393 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001394 LScopes.initialize(*MF);
1395 if (LScopes.empty()) return;
1396 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001397
Devang Pateleac9c072010-04-27 19:46:33 +00001398 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1399 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001400 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001401 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001402
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001403 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1404
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001405 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001406 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1407 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1408
Devang Patelb2b31a62010-05-26 19:37:24 +00001409 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001410 I != E; ++I) {
1411 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001412 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1413 II != IE; ++II) {
1414 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001415
Devang Patelb2b31a62010-05-26 19:37:24 +00001416 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00001417 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001418
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001419 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001420 const MDNode *Var =
1421 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001422
1423 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001424 if (isDbgValueInDefinedReg(MI))
1425 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1426
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001427 // Check the history of this variable.
1428 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1429 if (History.empty()) {
1430 UserVariables.push_back(Var);
1431 // The first mention of a function argument gets the FunctionBeginSym
1432 // label, so arguments are visible when breaking at function entry.
1433 DIVariable DV(Var);
1434 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1435 DISubprogram(getDISubprogram(DV.getContext()))
1436 .describes(MF->getFunction()))
1437 LabelsBeforeInsn[MI] = FunctionBeginSym;
1438 } else {
1439 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1440 const MachineInstr *Prev = History.back();
1441 if (Prev->isDebugValue()) {
1442 // Coalesce identical entries at the end of History.
1443 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001444 Prev->isIdenticalTo(History[History.size() - 2])) {
1445 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1446 << "\t" << *Prev
1447 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001448 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001449 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001450
1451 // Terminate old register assignments that don't reach MI;
1452 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1453 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1454 isDbgValueInDefinedReg(Prev)) {
1455 // Previous register assignment needs to terminate at the end of
1456 // its basic block.
1457 MachineBasicBlock::const_iterator LastMI =
1458 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001459 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001460 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001461 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1462 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001463 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001464 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001465 else {
1466 // Terminate after LastMI.
1467 History.push_back(LastMI);
1468 }
1469 }
1470 }
1471 }
1472 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001473 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001474 // Not a DBG_VALUE instruction.
1475 if (!MI->isLabel())
1476 AtBlockEntry = false;
1477
Devang Patel4243e672011-05-11 19:22:19 +00001478 // First known non DBG_VALUE location marks beginning of function
1479 // body.
1480 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1481 PrologEndLoc = MI->getDebugLoc();
1482
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001483 // Check if the instruction clobbers any registers with debug vars.
1484 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1485 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1486 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1487 continue;
1488 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1489 unsigned Reg = *AI; ++AI) {
1490 const MDNode *Var = LiveUserVar[Reg];
1491 if (!Var)
1492 continue;
1493 // Reg is now clobbered.
1494 LiveUserVar[Reg] = 0;
1495
1496 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001497 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1498 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001499 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001500 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1501 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001502 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001503 const MachineInstr *Prev = History.back();
1504 // Sanity-check: Register assignments are terminated at the end of
1505 // their block.
1506 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1507 continue;
1508 // Is the variable still in Reg?
1509 if (!isDbgValueInDefinedReg(Prev) ||
1510 Prev->getOperand(0).getReg() != Reg)
1511 continue;
1512 // Var is clobbered. Make sure the next instruction gets a label.
1513 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001514 }
1515 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001516 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001517 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001518 }
1519
1520 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1521 I != E; ++I) {
1522 SmallVectorImpl<const MachineInstr*> &History = I->second;
1523 if (History.empty())
1524 continue;
1525
1526 // Make sure the final register assignments are terminated.
1527 const MachineInstr *Prev = History.back();
1528 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1529 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel5bc942c2011-08-10 23:58:09 +00001530 MachineBasicBlock::const_iterator LastMI =
1531 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001532 if (LastMI == PrevMBB->end())
1533 // Drop DBG_VALUE for empty range.
1534 History.pop_back();
1535 else {
1536 // Terminate after LastMI.
1537 History.push_back(LastMI);
1538 }
1539 }
1540 // Request labels for the full history.
1541 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1542 const MachineInstr *MI = History[i];
1543 if (MI->isDebugValue())
1544 requestLabelBeforeInsn(MI);
1545 else
1546 requestLabelAfterInsn(MI);
1547 }
1548 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001549
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001550 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001551 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001552
1553 // Record beginning of function.
1554 if (!PrologEndLoc.isUnknown()) {
1555 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1556 MF->getFunction()->getContext());
1557 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1558 FnStartDL.getScope(MF->getFunction()->getContext()),
1559 DWARF2_FLAG_IS_STMT);
1560 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001561}
1562
Devang Patelbf47fdb2011-08-10 20:55:27 +00001563void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1564// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1565 ScopeVariables[LS].push_back(Var);
1566// Vars.push_back(Var);
1567}
1568
Devang Patel2c4ceb12009-11-21 02:48:08 +00001569/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001570///
Chris Lattnereec791a2010-01-26 23:18:02 +00001571void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001572 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001573
Devang Patelbf47fdb2011-08-10 20:55:27 +00001574 // Define end label for subprogram.
1575 FunctionEndSym = Asm->GetTempSymbol("func_end",
1576 Asm->getFunctionNumber());
1577 // Assumes in correct section after the entry point.
1578 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1579
1580 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1581 collectVariableInfo(MF, ProcessedVars);
1582
1583 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001584 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1585 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1586 LexicalScope *AScope = AList[i];
1587 DISubprogram SP(AScope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001588 if (SP.Verify()) {
1589 // Collect info for variables that were optimized out.
1590 StringRef FName = SP.getLinkageName();
1591 if (FName.empty())
1592 FName = SP.getName();
1593 if (NamedMDNode *NMD =
1594 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
1595 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001596 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00001597 if (!DV || !ProcessedVars.insert(DV))
1598 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001599 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1600 addScopeVariable(Scope, new DbgVariable(DV));
Devang Patel78e127d2010-06-25 22:07:34 +00001601 }
1602 }
1603 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001604 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
1605 constructScopeDIE(AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001606 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001607
1608 DIE *CurFnDIE = constructScopeDIE(LScopes.getCurrentFunctionScope());
1609
1610 if (!DisableFramePointerElim(*MF)) {
1611 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel5bc942c2011-08-10 23:58:09 +00001612 CompileUnit *TheCU = getCompileUnit(FnScope->getScopeNode());
1613 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1614 dwarf::DW_FORM_flag, 1);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001615 }
1616 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1617 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001618
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001619 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001620 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1621 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1622 DeleteContainerPointers(I->second);
1623 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001624 DeleteContainerPointers(CurrentFnArguments);
Devang Patel26c1e562010-05-20 16:36:41 +00001625 DbgVariableToFrameIndexMap.clear();
1626 VarToAbstractVarMap.clear();
1627 DbgVariableToDbgInstMap.clear();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001628 UserVariables.clear();
1629 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001630 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001631 LabelsBeforeInsn.clear();
1632 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001633 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001634}
1635
Devang Patel26c1e562010-05-20 16:36:41 +00001636/// recordVariableFrameIndex - Record a variable's index.
1637void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
1638 assert (V && "Invalid DbgVariable!");
1639 DbgVariableToFrameIndexMap[V] = Index;
1640}
1641
1642/// findVariableFrameIndex - Return true if frame index for the variable
1643/// is found. Update FI to hold value of the index.
1644bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
1645 assert (V && "Invalid DbgVariable!");
1646 DenseMap<const DbgVariable *, int>::iterator I =
1647 DbgVariableToFrameIndexMap.find(V);
1648 if (I == DbgVariableToFrameIndexMap.end())
1649 return false;
1650 *FI = I->second;
1651 return true;
1652}
1653
Chris Lattnerc6087842010-03-09 04:54:43 +00001654/// recordSourceLine - Register a source line with debug info. Returns the
1655/// unique label that was emitted and which provides correspondence to
1656/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001657void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1658 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001659 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001660 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001661 unsigned Src = 1;
1662 if (S) {
1663 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001664
Dan Gohman1cc0d622010-05-05 23:41:32 +00001665 if (Scope.isCompileUnit()) {
1666 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001667 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001668 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001669 } else if (Scope.isFile()) {
1670 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001671 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001672 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001673 } else if (Scope.isSubprogram()) {
1674 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001675 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001676 Dir = SP.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001677 } else if (Scope.isLexicalBlock()) {
1678 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001679 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001680 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001681 } else
1682 assert(0 && "Unexpected scope info");
1683
Devang Patel23670e52011-03-24 20:30:50 +00001684 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001685 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001686 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001687}
1688
Bill Wendling829e67b2009-05-20 23:22:40 +00001689//===----------------------------------------------------------------------===//
1690// Emit Methods
1691//===----------------------------------------------------------------------===//
1692
Devang Patel2c4ceb12009-11-21 02:48:08 +00001693/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001694///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001695unsigned
1696DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001697 // Get the children.
1698 const std::vector<DIE *> &Children = Die->getChildren();
1699
1700 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001701 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00001702 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00001703
1704 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001705 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001706
1707 // Get the abbreviation for this DIE.
1708 unsigned AbbrevNumber = Die->getAbbrevNumber();
1709 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1710
1711 // Set DIE offset
1712 Die->setOffset(Offset);
1713
1714 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001715 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001716
1717 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1718 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1719
1720 // Size the DIE attribute values.
1721 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1722 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001723 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001724
1725 // Size the DIE children if any.
1726 if (!Children.empty()) {
1727 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1728 "Children flag not set");
1729
1730 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001731 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001732
1733 // End of children marker.
1734 Offset += sizeof(int8_t);
1735 }
1736
1737 Die->setSize(Offset - Die->getOffset());
1738 return Offset;
1739}
1740
Devang Patel2c4ceb12009-11-21 02:48:08 +00001741/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001742///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001743void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001744 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1745 E = CUMap.end(); I != E; ++I) {
1746 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001747 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001748 sizeof(int32_t) + // Length of Compilation Unit Info
1749 sizeof(int16_t) + // DWARF version number
1750 sizeof(int32_t) + // Offset Into Abbrev. Section
1751 sizeof(int8_t); // Pointer Size (in bytes)
1752 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001753 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001754}
1755
Chris Lattner11b8f302010-04-04 23:02:02 +00001756/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1757/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00001758static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00001759 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00001760 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00001761 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001762
Chris Lattner9c69e285532010-04-04 22:59:04 +00001763 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1764 Asm->OutStreamer.EmitLabel(TmpSym);
1765 return TmpSym;
1766}
1767
1768/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1769/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001770void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001771 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001772
Bill Wendling94d04b82009-05-20 23:21:38 +00001773 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001774 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001775 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001776 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001777 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001778 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001779
Chris Lattner9c69e285532010-04-04 22:59:04 +00001780 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001781 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001782
Devang Patelaf608bd2010-08-24 00:06:12 +00001783 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001784 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1785 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1786 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001787 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001788 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001789 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1790 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001791
Devang Patelc3f5f782010-05-25 23:40:22 +00001792 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1793 "section_debug_loc");
1794
Chris Lattner9c69e285532010-04-04 22:59:04 +00001795 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001796 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001797}
1798
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001799/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001800///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001801void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001802 // Get the abbreviation for this DIE.
1803 unsigned AbbrevNumber = Die->getAbbrevNumber();
1804 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1805
Bill Wendling94d04b82009-05-20 23:21:38 +00001806 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001807 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001808 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1809 Twine::utohexstr(Die->getOffset()) + ":0x" +
1810 Twine::utohexstr(Die->getSize()) + " " +
1811 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001812 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001813
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001814 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001815 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1816
1817 // Emit the DIE attribute values.
1818 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1819 unsigned Attr = AbbrevData[i].getAttribute();
1820 unsigned Form = AbbrevData[i].getForm();
1821 assert(Form && "Too many attributes for DIE (check abbreviation)");
1822
Chris Lattner3f53c832010-04-04 18:52:31 +00001823 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001824 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001825
Bill Wendling94d04b82009-05-20 23:21:38 +00001826 switch (Attr) {
1827 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00001828 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00001829 break;
1830 case dwarf::DW_AT_abstract_origin: {
1831 DIEEntry *E = cast<DIEEntry>(Values[i]);
1832 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001833 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001834 Asm->EmitInt32(Addr);
1835 break;
1836 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001837 case dwarf::DW_AT_ranges: {
1838 // DW_AT_range Value encodes offset in debug_range section.
1839 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001840
1841 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1842 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1843 V->getValue(),
1844 4);
1845 } else {
1846 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1847 V->getValue(),
1848 DwarfDebugRangeSectionSym,
1849 4);
1850 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001851 break;
1852 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001853 case dwarf::DW_AT_location: {
1854 if (UseDotDebugLocEntry.count(Die) != 0) {
1855 DIELabel *L = cast<DIELabel>(Values[i]);
Daniel Dunbar83320a02011-03-16 22:16:39 +00001856 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patelc3f5f782010-05-25 23:40:22 +00001857 } else
1858 Values[i]->EmitValue(Asm, Form);
1859 break;
1860 }
Devang Patel2a361602010-09-29 19:08:08 +00001861 case dwarf::DW_AT_accessibility: {
1862 if (Asm->isVerbose()) {
1863 DIEInteger *V = cast<DIEInteger>(Values[i]);
1864 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1865 }
1866 Values[i]->EmitValue(Asm, Form);
1867 break;
1868 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001869 default:
1870 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001871 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001872 break;
1873 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001874 }
1875
1876 // Emit the DIE children if any.
1877 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1878 const std::vector<DIE *> &Children = Die->getChildren();
1879
1880 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001881 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001882
Chris Lattner3f53c832010-04-04 18:52:31 +00001883 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001884 Asm->OutStreamer.AddComment("End Of Children Mark");
1885 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001886 }
1887}
1888
Devang Patel8a241142009-12-09 18:24:21 +00001889/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001890///
Devang Patel8a241142009-12-09 18:24:21 +00001891void DwarfDebug::emitDebugInfo() {
1892 // Start debug info section.
1893 Asm->OutStreamer.SwitchSection(
1894 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001895 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1896 E = CUMap.end(); I != E; ++I) {
1897 CompileUnit *TheCU = I->second;
1898 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001899
Devang Patel163a9f72010-05-10 22:49:55 +00001900 // Emit the compile units header.
1901 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1902 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001903
Devang Patel163a9f72010-05-10 22:49:55 +00001904 // Emit size of content not including length itself
1905 unsigned ContentSize = Die->getSize() +
1906 sizeof(int16_t) + // DWARF version number
1907 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001908 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00001909
Devang Patel163a9f72010-05-10 22:49:55 +00001910 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1911 Asm->EmitInt32(ContentSize);
1912 Asm->OutStreamer.AddComment("DWARF version number");
1913 Asm->EmitInt16(dwarf::DWARF_VERSION);
1914 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1915 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1916 DwarfAbbrevSectionSym);
1917 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1918 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001919
Devang Patel163a9f72010-05-10 22:49:55 +00001920 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00001921 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1922 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001923}
1924
Devang Patel2c4ceb12009-11-21 02:48:08 +00001925/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001926///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001927void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00001928 // Check to see if it is worth the effort.
1929 if (!Abbreviations.empty()) {
1930 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001931 Asm->OutStreamer.SwitchSection(
1932 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001933
Chris Lattnerc0215722010-04-04 19:25:43 +00001934 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001935
1936 // For each abbrevation.
1937 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1938 // Get abbreviation data
1939 const DIEAbbrev *Abbrev = Abbreviations[i];
1940
1941 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001942 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00001943
1944 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001945 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00001946 }
1947
1948 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001949 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00001950
Chris Lattnerc0215722010-04-04 19:25:43 +00001951 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00001952 }
1953}
1954
Devang Patel2c4ceb12009-11-21 02:48:08 +00001955/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00001956/// the line matrix.
1957///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001958void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001959 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001960 Asm->OutStreamer.AddComment("Extended Op");
1961 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001962
Chris Lattner233f52b2010-03-09 23:52:58 +00001963 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00001964 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001965 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1966 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1967
1968 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001969
Chris Lattnerc0215722010-04-04 19:25:43 +00001970 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00001971 Asm->getTargetData().getPointerSize(),
1972 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00001973
1974 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001975 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1976 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001977 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001978 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001979}
1980
Devang Patel8a241142009-12-09 18:24:21 +00001981/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1982///
1983void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00001984 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1985 E = CUMap.end(); I != E; ++I) {
1986 CompileUnit *TheCU = I->second;
1987 // Start the dwarf pubnames section.
1988 Asm->OutStreamer.SwitchSection(
1989 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001990
Devang Patel163a9f72010-05-10 22:49:55 +00001991 Asm->OutStreamer.AddComment("Length of Public Names Info");
1992 Asm->EmitLabelDifference(
1993 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1994 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001995
Devang Patel163a9f72010-05-10 22:49:55 +00001996 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1997 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001998
Devang Patel163a9f72010-05-10 22:49:55 +00001999 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002000 Asm->EmitInt16(dwarf::DWARF_VERSION);
2001
Devang Patel163a9f72010-05-10 22:49:55 +00002002 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002003 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00002004 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002005
Devang Patel163a9f72010-05-10 22:49:55 +00002006 Asm->OutStreamer.AddComment("Compilation Unit Length");
2007 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2008 Asm->GetTempSymbol("info_begin", TheCU->getID()),
2009 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002010
Devang Patel163a9f72010-05-10 22:49:55 +00002011 const StringMap<DIE*> &Globals = TheCU->getGlobals();
2012 for (StringMap<DIE*>::const_iterator
2013 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2014 const char *Name = GI->getKeyData();
2015 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002016
Devang Patel163a9f72010-05-10 22:49:55 +00002017 Asm->OutStreamer.AddComment("DIE offset");
2018 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002019
Devang Patel163a9f72010-05-10 22:49:55 +00002020 if (Asm->isVerbose())
2021 Asm->OutStreamer.AddComment("External Name");
2022 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
2023 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002024
Devang Patel163a9f72010-05-10 22:49:55 +00002025 Asm->OutStreamer.AddComment("End Mark");
2026 Asm->EmitInt32(0);
2027 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00002028 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00002029 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002030}
2031
Devang Patel193f7202009-11-24 01:14:22 +00002032void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00002033 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2034 E = CUMap.end(); I != E; ++I) {
2035 CompileUnit *TheCU = I->second;
2036 // Start the dwarf pubnames section.
2037 Asm->OutStreamer.SwitchSection(
2038 Asm->getObjFileLowering().getDwarfPubTypesSection());
2039 Asm->OutStreamer.AddComment("Length of Public Types Info");
2040 Asm->EmitLabelDifference(
2041 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
2042 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002043
Devang Patel163a9f72010-05-10 22:49:55 +00002044 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
2045 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002046
Devang Patel163a9f72010-05-10 22:49:55 +00002047 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2048 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002049
Devang Patel163a9f72010-05-10 22:49:55 +00002050 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2051 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
2052 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002053
Devang Patel163a9f72010-05-10 22:49:55 +00002054 Asm->OutStreamer.AddComment("Compilation Unit Length");
2055 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2056 Asm->GetTempSymbol("info_begin", TheCU->getID()),
2057 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002058
Devang Patel163a9f72010-05-10 22:49:55 +00002059 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2060 for (StringMap<DIE*>::const_iterator
2061 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2062 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00002063 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002064
Devang Patel163a9f72010-05-10 22:49:55 +00002065 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2066 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002067
Devang Patel163a9f72010-05-10 22:49:55 +00002068 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
2069 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
2070 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002071
Devang Patel163a9f72010-05-10 22:49:55 +00002072 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002073 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00002074 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
2075 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00002076 }
Devang Patel193f7202009-11-24 01:14:22 +00002077}
2078
Devang Patel2c4ceb12009-11-21 02:48:08 +00002079/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002080///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002081void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002082 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002083 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002084
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002085 // Start the dwarf str section.
2086 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002087 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002088
Chris Lattnerbc733f52010-03-13 02:17:42 +00002089 // Get all of the string pool entries and put them in an array by their ID so
2090 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002091 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00002092 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002093
Chris Lattnerbc733f52010-03-13 02:17:42 +00002094 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2095 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2096 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002097
Chris Lattnerbc733f52010-03-13 02:17:42 +00002098 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002099
Chris Lattnerbc733f52010-03-13 02:17:42 +00002100 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002101 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002102 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002103
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002104 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002105 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002106 }
2107}
2108
Devang Patel2c4ceb12009-11-21 02:48:08 +00002109/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002110///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002111void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002112 if (DotDebugLocEntries.empty())
2113 return;
2114
Devang Patel6c3ea902011-02-04 22:57:18 +00002115 for (SmallVector<DotDebugLocEntry, 4>::iterator
2116 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2117 I != E; ++I) {
2118 DotDebugLocEntry &Entry = *I;
2119 if (I + 1 != DotDebugLocEntries.end())
2120 Entry.Merge(I+1);
2121 }
2122
Daniel Dunbar83320a02011-03-16 22:16:39 +00002123 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002124 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002125 Asm->getObjFileLowering().getDwarfLocSection());
2126 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002127 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2128 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002129 for (SmallVector<DotDebugLocEntry, 4>::iterator
2130 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002131 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002132 DotDebugLocEntry &Entry = *I;
2133 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002134 if (Entry.isEmpty()) {
2135 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2136 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002137 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002138 } else {
2139 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2140 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002141 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002142 Asm->OutStreamer.AddComment("Loc expr size");
2143 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2144 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2145 Asm->EmitLabelDifference(end, begin, 2);
2146 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002147 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002148 DIBasicType BTy(DV.getType());
2149 if (BTy.Verify() &&
2150 (BTy.getEncoding() == dwarf::DW_ATE_signed
2151 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2152 Asm->OutStreamer.AddComment("DW_OP_consts");
2153 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002154 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002155 } else {
2156 Asm->OutStreamer.AddComment("DW_OP_constu");
2157 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002158 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002159 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002160 } else if (Entry.isLocation()) {
2161 if (!DV.hasComplexAddress())
2162 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002163 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002164 else {
2165 // Complex address entry.
2166 unsigned N = DV.getNumAddrElements();
2167 unsigned i = 0;
2168 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2169 if (Entry.Loc.getOffset()) {
2170 i = 2;
2171 Asm->EmitDwarfRegOp(Entry.Loc);
2172 Asm->OutStreamer.AddComment("DW_OP_deref");
2173 Asm->EmitInt8(dwarf::DW_OP_deref);
2174 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2175 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2176 Asm->EmitSLEB128(DV.getAddrElement(1));
2177 } else {
2178 // If first address element is OpPlus then emit
2179 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2180 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2181 Asm->EmitDwarfRegOp(Loc);
2182 i = 2;
2183 }
2184 } else {
2185 Asm->EmitDwarfRegOp(Entry.Loc);
2186 }
2187
2188 // Emit remaining complex address elements.
2189 for (; i < N; ++i) {
2190 uint64_t Element = DV.getAddrElement(i);
2191 if (Element == DIBuilder::OpPlus) {
2192 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2193 Asm->EmitULEB128(DV.getAddrElement(++i));
2194 } else if (Element == DIBuilder::OpDeref)
2195 Asm->EmitInt8(dwarf::DW_OP_deref);
2196 else llvm_unreachable("unknown Opcode found in complex address");
2197 }
Devang Patelc26f5442011-04-28 02:22:40 +00002198 }
Devang Patelc26f5442011-04-28 02:22:40 +00002199 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002200 // else ... ignore constant fp. There is not any good way to
2201 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002202 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002203 }
2204 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002205}
2206
2207/// EmitDebugARanges - Emit visible names into a debug aranges section.
2208///
2209void DwarfDebug::EmitDebugARanges() {
2210 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002211 Asm->OutStreamer.SwitchSection(
2212 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002213}
2214
Devang Patel2c4ceb12009-11-21 02:48:08 +00002215/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002216///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002217void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002218 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002219 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002220 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002221 unsigned char Size = Asm->getTargetData().getPointerSize();
2222 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002223 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002224 I != E; ++I) {
2225 if (*I)
2226 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002227 else
Devang Pateleac9c072010-04-27 19:46:33 +00002228 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002229 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002230}
2231
Devang Patel2c4ceb12009-11-21 02:48:08 +00002232/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002233///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002234void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002235 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002236 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002237 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002238 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002239 }
2240}
2241
Devang Patel2c4ceb12009-11-21 02:48:08 +00002242/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002243/// Section Header:
2244/// 1. length of section
2245/// 2. Dwarf version number
2246/// 3. address size.
2247///
2248/// Entries (one "entry" for each function that was inlined):
2249///
2250/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2251/// otherwise offset into __debug_str for regular function name.
2252/// 2. offset into __debug_str section for regular function name.
2253/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2254/// instances for the function.
2255///
2256/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2257/// inlined instance; the die_offset points to the inlined_subroutine die in the
2258/// __debug_info section, and the low_pc is the starting address for the
2259/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002260void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002261 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002262 return;
2263
Devang Patel163a9f72010-05-10 22:49:55 +00002264 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002265 return;
2266
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002267 Asm->OutStreamer.SwitchSection(
2268 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002269
Chris Lattner233f52b2010-03-09 23:52:58 +00002270 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002271 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2272 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002273
Chris Lattnerc0215722010-04-04 19:25:43 +00002274 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002275
Chris Lattner233f52b2010-03-09 23:52:58 +00002276 Asm->OutStreamer.AddComment("Dwarf Version");
2277 Asm->EmitInt16(dwarf::DWARF_VERSION);
2278 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002279 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002280
Devang Patele9f8f5e2010-05-07 20:54:48 +00002281 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002282 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002283
Devang Patele9f8f5e2010-05-07 20:54:48 +00002284 const MDNode *Node = *I;
2285 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002286 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002287 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002288 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002289 StringRef LName = SP.getLinkageName();
2290 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002291
Chris Lattner233f52b2010-03-09 23:52:58 +00002292 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002293 if (LName.empty()) {
2294 Asm->OutStreamer.EmitBytes(Name, 0);
2295 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002296 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00002297 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2298 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002299
Chris Lattner233f52b2010-03-09 23:52:58 +00002300 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002301 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002302 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002303
Devang Patel53bb5c92009-11-10 23:06:00 +00002304 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002305 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002306 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002307 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002308
Chris Lattner3f53c832010-04-04 18:52:31 +00002309 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002310 Asm->OutStreamer.EmitSymbolValue(LI->first,
2311 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002312 }
2313 }
2314
Chris Lattnerc0215722010-04-04 19:25:43 +00002315 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002316}