blob: 003cc8c467461000864785ebe87d28d281dc6f1d [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 Patelffe966c2009-12-14 16:18:45 +0000182/// createSubprogramDIE - Create new DIE using SP.
Devang Patelee70fa72010-09-27 23:15:27 +0000183DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel163a9f72010-05-10 22:49:55 +0000184 CompileUnit *SPCU = getCompileUnit(SP);
185 DIE *SPDie = SPCU->getDIE(SP);
Devang Patelffe966c2009-12-14 16:18:45 +0000186 if (SPDie)
187 return SPDie;
188
189 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel5e06bb82011-04-22 23:10:17 +0000190
191 // DW_TAG_inlined_subroutine may refer to this DIE.
192 SPCU->insertDIE(SP, SPDie);
193
194 // Add to context owner.
195 SPCU->addToContextOwner(SPDie, SP.getContext());
196
197 // Add function template parameters.
198 SPCU->addTemplateParams(*SPDie, SP.getTemplateParams());
199
Devang Patelee4a3ab2011-05-03 21:50:34 +0000200 StringRef LinkageName = SP.getLinkageName();
201 if (!LinkageName.empty())
202 SPCU->addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
203 getRealLinkageName(LinkageName));
204
Devang Patel5e06bb82011-04-22 23:10:17 +0000205 // If this DIE is going to refer declaration info using AT_specification
206 // then there is no need to add other attributes.
207 if (SP.getFunctionDeclaration().isSubprogram())
208 return SPDie;
209
Devang Patel1eac3e72010-03-02 17:58:15 +0000210 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +0000211 if (!SP.getName().empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000212 SPCU->addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
213 SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +0000214
Devang Patel3cbee302011-04-12 22:53:02 +0000215 SPCU->addSourceLine(SPDie, SP);
Bill Wendling0310d762009-05-15 09:23:25 +0000216
Devang Patel7b172c62010-10-07 22:03:01 +0000217 if (SP.isPrototyped())
Devang Patel3cbee302011-04-12 22:53:02 +0000218 SPCU->addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Devang Patel5e06bb82011-04-22 23:10:17 +0000219
Bill Wendling0310d762009-05-15 09:23:25 +0000220 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000221 DICompositeType SPTy = SP.getType();
222 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +0000223 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +0000224
Devang Patel3c91b052010-03-08 20:52:55 +0000225 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel3cbee302011-04-12 22:53:02 +0000226 SPCU->addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000227 else
Devang Patel3cbee302011-04-12 22:53:02 +0000228 SPCU->addType(SPDie, DIType(Args.getElement(0)));
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000229
Devang Patel5d11eb02009-12-03 19:11:07 +0000230 unsigned VK = SP.getVirtuality();
231 if (VK) {
Devang Patel3cbee302011-04-12 22:53:02 +0000232 SPCU->addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
233 DIEBlock *Block = SPCU->getDIEBlock();
234 SPCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
235 SPCU->addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
236 SPCU->addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000237 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel2db49d72010-05-07 18:11:54 +0000238 SP.getContainingType()));
Devang Patel5d11eb02009-12-03 19:11:07 +0000239 }
240
Devang Patelee70fa72010-09-27 23:15:27 +0000241 if (!SP.isDefinition()) {
Devang Patel3cbee302011-04-12 22:53:02 +0000242 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
243
Bill Wendling0310d762009-05-15 09:23:25 +0000244 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +0000245 // be handled while processing variables.
246 DICompositeType SPTy = SP.getType();
247 DIArray Args = SPTy.getTypeArray();
248 unsigned SPTag = SPTy.getTag();
249
Bill Wendling0310d762009-05-15 09:23:25 +0000250 if (SPTag == dwarf::DW_TAG_subroutine_type)
251 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
252 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +0000253 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patel3cbee302011-04-12 22:53:02 +0000254 SPCU->addType(Arg, ATy);
Devang Patelb4645642010-02-06 01:02:37 +0000255 if (ATy.isArtificial())
Devang Patel3cbee302011-04-12 22:53:02 +0000256 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000257 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +0000258 }
259 }
260
Devang Patel4e0d19d2010-02-03 19:57:19 +0000261 if (SP.isArtificial())
Devang Patel3cbee302011-04-12 22:53:02 +0000262 SPCU->addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel4e0d19d2010-02-03 19:57:19 +0000263
Devang Patelccff8122010-04-30 19:38:23 +0000264 if (!SP.isLocalToUnit())
Devang Patel3cbee302011-04-12 22:53:02 +0000265 SPCU->addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000266
Devang Patelccff8122010-04-30 19:38:23 +0000267 if (SP.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000268 SPCU->addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Devang Patelccff8122010-04-30 19:38:23 +0000269
Jim Grosbach91729002010-07-21 23:03:52 +0000270 if (unsigned isa = Asm->getISAEncoding()) {
Devang Patel3cbee302011-04-12 22:53:02 +0000271 SPCU->addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
Jim Grosbach91729002010-07-21 23:03:52 +0000272 }
273
Bill Wendling0310d762009-05-15 09:23:25 +0000274 return SPDie;
275}
276
Devang Patel5f094002010-04-06 23:53:48 +0000277/// isSubprogramContext - Return true if Context is either a subprogram
278/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000279static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +0000280 if (!Context)
281 return false;
282 DIDescriptor D(Context);
283 if (D.isSubprogram())
284 return true;
285 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +0000286 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +0000287 return false;
288}
289
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000290/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +0000291/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
292/// If there are global variables in this scope then create and insert
293/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000294DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +0000295 CompileUnit *SPCU = getCompileUnit(SPNode);
296 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000297
Chris Lattnerd38fee82010-04-05 00:13:49 +0000298 assert(SPDie && "Unable to find subprogram DIE!");
299 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000300
Devang Patel5e06bb82011-04-22 23:10:17 +0000301 DISubprogram SPDecl = SP.getFunctionDeclaration();
302 if (SPDecl.isSubprogram())
303 // Refer function declaration directly.
Devang Patel3cbee302011-04-12 22:53:02 +0000304 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Devang Patel5e06bb82011-04-22 23:10:17 +0000305 createSubprogramDIE(SPDecl));
306 else {
307 // There is not any need to generate specification DIE for a function
308 // defined at compile unit level. If a function is defined inside another
309 // function then gdb prefers the definition at top level and but does not
310 // expect specification DIE in parent function. So avoid creating
311 // specification DIE for a function defined inside a function.
312 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
313 !SP.getContext().isFile() &&
314 !isSubprogramContext(SP.getContext())) {
315 SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
316
317 // Add arguments.
318 DICompositeType SPTy = SP.getType();
319 DIArray Args = SPTy.getTypeArray();
320 unsigned SPTag = SPTy.getTag();
321 if (SPTag == dwarf::DW_TAG_subroutine_type)
322 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
323 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
324 DIType ATy = DIType(DIType(Args.getElement(i)));
325 SPCU->addType(Arg, ATy);
326 if (ATy.isArtificial())
327 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
328 SPDie->addChild(Arg);
329 }
330 DIE *SPDeclDie = SPDie;
331 SPDie = new DIE(dwarf::DW_TAG_subprogram);
332 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
333 SPDeclDie);
334 SPCU->addDie(SPDie);
335 }
Chris Lattnerd38fee82010-04-05 00:13:49 +0000336 }
Devang Patel8aa61472010-07-07 22:20:57 +0000337 // Pick up abstract subprogram DIE.
338 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
339 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000340 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
341 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000342 SPCU->addDie(SPDie);
343 }
344
Devang Patel3cbee302011-04-12 22:53:02 +0000345 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
346 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
347 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
348 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000349 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
350 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000351 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000352
Chris Lattnerd38fee82010-04-05 00:13:49 +0000353 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000354}
355
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000356/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +0000357/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000358DIE *DwarfDebug::constructLexicalScopeDIE(LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000359
360 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
361 if (Scope->isAbstractScope())
362 return ScopeDIE;
363
Devang Patelbf47fdb2011-08-10 20:55:27 +0000364 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +0000365 if (Ranges.empty())
366 return 0;
367
Devang Patel3cbee302011-04-12 22:53:02 +0000368 CompileUnit *TheCU = getCompileUnit(Scope->getScopeNode());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000369 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Pateleac9c072010-04-27 19:46:33 +0000370 if (Ranges.size() > 1) {
371 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000372 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000373 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000374 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
375 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000376 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000377 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000378 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
379 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000380 }
381 DebugRangeSymbols.push_back(NULL);
382 DebugRangeSymbols.push_back(NULL);
383 return ScopeDIE;
384 }
385
Devang Patelc3f5f782010-05-25 23:40:22 +0000386 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
387 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000388
Devang Patelc3f5f782010-05-25 23:40:22 +0000389 if (End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +0000390
Chris Lattnerb7db7332010-03-09 01:58:53 +0000391 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
392 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000393
Devang Patel3cbee302011-04-12 22:53:02 +0000394 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
395 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000396
397 return ScopeDIE;
398}
399
Devang Patel2c4ceb12009-11-21 02:48:08 +0000400/// constructInlinedScopeDIE - This scope represents inlined body of
401/// a function. Construct DIE to represent this concrete inlined copy
402/// of the function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000403DIE *DwarfDebug::constructInlinedScopeDIE(LexicalScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +0000404
Devang Patelbf47fdb2011-08-10 20:55:27 +0000405 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000406 assert (Ranges.empty() == false
Devang Patelbf47fdb2011-08-10 20:55:27 +0000407 && "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000408
Devang Patel26a92002011-07-27 00:34:13 +0000409 if (!Scope->getScopeNode())
410 return NULL;
411 DIScope DS(Scope->getScopeNode());
412 DISubprogram InlinedSP = getDISubprogram(DS);
413 CompileUnit *TheCU = getCompileUnit(InlinedSP);
414 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
415 if (!OriginDIE) {
416 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
417 return NULL;
418 }
419
Devang Patelbf47fdb2011-08-10 20:55:27 +0000420 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelc3f5f782010-05-25 23:40:22 +0000421 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
422 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +0000423
Devang Patel0afbf232010-07-08 22:39:20 +0000424 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000425 assert (0 && "Unexpected Start and End labels for a inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000426 return 0;
427 }
Chris Lattnerb7db7332010-03-09 01:58:53 +0000428 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000429 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +0000430 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +0000431 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +0000432
Devang Pateld96efb82011-05-05 17:54:26 +0000433 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000434 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
435 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000436
Devang Patel26a92002011-07-27 00:34:13 +0000437 if (Ranges.size() > 1) {
438 // .debug_range section has not been laid out yet. Emit offset in
439 // .debug_range as a uint, size 4, for now. emitDIE will handle
440 // DW_AT_ranges appropriately.
441 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
442 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
Devang Patelbf47fdb2011-08-10 20:55:27 +0000443 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000444 RE = Ranges.end(); RI != RE; ++RI) {
445 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
446 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
447 }
448 DebugRangeSymbols.push_back(NULL);
449 DebugRangeSymbols.push_back(NULL);
450 } else {
451 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
452 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
453 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000454
455 InlinedSubprogramDIEs.insert(OriginDIE);
456
457 // Track the start label for this inlined function.
Devang Patel26a92002011-07-27 00:34:13 +0000458 //.debug_inlined section specification does not clearly state how
459 // to emit inlined scope that is split into multiple instruction ranges.
460 // For now, use first instruction range and emit low_pc/high_pc pair and
461 // corresponding .debug_inlined section entry for this pair.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000462 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +0000463 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000464
465 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000466 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000467 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +0000468 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +0000469 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +0000470 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +0000471
Devang Patel53bb5c92009-11-10 23:06:00 +0000472 DILocation DL(Scope->getInlinedAt());
Devang Patel3cbee302011-04-12 22:53:02 +0000473 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
474 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000475
476 return ScopeDIE;
477}
478
Devang Patel8594d422011-06-24 20:46:11 +0000479/// isUnsignedDIType - Return true if type encoding is unsigned.
480static bool isUnsignedDIType(DIType Ty) {
481 DIDerivedType DTy(Ty);
482 if (DTy.Verify())
483 return isUnsignedDIType(DTy.getTypeDerivedFrom());
484
485 DIBasicType BTy(Ty);
486 if (BTy.Verify()) {
487 unsigned Encoding = BTy.getEncoding();
488 if (Encoding == dwarf::DW_ATE_unsigned ||
489 Encoding == dwarf::DW_ATE_unsigned_char)
490 return true;
491 }
492 return false;
493}
Devang Patel2c4ceb12009-11-21 02:48:08 +0000494
495/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000496DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, LexicalScope *Scope) {
Devang Patel8bd11de2010-08-09 21:01:39 +0000497 StringRef Name = DV->getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000498 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +0000499 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +0000500
501 // Translate tag to proper Dwarf tag. The result variable is dropped for
502 // now.
503 unsigned Tag;
Devang Patel8bd11de2010-08-09 21:01:39 +0000504 switch (DV->getTag()) {
Devang Patel53bb5c92009-11-10 23:06:00 +0000505 case dwarf::DW_TAG_return_variable:
506 return NULL;
507 case dwarf::DW_TAG_arg_variable:
508 Tag = dwarf::DW_TAG_formal_parameter;
509 break;
510 case dwarf::DW_TAG_auto_variable: // fall thru
511 default:
512 Tag = dwarf::DW_TAG_variable;
513 break;
514 }
515
516 // Define variable debug information entry.
517 DIE *VariableDie = new DIE(Tag);
Devang Patel9341d102011-04-25 23:05:21 +0000518 CompileUnit *VariableCU = getCompileUnit(DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +0000519 DIE *AbsDIE = NULL;
Devang Patel26c1e562010-05-20 16:36:41 +0000520 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
521 V2AVI = VarToAbstractVarMap.find(DV);
522 if (V2AVI != VarToAbstractVarMap.end())
523 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000524
Devang Patel26c1e562010-05-20 16:36:41 +0000525 if (AbsDIE)
Devang Patel9341d102011-04-25 23:05:21 +0000526 VariableCU->addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel3cbee302011-04-12 22:53:02 +0000527 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000528 else {
Devang Patel9341d102011-04-25 23:05:21 +0000529 VariableCU->addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
530 Name);
531 VariableCU->addSourceLine(VariableDie, DV->getVariable());
Devang Patel53bb5c92009-11-10 23:06:00 +0000532
533 // Add variable type.
Devang Patel9341d102011-04-25 23:05:21 +0000534 VariableCU->addType(VariableDie, DV->getType());
Devang Patel53bb5c92009-11-10 23:06:00 +0000535 }
536
Devang Patel8bd11de2010-08-09 21:01:39 +0000537 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patel9341d102011-04-25 23:05:21 +0000538 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
539 dwarf::DW_FORM_flag, 1);
Devang Patel3cf763d2010-09-29 23:07:21 +0000540 else if (DIVariable(DV->getVariable()).isArtificial())
Devang Patel9341d102011-04-25 23:05:21 +0000541 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
542 dwarf::DW_FORM_flag, 1);
Devang Patelc3f5f782010-05-25 23:40:22 +0000543
544 if (Scope->isAbstractScope()) {
545 DV->setDIE(VariableDie);
546 return VariableDie;
547 }
548
549 // Add variable address.
550
551 unsigned Offset = DV->getDotDebugLocOffset();
552 if (Offset != ~0U) {
Devang Patel9341d102011-04-25 23:05:21 +0000553 VariableCU->addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
Devang Patelc3f5f782010-05-25 23:40:22 +0000554 Asm->GetTempSymbol("debug_loc", Offset));
555 DV->setDIE(VariableDie);
556 UseDotDebugLocEntry.insert(VariableDie);
557 return VariableDie;
558 }
559
560 // Check if variable is described by a DBG_VALUE instruction.
561 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
562 DbgVariableToDbgInstMap.find(DV);
563 if (DVI != DbgVariableToDbgInstMap.end()) {
564 const MachineInstr *DVInsn = DVI->second;
Devang Patelc3f5f782010-05-25 23:40:22 +0000565 bool updated = false;
Devang Patelc3f5f782010-05-25 23:40:22 +0000566 if (DVInsn->getNumOperands() == 3) {
Devang Patel0b48ead2010-08-31 22:22:42 +0000567 if (DVInsn->getOperand(0).isReg()) {
568 const MachineOperand RegOp = DVInsn->getOperand(0);
569 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
570 if (DVInsn->getOperand(1).isImm() &&
571 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
Devang Patele1cdf842011-04-27 22:45:24 +0000572 unsigned FrameReg = 0;
573 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
574 int Offset =
575 TFI->getFrameIndexReference(*Asm->MF,
576 DVInsn->getOperand(1).getImm(),
577 FrameReg);
578 MachineLocation Location(FrameReg, Offset);
579 VariableCU->addVariableAddress(DV, VariableDie, Location);
580
581 } else if (RegOp.getReg())
582 VariableCU->addVariableAddress(DV, VariableDie,
583 MachineLocation(RegOp.getReg()));
584 updated = true;
Devang Patel0b48ead2010-08-31 22:22:42 +0000585 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000586 else if (DVInsn->getOperand(0).isImm())
Devang Patelb58128e2011-05-27 16:45:18 +0000587 updated =
588 VariableCU->addConstantValue(VariableDie, DVInsn->getOperand(0),
589 DV->getType());
Jim Grosbach1e20b962010-07-21 21:21:52 +0000590 else if (DVInsn->getOperand(0).isFPImm())
591 updated =
Devang Patel9341d102011-04-25 23:05:21 +0000592 VariableCU->addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patel8594d422011-06-24 20:46:11 +0000593 else if (DVInsn->getOperand(0).isCImm())
594 updated =
595 VariableCU->addConstantValue(VariableDie,
596 DVInsn->getOperand(0).getCImm(),
597 isUnsignedDIType(DV->getType()));
Devang Patelc3f5f782010-05-25 23:40:22 +0000598 } else {
Devang Patele1cdf842011-04-27 22:45:24 +0000599 VariableCU->addVariableAddress(DV, VariableDie,
600 Asm->getDebugValueLocation(DVInsn));
601 updated = true;
Devang Patelc3f5f782010-05-25 23:40:22 +0000602 }
603 if (!updated) {
604 // If variableDie is not updated then DBG_VALUE instruction does not
605 // have valid variable info.
606 delete VariableDie;
607 return NULL;
608 }
609 DV->setDIE(VariableDie);
610 return VariableDie;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000611 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000612
613 // .. else use frame index, if available.
Devang Patelc3f5f782010-05-25 23:40:22 +0000614 int FI = 0;
Devang Patele1cdf842011-04-27 22:45:24 +0000615 if (findVariableFrameIndex(DV, &FI)) {
616 unsigned FrameReg = 0;
617 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
618 int Offset =
619 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
620 MachineLocation Location(FrameReg, Offset);
621 VariableCU->addVariableAddress(DV, VariableDie, Location);
622 }
623
Devang Patel53bb5c92009-11-10 23:06:00 +0000624 DV->setDIE(VariableDie);
625 return VariableDie;
626
627}
Devang Patel2c4ceb12009-11-21 02:48:08 +0000628
629/// constructScopeDIE - Construct a DIE for this scope.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000630DIE *DwarfDebug::constructScopeDIE(LexicalScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +0000631 if (!Scope || !Scope->getScopeNode())
632 return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000633
Devang Patel5bc9fec2011-02-19 01:31:27 +0000634 SmallVector <DIE *, 8> Children;
Devang Patel0478c152011-03-01 22:58:55 +0000635
636 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000637 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000638 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
639 if (DbgVariable *ArgDV = CurrentFnArguments[i])
640 if (DIE *Arg = constructVariableDIE(ArgDV, Scope))
641 Children.push_back(Arg);
642
Devang Patel5bc9fec2011-02-19 01:31:27 +0000643 // Collect lexical scope childrens first.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000644 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000645 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
646 if (DIE *Variable = constructVariableDIE(Variables[i], Scope))
647 Children.push_back(Variable);
Devang Patelbf47fdb2011-08-10 20:55:27 +0000648 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000649 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
650 if (DIE *Nested = constructScopeDIE(Scopes[j]))
651 Children.push_back(Nested);
Devang Patel3c91b052010-03-08 20:52:55 +0000652 DIScope DS(Scope->getScopeNode());
653 DIE *ScopeDIE = NULL;
654 if (Scope->getInlinedAt())
655 ScopeDIE = constructInlinedScopeDIE(Scope);
656 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000657 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000658 if (Scope->isAbstractScope()) {
Devang Patel163a9f72010-05-10 22:49:55 +0000659 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000660 // Note down abstract DIE.
661 if (ScopeDIE)
662 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
663 }
Devang Patel3c91b052010-03-08 20:52:55 +0000664 else
Devang Patel2db49d72010-05-07 18:11:54 +0000665 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000666 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000667 else {
668 // There is no need to emit empty lexical block DIE.
669 if (Children.empty())
670 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +0000671 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000672 }
673
Devang Patelaead63c2010-03-29 22:59:58 +0000674 if (!ScopeDIE) return NULL;
Jim Grosbach1e20b962010-07-21 21:21:52 +0000675
Devang Patel5bc9fec2011-02-19 01:31:27 +0000676 // Add children
677 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
678 E = Children.end(); I != E; ++I)
679 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000680
Jim Grosbach1e20b962010-07-21 21:21:52 +0000681 if (DS.isSubprogram())
Devang Patel3cbee302011-04-12 22:53:02 +0000682 getCompileUnit(DS)->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000683
Devang Patel193f7202009-11-24 01:14:22 +0000684 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000685}
686
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000687/// GetOrCreateSourceID - Look up the source id with the given directory and
688/// source file names. If none currently exists, create a new id and insert it
689/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
690/// maps as well.
Devang Patel2f584852010-07-24 00:53:22 +0000691
Devang Patel23670e52011-03-24 20:30:50 +0000692unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
693 StringRef DirName) {
Devang Patel1905a182010-09-16 20:57:49 +0000694 // If FE did not provide a file name, then assume stdin.
695 if (FileName.empty())
Devang Patel23670e52011-03-24 20:30:50 +0000696 return GetOrCreateSourceID("<stdin>", StringRef());
697
698 // MCStream expects full path name as filename.
Benjamin Kramerab5f7882011-06-05 14:36:47 +0000699 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
700 SmallString<128> FullPathName = DirName;
701 sys::path::append(FullPathName, FileName);
Devang Patel23670e52011-03-24 20:30:50 +0000702 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
703 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
704 }
Devang Patel1905a182010-09-16 20:57:49 +0000705
Rafael Espindola5c055632010-11-18 02:04:25 +0000706 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
707 if (Entry.getValue())
708 return Entry.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000709
Rafael Espindola5c055632010-11-18 02:04:25 +0000710 unsigned SrcId = SourceIdMap.size();
711 Entry.setValue(SrcId);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000712
Rafael Espindola5c055632010-11-18 02:04:25 +0000713 // Print out a .file directive to specify files for .loc directives.
Devang Patel23670e52011-03-24 20:30:50 +0000714 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000715
716 return SrcId;
717}
718
Jim Grosbach1e20b962010-07-21 21:21:52 +0000719/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel163a9f72010-05-10 22:49:55 +0000720/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000721void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000722 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000723 StringRef FN = DIUnit.getFilename();
724 StringRef Dir = DIUnit.getDirectory();
Devang Patel23670e52011-03-24 20:30:50 +0000725 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000726
727 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel3cbee302011-04-12 22:53:02 +0000728 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
729 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
730 DIUnit.getProducer());
731 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
732 DIUnit.getLanguage());
733 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +0000734 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
735 // simplifies debug range entries.
Devang Patel3cbee302011-04-12 22:53:02 +0000736 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +0000737 // DW_AT_stmt_list is a offset of line number information for this
Devang Patelaf608bd2010-08-24 00:06:12 +0000738 // compile unit in debug_line section.
Rafael Espindoladc52ecf2011-05-10 20:35:05 +0000739 if(Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindola597a7662011-05-04 17:44:06 +0000740 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patel3cbee302011-04-12 22:53:02 +0000741 Asm->GetTempSymbol("section_line"));
Devang Patelae84d5b2010-08-31 23:50:19 +0000742 else
Devang Patel3cbee302011-04-12 22:53:02 +0000743 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000744
Devang Patel65dbc902009-11-25 17:36:49 +0000745 if (!Dir.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000746 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000747 if (DIUnit.isOptimized())
Devang Patel3cbee302011-04-12 22:53:02 +0000748 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000749
Devang Patel65dbc902009-11-25 17:36:49 +0000750 StringRef Flags = DIUnit.getFlags();
751 if (!Flags.empty())
Devang Patel3cbee302011-04-12 22:53:02 +0000752 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
753
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000754 unsigned RVer = DIUnit.getRunTimeVersion();
755 if (RVer)
Devang Patel3cbee302011-04-12 22:53:02 +0000756 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000757 dwarf::DW_FORM_data1, RVer);
758
Devang Patel163a9f72010-05-10 22:49:55 +0000759 if (!FirstCU)
760 FirstCU = NewCU;
761 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000762}
763
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000764/// getCompileUnit - Get CompileUnit DIE.
Devang Patel163a9f72010-05-10 22:49:55 +0000765CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
766 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
767 DIDescriptor D(N);
768 const MDNode *CUNode = NULL;
769 if (D.isCompileUnit())
770 CUNode = N;
771 else if (D.isSubprogram())
772 CUNode = DISubprogram(N).getCompileUnit();
773 else if (D.isType())
774 CUNode = DIType(N).getCompileUnit();
775 else if (D.isGlobalVariable())
776 CUNode = DIGlobalVariable(N).getCompileUnit();
777 else if (D.isVariable())
778 CUNode = DIVariable(N).getCompileUnit();
779 else if (D.isNameSpace())
780 CUNode = DINameSpace(N).getCompileUnit();
781 else if (D.isFile())
782 CUNode = DIFile(N).getCompileUnit();
783 else
784 return FirstCU;
785
786 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
787 = CUMap.find(CUNode);
788 if (I == CUMap.end())
789 return FirstCU;
790 return I->second;
791}
792
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000793// Return const expression if value is a GEP to access merged global
Devang Patele449d1f2011-01-20 00:02:16 +0000794// constant. e.g.
795// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
796static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
797 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
798 if (!CE || CE->getNumOperands() != 3 ||
Bob Wilsoncfc55c22011-08-03 19:42:51 +0000799 CE->getOpcode() != Instruction::GetElementPtr)
Devang Patele449d1f2011-01-20 00:02:16 +0000800 return NULL;
801
Bob Wilsoncfc55c22011-08-03 19:42:51 +0000802 // First operand points to a global struct.
803 Value *Ptr = CE->getOperand(0);
804 if (!isa<GlobalValue>(Ptr) ||
805 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
Devang Patele449d1f2011-01-20 00:02:16 +0000806 return NULL;
807
808 // Second operand is zero.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000809 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
Devang Patele449d1f2011-01-20 00:02:16 +0000810 if (!CI || !CI->isZero())
811 return NULL;
812
813 // Third operand is offset.
814 if (!isa<ConstantInt>(CE->getOperand(2)))
815 return NULL;
816
817 return CE;
818}
819
Devang Patel163a9f72010-05-10 22:49:55 +0000820/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000821void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel9fa539c2010-08-10 01:37:23 +0000822 DIGlobalVariable GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000823
Devang Patel905cf5e2009-09-04 23:59:07 +0000824 // If debug information is malformed then ignore it.
Devang Patel9fa539c2010-08-10 01:37:23 +0000825 if (GV.Verify() == false)
Devang Patel905cf5e2009-09-04 23:59:07 +0000826 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000827
828 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000829 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel9fa539c2010-08-10 01:37:23 +0000830 if (TheCU->getDIE(GV))
Devang Patel13e16b62009-06-26 01:49:18 +0000831 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000832
Devang Patel9fa539c2010-08-10 01:37:23 +0000833 DIType GTy = GV.getType();
Devang Patel29368072010-08-10 07:11:13 +0000834 DIE *VariableDIE = new DIE(GV.getTag());
835
836 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000837
Devang Patel9fa539c2010-08-10 01:37:23 +0000838 // Add name.
Devang Patel3cbee302011-04-12 22:53:02 +0000839 TheCU->addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
840 GV.getDisplayName());
Devang Patel9fa539c2010-08-10 01:37:23 +0000841 StringRef LinkageName = GV.getLinkageName();
Devang Patel29368072010-08-10 07:11:13 +0000842 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel3cbee302011-04-12 22:53:02 +0000843 TheCU->addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
844 dwarf::DW_FORM_string,
845 getRealLinkageName(LinkageName));
Devang Patel9fa539c2010-08-10 01:37:23 +0000846 // Add type.
Devang Patel3cbee302011-04-12 22:53:02 +0000847 TheCU->addType(VariableDIE, GTy);
Devang Patele9ae06c2011-05-31 22:56:51 +0000848
Devang Patel9fa539c2010-08-10 01:37:23 +0000849 // Add scoping info.
850 if (!GV.isLocalToUnit()) {
Devang Patel3cbee302011-04-12 22:53:02 +0000851 TheCU->addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel9fa539c2010-08-10 01:37:23 +0000852 // Expose as global.
853 TheCU->addGlobal(GV.getName(), VariableDIE);
854 }
855 // Add line number info.
Devang Patel3cbee302011-04-12 22:53:02 +0000856 TheCU->addSourceLine(VariableDIE, GV);
Devang Patel9fa539c2010-08-10 01:37:23 +0000857 // Add to map.
858 TheCU->insertDIE(N, VariableDIE);
859 // Add to context owner.
860 DIDescriptor GVContext = GV.getContext();
Devang Patel3cbee302011-04-12 22:53:02 +0000861 TheCU->addToContextOwner(VariableDIE, GVContext);
Devang Patel9fa539c2010-08-10 01:37:23 +0000862 // Add location.
Devang Patel29368072010-08-10 07:11:13 +0000863 if (isGlobalVariable) {
864 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel3cbee302011-04-12 22:53:02 +0000865 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
866 TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patel29368072010-08-10 07:11:13 +0000867 Asm->Mang->getSymbol(GV.getGlobal()));
868 // Do not create specification DIE if context is either compile unit
869 // or a subprogram.
870 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
871 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
872 // Create specification DIE.
873 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel3cbee302011-04-12 22:53:02 +0000874 TheCU->addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
Devang Patel29368072010-08-10 07:11:13 +0000875 dwarf::DW_FORM_ref4, VariableDIE);
Devang Patel3cbee302011-04-12 22:53:02 +0000876 TheCU->addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
877 TheCU->addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel29368072010-08-10 07:11:13 +0000878 TheCU->addDie(VariableSpecDIE);
879 } else {
Devang Patel3cbee302011-04-12 22:53:02 +0000880 TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel29368072010-08-10 07:11:13 +0000881 }
Devang Patel8594d422011-06-24 20:46:11 +0000882 } else if (const ConstantInt *CI =
Devang Patel76a788c2011-01-06 21:39:25 +0000883 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
Devang Patel3cbee302011-04-12 22:53:02 +0000884 TheCU->addConstantValue(VariableDIE, CI, isUnsignedDIType(GTy));
Devang Patele449d1f2011-01-20 00:02:16 +0000885 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
886 // GV is a merged global.
887 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bob Wilsoncfc55c22011-08-03 19:42:51 +0000888 Value *Ptr = CE->getOperand(0);
Devang Patel3cbee302011-04-12 22:53:02 +0000889 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
890 TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
Bob Wilsoncfc55c22011-08-03 19:42:51 +0000891 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
Devang Patel3cbee302011-04-12 22:53:02 +0000892 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Bob Wilsoncfc55c22011-08-03 19:42:51 +0000893 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Devang Patel027cbf92011-08-03 01:25:46 +0000894 TheCU->addUInt(Block, 0, dwarf::DW_FORM_udata,
Bob Wilsoncfc55c22011-08-03 19:42:51 +0000895 Asm->getTargetData().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel3cbee302011-04-12 22:53:02 +0000896 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
897 TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Devang Patele449d1f2011-01-20 00:02:16 +0000898 }
Devang Patel76a788c2011-01-06 21:39:25 +0000899
Devang Patel13e16b62009-06-26 01:49:18 +0000900 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000901}
902
Devang Patel163a9f72010-05-10 22:49:55 +0000903/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000904void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000905 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000906
Stuart Hastings639336e2010-04-06 21:38:29 +0000907 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000908 CompileUnit *TheCU = getCompileUnit(N);
909 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +0000910 return;
911
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000912 if (!SP.isDefinition())
913 // This is a method declaration which will be handled while constructing
914 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000915 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000916
Stuart Hastings639336e2010-04-06 21:38:29 +0000917 DIE *SubprogramDie = createSubprogramDIE(SP);
918
919 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +0000920 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000921
922 // Add to context owner.
Devang Patel3cbee302011-04-12 22:53:02 +0000923 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +0000924
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000925 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +0000926 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +0000927
Devang Patel13e16b62009-06-26 01:49:18 +0000928 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000929}
930
Devang Patel2c4ceb12009-11-21 02:48:08 +0000931/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +0000932/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000933/// This is invoked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +0000934void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +0000935 if (DisableDebugInfoPrinting)
936 return;
937
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000938 // If module has named metadata anchors then use them, otherwise scan the
939 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000940 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
941 if (CU_Nodes) {
Devang Patela938dd02011-05-04 16:34:02 +0000942
943 NamedMDNode *GV_Nodes = M->getNamedMetadata("llvm.dbg.gv");
944 NamedMDNode *SP_Nodes = M->getNamedMetadata("llvm.dbg.sp");
945 if (!GV_Nodes && !SP_Nodes)
946 // If there are not any global variables or any functions then
947 // there is not any debug info in this module.
Devang Patel30692ab2011-05-03 16:45:22 +0000948 return;
949
Devang Patela938dd02011-05-04 16:34:02 +0000950 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
951 constructCompileUnit(CU_Nodes->getOperand(i));
Devang Patel30692ab2011-05-03 16:45:22 +0000952
Devang Patela938dd02011-05-04 16:34:02 +0000953 if (GV_Nodes)
954 for (unsigned i = 0, e = GV_Nodes->getNumOperands(); i != e; ++i)
955 constructGlobalVariableDIE(GV_Nodes->getOperand(i));
956
957 if (SP_Nodes)
958 for (unsigned i = 0, e = SP_Nodes->getNumOperands(); i != e; ++i)
959 constructSubprogramDIE(SP_Nodes->getOperand(i));
Devang Patel30692ab2011-05-03 16:45:22 +0000960
961 } else {
962
963 DebugInfoFinder DbgFinder;
964 DbgFinder.processModule(*M);
965
Devang Patela938dd02011-05-04 16:34:02 +0000966 bool HasDebugInfo = false;
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000967 // Scan all the compile-units to see if there are any marked as the main
968 // unit. If not, we do not generate debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000969 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
970 E = DbgFinder.compile_unit_end(); I != E; ++I) {
971 if (DICompileUnit(*I).isMain()) {
972 HasDebugInfo = true;
973 break;
974 }
975 }
Devang Patela938dd02011-05-04 16:34:02 +0000976 if (!HasDebugInfo) return;
Devang Patel30692ab2011-05-03 16:45:22 +0000977
978 // Create all the compile unit DIEs.
979 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
980 E = DbgFinder.compile_unit_end(); I != E; ++I)
981 constructCompileUnit(*I);
982
983 // Create DIEs for each global variable.
984 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
985 E = DbgFinder.global_variable_end(); I != E; ++I)
986 constructGlobalVariableDIE(*I);
987
988 // Create DIEs for each subprogram.
989 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
990 E = DbgFinder.subprogram_end(); I != E; ++I)
991 constructSubprogramDIE(*I);
Chris Lattnerd850ac72010-04-05 02:19:28 +0000992 }
Devang Patel30692ab2011-05-03 16:45:22 +0000993
Chris Lattnerd850ac72010-04-05 02:19:28 +0000994 // Tell MMI that we have debug info.
995 MMI->setDebugInfoAvailability(true);
Devang Patel30692ab2011-05-03 16:45:22 +0000996
Chris Lattnerbe15beb2010-04-04 23:17:54 +0000997 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +0000998 EmitSectionLabels();
Jim Grosbach1e20b962010-07-21 21:21:52 +0000999
Devang Patele7e5a0f2010-08-10 20:01:20 +00001000 //getOrCreateTypeDIE
1001 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
Devang Patel3cbee302011-04-12 22:53:02 +00001002 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1003 DIType Ty(NMD->getOperand(i));
1004 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
1005 }
Devang Patele7e5a0f2010-08-10 20:01:20 +00001006
Devang Patel1a7ca032010-09-28 18:08:20 +00001007 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
Devang Patel3cbee302011-04-12 22:53:02 +00001008 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1009 DIType Ty(NMD->getOperand(i));
1010 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
1011 }
Devang Patel1a7ca032010-09-28 18:08:20 +00001012
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001013 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001014 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001015}
1016
Devang Patel2c4ceb12009-11-21 02:48:08 +00001017/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001018///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001019void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +00001020 if (!FirstCU) return;
Devang Patel4a1cad62010-06-28 18:25:03 +00001021 const Module *M = MMI->getModule();
Devang Patelbf47fdb2011-08-10 20:55:27 +00001022 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patel4a1cad62010-06-28 18:25:03 +00001023 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
1024 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
1025 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
1026 DISubprogram SP(AllSPs->getOperand(SI));
1027 if (!SP.Verify()) continue;
1028
1029 // Collect info for variables that were optimized out.
Devang Patel8b3a6b62010-07-19 17:53:55 +00001030 if (!SP.isDefinition()) continue;
Devang Patel4a1cad62010-06-28 18:25:03 +00001031 StringRef FName = SP.getLinkageName();
1032 if (FName.empty())
1033 FName = SP.getName();
Devang Patel62367042010-11-10 22:19:21 +00001034 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patel4a1cad62010-06-28 18:25:03 +00001035 if (!NMD) continue;
1036 unsigned E = NMD->getNumOperands();
1037 if (!E) continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001038 LexicalScope *Scope = new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
Devang Patele9a1cca2010-08-02 17:32:15 +00001039 DeadFnScopeMap[SP] = Scope;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001040 SmallVector<DbgVariable *, 8> Variables;
Devang Patel4a1cad62010-06-28 18:25:03 +00001041 for (unsigned I = 0; I != E; ++I) {
1042 DIVariable DV(NMD->getOperand(I));
1043 if (!DV.Verify()) continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001044 Variables.push_back(new DbgVariable(DV));
Devang Patel4a1cad62010-06-28 18:25:03 +00001045 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001046
Devang Patel4a1cad62010-06-28 18:25:03 +00001047 // Construct subprogram DIE and add variables DIEs.
1048 constructSubprogramDIE(SP);
1049 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patel4a1cad62010-06-28 18:25:03 +00001050 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001051 if (DIE *VariableDIE = constructVariableDIE(Variables[i], Scope))
Devang Patel4a1cad62010-06-28 18:25:03 +00001052 ScopeDIE->addChild(VariableDIE);
1053 }
1054 }
1055 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001056
Devang Patel53bb5c92009-11-10 23:06:00 +00001057 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1058 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1059 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1060 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +00001061 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00001062 }
1063
Devang Patele9f8f5e2010-05-07 20:54:48 +00001064 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00001065 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1066 DIE *SPDie = CI->first;
Devang Patele9f8f5e2010-05-07 20:54:48 +00001067 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel5d11eb02009-12-03 19:11:07 +00001068 if (!N) continue;
Devang Patel163a9f72010-05-10 22:49:55 +00001069 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Patel5d11eb02009-12-03 19:11:07 +00001070 if (!NDie) continue;
Devang Patel3cbee302011-04-12 22:53:02 +00001071 getCompileUnit(N)->addDIEEntry(SPDie, dwarf::DW_AT_containing_type,
1072 dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00001073 }
1074
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001075 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001076 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00001077 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001078 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00001079 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001080
1081 // End text sections.
1082 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001083 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +00001084 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001085 }
1086
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001087 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001088 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001089
1090 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00001091 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001092
1093 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001094 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001095
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001096 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001097 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001098
Devang Patel193f7202009-11-24 01:14:22 +00001099 // Emit info into a debug pubtypes section.
1100 emitDebugPubTypes();
1101
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001102 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001103 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001104
1105 // Emit info into a debug aranges section.
1106 EmitDebugARanges();
1107
1108 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001109 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001110
1111 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001112 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001113
1114 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001115 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001116
Chris Lattnerbc733f52010-03-13 02:17:42 +00001117 // Emit info into a debug str section.
1118 emitDebugStr();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001119
Devang Patele9a1cca2010-08-02 17:32:15 +00001120 // clean up.
1121 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel163a9f72010-05-10 22:49:55 +00001122 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1123 E = CUMap.end(); I != E; ++I)
1124 delete I->second;
1125 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001126}
1127
Devang Patel53bb5c92009-11-10 23:06:00 +00001128/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelbf47fdb2011-08-10 20:55:27 +00001129DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +00001130 DebugLoc ScopeLoc) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001131 LLVMContext &Ctx = DV->getContext();
1132
1133 // More then one inlined variable corresponds to one abstract variable.
1134 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel53bb5c92009-11-10 23:06:00 +00001135
Devang Patel2db49d72010-05-07 18:11:54 +00001136 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001137 if (AbsDbgVariable)
1138 return AbsDbgVariable;
1139
Devang Patelbf47fdb2011-08-10 20:55:27 +00001140 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00001141 if (!Scope)
1142 return NULL;
1143
Devang Patel26c1e562010-05-20 16:36:41 +00001144 AbsDbgVariable = new DbgVariable(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001145 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00001146 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00001147 return AbsDbgVariable;
1148}
1149
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001150/// addCurrentFnArgument - If Var is a current function argument then add
1151/// it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +00001152bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +00001153 DbgVariable *Var, LexicalScope *Scope) {
1154 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +00001155 return false;
1156 DIVariable DV = Var->getVariable();
1157 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1158 return false;
1159 unsigned ArgNo = DV.getArgNumber();
1160 if (ArgNo == 0)
1161 return false;
1162
Devang Patelcb3a6572011-03-03 20:02:02 +00001163 size_t Size = CurrentFnArguments.size();
1164 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001165 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +00001166 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +00001167 // arguments does the function have at source level.
1168 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +00001169 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +00001170 CurrentFnArguments[ArgNo - 1] = Var;
1171 return true;
1172}
1173
Devang Patelee432862010-05-20 19:57:06 +00001174/// collectVariableInfoFromMMITable - Collect variable information from
1175/// side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001176void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001177DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +00001178 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +00001179 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1180 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1181 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00001182 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00001183 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00001184 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00001185 DIVariable DV(Var);
1186 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001187
Devang Patelbf47fdb2011-08-10 20:55:27 +00001188 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001189
Devang Patelfb0ee432009-11-10 23:20:04 +00001190 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00001191 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00001192 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001193
Devang Patel26c1e562010-05-20 16:36:41 +00001194 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
1195 DbgVariable *RegVar = new DbgVariable(DV);
1196 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel0478c152011-03-01 22:58:55 +00001197 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001198 addScopeVariable(Scope, RegVar);
Devang Patel26c1e562010-05-20 16:36:41 +00001199 if (AbsDbgVariable) {
1200 recordVariableFrameIndex(AbsDbgVariable, VP.first);
1201 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
1202 }
Devang Patele717faa2009-10-06 01:26:37 +00001203 }
Devang Patelee432862010-05-20 19:57:06 +00001204}
Devang Patel90a48ad2010-03-15 18:33:46 +00001205
Jim Grosbach1e20b962010-07-21 21:21:52 +00001206/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patelc3f5f782010-05-25 23:40:22 +00001207/// DBG_VALUE instruction, is in a defined reg.
1208static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
1209 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001210 return MI->getNumOperands() == 3 &&
1211 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
1212 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patelc3f5f782010-05-25 23:40:22 +00001213}
1214
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001215/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel90b40412011-07-08 17:09:57 +00001216/// at MI.
1217static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1218 const MCSymbol *FLabel,
1219 const MCSymbol *SLabel,
1220 const MachineInstr *MI) {
1221 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1222
1223 if (MI->getNumOperands() != 3) {
1224 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1225 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1226 }
1227 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1228 MachineLocation MLoc;
1229 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1230 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1231 }
1232 if (MI->getOperand(0).isImm())
1233 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1234 if (MI->getOperand(0).isFPImm())
1235 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1236 if (MI->getOperand(0).isCImm())
1237 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1238
1239 assert (0 && "Unexpected 3 operand DBG_VALUE instruction!");
1240 return DotDebugLocEntry();
1241}
1242
Devang Patelbf47fdb2011-08-10 20:55:27 +00001243/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001244void
Devang Patel78e127d2010-06-25 22:07:34 +00001245DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1246 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001247
Devang Patelee432862010-05-20 19:57:06 +00001248 /// collection info from MMI table.
1249 collectVariableInfoFromMMITable(MF, Processed);
1250
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001251 for (SmallVectorImpl<const MDNode*>::const_iterator
1252 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1253 ++UVI) {
1254 const MDNode *Var = *UVI;
1255 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001256 continue;
1257
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001258 // History contains relevant DBG_VALUE instructions for Var and instructions
1259 // clobbering it.
1260 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1261 if (History.empty())
1262 continue;
1263 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001264
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001265 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001266 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001267 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1268 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001269 Scope = LScopes.getCurrentFunctionScope();
Devang Patel40c7e412011-07-20 22:18:50 +00001270 else {
1271 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patelbf47fdb2011-08-10 20:55:27 +00001272 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel40c7e412011-07-20 22:18:50 +00001273 else {
1274 if (MDNode *IA = DV.getInlinedAt())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001275 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel40c7e412011-07-20 22:18:50 +00001276 else
Devang Patelbf47fdb2011-08-10 20:55:27 +00001277 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel40c7e412011-07-20 22:18:50 +00001278 }
1279 }
Devang Patelee432862010-05-20 19:57:06 +00001280 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001281 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001282 continue;
1283
1284 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001285 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patelee432862010-05-20 19:57:06 +00001286 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel0478c152011-03-01 22:58:55 +00001287 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001288 addScopeVariable(Scope, RegVar);
Devang Patelc0c5a262010-05-21 00:10:20 +00001289 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
1290 DbgVariableToDbgInstMap[AbsVar] = MInsn;
1291 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00001292 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001293
1294 // Simple ranges that are fully coalesced.
1295 if (History.size() <= 1 || (History.size() == 2 &&
1296 MInsn->isIdenticalTo(History.back()))) {
Devang Patelc3f5f782010-05-25 23:40:22 +00001297 DbgVariableToDbgInstMap[RegVar] = MInsn;
1298 continue;
1299 }
1300
1301 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001302 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001303
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001304 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1305 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1306 const MachineInstr *Begin = *HI;
1307 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001308
Devang Patel4ada1d72011-06-01 23:00:17 +00001309 // Check if DBG_VALUE is truncating a range.
1310 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1311 && !Begin->getOperand(0).getReg())
1312 continue;
1313
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001314 // Compute the range for a register location.
1315 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1316 const MCSymbol *SLabel = 0;
1317
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001318 if (HI + 1 == HE)
1319 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001320 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001321 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001322 else {
1323 const MachineInstr *End = HI[1];
Devang Patel476df5f2011-07-07 21:44:42 +00001324 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1325 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001326 if (End->isDebugValue())
1327 SLabel = getLabelBeforeInsn(End);
1328 else {
1329 // End is a normal instruction clobbering the range.
1330 SLabel = getLabelAfterInsn(End);
1331 assert(SLabel && "Forgot label after clobber instruction");
1332 ++HI;
1333 }
1334 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001335
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001336 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel90b40412011-07-08 17:09:57 +00001337 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001338 }
1339 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001340 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001341
1342 // Collect info for variables that were optimized out.
Devang Pateld1bbc6b2010-06-22 01:01:58 +00001343 const Function *F = MF->getFunction();
Devang Patel62367042010-11-10 22:19:21 +00001344 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patel98e1cac2010-05-14 21:01:35 +00001345 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001346 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patelee432862010-05-20 19:57:06 +00001347 if (!DV || !Processed.insert(DV))
Devang Patel98e1cac2010-05-14 21:01:35 +00001348 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001349 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1350 addScopeVariable(Scope, new DbgVariable(DV));
Devang Patel98e1cac2010-05-14 21:01:35 +00001351 }
1352 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001353}
Devang Patel98e1cac2010-05-14 21:01:35 +00001354
Devang Patelc3f5f782010-05-25 23:40:22 +00001355/// getLabelBeforeInsn - Return Label preceding the instruction.
1356const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001357 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1358 assert(Label && "Didn't insert label before instruction");
1359 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001360}
1361
1362/// getLabelAfterInsn - Return Label immediately following the instruction.
1363const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001364 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001365}
1366
Devang Patelcbbe2872010-10-26 17:49:02 +00001367/// beginInstruction - Process beginning of an instruction.
1368void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001369 // Check if source location changes, but ignore DBG_VALUE locations.
1370 if (!MI->isDebugValue()) {
1371 DebugLoc DL = MI->getDebugLoc();
1372 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel4243e672011-05-11 19:22:19 +00001373 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001374 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001375 if (DL == PrologEndLoc) {
1376 Flags |= DWARF2_FLAG_PROLOGUE_END;
1377 PrologEndLoc = DebugLoc();
1378 }
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001379 if (!DL.isUnknown()) {
1380 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001381 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001382 } else
Devang Patel4243e672011-05-11 19:22:19 +00001383 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001384 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001385 }
Devang Patelaead63c2010-03-29 22:59:58 +00001386
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001387 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001388 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1389 LabelsBeforeInsn.find(MI);
1390
1391 // No label needed.
1392 if (I == LabelsBeforeInsn.end())
1393 return;
1394
1395 // Label already assigned.
1396 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001397 return;
Devang Patel553881b2010-03-29 17:20:31 +00001398
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001399 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001400 PrevLabel = MMI->getContext().CreateTempSymbol();
1401 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001402 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001403 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001404}
1405
Devang Patelcbbe2872010-10-26 17:49:02 +00001406/// endInstruction - Process end of an instruction.
1407void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001408 // Don't create a new label after DBG_VALUE instructions.
1409 // They don't generate code.
1410 if (!MI->isDebugValue())
1411 PrevLabel = 0;
1412
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001413 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1414 LabelsAfterInsn.find(MI);
1415
1416 // No label needed.
1417 if (I == LabelsAfterInsn.end())
1418 return;
1419
1420 // Label already assigned.
1421 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001422 return;
1423
1424 // We need a label after this instruction.
1425 if (!PrevLabel) {
1426 PrevLabel = MMI->getContext().CreateTempSymbol();
1427 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001428 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001429 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001430}
1431
Jim Grosbach1e20b962010-07-21 21:21:52 +00001432/// identifyScopeMarkers() -
Devang Patelbf47fdb2011-08-10 20:55:27 +00001433/// Each LexicalScope has first instruction and last instruction to mark beginning
Devang Pateleac9c072010-04-27 19:46:33 +00001434/// and end of a scope respectively. Create an inverse map that list scopes
1435/// starts (and ends) with an instruction. One instruction may start (or end)
1436/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001437void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001438 SmallVector<LexicalScope *, 4> WorkList;
1439 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001440 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001441 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001442
Devang Patelbf47fdb2011-08-10 20:55:27 +00001443 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001444 if (!Children.empty())
Devang Patelbf47fdb2011-08-10 20:55:27 +00001445 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001446 SE = Children.end(); SI != SE; ++SI)
1447 WorkList.push_back(*SI);
1448
Devang Patel53bb5c92009-11-10 23:06:00 +00001449 if (S->isAbstractScope())
1450 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001451
Devang Patelbf47fdb2011-08-10 20:55:27 +00001452 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001453 if (Ranges.empty())
1454 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001455 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001456 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001457 assert(RI->first && "InsnRange does not have first instruction!");
1458 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001459 requestLabelBeforeInsn(RI->first);
1460 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001461 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001462 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001463}
1464
Devang Patela3f48672011-05-09 22:14:49 +00001465/// getScopeNode - Get MDNode for DebugLoc's scope.
1466static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1467 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1468 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1469 return DL.getScope(Ctx);
1470}
1471
Devang Patel4243e672011-05-11 19:22:19 +00001472/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1473/// line number info for the function.
1474static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1475 const MDNode *Scope = getScopeNode(DL, Ctx);
1476 DISubprogram SP = getDISubprogram(Scope);
1477 if (SP.Verify())
1478 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1479 return DebugLoc();
1480}
1481
Devang Patel2c4ceb12009-11-21 02:48:08 +00001482/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001483/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00001484void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001485 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001486 LScopes.initialize(*MF);
1487 if (LScopes.empty()) return;
1488 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001489
Devang Pateleac9c072010-04-27 19:46:33 +00001490 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1491 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001492 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001493 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001494
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001495 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1496
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001497 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001498 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1499 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1500
Devang Patelb2b31a62010-05-26 19:37:24 +00001501 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001502 I != E; ++I) {
1503 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001504 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1505 II != IE; ++II) {
1506 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001507
Devang Patelb2b31a62010-05-26 19:37:24 +00001508 if (MI->isDebugValue()) {
Devang Patelb2b31a62010-05-26 19:37:24 +00001509 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001510
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001511 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001512 const MDNode *Var =
1513 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001514
1515 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001516 if (isDbgValueInDefinedReg(MI))
1517 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1518
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001519 // Check the history of this variable.
1520 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1521 if (History.empty()) {
1522 UserVariables.push_back(Var);
1523 // The first mention of a function argument gets the FunctionBeginSym
1524 // label, so arguments are visible when breaking at function entry.
1525 DIVariable DV(Var);
1526 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1527 DISubprogram(getDISubprogram(DV.getContext()))
1528 .describes(MF->getFunction()))
1529 LabelsBeforeInsn[MI] = FunctionBeginSym;
1530 } else {
1531 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1532 const MachineInstr *Prev = History.back();
1533 if (Prev->isDebugValue()) {
1534 // Coalesce identical entries at the end of History.
1535 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001536 Prev->isIdenticalTo(History[History.size() - 2])) {
1537 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1538 << "\t" << *Prev
1539 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001540 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001541 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001542
1543 // Terminate old register assignments that don't reach MI;
1544 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1545 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1546 isDbgValueInDefinedReg(Prev)) {
1547 // Previous register assignment needs to terminate at the end of
1548 // its basic block.
1549 MachineBasicBlock::const_iterator LastMI =
1550 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001551 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001552 // Drop DBG_VALUE for empty range.
Devang Patel79862892011-07-07 00:14:27 +00001553 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1554 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001555 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001556 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001557 else {
1558 // Terminate after LastMI.
1559 History.push_back(LastMI);
1560 }
1561 }
1562 }
1563 }
1564 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001565 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001566 // Not a DBG_VALUE instruction.
1567 if (!MI->isLabel())
1568 AtBlockEntry = false;
1569
Devang Patel4243e672011-05-11 19:22:19 +00001570 // First known non DBG_VALUE location marks beginning of function
1571 // body.
1572 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1573 PrologEndLoc = MI->getDebugLoc();
1574
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001575 // Check if the instruction clobbers any registers with debug vars.
1576 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1577 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1578 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1579 continue;
1580 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1581 unsigned Reg = *AI; ++AI) {
1582 const MDNode *Var = LiveUserVar[Reg];
1583 if (!Var)
1584 continue;
1585 // Reg is now clobbered.
1586 LiveUserVar[Reg] = 0;
1587
1588 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001589 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1590 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001591 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001592 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1593 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001594 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001595 const MachineInstr *Prev = History.back();
1596 // Sanity-check: Register assignments are terminated at the end of
1597 // their block.
1598 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1599 continue;
1600 // Is the variable still in Reg?
1601 if (!isDbgValueInDefinedReg(Prev) ||
1602 Prev->getOperand(0).getReg() != Reg)
1603 continue;
1604 // Var is clobbered. Make sure the next instruction gets a label.
1605 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001606 }
1607 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001608 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001609 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001610 }
1611
1612 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1613 I != E; ++I) {
1614 SmallVectorImpl<const MachineInstr*> &History = I->second;
1615 if (History.empty())
1616 continue;
1617
1618 // Make sure the final register assignments are terminated.
1619 const MachineInstr *Prev = History.back();
1620 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1621 const MachineBasicBlock *PrevMBB = Prev->getParent();
1622 MachineBasicBlock::const_iterator LastMI = PrevMBB->getLastNonDebugInstr();
1623 if (LastMI == PrevMBB->end())
1624 // Drop DBG_VALUE for empty range.
1625 History.pop_back();
1626 else {
1627 // Terminate after LastMI.
1628 History.push_back(LastMI);
1629 }
1630 }
1631 // Request labels for the full history.
1632 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1633 const MachineInstr *MI = History[i];
1634 if (MI->isDebugValue())
1635 requestLabelBeforeInsn(MI);
1636 else
1637 requestLabelAfterInsn(MI);
1638 }
1639 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001640
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001641 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001642 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001643
1644 // Record beginning of function.
1645 if (!PrologEndLoc.isUnknown()) {
1646 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1647 MF->getFunction()->getContext());
1648 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1649 FnStartDL.getScope(MF->getFunction()->getContext()),
1650 DWARF2_FLAG_IS_STMT);
1651 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001652}
1653
Devang Patelbf47fdb2011-08-10 20:55:27 +00001654void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1655// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1656 ScopeVariables[LS].push_back(Var);
1657// Vars.push_back(Var);
1658}
1659
Devang Patel2c4ceb12009-11-21 02:48:08 +00001660/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001661///
Chris Lattnereec791a2010-01-26 23:18:02 +00001662void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001663 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001664
Devang Patelbf47fdb2011-08-10 20:55:27 +00001665 // Define end label for subprogram.
1666 FunctionEndSym = Asm->GetTempSymbol("func_end",
1667 Asm->getFunctionNumber());
1668 // Assumes in correct section after the entry point.
1669 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1670
1671 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1672 collectVariableInfo(MF, ProcessedVars);
1673
1674 // Construct abstract scopes.
1675 SmallVector<LexicalScope *, 4> &AList = LScopes.getAbstractScopesList();
1676 for (SmallVector<LexicalScope *, 4>::iterator AI = AList.begin(),
1677 AE = AList.end(); AI != AE; ++AI) {
1678 DISubprogram SP((*AI)->getScopeNode());
1679 if (SP.Verify()) {
1680 // Collect info for variables that were optimized out.
1681 StringRef FName = SP.getLinkageName();
1682 if (FName.empty())
1683 FName = SP.getName();
1684 if (NamedMDNode *NMD =
1685 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
1686 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman872814a2010-07-21 18:54:18 +00001687 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel78e127d2010-06-25 22:07:34 +00001688 if (!DV || !ProcessedVars.insert(DV))
1689 continue;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001690 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1691 addScopeVariable(Scope, new DbgVariable(DV));
Devang Patel78e127d2010-06-25 22:07:34 +00001692 }
1693 }
1694 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001695 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
1696 constructScopeDIE(*AI);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001697 }
Devang Patelbf47fdb2011-08-10 20:55:27 +00001698
1699 DIE *CurFnDIE = constructScopeDIE(LScopes.getCurrentFunctionScope());
1700
1701 if (!DisableFramePointerElim(*MF)) {
1702 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1703 getCompileUnit(FnScope->getScopeNode())->addUInt(CurFnDIE,
1704 dwarf::DW_AT_APPLE_omit_frame_ptr,
1705 dwarf::DW_FORM_flag, 1);
1706 }
1707 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1708 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001709
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001710 // Clear debug info
Devang Patelbf47fdb2011-08-10 20:55:27 +00001711 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1712 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1713 DeleteContainerPointers(I->second);
1714 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001715 DeleteContainerPointers(CurrentFnArguments);
Devang Patel26c1e562010-05-20 16:36:41 +00001716 DbgVariableToFrameIndexMap.clear();
1717 VarToAbstractVarMap.clear();
1718 DbgVariableToDbgInstMap.clear();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001719 UserVariables.clear();
1720 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001721 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001722 LabelsBeforeInsn.clear();
1723 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001724 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001725}
1726
Devang Patel26c1e562010-05-20 16:36:41 +00001727/// recordVariableFrameIndex - Record a variable's index.
1728void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
1729 assert (V && "Invalid DbgVariable!");
1730 DbgVariableToFrameIndexMap[V] = Index;
1731}
1732
1733/// findVariableFrameIndex - Return true if frame index for the variable
1734/// is found. Update FI to hold value of the index.
1735bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
1736 assert (V && "Invalid DbgVariable!");
1737 DenseMap<const DbgVariable *, int>::iterator I =
1738 DbgVariableToFrameIndexMap.find(V);
1739 if (I == DbgVariableToFrameIndexMap.end())
1740 return false;
1741 *FI = I->second;
1742 return true;
1743}
1744
Chris Lattnerc6087842010-03-09 04:54:43 +00001745/// recordSourceLine - Register a source line with debug info. Returns the
1746/// unique label that was emitted and which provides correspondence to
1747/// the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001748void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1749 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001750 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001751 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001752 unsigned Src = 1;
1753 if (S) {
1754 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001755
Dan Gohman1cc0d622010-05-05 23:41:32 +00001756 if (Scope.isCompileUnit()) {
1757 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001758 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001759 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001760 } else if (Scope.isFile()) {
1761 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001762 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001763 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001764 } else if (Scope.isSubprogram()) {
1765 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001766 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001767 Dir = SP.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001768 } else if (Scope.isLexicalBlock()) {
1769 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001770 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001771 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001772 } else
1773 assert(0 && "Unexpected scope info");
1774
Devang Patel23670e52011-03-24 20:30:50 +00001775 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001776 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001777 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001778}
1779
Bill Wendling829e67b2009-05-20 23:22:40 +00001780//===----------------------------------------------------------------------===//
1781// Emit Methods
1782//===----------------------------------------------------------------------===//
1783
Devang Patel2c4ceb12009-11-21 02:48:08 +00001784/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00001785///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001786unsigned
1787DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001788 // Get the children.
1789 const std::vector<DIE *> &Children = Die->getChildren();
1790
1791 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001792 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00001793 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00001794
1795 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001796 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001797
1798 // Get the abbreviation for this DIE.
1799 unsigned AbbrevNumber = Die->getAbbrevNumber();
1800 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1801
1802 // Set DIE offset
1803 Die->setOffset(Offset);
1804
1805 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001806 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001807
1808 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1809 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1810
1811 // Size the DIE attribute values.
1812 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1813 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001814 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001815
1816 // Size the DIE children if any.
1817 if (!Children.empty()) {
1818 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1819 "Children flag not set");
1820
1821 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001822 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00001823
1824 // End of children marker.
1825 Offset += sizeof(int8_t);
1826 }
1827
1828 Die->setSize(Offset - Die->getOffset());
1829 return Offset;
1830}
1831
Devang Patel2c4ceb12009-11-21 02:48:08 +00001832/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00001833///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001834void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00001835 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1836 E = CUMap.end(); I != E; ++I) {
1837 // Compute size of compile unit header.
Devang Patel513edf62011-04-12 23:10:47 +00001838 unsigned Offset =
Devang Patel163a9f72010-05-10 22:49:55 +00001839 sizeof(int32_t) + // Length of Compilation Unit Info
1840 sizeof(int16_t) + // DWARF version number
1841 sizeof(int32_t) + // Offset Into Abbrev. Section
1842 sizeof(int8_t); // Pointer Size (in bytes)
1843 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel163a9f72010-05-10 22:49:55 +00001844 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001845}
1846
Chris Lattner11b8f302010-04-04 23:02:02 +00001847/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1848/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00001849static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00001850 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00001851 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00001852 if (!SymbolStem) return 0;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001853
Chris Lattner9c69e285532010-04-04 22:59:04 +00001854 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1855 Asm->OutStreamer.EmitLabel(TmpSym);
1856 return TmpSym;
1857}
1858
1859/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1860/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00001861void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001862 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001863
Bill Wendling94d04b82009-05-20 23:21:38 +00001864 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001865 DwarfInfoSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001866 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001867 DwarfAbbrevSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001868 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00001869 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001870
Chris Lattner9c69e285532010-04-04 22:59:04 +00001871 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00001872 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001873
Devang Patelaf608bd2010-08-24 00:06:12 +00001874 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner11b8f302010-04-04 23:02:02 +00001875 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1876 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1877 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001878 DwarfStrSectionSym =
Chris Lattner9c69e285532010-04-04 22:59:04 +00001879 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00001880 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1881 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001882
Devang Patelc3f5f782010-05-25 23:40:22 +00001883 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1884 "section_debug_loc");
1885
Chris Lattner9c69e285532010-04-04 22:59:04 +00001886 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00001887 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001888}
1889
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001890/// emitDIE - Recursively emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00001891///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001892void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001893 // Get the abbreviation for this DIE.
1894 unsigned AbbrevNumber = Die->getAbbrevNumber();
1895 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1896
Bill Wendling94d04b82009-05-20 23:21:38 +00001897 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001898 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00001899 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1900 Twine::utohexstr(Die->getOffset()) + ":0x" +
1901 Twine::utohexstr(Die->getSize()) + " " +
1902 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001903 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001904
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00001905 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00001906 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1907
1908 // Emit the DIE attribute values.
1909 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1910 unsigned Attr = AbbrevData[i].getAttribute();
1911 unsigned Form = AbbrevData[i].getForm();
1912 assert(Form && "Too many attributes for DIE (check abbreviation)");
1913
Chris Lattner3f53c832010-04-04 18:52:31 +00001914 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00001915 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001916
Bill Wendling94d04b82009-05-20 23:21:38 +00001917 switch (Attr) {
1918 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00001919 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00001920 break;
1921 case dwarf::DW_AT_abstract_origin: {
1922 DIEEntry *E = cast<DIEEntry>(Values[i]);
1923 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00001924 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00001925 Asm->EmitInt32(Addr);
1926 break;
1927 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001928 case dwarf::DW_AT_ranges: {
1929 // DW_AT_range Value encodes offset in debug_range section.
1930 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00001931
1932 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1933 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1934 V->getValue(),
1935 4);
1936 } else {
1937 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1938 V->getValue(),
1939 DwarfDebugRangeSectionSym,
1940 4);
1941 }
Devang Patelf2548ca2010-04-16 23:33:45 +00001942 break;
1943 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001944 case dwarf::DW_AT_location: {
1945 if (UseDotDebugLocEntry.count(Die) != 0) {
1946 DIELabel *L = cast<DIELabel>(Values[i]);
Daniel Dunbar83320a02011-03-16 22:16:39 +00001947 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patelc3f5f782010-05-25 23:40:22 +00001948 } else
1949 Values[i]->EmitValue(Asm, Form);
1950 break;
1951 }
Devang Patel2a361602010-09-29 19:08:08 +00001952 case dwarf::DW_AT_accessibility: {
1953 if (Asm->isVerbose()) {
1954 DIEInteger *V = cast<DIEInteger>(Values[i]);
1955 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1956 }
1957 Values[i]->EmitValue(Asm, Form);
1958 break;
1959 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001960 default:
1961 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001962 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001963 break;
1964 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001965 }
1966
1967 // Emit the DIE children if any.
1968 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1969 const std::vector<DIE *> &Children = Die->getChildren();
1970
1971 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001972 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00001973
Chris Lattner3f53c832010-04-04 18:52:31 +00001974 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00001975 Asm->OutStreamer.AddComment("End Of Children Mark");
1976 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001977 }
1978}
1979
Devang Patel8a241142009-12-09 18:24:21 +00001980/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00001981///
Devang Patel8a241142009-12-09 18:24:21 +00001982void DwarfDebug::emitDebugInfo() {
1983 // Start debug info section.
1984 Asm->OutStreamer.SwitchSection(
1985 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00001986 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1987 E = CUMap.end(); I != E; ++I) {
1988 CompileUnit *TheCU = I->second;
1989 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001990
Devang Patel163a9f72010-05-10 22:49:55 +00001991 // Emit the compile units header.
1992 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1993 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00001994
Devang Patel163a9f72010-05-10 22:49:55 +00001995 // Emit size of content not including length itself
1996 unsigned ContentSize = Die->getSize() +
1997 sizeof(int16_t) + // DWARF version number
1998 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00001999 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00002000
Devang Patel163a9f72010-05-10 22:49:55 +00002001 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2002 Asm->EmitInt32(ContentSize);
2003 Asm->OutStreamer.AddComment("DWARF version number");
2004 Asm->EmitInt16(dwarf::DWARF_VERSION);
2005 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2006 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2007 DwarfAbbrevSectionSym);
2008 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2009 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002010
Devang Patel163a9f72010-05-10 22:49:55 +00002011 emitDIE(Die);
Devang Patel163a9f72010-05-10 22:49:55 +00002012 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
2013 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002014}
2015
Devang Patel2c4ceb12009-11-21 02:48:08 +00002016/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002017///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002018void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002019 // Check to see if it is worth the effort.
2020 if (!Abbreviations.empty()) {
2021 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002022 Asm->OutStreamer.SwitchSection(
2023 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002024
Chris Lattnerc0215722010-04-04 19:25:43 +00002025 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002026
2027 // For each abbrevation.
2028 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2029 // Get abbreviation data
2030 const DIEAbbrev *Abbrev = Abbreviations[i];
2031
2032 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002033 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00002034
2035 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002036 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00002037 }
2038
2039 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002040 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002041
Chris Lattnerc0215722010-04-04 19:25:43 +00002042 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002043 }
2044}
2045
Devang Patel2c4ceb12009-11-21 02:48:08 +00002046/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002047/// the line matrix.
2048///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002049void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002050 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00002051 Asm->OutStreamer.AddComment("Extended Op");
2052 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002053
Chris Lattner233f52b2010-03-09 23:52:58 +00002054 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002055 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00002056 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2057 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2058
2059 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00002060
Chris Lattnerc0215722010-04-04 19:25:43 +00002061 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00002062 Asm->getTargetData().getPointerSize(),
2063 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002064
2065 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00002066 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2067 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002068 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00002069 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002070}
2071
Devang Patel8a241142009-12-09 18:24:21 +00002072/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2073///
2074void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00002075 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2076 E = CUMap.end(); I != E; ++I) {
2077 CompileUnit *TheCU = I->second;
2078 // Start the dwarf pubnames section.
2079 Asm->OutStreamer.SwitchSection(
2080 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002081
Devang Patel163a9f72010-05-10 22:49:55 +00002082 Asm->OutStreamer.AddComment("Length of Public Names Info");
2083 Asm->EmitLabelDifference(
2084 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
2085 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002086
Devang Patel163a9f72010-05-10 22:49:55 +00002087 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
2088 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002089
Devang Patel163a9f72010-05-10 22:49:55 +00002090 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002091 Asm->EmitInt16(dwarf::DWARF_VERSION);
2092
Devang Patel163a9f72010-05-10 22:49:55 +00002093 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002094 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel163a9f72010-05-10 22:49:55 +00002095 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002096
Devang Patel163a9f72010-05-10 22:49:55 +00002097 Asm->OutStreamer.AddComment("Compilation Unit Length");
2098 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2099 Asm->GetTempSymbol("info_begin", TheCU->getID()),
2100 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002101
Devang Patel163a9f72010-05-10 22:49:55 +00002102 const StringMap<DIE*> &Globals = TheCU->getGlobals();
2103 for (StringMap<DIE*>::const_iterator
2104 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2105 const char *Name = GI->getKeyData();
2106 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002107
Devang Patel163a9f72010-05-10 22:49:55 +00002108 Asm->OutStreamer.AddComment("DIE offset");
2109 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002110
Devang Patel163a9f72010-05-10 22:49:55 +00002111 if (Asm->isVerbose())
2112 Asm->OutStreamer.AddComment("External Name");
2113 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
2114 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002115
Devang Patel163a9f72010-05-10 22:49:55 +00002116 Asm->OutStreamer.AddComment("End Mark");
2117 Asm->EmitInt32(0);
2118 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00002119 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00002120 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002121}
2122
Devang Patel193f7202009-11-24 01:14:22 +00002123void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00002124 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2125 E = CUMap.end(); I != E; ++I) {
2126 CompileUnit *TheCU = I->second;
2127 // Start the dwarf pubnames section.
2128 Asm->OutStreamer.SwitchSection(
2129 Asm->getObjFileLowering().getDwarfPubTypesSection());
2130 Asm->OutStreamer.AddComment("Length of Public Types Info");
2131 Asm->EmitLabelDifference(
2132 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
2133 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002134
Devang Patel163a9f72010-05-10 22:49:55 +00002135 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
2136 TheCU->getID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002137
Devang Patel163a9f72010-05-10 22:49:55 +00002138 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2139 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002140
Devang Patel163a9f72010-05-10 22:49:55 +00002141 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2142 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
2143 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002144
Devang Patel163a9f72010-05-10 22:49:55 +00002145 Asm->OutStreamer.AddComment("Compilation Unit Length");
2146 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2147 Asm->GetTempSymbol("info_begin", TheCU->getID()),
2148 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002149
Devang Patel163a9f72010-05-10 22:49:55 +00002150 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2151 for (StringMap<DIE*>::const_iterator
2152 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2153 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00002154 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002155
Devang Patel163a9f72010-05-10 22:49:55 +00002156 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2157 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002158
Devang Patel163a9f72010-05-10 22:49:55 +00002159 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
2160 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
2161 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002162
Devang Patel163a9f72010-05-10 22:49:55 +00002163 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002164 Asm->EmitInt32(0);
Devang Patel163a9f72010-05-10 22:49:55 +00002165 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
2166 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00002167 }
Devang Patel193f7202009-11-24 01:14:22 +00002168}
2169
Devang Patel2c4ceb12009-11-21 02:48:08 +00002170/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002171///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002172void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002173 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002174 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002175
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002176 // Start the dwarf str section.
2177 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002178 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002179
Chris Lattnerbc733f52010-03-13 02:17:42 +00002180 // Get all of the string pool entries and put them in an array by their ID so
2181 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002182 SmallVector<std::pair<unsigned,
Chris Lattnerbc733f52010-03-13 02:17:42 +00002183 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002184
Chris Lattnerbc733f52010-03-13 02:17:42 +00002185 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2186 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2187 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002188
Chris Lattnerbc733f52010-03-13 02:17:42 +00002189 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002190
Chris Lattnerbc733f52010-03-13 02:17:42 +00002191 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002192 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002193 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002194
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002195 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002196 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002197 }
2198}
2199
Devang Patel2c4ceb12009-11-21 02:48:08 +00002200/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002201///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002202void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002203 if (DotDebugLocEntries.empty())
2204 return;
2205
Devang Patel6c3ea902011-02-04 22:57:18 +00002206 for (SmallVector<DotDebugLocEntry, 4>::iterator
2207 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2208 I != E; ++I) {
2209 DotDebugLocEntry &Entry = *I;
2210 if (I + 1 != DotDebugLocEntries.end())
2211 Entry.Merge(I+1);
2212 }
2213
Daniel Dunbar83320a02011-03-16 22:16:39 +00002214 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002215 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002216 Asm->getObjFileLowering().getDwarfLocSection());
2217 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002218 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2219 unsigned index = 1;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002220 for (SmallVector<DotDebugLocEntry, 4>::iterator
2221 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002222 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002223 DotDebugLocEntry &Entry = *I;
2224 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002225 if (Entry.isEmpty()) {
2226 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2227 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel80250682010-05-26 23:55:23 +00002228 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002229 } else {
2230 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2231 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patelc26f5442011-04-28 02:22:40 +00002232 DIVariable DV(Entry.Variable);
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002233 Asm->OutStreamer.AddComment("Loc expr size");
2234 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2235 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2236 Asm->EmitLabelDifference(end, begin, 2);
2237 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002238 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002239 DIBasicType BTy(DV.getType());
2240 if (BTy.Verify() &&
2241 (BTy.getEncoding() == dwarf::DW_ATE_signed
2242 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2243 Asm->OutStreamer.AddComment("DW_OP_consts");
2244 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002245 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002246 } else {
2247 Asm->OutStreamer.AddComment("DW_OP_constu");
2248 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002249 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002250 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002251 } else if (Entry.isLocation()) {
2252 if (!DV.hasComplexAddress())
2253 // Regular entry.
Devang Patelc26f5442011-04-28 02:22:40 +00002254 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Patel80efd4e2011-07-08 16:49:43 +00002255 else {
2256 // Complex address entry.
2257 unsigned N = DV.getNumAddrElements();
2258 unsigned i = 0;
2259 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2260 if (Entry.Loc.getOffset()) {
2261 i = 2;
2262 Asm->EmitDwarfRegOp(Entry.Loc);
2263 Asm->OutStreamer.AddComment("DW_OP_deref");
2264 Asm->EmitInt8(dwarf::DW_OP_deref);
2265 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2266 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2267 Asm->EmitSLEB128(DV.getAddrElement(1));
2268 } else {
2269 // If first address element is OpPlus then emit
2270 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2271 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2272 Asm->EmitDwarfRegOp(Loc);
2273 i = 2;
2274 }
2275 } else {
2276 Asm->EmitDwarfRegOp(Entry.Loc);
2277 }
2278
2279 // Emit remaining complex address elements.
2280 for (; i < N; ++i) {
2281 uint64_t Element = DV.getAddrElement(i);
2282 if (Element == DIBuilder::OpPlus) {
2283 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2284 Asm->EmitULEB128(DV.getAddrElement(++i));
2285 } else if (Element == DIBuilder::OpDeref)
2286 Asm->EmitInt8(dwarf::DW_OP_deref);
2287 else llvm_unreachable("unknown Opcode found in complex address");
2288 }
Devang Patelc26f5442011-04-28 02:22:40 +00002289 }
Devang Patelc26f5442011-04-28 02:22:40 +00002290 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002291 // else ... ignore constant fp. There is not any good way to
2292 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002293 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002294 }
2295 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002296}
2297
2298/// EmitDebugARanges - Emit visible names into a debug aranges section.
2299///
2300void DwarfDebug::EmitDebugARanges() {
2301 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002302 Asm->OutStreamer.SwitchSection(
2303 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002304}
2305
Devang Patel2c4ceb12009-11-21 02:48:08 +00002306/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002307///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002308void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002309 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002310 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00002311 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00002312 unsigned char Size = Asm->getTargetData().getPointerSize();
2313 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002314 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002315 I != E; ++I) {
2316 if (*I)
2317 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002318 else
Devang Pateleac9c072010-04-27 19:46:33 +00002319 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00002320 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002321}
2322
Devang Patel2c4ceb12009-11-21 02:48:08 +00002323/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002324///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002325void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002326 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002327 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002328 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002329 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002330 }
2331}
2332
Devang Patel2c4ceb12009-11-21 02:48:08 +00002333/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002334/// Section Header:
2335/// 1. length of section
2336/// 2. Dwarf version number
2337/// 3. address size.
2338///
2339/// Entries (one "entry" for each function that was inlined):
2340///
2341/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2342/// otherwise offset into __debug_str for regular function name.
2343/// 2. offset into __debug_str section for regular function name.
2344/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2345/// instances for the function.
2346///
2347/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2348/// inlined instance; the die_offset points to the inlined_subroutine die in the
2349/// __debug_info section, and the low_pc is the starting address for the
2350/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002351void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002352 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002353 return;
2354
Devang Patel163a9f72010-05-10 22:49:55 +00002355 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002356 return;
2357
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002358 Asm->OutStreamer.SwitchSection(
2359 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00002360
Chris Lattner233f52b2010-03-09 23:52:58 +00002361 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00002362 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2363 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00002364
Chris Lattnerc0215722010-04-04 19:25:43 +00002365 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002366
Chris Lattner233f52b2010-03-09 23:52:58 +00002367 Asm->OutStreamer.AddComment("Dwarf Version");
2368 Asm->EmitInt16(dwarf::DWARF_VERSION);
2369 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002370 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002371
Devang Patele9f8f5e2010-05-07 20:54:48 +00002372 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00002373 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002374
Devang Patele9f8f5e2010-05-07 20:54:48 +00002375 const MDNode *Node = *I;
2376 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002377 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002378 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002379 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002380 StringRef LName = SP.getLinkageName();
2381 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002382
Chris Lattner233f52b2010-03-09 23:52:58 +00002383 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002384 if (LName.empty()) {
2385 Asm->OutStreamer.EmitBytes(Name, 0);
2386 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002387 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00002388 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2389 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00002390
Chris Lattner233f52b2010-03-09 23:52:58 +00002391 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00002392 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002393 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00002394
Devang Patel53bb5c92009-11-10 23:06:00 +00002395 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002396 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00002397 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00002398 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002399
Chris Lattner3f53c832010-04-04 18:52:31 +00002400 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002401 Asm->OutStreamer.EmitSymbolValue(LI->first,
2402 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002403 }
2404 }
2405
Chris Lattnerc0215722010-04-04 19:25:43 +00002406 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002407}