blob: 76dc43feae914e71c6544ebeabc98fc9f395a5b7 [file] [log] [blame]
Bill Wendling2f921f82009-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 Lattnerb14490d2010-03-09 00:39:24 +000013
Devang Patel80ae3492009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000016#include "DIE.h"
Devang Patel5eb43192011-04-12 17:40:32 +000017#include "DwarfCompileUnit.h"
Bill Wendling30346342010-04-05 22:59:21 +000018#include "llvm/Constants.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000019#include "llvm/Module.h"
Devang Patel2d9e5322011-01-20 00:02:16 +000020#include "llvm/Instructions.h"
David Greene829b3e82009-08-19 21:52:55 +000021#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000023#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000024#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000025#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000026#include "llvm/MC/MCSymbol.h"
Chris Lattnerf62e3ee2010-01-16 21:57:06 +000027#include "llvm/Target/Mangler.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000028#include "llvm/Target/TargetData.h"
Anton Korobeynikov2f931282011-01-10 12:39:04 +000029#include "llvm/Target/TargetFrameLowering.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner21dc46e2010-04-04 18:06:11 +000031#include "llvm/Target/TargetMachine.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel61880932010-04-19 19:14:02 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000034#include "llvm/Analysis/DebugInfo.h"
Devang Patela5d93242011-02-24 18:49:30 +000035#include "llvm/Analysis/DIBuilder.h"
Devang Patela86114b2010-10-25 20:45:32 +000036#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000037#include "llvm/ADT/STLExtras.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000038#include "llvm/ADT/StringExtras.h"
Devang Patel6c74a872010-04-27 19:46:33 +000039#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000040#include "llvm/Support/Debug.h"
41#include "llvm/Support/ErrorHandling.h"
Devang Patel1973df22010-01-26 21:39:14 +000042#include "llvm/Support/ValueHandle.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000043#include "llvm/Support/FormattedStream.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000044#include "llvm/Support/Timer.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000045#include "llvm/Support/Path.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000046using namespace llvm;
47
Jim Grosbacha8683bb2010-07-21 21:21:52 +000048static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel30265c42010-07-07 20:12:52 +000049 cl::Hidden,
Devang Patel6c74a872010-04-27 19:46:33 +000050 cl::desc("Disable debug info printing"));
51
Dan Gohman7421ae42010-05-07 01:08:53 +000052static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000053 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman7421ae42010-05-07 01:08:53 +000054 cl::init(false));
55
Bill Wendlingfcc14142010-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 Wendling2f921f82009-05-15 09:23:25 +000061//===----------------------------------------------------------------------===//
62
63/// Configuration values for initial hash set sizes (log2).
64///
Bill Wendling2f921f82009-05-15 09:23:25 +000065static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +000066
67namespace llvm {
68
Nick Lewycky019d2552011-07-29 03:49:23 +000069DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-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 Patel6d9f9fe2010-08-09 21:01:39 +0000114 }
115 return Ty;
116 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000117 return Ty;
118}
Bill Wendling2f921f82009-05-15 09:23:25 +0000119
Chris Lattnerf5d06362010-04-05 04:09:20 +0000120} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000121
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000122DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel1a0df9a2010-05-10 22:49:55 +0000123 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000124 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel7e623022011-08-10 20:55:27 +0000125 PrevLabel(NULL) {
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000126 NextStringPoolNumber = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000127
Rafael Espindolaa7160962011-05-06 14:56:22 +0000128 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattnere58b5472010-04-04 23:10:38 +0000129 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000130 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000131 FunctionBeginSym = FunctionEndSym = 0;
Dan Gohman6e681a52010-06-18 15:56:31 +0000132 {
133 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
134 beginModule(M);
Torok Edwinf8dba242010-04-07 10:44:46 +0000135 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000136}
137DwarfDebug::~DwarfDebug() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000138}
139
Chris Lattnerb7aa9522010-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 Lattnera179b522010-04-04 19:25:43 +0000145 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000146}
147
148
Devang Patel930143b2009-11-21 02:48:08 +0000149/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000150///
Devang Patel930143b2009-11-21 02:48:08 +0000151void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-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 Grosbacha8683bb2010-07-21 21:21:52 +0000172/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel43ef34d2010-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 Patel525dda02009-12-14 16:18:45 +0000182/// createSubprogramDIE - Create new DIE using SP.
Devang Patel185051c2010-09-27 23:15:27 +0000183DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000184 CompileUnit *SPCU = getCompileUnit(SP);
185 DIE *SPDie = SPCU->getDIE(SP);
Devang Patel525dda02009-12-14 16:18:45 +0000186 if (SPDie)
187 return SPDie;
188
189 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1d6bbd42011-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 Patel543596d2011-05-03 21:50:34 +0000200 StringRef LinkageName = SP.getLinkageName();
201 if (!LinkageName.empty())
Devang Patel784077e2011-08-10 23:58:09 +0000202 SPCU->addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
203 dwarf::DW_FORM_string,
Devang Patel543596d2011-05-03 21:50:34 +0000204 getRealLinkageName(LinkageName));
205
Devang Patel1d6bbd42011-04-22 23:10:17 +0000206 // If this DIE is going to refer declaration info using AT_specification
207 // then there is no need to add other attributes.
208 if (SP.getFunctionDeclaration().isSubprogram())
209 return SPDie;
210
Devang Patelf200b392010-03-02 17:58:15 +0000211 // Constructors and operators for anonymous aggregates do not have names.
Devang Pateld0fa3042010-03-02 01:26:20 +0000212 if (!SP.getName().empty())
Devang Patelf20c4f72011-04-12 22:53:02 +0000213 SPCU->addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
214 SP.getName());
Bill Wendling2f921f82009-05-15 09:23:25 +0000215
Devang Patelf20c4f72011-04-12 22:53:02 +0000216 SPCU->addSourceLine(SPDie, SP);
Bill Wendling2f921f82009-05-15 09:23:25 +0000217
Devang Patel3a24f922010-10-07 22:03:01 +0000218 if (SP.isPrototyped())
Devang Patelf20c4f72011-04-12 22:53:02 +0000219 SPCU->addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Devang Patel1d6bbd42011-04-22 23:10:17 +0000220
Bill Wendling2f921f82009-05-15 09:23:25 +0000221 // Add Return Type.
Devang Patel236526d2009-12-03 01:25:38 +0000222 DICompositeType SPTy = SP.getType();
223 DIArray Args = SPTy.getTypeArray();
Bill Wendling2f921f82009-05-15 09:23:25 +0000224 unsigned SPTag = SPTy.getTag();
Devang Pateleb57c592009-12-03 19:11:07 +0000225
Devang Patel3b548aa2010-03-08 20:52:55 +0000226 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelf20c4f72011-04-12 22:53:02 +0000227 SPCU->addType(SPDie, SPTy);
Devang Patel236526d2009-12-03 01:25:38 +0000228 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000229 SPCU->addType(SPDie, DIType(Args.getElement(0)));
Devang Patel236526d2009-12-03 01:25:38 +0000230
Devang Pateleb57c592009-12-03 19:11:07 +0000231 unsigned VK = SP.getVirtuality();
232 if (VK) {
Devang Patelf20c4f72011-04-12 22:53:02 +0000233 SPCU->addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
234 DIEBlock *Block = SPCU->getDIEBlock();
235 SPCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
236 SPCU->addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
237 SPCU->addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000238 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000239 SP.getContainingType()));
Devang Pateleb57c592009-12-03 19:11:07 +0000240 }
241
Devang Patel185051c2010-09-27 23:15:27 +0000242 if (!SP.isDefinition()) {
Devang Patelf20c4f72011-04-12 22:53:02 +0000243 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
244
Bill Wendling2f921f82009-05-15 09:23:25 +0000245 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel236526d2009-12-03 01:25:38 +0000246 // be handled while processing variables.
247 DICompositeType SPTy = SP.getType();
248 DIArray Args = SPTy.getTypeArray();
249 unsigned SPTag = SPTy.getTag();
250
Bill Wendling2f921f82009-05-15 09:23:25 +0000251 if (SPTag == dwarf::DW_TAG_subroutine_type)
252 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
253 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000254 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelf20c4f72011-04-12 22:53:02 +0000255 SPCU->addType(Arg, ATy);
Devang Patel6efc8e52010-02-06 01:02:37 +0000256 if (ATy.isArtificial())
Devang Patelf20c4f72011-04-12 22:53:02 +0000257 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel930143b2009-11-21 02:48:08 +0000258 SPDie->addChild(Arg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000259 }
260 }
261
Devang Patel999b4992010-02-03 19:57:19 +0000262 if (SP.isArtificial())
Devang Patelf20c4f72011-04-12 22:53:02 +0000263 SPCU->addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel999b4992010-02-03 19:57:19 +0000264
Devang Patelb4e3b902010-04-30 19:38:23 +0000265 if (!SP.isLocalToUnit())
Devang Patelf20c4f72011-04-12 22:53:02 +0000266 SPCU->addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000267
Devang Patelb4e3b902010-04-30 19:38:23 +0000268 if (SP.isOptimized())
Devang Patelf20c4f72011-04-12 22:53:02 +0000269 SPCU->addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Devang Patelb4e3b902010-04-30 19:38:23 +0000270
Jim Grosbach965a73a2010-07-21 23:03:52 +0000271 if (unsigned isa = Asm->getISAEncoding()) {
Devang Patelf20c4f72011-04-12 22:53:02 +0000272 SPCU->addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
Jim Grosbach965a73a2010-07-21 23:03:52 +0000273 }
274
Bill Wendling2f921f82009-05-15 09:23:25 +0000275 return SPDie;
276}
277
Devang Patel019922d2010-04-06 23:53:48 +0000278/// isSubprogramContext - Return true if Context is either a subprogram
279/// or another context nested inside a subprogram.
Devang Patel32cc43c2010-05-07 20:54:48 +0000280static bool isSubprogramContext(const MDNode *Context) {
Devang Patel019922d2010-04-06 23:53:48 +0000281 if (!Context)
282 return false;
283 DIDescriptor D(Context);
284 if (D.isSubprogram())
285 return true;
286 if (D.isType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000287 return isSubprogramContext(DIType(Context).getContext());
Devang Patel019922d2010-04-06 23:53:48 +0000288 return false;
289}
290
Jim Grosbach042483e2009-11-21 23:12:12 +0000291/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +0000292/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
293/// If there are global variables in this scope then create and insert
294/// DIEs for these variables.
Devang Patel32cc43c2010-05-07 20:54:48 +0000295DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000296 CompileUnit *SPCU = getCompileUnit(SPNode);
297 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +0000298
Chris Lattner3a383cb2010-04-05 00:13:49 +0000299 assert(SPDie && "Unable to find subprogram DIE!");
300 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000301
Devang Patel1d6bbd42011-04-22 23:10:17 +0000302 DISubprogram SPDecl = SP.getFunctionDeclaration();
303 if (SPDecl.isSubprogram())
304 // Refer function declaration directly.
Devang Patelf20c4f72011-04-12 22:53:02 +0000305 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Devang Patel1d6bbd42011-04-22 23:10:17 +0000306 createSubprogramDIE(SPDecl));
307 else {
308 // There is not any need to generate specification DIE for a function
309 // defined at compile unit level. If a function is defined inside another
310 // function then gdb prefers the definition at top level and but does not
311 // expect specification DIE in parent function. So avoid creating
312 // specification DIE for a function defined inside a function.
313 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
314 !SP.getContext().isFile() &&
315 !isSubprogramContext(SP.getContext())) {
316 SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
317
318 // Add arguments.
319 DICompositeType SPTy = SP.getType();
320 DIArray Args = SPTy.getTypeArray();
321 unsigned SPTag = SPTy.getTag();
322 if (SPTag == dwarf::DW_TAG_subroutine_type)
323 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
324 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
325 DIType ATy = DIType(DIType(Args.getElement(i)));
326 SPCU->addType(Arg, ATy);
327 if (ATy.isArtificial())
328 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
329 SPDie->addChild(Arg);
330 }
331 DIE *SPDeclDie = SPDie;
332 SPDie = new DIE(dwarf::DW_TAG_subprogram);
333 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
334 SPDeclDie);
335 SPCU->addDie(SPDie);
336 }
Chris Lattner3a383cb2010-04-05 00:13:49 +0000337 }
Devang Patela37a95e2010-07-07 22:20:57 +0000338 // Pick up abstract subprogram DIE.
339 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
340 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf20c4f72011-04-12 22:53:02 +0000341 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
342 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patela37a95e2010-07-07 22:20:57 +0000343 SPCU->addDie(SPDie);
344 }
345
Devang Patelf20c4f72011-04-12 22:53:02 +0000346 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
347 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
348 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
349 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattner3a383cb2010-04-05 00:13:49 +0000350 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
351 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000352 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000353
Chris Lattner3a383cb2010-04-05 00:13:49 +0000354 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000355}
356
Jim Grosbach042483e2009-11-21 23:12:12 +0000357/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +0000358/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Patel7e623022011-08-10 20:55:27 +0000359DIE *DwarfDebug::constructLexicalScopeDIE(LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000360
361 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
362 if (Scope->isAbstractScope())
363 return ScopeDIE;
364
Devang Patel7e623022011-08-10 20:55:27 +0000365 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +0000366 if (Ranges.empty())
367 return 0;
368
Devang Patelf20c4f72011-04-12 22:53:02 +0000369 CompileUnit *TheCU = getCompileUnit(Scope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +0000370 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel6c74a872010-04-27 19:46:33 +0000371 if (Ranges.size() > 1) {
372 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000373 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000374 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000375 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000376 DebugRangeSymbols.size()
377 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000378 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +0000379 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000380 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
381 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000382 }
383 DebugRangeSymbols.push_back(NULL);
384 DebugRangeSymbols.push_back(NULL);
385 return ScopeDIE;
386 }
387
Devang Patel9fc11702010-05-25 23:40:22 +0000388 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
389 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000390
Devang Patel9fc11702010-05-25 23:40:22 +0000391 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000392
Chris Lattnere13c3722010-03-09 01:58:53 +0000393 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
394 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000395
Devang Patelf20c4f72011-04-12 22:53:02 +0000396 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
397 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000398
399 return ScopeDIE;
400}
401
Devang Patel930143b2009-11-21 02:48:08 +0000402/// constructInlinedScopeDIE - This scope represents inlined body of
403/// a function. Construct DIE to represent this concrete inlined copy
404/// of the function.
Devang Patel7e623022011-08-10 20:55:27 +0000405DIE *DwarfDebug::constructInlinedScopeDIE(LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000406
Devang Patel7e623022011-08-10 20:55:27 +0000407 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000408 assert (Ranges.empty() == false
Devang Patel7e623022011-08-10 20:55:27 +0000409 && "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000410
Devang Patelf098ce22011-07-27 00:34:13 +0000411 if (!Scope->getScopeNode())
412 return NULL;
413 DIScope DS(Scope->getScopeNode());
414 DISubprogram InlinedSP = getDISubprogram(DS);
415 CompileUnit *TheCU = getCompileUnit(InlinedSP);
416 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
417 if (!OriginDIE) {
418 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
419 return NULL;
420 }
421
Devang Patel7e623022011-08-10 20:55:27 +0000422 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +0000423 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
424 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000425
Devang Patel4c6bd662010-07-08 22:39:20 +0000426 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky019d2552011-07-29 03:49:23 +0000427 assert (0 && "Unexpected Start and End labels for a inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000428 return 0;
429 }
Chris Lattnere13c3722010-03-09 01:58:53 +0000430 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000431 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +0000432 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000433 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000434
Devang Patel73bc1722011-05-05 17:54:26 +0000435 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patelf20c4f72011-04-12 22:53:02 +0000436 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
437 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000438
Devang Patelf098ce22011-07-27 00:34:13 +0000439 if (Ranges.size() > 1) {
440 // .debug_range section has not been laid out yet. Emit offset in
441 // .debug_range as a uint, size 4, for now. emitDIE will handle
442 // DW_AT_ranges appropriately.
443 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000444 DebugRangeSymbols.size()
445 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000446 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patelf098ce22011-07-27 00:34:13 +0000447 RE = Ranges.end(); RI != RE; ++RI) {
448 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
449 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
450 }
451 DebugRangeSymbols.push_back(NULL);
452 DebugRangeSymbols.push_back(NULL);
453 } else {
Devang Patel784077e2011-08-10 23:58:09 +0000454 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
455 StartLabel);
456 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
457 EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000458 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000459
460 InlinedSubprogramDIEs.insert(OriginDIE);
461
462 // Track the start label for this inlined function.
Devang Patelf098ce22011-07-27 00:34:13 +0000463 //.debug_inlined section specification does not clearly state how
464 // to emit inlined scope that is split into multiple instruction ranges.
465 // For now, use first instruction range and emit low_pc/high_pc pair and
466 // corresponding .debug_inlined section entry for this pair.
Devang Patel32cc43c2010-05-07 20:54:48 +0000467 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000468 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000469
470 if (I == InlineInfo.end()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000471 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach00e9c612009-11-22 19:20:36 +0000472 ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000473 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000474 } else
Chris Lattner085b6522010-03-09 00:31:02 +0000475 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000476
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000477 DILocation DL(Scope->getInlinedAt());
Devang Patelf20c4f72011-04-12 22:53:02 +0000478 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
479 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000480
481 return ScopeDIE;
482}
483
Devang Patelf071d722011-06-24 20:46:11 +0000484/// isUnsignedDIType - Return true if type encoding is unsigned.
485static bool isUnsignedDIType(DIType Ty) {
486 DIDerivedType DTy(Ty);
487 if (DTy.Verify())
488 return isUnsignedDIType(DTy.getTypeDerivedFrom());
489
490 DIBasicType BTy(Ty);
491 if (BTy.Verify()) {
492 unsigned Encoding = BTy.getEncoding();
493 if (Encoding == dwarf::DW_ATE_unsigned ||
494 Encoding == dwarf::DW_ATE_unsigned_char)
495 return true;
496 }
497 return false;
498}
Devang Patel930143b2009-11-21 02:48:08 +0000499
500/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel7e623022011-08-10 20:55:27 +0000501DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, LexicalScope *Scope) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000502 StringRef Name = DV->getName();
Devang Patel2d9caf92009-11-25 17:36:49 +0000503 if (Name.empty())
Devang Patel97f99fa2009-11-13 02:25:26 +0000504 return NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000505
506 // Translate tag to proper Dwarf tag. The result variable is dropped for
507 // now.
508 unsigned Tag;
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000509 switch (DV->getTag()) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000510 case dwarf::DW_TAG_return_variable:
511 return NULL;
512 case dwarf::DW_TAG_arg_variable:
513 Tag = dwarf::DW_TAG_formal_parameter;
514 break;
515 case dwarf::DW_TAG_auto_variable: // fall thru
516 default:
517 Tag = dwarf::DW_TAG_variable;
518 break;
519 }
520
521 // Define variable debug information entry.
522 DIE *VariableDie = new DIE(Tag);
Devang Patel2606f4d2011-04-25 23:05:21 +0000523 CompileUnit *VariableCU = getCompileUnit(DV->getVariable());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000524 DIE *AbsDIE = NULL;
Devang Patele1c53f22010-05-20 16:36:41 +0000525 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
526 V2AVI = VarToAbstractVarMap.find(DV);
527 if (V2AVI != VarToAbstractVarMap.end())
528 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach042483e2009-11-21 23:12:12 +0000529
Devang Patele1c53f22010-05-20 16:36:41 +0000530 if (AbsDIE)
Devang Patel2606f4d2011-04-25 23:05:21 +0000531 VariableCU->addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patelf20c4f72011-04-12 22:53:02 +0000532 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000533 else {
Devang Patel2606f4d2011-04-25 23:05:21 +0000534 VariableCU->addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
535 Name);
536 VariableCU->addSourceLine(VariableDie, DV->getVariable());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000537
538 // Add variable type.
Devang Patel2606f4d2011-04-25 23:05:21 +0000539 VariableCU->addType(VariableDie, DV->getType());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000540 }
541
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000542 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patel2606f4d2011-04-25 23:05:21 +0000543 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
544 dwarf::DW_FORM_flag, 1);
Devang Patelbea08d12010-09-29 23:07:21 +0000545 else if (DIVariable(DV->getVariable()).isArtificial())
Devang Patel2606f4d2011-04-25 23:05:21 +0000546 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
547 dwarf::DW_FORM_flag, 1);
Devang Patel9fc11702010-05-25 23:40:22 +0000548
549 if (Scope->isAbstractScope()) {
550 DV->setDIE(VariableDie);
551 return VariableDie;
552 }
553
554 // Add variable address.
555
556 unsigned Offset = DV->getDotDebugLocOffset();
557 if (Offset != ~0U) {
Devang Patel784077e2011-08-10 23:58:09 +0000558 VariableCU->addLabel(VariableDie, dwarf::DW_AT_location,
559 dwarf::DW_FORM_data4,
560 Asm->GetTempSymbol("debug_loc", Offset));
Devang Patel9fc11702010-05-25 23:40:22 +0000561 DV->setDIE(VariableDie);
562 UseDotDebugLocEntry.insert(VariableDie);
563 return VariableDie;
564 }
565
566 // Check if variable is described by a DBG_VALUE instruction.
567 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
568 DbgVariableToDbgInstMap.find(DV);
569 if (DVI != DbgVariableToDbgInstMap.end()) {
570 const MachineInstr *DVInsn = DVI->second;
Devang Patel9fc11702010-05-25 23:40:22 +0000571 bool updated = false;
Devang Patel9fc11702010-05-25 23:40:22 +0000572 if (DVInsn->getNumOperands() == 3) {
Devang Patel86ec8b32010-08-31 22:22:42 +0000573 if (DVInsn->getOperand(0).isReg()) {
574 const MachineOperand RegOp = DVInsn->getOperand(0);
575 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
576 if (DVInsn->getOperand(1).isImm() &&
577 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
Devang Patel77dc5412011-04-27 22:45:24 +0000578 unsigned FrameReg = 0;
579 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
580 int Offset =
581 TFI->getFrameIndexReference(*Asm->MF,
582 DVInsn->getOperand(1).getImm(),
583 FrameReg);
584 MachineLocation Location(FrameReg, Offset);
585 VariableCU->addVariableAddress(DV, VariableDie, Location);
586
587 } else if (RegOp.getReg())
588 VariableCU->addVariableAddress(DV, VariableDie,
589 MachineLocation(RegOp.getReg()));
590 updated = true;
Devang Patel86ec8b32010-08-31 22:22:42 +0000591 }
Devang Patel9fc11702010-05-25 23:40:22 +0000592 else if (DVInsn->getOperand(0).isImm())
Devang Patel3c6aed22011-05-27 16:45:18 +0000593 updated =
594 VariableCU->addConstantValue(VariableDie, DVInsn->getOperand(0),
595 DV->getType());
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000596 else if (DVInsn->getOperand(0).isFPImm())
597 updated =
Devang Patel2606f4d2011-04-25 23:05:21 +0000598 VariableCU->addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patelf071d722011-06-24 20:46:11 +0000599 else if (DVInsn->getOperand(0).isCImm())
600 updated =
601 VariableCU->addConstantValue(VariableDie,
602 DVInsn->getOperand(0).getCImm(),
603 isUnsignedDIType(DV->getType()));
Devang Patel9fc11702010-05-25 23:40:22 +0000604 } else {
Devang Patel77dc5412011-04-27 22:45:24 +0000605 VariableCU->addVariableAddress(DV, VariableDie,
606 Asm->getDebugValueLocation(DVInsn));
607 updated = true;
Devang Patel9fc11702010-05-25 23:40:22 +0000608 }
609 if (!updated) {
610 // If variableDie is not updated then DBG_VALUE instruction does not
611 // have valid variable info.
612 delete VariableDie;
613 return NULL;
614 }
615 DV->setDIE(VariableDie);
616 return VariableDie;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000617 }
Devang Patel9fc11702010-05-25 23:40:22 +0000618
619 // .. else use frame index, if available.
Devang Patel9fc11702010-05-25 23:40:22 +0000620 int FI = 0;
Devang Patel77dc5412011-04-27 22:45:24 +0000621 if (findVariableFrameIndex(DV, &FI)) {
622 unsigned FrameReg = 0;
623 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
624 int Offset =
625 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
626 MachineLocation Location(FrameReg, Offset);
627 VariableCU->addVariableAddress(DV, VariableDie, Location);
628 }
629
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000630 DV->setDIE(VariableDie);
631 return VariableDie;
632
633}
Devang Patel930143b2009-11-21 02:48:08 +0000634
635/// constructScopeDIE - Construct a DIE for this scope.
Devang Patel7e623022011-08-10 20:55:27 +0000636DIE *DwarfDebug::constructScopeDIE(LexicalScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000637 if (!Scope || !Scope->getScopeNode())
638 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000639
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000640 SmallVector <DIE *, 8> Children;
Devang Patel6c622ef2011-03-01 22:58:55 +0000641
642 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000643 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000644 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
645 if (DbgVariable *ArgDV = CurrentFnArguments[i])
646 if (DIE *Arg = constructVariableDIE(ArgDV, Scope))
647 Children.push_back(Arg);
648
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000649 // Collect lexical scope childrens first.
Devang Patel7e623022011-08-10 20:55:27 +0000650 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000651 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
652 if (DIE *Variable = constructVariableDIE(Variables[i], Scope))
653 Children.push_back(Variable);
Devang Patel7e623022011-08-10 20:55:27 +0000654 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000655 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
656 if (DIE *Nested = constructScopeDIE(Scopes[j]))
657 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000658 DIScope DS(Scope->getScopeNode());
659 DIE *ScopeDIE = NULL;
660 if (Scope->getInlinedAt())
661 ScopeDIE = constructInlinedScopeDIE(Scope);
662 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000663 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000664 if (Scope->isAbstractScope()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000665 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000666 // Note down abstract DIE.
667 if (ScopeDIE)
668 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
669 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000670 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000671 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000672 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000673 else {
674 // There is no need to emit empty lexical block DIE.
675 if (Children.empty())
676 return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +0000677 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000678 }
679
Devang Patel23b2ae62010-03-29 22:59:58 +0000680 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000681
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000682 // Add children
683 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
684 E = Children.end(); I != E; ++I)
685 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000686
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000687 if (DS.isSubprogram())
Devang Patelf20c4f72011-04-12 22:53:02 +0000688 getCompileUnit(DS)->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000689
Devang Patel04d2f2d2009-11-24 01:14:22 +0000690 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000691}
692
Bill Wendling2b128d72009-05-20 23:19:06 +0000693/// GetOrCreateSourceID - Look up the source id with the given directory and
694/// source file names. If none currently exists, create a new id and insert it
695/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
696/// maps as well.
Devang Patel498877d2010-07-24 00:53:22 +0000697
Devang Patele01b75c2011-03-24 20:30:50 +0000698unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
699 StringRef DirName) {
Devang Patel871d0b12010-09-16 20:57:49 +0000700 // If FE did not provide a file name, then assume stdin.
701 if (FileName.empty())
Devang Patele01b75c2011-03-24 20:30:50 +0000702 return GetOrCreateSourceID("<stdin>", StringRef());
703
704 // MCStream expects full path name as filename.
Benjamin Kramer440c3b72011-06-05 14:36:47 +0000705 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
706 SmallString<128> FullPathName = DirName;
707 sys::path::append(FullPathName, FileName);
Devang Patele01b75c2011-03-24 20:30:50 +0000708 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
709 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
710 }
Devang Patel871d0b12010-09-16 20:57:49 +0000711
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000712 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
713 if (Entry.getValue())
714 return Entry.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000715
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000716 unsigned SrcId = SourceIdMap.size();
717 Entry.setValue(SrcId);
Bill Wendling2b128d72009-05-20 23:19:06 +0000718
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000719 // Print out a .file directive to specify files for .loc directives.
Devang Patele01b75c2011-03-24 20:30:50 +0000720 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendling2b128d72009-05-20 23:19:06 +0000721
722 return SrcId;
723}
724
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000725/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel1a0df9a2010-05-10 22:49:55 +0000726/// metadata node with tag DW_TAG_compile_unit.
Devang Patel32cc43c2010-05-07 20:54:48 +0000727void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000728 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +0000729 StringRef FN = DIUnit.getFilename();
730 StringRef Dir = DIUnit.getDirectory();
Devang Patele01b75c2011-03-24 20:30:50 +0000731 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000732
733 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelf20c4f72011-04-12 22:53:02 +0000734 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
735 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
736 DIUnit.getProducer());
737 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
738 DIUnit.getLanguage());
739 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patelbd798ce2010-04-26 22:54:28 +0000740 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
741 // simplifies debug range entries.
Devang Patelf20c4f72011-04-12 22:53:02 +0000742 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +0000743 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +0000744 // compile unit in debug_line section.
Rafael Espindola2fe0ee12011-05-10 20:35:05 +0000745 if(Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindolaa7558912011-05-04 17:44:06 +0000746 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patelf20c4f72011-04-12 22:53:02 +0000747 Asm->GetTempSymbol("section_line"));
Devang Patelea636392010-08-31 23:50:19 +0000748 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000749 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +0000750
Devang Patel2d9caf92009-11-25 17:36:49 +0000751 if (!Dir.empty())
Devang Patelf20c4f72011-04-12 22:53:02 +0000752 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000753 if (DIUnit.isOptimized())
Devang Patelf20c4f72011-04-12 22:53:02 +0000754 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +0000755
Devang Patel2d9caf92009-11-25 17:36:49 +0000756 StringRef Flags = DIUnit.getFlags();
757 if (!Flags.empty())
Devang Patel784077e2011-08-10 23:58:09 +0000758 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string,
759 Flags);
Devang Patelf20c4f72011-04-12 22:53:02 +0000760
Bill Wendling2b128d72009-05-20 23:19:06 +0000761 unsigned RVer = DIUnit.getRunTimeVersion();
762 if (RVer)
Devang Patelf20c4f72011-04-12 22:53:02 +0000763 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000764 dwarf::DW_FORM_data1, RVer);
765
Devang Patel1a0df9a2010-05-10 22:49:55 +0000766 if (!FirstCU)
767 FirstCU = NewCU;
768 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendling2b128d72009-05-20 23:19:06 +0000769}
770
Nick Lewycky019d2552011-07-29 03:49:23 +0000771/// getCompileUnit - Get CompileUnit DIE.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000772CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
773 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
774 DIDescriptor D(N);
775 const MDNode *CUNode = NULL;
776 if (D.isCompileUnit())
777 CUNode = N;
778 else if (D.isSubprogram())
779 CUNode = DISubprogram(N).getCompileUnit();
780 else if (D.isType())
781 CUNode = DIType(N).getCompileUnit();
782 else if (D.isGlobalVariable())
783 CUNode = DIGlobalVariable(N).getCompileUnit();
784 else if (D.isVariable())
785 CUNode = DIVariable(N).getCompileUnit();
786 else if (D.isNameSpace())
787 CUNode = DINameSpace(N).getCompileUnit();
788 else if (D.isFile())
789 CUNode = DIFile(N).getCompileUnit();
790 else
791 return FirstCU;
792
793 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
794 = CUMap.find(CUNode);
795 if (I == CUMap.end())
796 return FirstCU;
797 return I->second;
798}
799
Nick Lewycky019d2552011-07-29 03:49:23 +0000800// Return const expression if value is a GEP to access merged global
Devang Patel2d9e5322011-01-20 00:02:16 +0000801// constant. e.g.
802// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
803static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
804 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
805 if (!CE || CE->getNumOperands() != 3 ||
Bob Wilson0a8d5c62011-08-03 19:42:51 +0000806 CE->getOpcode() != Instruction::GetElementPtr)
Devang Patel2d9e5322011-01-20 00:02:16 +0000807 return NULL;
808
Bob Wilson0a8d5c62011-08-03 19:42:51 +0000809 // First operand points to a global struct.
810 Value *Ptr = CE->getOperand(0);
811 if (!isa<GlobalValue>(Ptr) ||
812 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
Devang Patel2d9e5322011-01-20 00:02:16 +0000813 return NULL;
814
815 // Second operand is zero.
Nick Lewycky019d2552011-07-29 03:49:23 +0000816 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
Devang Patel2d9e5322011-01-20 00:02:16 +0000817 if (!CI || !CI->isZero())
818 return NULL;
819
820 // Third operand is offset.
821 if (!isa<ConstantInt>(CE->getOperand(2)))
822 return NULL;
823
824 return CE;
825}
826
Devang Patel1a0df9a2010-05-10 22:49:55 +0000827/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +0000828void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel469c12d22010-08-10 01:37:23 +0000829 DIGlobalVariable GV(N);
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000830
Devang Patelf5d53602009-09-04 23:59:07 +0000831 // If debug information is malformed then ignore it.
Devang Patel469c12d22010-08-10 01:37:23 +0000832 if (GV.Verify() == false)
Devang Patelf5d53602009-09-04 23:59:07 +0000833 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000834
835 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000836 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel469c12d22010-08-10 01:37:23 +0000837 if (TheCU->getDIE(GV))
Devang Patel0751a282009-06-26 01:49:18 +0000838 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000839
Devang Patel469c12d22010-08-10 01:37:23 +0000840 DIType GTy = GV.getType();
Devang Patelb2197462010-08-10 07:11:13 +0000841 DIE *VariableDIE = new DIE(GV.getTag());
842
843 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +0000844
Devang Patel469c12d22010-08-10 01:37:23 +0000845 // Add name.
Devang Patelf20c4f72011-04-12 22:53:02 +0000846 TheCU->addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
847 GV.getDisplayName());
Devang Patel469c12d22010-08-10 01:37:23 +0000848 StringRef LinkageName = GV.getLinkageName();
Devang Patelb2197462010-08-10 07:11:13 +0000849 if (!LinkageName.empty() && isGlobalVariable)
Devang Patelf20c4f72011-04-12 22:53:02 +0000850 TheCU->addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
851 dwarf::DW_FORM_string,
852 getRealLinkageName(LinkageName));
Devang Patel469c12d22010-08-10 01:37:23 +0000853 // Add type.
Devang Patelf20c4f72011-04-12 22:53:02 +0000854 TheCU->addType(VariableDIE, GTy);
Devang Patele9853f22011-05-31 22:56:51 +0000855
Devang Patel469c12d22010-08-10 01:37:23 +0000856 // Add scoping info.
857 if (!GV.isLocalToUnit()) {
Devang Patelf20c4f72011-04-12 22:53:02 +0000858 TheCU->addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel469c12d22010-08-10 01:37:23 +0000859 // Expose as global.
860 TheCU->addGlobal(GV.getName(), VariableDIE);
861 }
862 // Add line number info.
Devang Patelf20c4f72011-04-12 22:53:02 +0000863 TheCU->addSourceLine(VariableDIE, GV);
Devang Patel469c12d22010-08-10 01:37:23 +0000864 // Add to map.
865 TheCU->insertDIE(N, VariableDIE);
866 // Add to context owner.
867 DIDescriptor GVContext = GV.getContext();
Devang Patelf20c4f72011-04-12 22:53:02 +0000868 TheCU->addToContextOwner(VariableDIE, GVContext);
Devang Patel469c12d22010-08-10 01:37:23 +0000869 // Add location.
Devang Patelb2197462010-08-10 07:11:13 +0000870 if (isGlobalVariable) {
871 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelf20c4f72011-04-12 22:53:02 +0000872 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
873 TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patelb2197462010-08-10 07:11:13 +0000874 Asm->Mang->getSymbol(GV.getGlobal()));
875 // Do not create specification DIE if context is either compile unit
876 // or a subprogram.
877 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
878 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
879 // Create specification DIE.
880 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patelf20c4f72011-04-12 22:53:02 +0000881 TheCU->addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
Devang Patelb2197462010-08-10 07:11:13 +0000882 dwarf::DW_FORM_ref4, VariableDIE);
Devang Patelf20c4f72011-04-12 22:53:02 +0000883 TheCU->addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel784077e2011-08-10 23:58:09 +0000884 TheCU->addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag,
885 1);
Devang Patelb2197462010-08-10 07:11:13 +0000886 TheCU->addDie(VariableSpecDIE);
887 } else {
Devang Patelf20c4f72011-04-12 22:53:02 +0000888 TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Devang Patelb2197462010-08-10 07:11:13 +0000889 }
Devang Patelf071d722011-06-24 20:46:11 +0000890 } else if (const ConstantInt *CI =
Devang Patel70eb9822011-01-06 21:39:25 +0000891 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
Devang Patelf20c4f72011-04-12 22:53:02 +0000892 TheCU->addConstantValue(VariableDIE, CI, isUnsignedDIType(GTy));
Devang Patel2d9e5322011-01-20 00:02:16 +0000893 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
894 // GV is a merged global.
895 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bob Wilson0a8d5c62011-08-03 19:42:51 +0000896 Value *Ptr = CE->getOperand(0);
Devang Patelf20c4f72011-04-12 22:53:02 +0000897 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
898 TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
Bob Wilson0a8d5c62011-08-03 19:42:51 +0000899 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
Devang Patelf20c4f72011-04-12 22:53:02 +0000900 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Bob Wilson0a8d5c62011-08-03 19:42:51 +0000901 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Devang Pateldc9cbaa2011-08-03 01:25:46 +0000902 TheCU->addUInt(Block, 0, dwarf::DW_FORM_udata,
Bob Wilson0a8d5c62011-08-03 19:42:51 +0000903 Asm->getTargetData().getIndexedOffset(Ptr->getType(), Idx));
Devang Patelf20c4f72011-04-12 22:53:02 +0000904 TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
905 TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel2d9e5322011-01-20 00:02:16 +0000906 }
Devang Patel70eb9822011-01-06 21:39:25 +0000907
Devang Patel0751a282009-06-26 01:49:18 +0000908 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000909}
910
Devang Patel1a0df9a2010-05-10 22:49:55 +0000911/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +0000912void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000913 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000914
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000915 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000916 CompileUnit *TheCU = getCompileUnit(N);
917 if (TheCU->getDIE(N))
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000918 return;
919
Bill Wendling2b128d72009-05-20 23:19:06 +0000920 if (!SP.isDefinition())
921 // This is a method declaration which will be handled while constructing
922 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000923 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000924
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000925 DIE *SubprogramDie = createSubprogramDIE(SP);
926
927 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000928 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000929
930 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +0000931 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +0000932
Bill Wendling2b128d72009-05-20 23:19:06 +0000933 // Expose as global.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000934 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000935
Devang Patel0751a282009-06-26 01:49:18 +0000936 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000937}
938
Devang Patel930143b2009-11-21 02:48:08 +0000939/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000940/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky019d2552011-07-29 03:49:23 +0000941/// This is invoked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +0000942void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +0000943 if (DisableDebugInfoPrinting)
944 return;
945
Nick Lewycky019d2552011-07-29 03:49:23 +0000946 // If module has named metadata anchors then use them, otherwise scan the
947 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000948 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
949 if (CU_Nodes) {
Devang Patelc981f622011-05-04 16:34:02 +0000950
951 NamedMDNode *GV_Nodes = M->getNamedMetadata("llvm.dbg.gv");
952 NamedMDNode *SP_Nodes = M->getNamedMetadata("llvm.dbg.sp");
953 if (!GV_Nodes && !SP_Nodes)
954 // If there are not any global variables or any functions then
955 // there is not any debug info in this module.
Devang Patele02e5852011-05-03 16:45:22 +0000956 return;
957
Devang Patelc981f622011-05-04 16:34:02 +0000958 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
959 constructCompileUnit(CU_Nodes->getOperand(i));
Devang Patele02e5852011-05-03 16:45:22 +0000960
Devang Patelc981f622011-05-04 16:34:02 +0000961 if (GV_Nodes)
962 for (unsigned i = 0, e = GV_Nodes->getNumOperands(); i != e; ++i)
963 constructGlobalVariableDIE(GV_Nodes->getOperand(i));
964
965 if (SP_Nodes)
966 for (unsigned i = 0, e = SP_Nodes->getNumOperands(); i != e; ++i)
967 constructSubprogramDIE(SP_Nodes->getOperand(i));
Devang Patele02e5852011-05-03 16:45:22 +0000968
969 } else {
970
971 DebugInfoFinder DbgFinder;
972 DbgFinder.processModule(*M);
973
Devang Patelc981f622011-05-04 16:34:02 +0000974 bool HasDebugInfo = false;
Nick Lewycky019d2552011-07-29 03:49:23 +0000975 // Scan all the compile-units to see if there are any marked as the main
976 // unit. If not, we do not generate debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000977 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
978 E = DbgFinder.compile_unit_end(); I != E; ++I) {
979 if (DICompileUnit(*I).isMain()) {
980 HasDebugInfo = true;
981 break;
982 }
983 }
Devang Patelc981f622011-05-04 16:34:02 +0000984 if (!HasDebugInfo) return;
Devang Patele02e5852011-05-03 16:45:22 +0000985
986 // Create all the compile unit DIEs.
987 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
988 E = DbgFinder.compile_unit_end(); I != E; ++I)
989 constructCompileUnit(*I);
990
991 // Create DIEs for each global variable.
992 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
993 E = DbgFinder.global_variable_end(); I != E; ++I)
994 constructGlobalVariableDIE(*I);
995
996 // Create DIEs for each subprogram.
997 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
998 E = DbgFinder.subprogram_end(); I != E; ++I)
999 constructSubprogramDIE(*I);
Chris Lattner7cfa70e2010-04-05 02:19:28 +00001000 }
Devang Patele02e5852011-05-03 16:45:22 +00001001
Chris Lattner7cfa70e2010-04-05 02:19:28 +00001002 // Tell MMI that we have debug info.
1003 MMI->setDebugInfoAvailability(true);
Devang Patele02e5852011-05-03 16:45:22 +00001004
Chris Lattnerd442aa32010-04-04 23:17:54 +00001005 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +00001006 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001007
Devang Patel8e06a5e2010-08-10 20:01:20 +00001008 //getOrCreateTypeDIE
1009 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
Devang Patelf20c4f72011-04-12 22:53:02 +00001010 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1011 DIType Ty(NMD->getOperand(i));
1012 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
1013 }
Devang Patel8e06a5e2010-08-10 20:01:20 +00001014
Devang Patel7a554812010-09-28 18:08:20 +00001015 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
Devang Patelf20c4f72011-04-12 22:53:02 +00001016 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1017 DIType Ty(NMD->getOperand(i));
1018 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
1019 }
Devang Patel7a554812010-09-28 18:08:20 +00001020
Bill Wendling2b128d72009-05-20 23:19:06 +00001021 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +00001022 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +00001023}
1024
Devang Patel930143b2009-11-21 02:48:08 +00001025/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +00001026///
Devang Patel930143b2009-11-21 02:48:08 +00001027void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001028 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +00001029 const Module *M = MMI->getModule();
Devang Patel7e623022011-08-10 20:55:27 +00001030 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +00001031 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
1032 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
1033 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
1034 DISubprogram SP(AllSPs->getOperand(SI));
1035 if (!SP.Verify()) continue;
1036
1037 // Collect info for variables that were optimized out.
Devang Patel18efced2010-07-19 17:53:55 +00001038 if (!SP.isDefinition()) continue;
Devang Patelf3b2db62010-06-28 18:25:03 +00001039 StringRef FName = SP.getLinkageName();
1040 if (FName.empty())
1041 FName = SP.getName();
Devang Patel364bf042010-11-10 22:19:21 +00001042 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patelf3b2db62010-06-28 18:25:03 +00001043 if (!NMD) continue;
1044 unsigned E = NMD->getNumOperands();
1045 if (!E) continue;
Devang Patel784077e2011-08-10 23:58:09 +00001046 LexicalScope *Scope = new LexicalScope(NULL, DIDescriptor(SP), NULL,
1047 false);
Devang Pateld0701282010-08-02 17:32:15 +00001048 DeadFnScopeMap[SP] = Scope;
Benjamin Kramerfa7e6a52011-08-11 18:39:28 +00001049 SmallVector<DbgVariable, 8> Variables;
Devang Patelf3b2db62010-06-28 18:25:03 +00001050 for (unsigned I = 0; I != E; ++I) {
1051 DIVariable DV(NMD->getOperand(I));
1052 if (!DV.Verify()) continue;
Benjamin Kramerfa7e6a52011-08-11 18:39:28 +00001053 Variables.push_back(DbgVariable(DV));
Devang Patelf3b2db62010-06-28 18:25:03 +00001054 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001055
Devang Patelf3b2db62010-06-28 18:25:03 +00001056 // Construct subprogram DIE and add variables DIEs.
1057 constructSubprogramDIE(SP);
1058 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patelf3b2db62010-06-28 18:25:03 +00001059 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Benjamin Kramerfa7e6a52011-08-11 18:39:28 +00001060 if (DIE *VariableDIE = constructVariableDIE(&Variables[i], Scope))
Devang Patelf3b2db62010-06-28 18:25:03 +00001061 ScopeDIE->addChild(VariableDIE);
1062 }
1063 }
1064 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001065
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001066 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1067 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1068 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1069 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +00001070 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001071 }
1072
Devang Patel32cc43c2010-05-07 20:54:48 +00001073 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Pateleb57c592009-12-03 19:11:07 +00001074 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1075 DIE *SPDie = CI->first;
Devang Patel32cc43c2010-05-07 20:54:48 +00001076 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Pateleb57c592009-12-03 19:11:07 +00001077 if (!N) continue;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001078 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Pateleb57c592009-12-03 19:11:07 +00001079 if (!NDie) continue;
Devang Patelf20c4f72011-04-12 22:53:02 +00001080 getCompileUnit(N)->addDIEEntry(SPDie, dwarf::DW_AT_containing_type,
1081 dwarf::DW_FORM_ref4, NDie);
Devang Pateleb57c592009-12-03 19:11:07 +00001082 }
1083
Bill Wendling2b128d72009-05-20 23:19:06 +00001084 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001085 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +00001086 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001087 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +00001088 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +00001089
1090 // End text sections.
1091 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001092 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +00001093 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +00001094 }
1095
Bill Wendling2b128d72009-05-20 23:19:06 +00001096 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +00001097 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +00001098
1099 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +00001100 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001101
1102 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +00001103 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +00001104
Bill Wendling2b128d72009-05-20 23:19:06 +00001105 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +00001106 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +00001107
Devang Patel04d2f2d2009-11-24 01:14:22 +00001108 // Emit info into a debug pubtypes section.
1109 emitDebugPubTypes();
1110
Bill Wendling2b128d72009-05-20 23:19:06 +00001111 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00001112 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +00001113
1114 // Emit info into a debug aranges section.
1115 EmitDebugARanges();
1116
1117 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00001118 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +00001119
1120 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00001121 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001122
1123 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +00001124 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001125
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001126 // Emit info into a debug str section.
1127 emitDebugStr();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001128
Devang Pateld0701282010-08-02 17:32:15 +00001129 // clean up.
1130 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001131 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1132 E = CUMap.end(); I != E; ++I)
1133 delete I->second;
1134 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +00001135}
1136
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001137/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001138DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001139 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001140 LLVMContext &Ctx = DV->getContext();
1141 // More then one inlined variable corresponds to one abstract variable.
1142 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001143 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001144 if (AbsDbgVariable)
1145 return AbsDbgVariable;
1146
Devang Patel7e623022011-08-10 20:55:27 +00001147 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001148 if (!Scope)
1149 return NULL;
1150
Devang Patele1c53f22010-05-20 16:36:41 +00001151 AbsDbgVariable = new DbgVariable(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001152 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001153 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001154 return AbsDbgVariable;
1155}
1156
Nick Lewycky019d2552011-07-29 03:49:23 +00001157/// addCurrentFnArgument - If Var is a current function argument then add
1158/// it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +00001159bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +00001160 DbgVariable *Var, LexicalScope *Scope) {
1161 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001162 return false;
1163 DIVariable DV = Var->getVariable();
1164 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1165 return false;
1166 unsigned ArgNo = DV.getArgNumber();
1167 if (ArgNo == 0)
1168 return false;
1169
Devang Patel4ab660b2011-03-03 20:02:02 +00001170 size_t Size = CurrentFnArguments.size();
1171 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001172 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001173 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001174 // arguments does the function have at source level.
1175 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001176 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001177 CurrentFnArguments[ArgNo - 1] = Var;
1178 return true;
1179}
1180
Devang Patel490c8ab2010-05-20 19:57:06 +00001181/// collectVariableInfoFromMMITable - Collect variable information from
1182/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001183void
Nick Lewycky019d2552011-07-29 03:49:23 +00001184DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patel490c8ab2010-05-20 19:57:06 +00001185 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +00001186 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1187 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1188 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00001189 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001190 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001191 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001192 DIVariable DV(Var);
1193 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001194
Devang Patel7e623022011-08-10 20:55:27 +00001195 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001196
Devang Patelcdb7d442009-11-10 23:20:04 +00001197 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001198 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001199 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001200
Devang Patele1c53f22010-05-20 16:36:41 +00001201 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
1202 DbgVariable *RegVar = new DbgVariable(DV);
1203 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +00001204 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001205 addScopeVariable(Scope, RegVar);
Devang Patele1c53f22010-05-20 16:36:41 +00001206 if (AbsDbgVariable) {
1207 recordVariableFrameIndex(AbsDbgVariable, VP.first);
1208 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
1209 }
Devang Patel475d32a2009-10-06 01:26:37 +00001210 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001211}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001212
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001213/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +00001214/// DBG_VALUE instruction, is in a defined reg.
1215static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
1216 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001217 return MI->getNumOperands() == 3 &&
1218 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
1219 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +00001220}
1221
Nick Lewycky019d2552011-07-29 03:49:23 +00001222/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel2442a892011-07-08 17:09:57 +00001223/// at MI.
1224static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1225 const MCSymbol *FLabel,
1226 const MCSymbol *SLabel,
1227 const MachineInstr *MI) {
1228 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1229
1230 if (MI->getNumOperands() != 3) {
1231 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
1232 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1233 }
1234 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
1235 MachineLocation MLoc;
1236 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
1237 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1238 }
1239 if (MI->getOperand(0).isImm())
1240 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1241 if (MI->getOperand(0).isFPImm())
1242 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1243 if (MI->getOperand(0).isCImm())
1244 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1245
1246 assert (0 && "Unexpected 3 operand DBG_VALUE instruction!");
1247 return DotDebugLocEntry();
1248}
1249
Devang Patel7e623022011-08-10 20:55:27 +00001250/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001251void
Devang Patel5c0f85c2010-06-25 22:07:34 +00001252DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1253 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001254
Devang Patel490c8ab2010-05-20 19:57:06 +00001255 /// collection info from MMI table.
1256 collectVariableInfoFromMMITable(MF, Processed);
1257
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001258 for (SmallVectorImpl<const MDNode*>::const_iterator
1259 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1260 ++UVI) {
1261 const MDNode *Var = *UVI;
1262 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001263 continue;
1264
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001265 // History contains relevant DBG_VALUE instructions for Var and instructions
1266 // clobbering it.
1267 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1268 if (History.empty())
1269 continue;
1270 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001271
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001272 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001273 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001274 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1275 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001276 Scope = LScopes.getCurrentFunctionScope();
Devang Patel8fb9fd62011-07-20 22:18:50 +00001277 else {
1278 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patel7e623022011-08-10 20:55:27 +00001279 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel8fb9fd62011-07-20 22:18:50 +00001280 else {
1281 if (MDNode *IA = DV.getInlinedAt())
Devang Patel7e623022011-08-10 20:55:27 +00001282 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001283 else
Devang Patel7e623022011-08-10 20:55:27 +00001284 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001285 }
1286 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001287 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001288 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001289 continue;
1290
1291 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001292 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel490c8ab2010-05-20 19:57:06 +00001293 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel6c622ef2011-03-01 22:58:55 +00001294 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001295 addScopeVariable(Scope, RegVar);
Devang Patelfbd6c452010-05-21 00:10:20 +00001296 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
1297 DbgVariableToDbgInstMap[AbsVar] = MInsn;
1298 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patela3e9c9c2010-03-15 18:33:46 +00001299 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001300
1301 // Simple ranges that are fully coalesced.
1302 if (History.size() <= 1 || (History.size() == 2 &&
1303 MInsn->isIdenticalTo(History.back()))) {
Devang Patel9fc11702010-05-25 23:40:22 +00001304 DbgVariableToDbgInstMap[RegVar] = MInsn;
1305 continue;
1306 }
1307
1308 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001309 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001310
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001311 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1312 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1313 const MachineInstr *Begin = *HI;
1314 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001315
Devang Patele7181b52011-06-01 23:00:17 +00001316 // Check if DBG_VALUE is truncating a range.
1317 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1318 && !Begin->getOperand(0).getReg())
1319 continue;
1320
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001321 // Compute the range for a register location.
1322 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1323 const MCSymbol *SLabel = 0;
1324
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001325 if (HI + 1 == HE)
1326 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001327 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001328 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001329 else {
1330 const MachineInstr *End = HI[1];
Devang Patel53b050a2011-07-07 21:44:42 +00001331 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1332 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001333 if (End->isDebugValue())
1334 SLabel = getLabelBeforeInsn(End);
1335 else {
1336 // End is a normal instruction clobbering the range.
1337 SLabel = getLabelAfterInsn(End);
1338 assert(SLabel && "Forgot label after clobber instruction");
1339 ++HI;
1340 }
1341 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001342
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001343 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel2442a892011-07-08 17:09:57 +00001344 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001345 }
1346 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001347 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001348
1349 // Collect info for variables that were optimized out.
Devang Patelad517352010-06-22 01:01:58 +00001350 const Function *F = MF->getFunction();
Devang Patel364bf042010-11-10 22:19:21 +00001351 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patele0a94bf2010-05-14 21:01:35 +00001352 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00001353 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001354 if (!DV || !Processed.insert(DV))
Devang Patele0a94bf2010-05-14 21:01:35 +00001355 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001356 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1357 addScopeVariable(Scope, new DbgVariable(DV));
Devang Patele0a94bf2010-05-14 21:01:35 +00001358 }
1359 }
Devang Patel9fc11702010-05-25 23:40:22 +00001360}
Devang Patele0a94bf2010-05-14 21:01:35 +00001361
Devang Patel9fc11702010-05-25 23:40:22 +00001362/// getLabelBeforeInsn - Return Label preceding the instruction.
1363const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001364 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1365 assert(Label && "Didn't insert label before instruction");
1366 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001367}
1368
1369/// getLabelAfterInsn - Return Label immediately following the instruction.
1370const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001371 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001372}
1373
Devang Patelb5694e72010-10-26 17:49:02 +00001374/// beginInstruction - Process beginning of an instruction.
1375void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001376 // Check if source location changes, but ignore DBG_VALUE locations.
1377 if (!MI->isDebugValue()) {
1378 DebugLoc DL = MI->getDebugLoc();
1379 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel34a66202011-05-11 19:22:19 +00001380 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001381 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001382 if (DL == PrologEndLoc) {
1383 Flags |= DWARF2_FLAG_PROLOGUE_END;
1384 PrologEndLoc = DebugLoc();
1385 }
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001386 if (!DL.isUnknown()) {
1387 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001388 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001389 } else
Devang Patel34a66202011-05-11 19:22:19 +00001390 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001391 }
Devang Patel9fc11702010-05-25 23:40:22 +00001392 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001393
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001394 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001395 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1396 LabelsBeforeInsn.find(MI);
1397
1398 // No label needed.
1399 if (I == LabelsBeforeInsn.end())
1400 return;
1401
1402 // Label already assigned.
1403 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001404 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001405
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001406 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001407 PrevLabel = MMI->getContext().CreateTempSymbol();
1408 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001409 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001410 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001411}
1412
Devang Patelb5694e72010-10-26 17:49:02 +00001413/// endInstruction - Process end of an instruction.
1414void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001415 // Don't create a new label after DBG_VALUE instructions.
1416 // They don't generate code.
1417 if (!MI->isDebugValue())
1418 PrevLabel = 0;
1419
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001420 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1421 LabelsAfterInsn.find(MI);
1422
1423 // No label needed.
1424 if (I == LabelsAfterInsn.end())
1425 return;
1426
1427 // Label already assigned.
1428 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001429 return;
1430
1431 // We need a label after this instruction.
1432 if (!PrevLabel) {
1433 PrevLabel = MMI->getContext().CreateTempSymbol();
1434 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001435 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001436 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001437}
1438
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001439/// identifyScopeMarkers() -
Devang Patel784077e2011-08-10 23:58:09 +00001440/// Each LexicalScope has first instruction and last instruction to mark
1441/// beginning and end of a scope respectively. Create an inverse map that list
1442/// scopes starts (and ends) with an instruction. One instruction may start (or
1443/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001444void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001445 SmallVector<LexicalScope *, 4> WorkList;
1446 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001447 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001448 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001449
Devang Patel7e623022011-08-10 20:55:27 +00001450 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001451 if (!Children.empty())
Devang Patel7e623022011-08-10 20:55:27 +00001452 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001453 SE = Children.end(); SI != SE; ++SI)
1454 WorkList.push_back(*SI);
1455
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001456 if (S->isAbstractScope())
1457 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001458
Devang Patel7e623022011-08-10 20:55:27 +00001459 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001460 if (Ranges.empty())
1461 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001462 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +00001463 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001464 assert(RI->first && "InsnRange does not have first instruction!");
1465 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001466 requestLabelBeforeInsn(RI->first);
1467 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001468 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001469 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001470}
1471
Devang Patel589845d2011-05-09 22:14:49 +00001472/// getScopeNode - Get MDNode for DebugLoc's scope.
1473static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1474 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1475 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1476 return DL.getScope(Ctx);
1477}
1478
Devang Patel34a66202011-05-11 19:22:19 +00001479/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1480/// line number info for the function.
1481static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1482 const MDNode *Scope = getScopeNode(DL, Ctx);
1483 DISubprogram SP = getDISubprogram(Scope);
1484 if (SP.Verify())
1485 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1486 return DebugLoc();
1487}
1488
Devang Patel930143b2009-11-21 02:48:08 +00001489/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00001490/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00001491void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001492 if (!MMI->hasDebugInfo()) return;
Devang Patel7e623022011-08-10 20:55:27 +00001493 LScopes.initialize(*MF);
1494 if (LScopes.empty()) return;
1495 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001496
Devang Patel6c74a872010-04-27 19:46:33 +00001497 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1498 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001499 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001500 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001501
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001502 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1503
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001504 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001505 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1506 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1507
Devang Patel002d54d2010-05-26 19:37:24 +00001508 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001509 I != E; ++I) {
1510 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001511 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1512 II != IE; ++II) {
1513 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001514
Devang Patel002d54d2010-05-26 19:37:24 +00001515 if (MI->isDebugValue()) {
Devang Patel002d54d2010-05-26 19:37:24 +00001516 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001517
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001518 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001519 const MDNode *Var =
1520 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001521
1522 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001523 if (isDbgValueInDefinedReg(MI))
1524 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1525
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001526 // Check the history of this variable.
1527 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1528 if (History.empty()) {
1529 UserVariables.push_back(Var);
1530 // The first mention of a function argument gets the FunctionBeginSym
1531 // label, so arguments are visible when breaking at function entry.
1532 DIVariable DV(Var);
1533 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1534 DISubprogram(getDISubprogram(DV.getContext()))
1535 .describes(MF->getFunction()))
1536 LabelsBeforeInsn[MI] = FunctionBeginSym;
1537 } else {
1538 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1539 const MachineInstr *Prev = History.back();
1540 if (Prev->isDebugValue()) {
1541 // Coalesce identical entries at the end of History.
1542 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001543 Prev->isIdenticalTo(History[History.size() - 2])) {
1544 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1545 << "\t" << *Prev
1546 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001547 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001548 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001549
1550 // Terminate old register assignments that don't reach MI;
1551 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1552 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1553 isDbgValueInDefinedReg(Prev)) {
1554 // Previous register assignment needs to terminate at the end of
1555 // its basic block.
1556 MachineBasicBlock::const_iterator LastMI =
1557 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001558 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001559 // Drop DBG_VALUE for empty range.
Devang Patelb7a328e2011-07-07 00:14:27 +00001560 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1561 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001562 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001563 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001564 else {
1565 // Terminate after LastMI.
1566 History.push_back(LastMI);
1567 }
1568 }
1569 }
1570 }
1571 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001572 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001573 // Not a DBG_VALUE instruction.
1574 if (!MI->isLabel())
1575 AtBlockEntry = false;
1576
Devang Patel34a66202011-05-11 19:22:19 +00001577 // First known non DBG_VALUE location marks beginning of function
1578 // body.
1579 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1580 PrologEndLoc = MI->getDebugLoc();
1581
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001582 // Check if the instruction clobbers any registers with debug vars.
1583 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1584 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1585 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1586 continue;
1587 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1588 unsigned Reg = *AI; ++AI) {
1589 const MDNode *Var = LiveUserVar[Reg];
1590 if (!Var)
1591 continue;
1592 // Reg is now clobbered.
1593 LiveUserVar[Reg] = 0;
1594
1595 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001596 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1597 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001598 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001599 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1600 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001601 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001602 const MachineInstr *Prev = History.back();
1603 // Sanity-check: Register assignments are terminated at the end of
1604 // their block.
1605 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1606 continue;
1607 // Is the variable still in Reg?
1608 if (!isDbgValueInDefinedReg(Prev) ||
1609 Prev->getOperand(0).getReg() != Reg)
1610 continue;
1611 // Var is clobbered. Make sure the next instruction gets a label.
1612 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001613 }
1614 }
Devang Patel002d54d2010-05-26 19:37:24 +00001615 }
Devang Patel002d54d2010-05-26 19:37:24 +00001616 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001617 }
1618
1619 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1620 I != E; ++I) {
1621 SmallVectorImpl<const MachineInstr*> &History = I->second;
1622 if (History.empty())
1623 continue;
1624
1625 // Make sure the final register assignments are terminated.
1626 const MachineInstr *Prev = History.back();
1627 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1628 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel784077e2011-08-10 23:58:09 +00001629 MachineBasicBlock::const_iterator LastMI =
1630 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001631 if (LastMI == PrevMBB->end())
1632 // Drop DBG_VALUE for empty range.
1633 History.pop_back();
1634 else {
1635 // Terminate after LastMI.
1636 History.push_back(LastMI);
1637 }
1638 }
1639 // Request labels for the full history.
1640 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1641 const MachineInstr *MI = History[i];
1642 if (MI->isDebugValue())
1643 requestLabelBeforeInsn(MI);
1644 else
1645 requestLabelAfterInsn(MI);
1646 }
1647 }
Devang Patel002d54d2010-05-26 19:37:24 +00001648
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001649 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001650 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001651
1652 // Record beginning of function.
1653 if (!PrologEndLoc.isUnknown()) {
1654 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1655 MF->getFunction()->getContext());
1656 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1657 FnStartDL.getScope(MF->getFunction()->getContext()),
1658 DWARF2_FLAG_IS_STMT);
1659 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001660}
1661
Devang Patel7e623022011-08-10 20:55:27 +00001662void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1663// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1664 ScopeVariables[LS].push_back(Var);
1665// Vars.push_back(Var);
1666}
1667
Devang Patel930143b2009-11-21 02:48:08 +00001668/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00001669///
Chris Lattner76555b52010-01-26 23:18:02 +00001670void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patel7e623022011-08-10 20:55:27 +00001671 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00001672
Devang Patel7e623022011-08-10 20:55:27 +00001673 // Define end label for subprogram.
1674 FunctionEndSym = Asm->GetTempSymbol("func_end",
1675 Asm->getFunctionNumber());
1676 // Assumes in correct section after the entry point.
1677 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1678
1679 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1680 collectVariableInfo(MF, ProcessedVars);
1681
1682 // Construct abstract scopes.
1683 SmallVector<LexicalScope *, 4> &AList = LScopes.getAbstractScopesList();
1684 for (SmallVector<LexicalScope *, 4>::iterator AI = AList.begin(),
1685 AE = AList.end(); AI != AE; ++AI) {
1686 DISubprogram SP((*AI)->getScopeNode());
1687 if (SP.Verify()) {
1688 // Collect info for variables that were optimized out.
1689 StringRef FName = SP.getLinkageName();
1690 if (FName.empty())
1691 FName = SP.getName();
1692 if (NamedMDNode *NMD =
1693 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
1694 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00001695 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001696 if (!DV || !ProcessedVars.insert(DV))
1697 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001698 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1699 addScopeVariable(Scope, new DbgVariable(DV));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001700 }
1701 }
1702 }
Devang Patel7e623022011-08-10 20:55:27 +00001703 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
1704 constructScopeDIE(*AI);
Bill Wendling2b128d72009-05-20 23:19:06 +00001705 }
Devang Patel7e623022011-08-10 20:55:27 +00001706
1707 DIE *CurFnDIE = constructScopeDIE(LScopes.getCurrentFunctionScope());
1708
1709 if (!DisableFramePointerElim(*MF)) {
1710 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel784077e2011-08-10 23:58:09 +00001711 CompileUnit *TheCU = getCompileUnit(FnScope->getScopeNode());
1712 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1713 dwarf::DW_FORM_flag, 1);
Devang Patel7e623022011-08-10 20:55:27 +00001714 }
1715 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1716 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00001717
Bill Wendling2b128d72009-05-20 23:19:06 +00001718 // Clear debug info
Devang Patel7e623022011-08-10 20:55:27 +00001719 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1720 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1721 DeleteContainerPointers(I->second);
1722 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001723 DeleteContainerPointers(CurrentFnArguments);
Devang Patele1c53f22010-05-20 16:36:41 +00001724 DbgVariableToFrameIndexMap.clear();
1725 VarToAbstractVarMap.clear();
1726 DbgVariableToDbgInstMap.clear();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001727 UserVariables.clear();
1728 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001729 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001730 LabelsBeforeInsn.clear();
1731 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001732 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001733}
1734
Devang Patele1c53f22010-05-20 16:36:41 +00001735/// recordVariableFrameIndex - Record a variable's index.
1736void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
1737 assert (V && "Invalid DbgVariable!");
1738 DbgVariableToFrameIndexMap[V] = Index;
1739}
1740
1741/// findVariableFrameIndex - Return true if frame index for the variable
1742/// is found. Update FI to hold value of the index.
1743bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
1744 assert (V && "Invalid DbgVariable!");
1745 DenseMap<const DbgVariable *, int>::iterator I =
1746 DbgVariableToFrameIndexMap.find(V);
1747 if (I == DbgVariableToFrameIndexMap.end())
1748 return false;
1749 *FI = I->second;
1750 return true;
1751}
1752
Chris Lattnerba35a672010-03-09 04:54:43 +00001753/// recordSourceLine - Register a source line with debug info. Returns the
1754/// unique label that was emitted and which provides correspondence to
1755/// the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001756void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1757 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001758 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001759 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001760 unsigned Src = 1;
1761 if (S) {
1762 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001763
Dan Gohman50849c62010-05-05 23:41:32 +00001764 if (Scope.isCompileUnit()) {
1765 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001766 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001767 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001768 } else if (Scope.isFile()) {
1769 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001770 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001771 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001772 } else if (Scope.isSubprogram()) {
1773 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001774 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001775 Dir = SP.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001776 } else if (Scope.isLexicalBlock()) {
1777 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001778 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001779 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001780 } else
1781 assert(0 && "Unexpected scope info");
1782
Devang Patele01b75c2011-03-24 20:30:50 +00001783 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00001784 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001785 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001786}
1787
Bill Wendling806535f2009-05-20 23:22:40 +00001788//===----------------------------------------------------------------------===//
1789// Emit Methods
1790//===----------------------------------------------------------------------===//
1791
Devang Patel930143b2009-11-21 02:48:08 +00001792/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00001793///
Jim Grosbach00e9c612009-11-22 19:20:36 +00001794unsigned
1795DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00001796 // Get the children.
1797 const std::vector<DIE *> &Children = Die->getChildren();
1798
1799 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001800 if (!Last && !Children.empty())
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001801 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling480ff322009-05-20 23:21:38 +00001802
1803 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001804 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001805
1806 // Get the abbreviation for this DIE.
1807 unsigned AbbrevNumber = Die->getAbbrevNumber();
1808 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1809
1810 // Set DIE offset
1811 Die->setOffset(Offset);
1812
1813 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001814 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001815
1816 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1817 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1818
1819 // Size the DIE attribute values.
1820 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1821 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001822 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001823
1824 // Size the DIE children if any.
1825 if (!Children.empty()) {
1826 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1827 "Children flag not set");
1828
1829 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001830 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00001831
1832 // End of children marker.
1833 Offset += sizeof(int8_t);
1834 }
1835
1836 Die->setSize(Offset - Die->getOffset());
1837 return Offset;
1838}
1839
Devang Patel930143b2009-11-21 02:48:08 +00001840/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00001841///
Devang Patel930143b2009-11-21 02:48:08 +00001842void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001843 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1844 E = CUMap.end(); I != E; ++I) {
1845 // Compute size of compile unit header.
Devang Patel28dce702011-04-12 23:10:47 +00001846 unsigned Offset =
Devang Patel1a0df9a2010-05-10 22:49:55 +00001847 sizeof(int32_t) + // Length of Compilation Unit Info
1848 sizeof(int16_t) + // DWARF version number
1849 sizeof(int32_t) + // Offset Into Abbrev. Section
1850 sizeof(int8_t); // Pointer Size (in bytes)
1851 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001852 }
Bill Wendling480ff322009-05-20 23:21:38 +00001853}
1854
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001855/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1856/// temporary label to it if SymbolStem is specified.
Chris Lattner6629ca92010-04-04 22:59:04 +00001857static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001858 const char *SymbolStem = 0) {
Chris Lattner6629ca92010-04-04 22:59:04 +00001859 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001860 if (!SymbolStem) return 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001861
Chris Lattner6629ca92010-04-04 22:59:04 +00001862 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1863 Asm->OutStreamer.EmitLabel(TmpSym);
1864 return TmpSym;
1865}
1866
1867/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1868/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00001869void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001870 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001871
Bill Wendling480ff322009-05-20 23:21:38 +00001872 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001873 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001874 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001875 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001876 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001877 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001878
Chris Lattner6629ca92010-04-04 22:59:04 +00001879 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001880 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001881
Devang Patel4a213872010-08-24 00:06:12 +00001882 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001883 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1884 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1885 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001886 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001887 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00001888 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1889 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001890
Devang Patel9fc11702010-05-25 23:40:22 +00001891 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1892 "section_debug_loc");
1893
Chris Lattner6629ca92010-04-04 22:59:04 +00001894 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00001895 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001896}
1897
Nick Lewycky019d2552011-07-29 03:49:23 +00001898/// emitDIE - Recursively emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00001899///
Devang Patel930143b2009-11-21 02:48:08 +00001900void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001901 // Get the abbreviation for this DIE.
1902 unsigned AbbrevNumber = Die->getAbbrevNumber();
1903 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1904
Bill Wendling480ff322009-05-20 23:21:38 +00001905 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001906 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00001907 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1908 Twine::utohexstr(Die->getOffset()) + ":0x" +
1909 Twine::utohexstr(Die->getSize()) + " " +
1910 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00001911 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001912
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001913 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00001914 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1915
1916 // Emit the DIE attribute values.
1917 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1918 unsigned Attr = AbbrevData[i].getAttribute();
1919 unsigned Form = AbbrevData[i].getForm();
1920 assert(Form && "Too many attributes for DIE (check abbreviation)");
1921
Chris Lattner7bde8c02010-04-04 18:52:31 +00001922 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001923 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001924
Bill Wendling480ff322009-05-20 23:21:38 +00001925 switch (Attr) {
1926 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00001927 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00001928 break;
1929 case dwarf::DW_AT_abstract_origin: {
1930 DIEEntry *E = cast<DIEEntry>(Values[i]);
1931 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001932 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00001933 Asm->EmitInt32(Addr);
1934 break;
1935 }
Devang Patel12563b32010-04-16 23:33:45 +00001936 case dwarf::DW_AT_ranges: {
1937 // DW_AT_range Value encodes offset in debug_range section.
1938 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00001939
1940 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1941 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1942 V->getValue(),
1943 4);
1944 } else {
1945 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1946 V->getValue(),
1947 DwarfDebugRangeSectionSym,
1948 4);
1949 }
Devang Patel12563b32010-04-16 23:33:45 +00001950 break;
1951 }
Devang Patel9fc11702010-05-25 23:40:22 +00001952 case dwarf::DW_AT_location: {
1953 if (UseDotDebugLocEntry.count(Die) != 0) {
1954 DIELabel *L = cast<DIELabel>(Values[i]);
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001955 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patel9fc11702010-05-25 23:40:22 +00001956 } else
1957 Values[i]->EmitValue(Asm, Form);
1958 break;
1959 }
Devang Patela1bd5a12010-09-29 19:08:08 +00001960 case dwarf::DW_AT_accessibility: {
1961 if (Asm->isVerbose()) {
1962 DIEInteger *V = cast<DIEInteger>(Values[i]);
1963 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1964 }
1965 Values[i]->EmitValue(Asm, Form);
1966 break;
1967 }
Bill Wendling480ff322009-05-20 23:21:38 +00001968 default:
1969 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001970 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001971 break;
1972 }
Bill Wendling480ff322009-05-20 23:21:38 +00001973 }
1974
1975 // Emit the DIE children if any.
1976 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1977 const std::vector<DIE *> &Children = Die->getChildren();
1978
1979 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001980 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00001981
Chris Lattner7bde8c02010-04-04 18:52:31 +00001982 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00001983 Asm->OutStreamer.AddComment("End Of Children Mark");
1984 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001985 }
1986}
1987
Devang Patel9ccfb642009-12-09 18:24:21 +00001988/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00001989///
Devang Patel9ccfb642009-12-09 18:24:21 +00001990void DwarfDebug::emitDebugInfo() {
1991 // Start debug info section.
1992 Asm->OutStreamer.SwitchSection(
1993 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001994 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1995 E = CUMap.end(); I != E; ++I) {
1996 CompileUnit *TheCU = I->second;
1997 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001998
Devang Patel1a0df9a2010-05-10 22:49:55 +00001999 // Emit the compile units header.
2000 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2001 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002002
Devang Patel1a0df9a2010-05-10 22:49:55 +00002003 // Emit size of content not including length itself
2004 unsigned ContentSize = Die->getSize() +
2005 sizeof(int16_t) + // DWARF version number
2006 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00002007 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002008
Devang Patel1a0df9a2010-05-10 22:49:55 +00002009 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2010 Asm->EmitInt32(ContentSize);
2011 Asm->OutStreamer.AddComment("DWARF version number");
2012 Asm->EmitInt16(dwarf::DWARF_VERSION);
2013 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2014 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2015 DwarfAbbrevSectionSym);
2016 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2017 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002018
Devang Patel1a0df9a2010-05-10 22:49:55 +00002019 emitDIE(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +00002020 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
2021 }
Bill Wendling480ff322009-05-20 23:21:38 +00002022}
2023
Devang Patel930143b2009-11-21 02:48:08 +00002024/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00002025///
Devang Patel930143b2009-11-21 02:48:08 +00002026void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00002027 // Check to see if it is worth the effort.
2028 if (!Abbreviations.empty()) {
2029 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002030 Asm->OutStreamer.SwitchSection(
2031 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002032
Chris Lattnera179b522010-04-04 19:25:43 +00002033 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00002034
2035 // For each abbrevation.
2036 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2037 // Get abbreviation data
2038 const DIEAbbrev *Abbrev = Abbreviations[i];
2039
2040 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00002041 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002042
2043 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002044 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00002045 }
2046
2047 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00002048 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002049
Chris Lattnera179b522010-04-04 19:25:43 +00002050 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00002051 }
2052}
2053
Devang Patel930143b2009-11-21 02:48:08 +00002054/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00002055/// the line matrix.
2056///
Devang Patel930143b2009-11-21 02:48:08 +00002057void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002058 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002059 Asm->OutStreamer.AddComment("Extended Op");
2060 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002061
Chris Lattner566cae92010-03-09 23:52:58 +00002062 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002063 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002064 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2065 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2066
2067 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002068
Chris Lattnera179b522010-04-04 19:25:43 +00002069 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00002070 Asm->getTargetData().getPointerSize(),
2071 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00002072
2073 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002074 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2075 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002076 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002077 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002078}
2079
Devang Patel9ccfb642009-12-09 18:24:21 +00002080/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2081///
2082void DwarfDebug::emitDebugPubNames() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002083 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2084 E = CUMap.end(); I != E; ++I) {
2085 CompileUnit *TheCU = I->second;
2086 // Start the dwarf pubnames section.
2087 Asm->OutStreamer.SwitchSection(
2088 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002089
Devang Patel1a0df9a2010-05-10 22:49:55 +00002090 Asm->OutStreamer.AddComment("Length of Public Names Info");
2091 Asm->EmitLabelDifference(
2092 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
2093 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002094
Devang Patel1a0df9a2010-05-10 22:49:55 +00002095 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
2096 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002097
Devang Patel1a0df9a2010-05-10 22:49:55 +00002098 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002099 Asm->EmitInt16(dwarf::DWARF_VERSION);
2100
Devang Patel1a0df9a2010-05-10 22:49:55 +00002101 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002102 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00002103 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002104
Devang Patel1a0df9a2010-05-10 22:49:55 +00002105 Asm->OutStreamer.AddComment("Compilation Unit Length");
2106 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2107 Asm->GetTempSymbol("info_begin", TheCU->getID()),
2108 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002109
Devang Patel1a0df9a2010-05-10 22:49:55 +00002110 const StringMap<DIE*> &Globals = TheCU->getGlobals();
2111 for (StringMap<DIE*>::const_iterator
2112 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2113 const char *Name = GI->getKeyData();
2114 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002115
Devang Patel1a0df9a2010-05-10 22:49:55 +00002116 Asm->OutStreamer.AddComment("DIE offset");
2117 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002118
Devang Patel1a0df9a2010-05-10 22:49:55 +00002119 if (Asm->isVerbose())
2120 Asm->OutStreamer.AddComment("External Name");
2121 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
2122 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002123
Devang Patel1a0df9a2010-05-10 22:49:55 +00002124 Asm->OutStreamer.AddComment("End Mark");
2125 Asm->EmitInt32(0);
2126 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky019d2552011-07-29 03:49:23 +00002127 TheCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00002128 }
Bill Wendling480ff322009-05-20 23:21:38 +00002129}
2130
Devang Patel04d2f2d2009-11-24 01:14:22 +00002131void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002132 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2133 E = CUMap.end(); I != E; ++I) {
2134 CompileUnit *TheCU = I->second;
2135 // Start the dwarf pubnames section.
2136 Asm->OutStreamer.SwitchSection(
2137 Asm->getObjFileLowering().getDwarfPubTypesSection());
2138 Asm->OutStreamer.AddComment("Length of Public Types Info");
2139 Asm->EmitLabelDifference(
2140 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
2141 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002142
Devang Patel1a0df9a2010-05-10 22:49:55 +00002143 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
2144 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002145
Devang Patel1a0df9a2010-05-10 22:49:55 +00002146 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2147 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002148
Devang Patel1a0df9a2010-05-10 22:49:55 +00002149 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2150 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
2151 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002152
Devang Patel1a0df9a2010-05-10 22:49:55 +00002153 Asm->OutStreamer.AddComment("Compilation Unit Length");
2154 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2155 Asm->GetTempSymbol("info_begin", TheCU->getID()),
2156 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002157
Devang Patel1a0df9a2010-05-10 22:49:55 +00002158 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2159 for (StringMap<DIE*>::const_iterator
2160 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2161 const char *Name = GI->getKeyData();
Nick Lewycky019d2552011-07-29 03:49:23 +00002162 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002163
Devang Patel1a0df9a2010-05-10 22:49:55 +00002164 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2165 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002166
Devang Patel1a0df9a2010-05-10 22:49:55 +00002167 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
2168 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
2169 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002170
Devang Patel1a0df9a2010-05-10 22:49:55 +00002171 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002172 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00002173 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
2174 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00002175 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002176}
2177
Devang Patel930143b2009-11-21 02:48:08 +00002178/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00002179///
Devang Patel930143b2009-11-21 02:48:08 +00002180void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00002181 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00002182 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002183
Chris Lattner3d72a672010-03-09 23:38:23 +00002184 // Start the dwarf str section.
2185 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002186 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002187
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002188 // Get all of the string pool entries and put them in an array by their ID so
2189 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002190 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002191 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002192
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002193 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2194 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2195 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002196
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002197 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002198
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002199 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002200 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002201 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002202
Chris Lattner3d72a672010-03-09 23:38:23 +00002203 // Emit the string itself.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002204 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00002205 }
2206}
2207
Devang Patel930143b2009-11-21 02:48:08 +00002208/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00002209///
Devang Patel930143b2009-11-21 02:48:08 +00002210void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002211 if (DotDebugLocEntries.empty())
2212 return;
2213
Devang Patel116a9d72011-02-04 22:57:18 +00002214 for (SmallVector<DotDebugLocEntry, 4>::iterator
2215 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2216 I != E; ++I) {
2217 DotDebugLocEntry &Entry = *I;
2218 if (I + 1 != DotDebugLocEntries.end())
2219 Entry.Merge(I+1);
2220 }
2221
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002222 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002223 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00002224 Asm->getObjFileLowering().getDwarfLocSection());
2225 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002226 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2227 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002228 for (SmallVector<DotDebugLocEntry, 4>::iterator
2229 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002230 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002231 DotDebugLocEntry &Entry = *I;
2232 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002233 if (Entry.isEmpty()) {
2234 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2235 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002236 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002237 } else {
2238 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2239 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patel3e021532011-04-28 02:22:40 +00002240 DIVariable DV(Entry.Variable);
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002241 Asm->OutStreamer.AddComment("Loc expr size");
2242 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2243 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2244 Asm->EmitLabelDifference(end, begin, 2);
2245 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002246 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002247 DIBasicType BTy(DV.getType());
2248 if (BTy.Verify() &&
2249 (BTy.getEncoding() == dwarf::DW_ATE_signed
2250 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2251 Asm->OutStreamer.AddComment("DW_OP_consts");
2252 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002253 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002254 } else {
2255 Asm->OutStreamer.AddComment("DW_OP_constu");
2256 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002257 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002258 }
Devang Pateled9fd452011-07-08 16:49:43 +00002259 } else if (Entry.isLocation()) {
2260 if (!DV.hasComplexAddress())
2261 // Regular entry.
Devang Patel3e021532011-04-28 02:22:40 +00002262 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Pateled9fd452011-07-08 16:49:43 +00002263 else {
2264 // Complex address entry.
2265 unsigned N = DV.getNumAddrElements();
2266 unsigned i = 0;
2267 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2268 if (Entry.Loc.getOffset()) {
2269 i = 2;
2270 Asm->EmitDwarfRegOp(Entry.Loc);
2271 Asm->OutStreamer.AddComment("DW_OP_deref");
2272 Asm->EmitInt8(dwarf::DW_OP_deref);
2273 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2274 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2275 Asm->EmitSLEB128(DV.getAddrElement(1));
2276 } else {
2277 // If first address element is OpPlus then emit
2278 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2279 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2280 Asm->EmitDwarfRegOp(Loc);
2281 i = 2;
2282 }
2283 } else {
2284 Asm->EmitDwarfRegOp(Entry.Loc);
2285 }
2286
2287 // Emit remaining complex address elements.
2288 for (; i < N; ++i) {
2289 uint64_t Element = DV.getAddrElement(i);
2290 if (Element == DIBuilder::OpPlus) {
2291 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2292 Asm->EmitULEB128(DV.getAddrElement(++i));
2293 } else if (Element == DIBuilder::OpDeref)
2294 Asm->EmitInt8(dwarf::DW_OP_deref);
2295 else llvm_unreachable("unknown Opcode found in complex address");
2296 }
Devang Patel3e021532011-04-28 02:22:40 +00002297 }
Devang Patel3e021532011-04-28 02:22:40 +00002298 }
Devang Pateled9fd452011-07-08 16:49:43 +00002299 // else ... ignore constant fp. There is not any good way to
2300 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002301 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002302 }
2303 }
Bill Wendling480ff322009-05-20 23:21:38 +00002304}
2305
2306/// EmitDebugARanges - Emit visible names into a debug aranges section.
2307///
2308void DwarfDebug::EmitDebugARanges() {
2309 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002310 Asm->OutStreamer.SwitchSection(
2311 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002312}
2313
Devang Patel930143b2009-11-21 02:48:08 +00002314/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00002315///
Devang Patel930143b2009-11-21 02:48:08 +00002316void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002317 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002318 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00002319 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00002320 unsigned char Size = Asm->getTargetData().getPointerSize();
2321 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002322 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00002323 I != E; ++I) {
2324 if (*I)
2325 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00002326 else
Devang Patel6c74a872010-04-27 19:46:33 +00002327 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00002328 }
Bill Wendling480ff322009-05-20 23:21:38 +00002329}
2330
Devang Patel930143b2009-11-21 02:48:08 +00002331/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00002332///
Devang Patel930143b2009-11-21 02:48:08 +00002333void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002334 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002335 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002336 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002337 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002338 }
2339}
2340
Devang Patel930143b2009-11-21 02:48:08 +00002341/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00002342/// Section Header:
2343/// 1. length of section
2344/// 2. Dwarf version number
2345/// 3. address size.
2346///
2347/// Entries (one "entry" for each function that was inlined):
2348///
2349/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2350/// otherwise offset into __debug_str for regular function name.
2351/// 2. offset into __debug_str section for regular function name.
2352/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2353/// instances for the function.
2354///
2355/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2356/// inlined instance; the die_offset points to the inlined_subroutine die in the
2357/// __debug_info section, and the low_pc is the starting address for the
2358/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002359void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00002360 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002361 return;
2362
Devang Patel1a0df9a2010-05-10 22:49:55 +00002363 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002364 return;
2365
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002366 Asm->OutStreamer.SwitchSection(
2367 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002368
Chris Lattner566cae92010-03-09 23:52:58 +00002369 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002370 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2371 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002372
Chris Lattnera179b522010-04-04 19:25:43 +00002373 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002374
Chris Lattner566cae92010-03-09 23:52:58 +00002375 Asm->OutStreamer.AddComment("Dwarf Version");
2376 Asm->EmitInt16(dwarf::DWARF_VERSION);
2377 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002378 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002379
Devang Patel32cc43c2010-05-07 20:54:48 +00002380 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002381 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002382
Devang Patel32cc43c2010-05-07 20:54:48 +00002383 const MDNode *Node = *I;
2384 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00002385 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002386 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002387 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002388 StringRef LName = SP.getLinkageName();
2389 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002390
Chris Lattner566cae92010-03-09 23:52:58 +00002391 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002392 if (LName.empty()) {
2393 Asm->OutStreamer.EmitBytes(Name, 0);
2394 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002395 } else
Chris Lattner70a4fce2010-04-04 23:25:33 +00002396 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2397 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002398
Chris Lattner566cae92010-03-09 23:52:58 +00002399 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00002400 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00002401 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002402
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002403 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002404 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002405 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002406 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002407
Chris Lattner7bde8c02010-04-04 18:52:31 +00002408 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002409 Asm->OutStreamer.EmitSymbolValue(LI->first,
2410 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00002411 }
2412 }
2413
Chris Lattnera179b522010-04-04 19:25:43 +00002414 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002415}