blob: 86b30b767fc6d1cf6c05969a41e26b8cd9ab9c89 [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"
Bill Wendling2f921f82009-05-15 09:23:25 +000027#include "llvm/Target/TargetData.h"
Anton Korobeynikov2f931282011-01-10 12:39:04 +000028#include "llvm/Target/TargetFrameLowering.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000029#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner21dc46e2010-04-04 18:06:11 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel61880932010-04-19 19:14:02 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000033#include "llvm/Analysis/DebugInfo.h"
Devang Patela5d93242011-02-24 18:49:30 +000034#include "llvm/Analysis/DIBuilder.h"
Devang Patela86114b2010-10-25 20:45:32 +000035#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000036#include "llvm/ADT/STLExtras.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000037#include "llvm/ADT/StringExtras.h"
Devang Patel6c74a872010-04-27 19:46:33 +000038#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000039#include "llvm/Support/Debug.h"
40#include "llvm/Support/ErrorHandling.h"
Devang Patel1973df22010-01-26 21:39:14 +000041#include "llvm/Support/ValueHandle.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000042#include "llvm/Support/FormattedStream.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000043#include "llvm/Support/Timer.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000044#include "llvm/Support/Path.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000045using namespace llvm;
46
Jim Grosbacha8683bb2010-07-21 21:21:52 +000047static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel30265c42010-07-07 20:12:52 +000048 cl::Hidden,
Devang Patel6c74a872010-04-27 19:46:33 +000049 cl::desc("Disable debug info printing"));
50
Dan Gohman7421ae42010-05-07 01:08:53 +000051static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000052 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman7421ae42010-05-07 01:08:53 +000053 cl::init(false));
54
Bill Wendlingfcc14142010-04-07 09:28:04 +000055namespace {
56 const char *DWARFGroupName = "DWARF Emission";
57 const char *DbgTimerName = "DWARF Debug Writer";
58} // end anonymous namespace
59
Bill Wendling2f921f82009-05-15 09:23:25 +000060//===----------------------------------------------------------------------===//
61
62/// Configuration values for initial hash set sizes (log2).
63///
Bill Wendling2f921f82009-05-15 09:23:25 +000064static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +000065
66namespace llvm {
67
Nick Lewycky019d2552011-07-29 03:49:23 +000068DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +000069 DIType Ty = Var.getType();
70 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
71 // addresses instead.
72 if (Var.isBlockByrefVariable()) {
73 /* Byref variables, in Blocks, are declared by the programmer as
74 "SomeType VarName;", but the compiler creates a
75 __Block_byref_x_VarName struct, and gives the variable VarName
76 either the struct, or a pointer to the struct, as its type. This
77 is necessary for various behind-the-scenes things the compiler
78 needs to do with by-reference variables in blocks.
79
80 However, as far as the original *programmer* is concerned, the
81 variable should still have type 'SomeType', as originally declared.
82
83 The following function dives into the __Block_byref_x_VarName
84 struct to find the original type of the variable. This will be
85 passed back to the code generating the type for the Debug
86 Information Entry for the variable 'VarName'. 'VarName' will then
87 have the original type 'SomeType' in its debug information.
88
89 The original type 'SomeType' will be the type of the field named
90 'VarName' inside the __Block_byref_x_VarName struct.
91
92 NOTE: In order for this to not completely fail on the debugger
93 side, the Debug Information Entry for the variable VarName needs to
94 have a DW_AT_location that tells the debugger how to unwind through
95 the pointers and __Block_byref_x_VarName struct to find the actual
96 value of the variable. The function addBlockByrefType does this. */
97 DIType subType = Ty;
98 unsigned tag = Ty.getTag();
99
100 if (tag == dwarf::DW_TAG_pointer_type) {
101 DIDerivedType DTy = DIDerivedType(Ty);
102 subType = DTy.getTypeDerivedFrom();
103 }
104
105 DICompositeType blockStruct = DICompositeType(subType);
106 DIArray Elements = blockStruct.getTypeArray();
107
108 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
109 DIDescriptor Element = Elements.getElement(i);
110 DIDerivedType DT = DIDerivedType(Element);
111 if (getName() == DT.getName())
112 return (DT.getTypeDerivedFrom());
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000113 }
114 return Ty;
115 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000116 return Ty;
117}
Bill Wendling2f921f82009-05-15 09:23:25 +0000118
Chris Lattnerf5d06362010-04-05 04:09:20 +0000119} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000120
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000121DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel1a0df9a2010-05-10 22:49:55 +0000122 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000123 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel7e623022011-08-10 20:55:27 +0000124 PrevLabel(NULL) {
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000125 NextStringPoolNumber = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000126
Rafael Espindolaa7160962011-05-06 14:56:22 +0000127 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattnere58b5472010-04-04 23:10:38 +0000128 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000129 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000130 FunctionBeginSym = FunctionEndSym = 0;
Dan Gohman6e681a52010-06-18 15:56:31 +0000131 {
132 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
133 beginModule(M);
Torok Edwinf8dba242010-04-07 10:44:46 +0000134 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000135}
136DwarfDebug::~DwarfDebug() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000137}
138
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000139MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
140 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
141 if (Entry.first) return Entry.first;
142
143 Entry.second = NextStringPoolNumber++;
Chris Lattnera179b522010-04-04 19:25:43 +0000144 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000145}
146
147
Devang Patel930143b2009-11-21 02:48:08 +0000148/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000149///
Devang Patel930143b2009-11-21 02:48:08 +0000150void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000151 // Profile the node so that we can make it unique.
152 FoldingSetNodeID ID;
153 Abbrev.Profile(ID);
154
155 // Check the set for priors.
156 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
157
158 // If it's newly added.
159 if (InSet == &Abbrev) {
160 // Add to abbreviation list.
161 Abbreviations.push_back(&Abbrev);
162
163 // Assign the vector position + 1 as its number.
164 Abbrev.setNumber(Abbreviations.size());
165 } else {
166 // Assign existing abbreviation number.
167 Abbrev.setNumber(InSet->getNumber());
168 }
169}
170
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000171/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel43ef34d2010-01-05 01:46:14 +0000172/// printer to not emit usual symbol prefix before the symbol name is used then
173/// return linkage name after skipping this special LLVM prefix.
174static StringRef getRealLinkageName(StringRef LinkageName) {
175 char One = '\1';
176 if (LinkageName.startswith(StringRef(&One, 1)))
177 return LinkageName.substr(1);
178 return LinkageName;
179}
180
Jim Grosbach042483e2009-11-21 23:12:12 +0000181/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +0000182/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
183/// If there are global variables in this scope then create and insert
184/// DIEs for these variables.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000185DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
186 const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000187 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +0000188
Chris Lattner3a383cb2010-04-05 00:13:49 +0000189 assert(SPDie && "Unable to find subprogram DIE!");
190 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000191
Devang Patel1d6bbd42011-04-22 23:10:17 +0000192 DISubprogram SPDecl = SP.getFunctionDeclaration();
193 if (SPDecl.isSubprogram())
194 // Refer function declaration directly.
Devang Patelf20c4f72011-04-12 22:53:02 +0000195 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Devang Patel89543712011-08-15 17:24:54 +0000196 SPCU->getOrCreateSubprogramDIE(SPDecl));
Devang Patel1d6bbd42011-04-22 23:10:17 +0000197 else {
198 // There is not any need to generate specification DIE for a function
199 // defined at compile unit level. If a function is defined inside another
200 // function then gdb prefers the definition at top level and but does not
201 // expect specification DIE in parent function. So avoid creating
202 // specification DIE for a function defined inside a function.
203 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
204 !SP.getContext().isFile() &&
205 !isSubprogramContext(SP.getContext())) {
206 SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
207
208 // Add arguments.
209 DICompositeType SPTy = SP.getType();
210 DIArray Args = SPTy.getTypeArray();
211 unsigned SPTag = SPTy.getTag();
212 if (SPTag == dwarf::DW_TAG_subroutine_type)
213 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
214 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
215 DIType ATy = DIType(DIType(Args.getElement(i)));
216 SPCU->addType(Arg, ATy);
217 if (ATy.isArtificial())
218 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
219 SPDie->addChild(Arg);
220 }
221 DIE *SPDeclDie = SPDie;
222 SPDie = new DIE(dwarf::DW_TAG_subprogram);
223 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
224 SPDeclDie);
225 SPCU->addDie(SPDie);
226 }
Chris Lattner3a383cb2010-04-05 00:13:49 +0000227 }
Devang Patela37a95e2010-07-07 22:20:57 +0000228 // Pick up abstract subprogram DIE.
229 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
230 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf20c4f72011-04-12 22:53:02 +0000231 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
232 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patela37a95e2010-07-07 22:20:57 +0000233 SPCU->addDie(SPDie);
234 }
235
Devang Patelf20c4f72011-04-12 22:53:02 +0000236 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
237 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
238 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
239 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattner3a383cb2010-04-05 00:13:49 +0000240 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
241 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000242 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000243
Chris Lattner3a383cb2010-04-05 00:13:49 +0000244 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000245}
246
Jim Grosbach042483e2009-11-21 23:12:12 +0000247/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +0000248/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000249DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
250 LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000251
252 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
253 if (Scope->isAbstractScope())
254 return ScopeDIE;
255
Devang Patel7e623022011-08-10 20:55:27 +0000256 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +0000257 if (Ranges.empty())
258 return 0;
259
Devang Patel7e623022011-08-10 20:55:27 +0000260 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel6c74a872010-04-27 19:46:33 +0000261 if (Ranges.size() > 1) {
262 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000263 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000264 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000265 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000266 DebugRangeSymbols.size()
267 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000268 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +0000269 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000270 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
271 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000272 }
273 DebugRangeSymbols.push_back(NULL);
274 DebugRangeSymbols.push_back(NULL);
275 return ScopeDIE;
276 }
277
Devang Patel9fc11702010-05-25 23:40:22 +0000278 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
279 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000280
Devang Patel9fc11702010-05-25 23:40:22 +0000281 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000282
Chris Lattnere13c3722010-03-09 01:58:53 +0000283 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
284 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000285
Devang Patelf20c4f72011-04-12 22:53:02 +0000286 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
287 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000288
289 return ScopeDIE;
290}
291
Devang Patel930143b2009-11-21 02:48:08 +0000292/// constructInlinedScopeDIE - This scope represents inlined body of
293/// a function. Construct DIE to represent this concrete inlined copy
294/// of the function.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000295DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
296 LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000297
Devang Patel7e623022011-08-10 20:55:27 +0000298 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000299 assert (Ranges.empty() == false
Devang Patel7e623022011-08-10 20:55:27 +0000300 && "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000301
Devang Patelf098ce22011-07-27 00:34:13 +0000302 if (!Scope->getScopeNode())
303 return NULL;
304 DIScope DS(Scope->getScopeNode());
305 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patelf098ce22011-07-27 00:34:13 +0000306 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
307 if (!OriginDIE) {
308 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
309 return NULL;
310 }
311
Devang Patel7e623022011-08-10 20:55:27 +0000312 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +0000313 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
314 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000315
Devang Patel4c6bd662010-07-08 22:39:20 +0000316 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky019d2552011-07-29 03:49:23 +0000317 assert (0 && "Unexpected Start and End labels for a inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000318 return 0;
319 }
Chris Lattnere13c3722010-03-09 01:58:53 +0000320 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000321 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +0000322 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000323 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000324
Devang Patel73bc1722011-05-05 17:54:26 +0000325 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patelf20c4f72011-04-12 22:53:02 +0000326 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
327 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000328
Devang Patelf098ce22011-07-27 00:34:13 +0000329 if (Ranges.size() > 1) {
330 // .debug_range section has not been laid out yet. Emit offset in
331 // .debug_range as a uint, size 4, for now. emitDIE will handle
332 // DW_AT_ranges appropriately.
333 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000334 DebugRangeSymbols.size()
335 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000336 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patelf098ce22011-07-27 00:34:13 +0000337 RE = Ranges.end(); RI != RE; ++RI) {
338 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
339 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
340 }
341 DebugRangeSymbols.push_back(NULL);
342 DebugRangeSymbols.push_back(NULL);
343 } else {
Devang Patel784077e2011-08-10 23:58:09 +0000344 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
345 StartLabel);
346 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
347 EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000348 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000349
350 InlinedSubprogramDIEs.insert(OriginDIE);
351
352 // Track the start label for this inlined function.
Devang Patelf098ce22011-07-27 00:34:13 +0000353 //.debug_inlined section specification does not clearly state how
354 // to emit inlined scope that is split into multiple instruction ranges.
355 // For now, use first instruction range and emit low_pc/high_pc pair and
356 // corresponding .debug_inlined section entry for this pair.
Devang Patel32cc43c2010-05-07 20:54:48 +0000357 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000358 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000359
360 if (I == InlineInfo.end()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000361 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach00e9c612009-11-22 19:20:36 +0000362 ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000363 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000364 } else
Chris Lattner085b6522010-03-09 00:31:02 +0000365 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000366
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000367 DILocation DL(Scope->getInlinedAt());
Devang Patelf20c4f72011-04-12 22:53:02 +0000368 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
369 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000370
371 return ScopeDIE;
372}
373
Devang Patel930143b2009-11-21 02:48:08 +0000374/// constructScopeDIE - Construct a DIE for this scope.
Devang Patel3acc70e2011-08-15 22:04:40 +0000375DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000376 if (!Scope || !Scope->getScopeNode())
377 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000378
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000379 SmallVector <DIE *, 8> Children;
Devang Patel6c622ef2011-03-01 22:58:55 +0000380
381 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000382 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000383 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
384 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Patel3acc70e2011-08-15 22:04:40 +0000385 if (DIE *Arg =
386 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel6c622ef2011-03-01 22:58:55 +0000387 Children.push_back(Arg);
388
Eric Christopherf84354b2011-10-03 15:49:16 +0000389 // Collect lexical scope children first.
Devang Patel7e623022011-08-10 20:55:27 +0000390 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000391 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Patel3acc70e2011-08-15 22:04:40 +0000392 if (DIE *Variable =
393 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000394 Children.push_back(Variable);
Devang Patel7e623022011-08-10 20:55:27 +0000395 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000396 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000397 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000398 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000399 DIScope DS(Scope->getScopeNode());
400 DIE *ScopeDIE = NULL;
401 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000402 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000403 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000404 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000405 if (Scope->isAbstractScope()) {
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000406 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000407 // Note down abstract DIE.
408 if (ScopeDIE)
409 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
410 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000411 else
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000412 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000413 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000414 else {
415 // There is no need to emit empty lexical block DIE.
416 if (Children.empty())
417 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000418 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000419 }
420
Devang Patel23b2ae62010-03-29 22:59:58 +0000421 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000422
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000423 // Add children
424 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
425 E = Children.end(); I != E; ++I)
426 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000427
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000428 if (DS.isSubprogram())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000429 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000430
Devang Patel04d2f2d2009-11-24 01:14:22 +0000431 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000432}
433
Bill Wendling2b128d72009-05-20 23:19:06 +0000434/// GetOrCreateSourceID - Look up the source id with the given directory and
435/// source file names. If none currently exists, create a new id and insert it
436/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
437/// maps as well.
Devang Patel498877d2010-07-24 00:53:22 +0000438
Devang Patele01b75c2011-03-24 20:30:50 +0000439unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
440 StringRef DirName) {
Devang Patel871d0b12010-09-16 20:57:49 +0000441 // If FE did not provide a file name, then assume stdin.
442 if (FileName.empty())
Devang Patele01b75c2011-03-24 20:30:50 +0000443 return GetOrCreateSourceID("<stdin>", StringRef());
444
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000445 unsigned SrcId = SourceIdMap.size()+1;
446 std::pair<std::string, std::string> SourceName =
447 std::make_pair(FileName, DirName);
448 std::pair<std::pair<std::string, std::string>, unsigned> Entry =
449 make_pair(SourceName, SrcId);
Devang Patel871d0b12010-09-16 20:57:49 +0000450
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000451 std::map<std::pair<std::string, std::string>, unsigned>::iterator I;
452 bool NewlyInserted;
453 tie(I, NewlyInserted) = SourceIdMap.insert(Entry);
454 if (!NewlyInserted)
455 return I->second;
Bill Wendling2b128d72009-05-20 23:19:06 +0000456
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000457 // Print out a .file directive to specify files for .loc directives.
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000458 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.first.second,
459 Entry.first.first);
Bill Wendling2b128d72009-05-20 23:19:06 +0000460
461 return SrcId;
462}
463
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000464/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel1a0df9a2010-05-10 22:49:55 +0000465/// metadata node with tag DW_TAG_compile_unit.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000466CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000467 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +0000468 StringRef FN = DIUnit.getFilename();
469 StringRef Dir = DIUnit.getDirectory();
Devang Patele01b75c2011-03-24 20:30:50 +0000470 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000471
472 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelf20c4f72011-04-12 22:53:02 +0000473 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
474 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
475 DIUnit.getProducer());
476 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
477 DIUnit.getLanguage());
478 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patelbd798ce2010-04-26 22:54:28 +0000479 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
480 // simplifies debug range entries.
Devang Patelf20c4f72011-04-12 22:53:02 +0000481 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +0000482 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +0000483 // compile unit in debug_line section.
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000484 if (Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindolaa7558912011-05-04 17:44:06 +0000485 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patelf20c4f72011-04-12 22:53:02 +0000486 Asm->GetTempSymbol("section_line"));
Devang Patelea636392010-08-31 23:50:19 +0000487 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000488 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +0000489
Devang Patel2d9caf92009-11-25 17:36:49 +0000490 if (!Dir.empty())
Devang Patelf20c4f72011-04-12 22:53:02 +0000491 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000492 if (DIUnit.isOptimized())
Devang Patelf20c4f72011-04-12 22:53:02 +0000493 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +0000494
Devang Patel2d9caf92009-11-25 17:36:49 +0000495 StringRef Flags = DIUnit.getFlags();
496 if (!Flags.empty())
Devang Patel784077e2011-08-10 23:58:09 +0000497 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string,
498 Flags);
Devang Patelf20c4f72011-04-12 22:53:02 +0000499
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000500 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000501 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000502 dwarf::DW_FORM_data1, RVer);
503
Devang Patel1a0df9a2010-05-10 22:49:55 +0000504 if (!FirstCU)
505 FirstCU = NewCU;
506 CUMap.insert(std::make_pair(N, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000507 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000508}
509
Devang Patel1a0df9a2010-05-10 22:49:55 +0000510/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel1f4f98d2011-08-15 23:36:40 +0000511void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
512 const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000513 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000514 if (!SP.isDefinition())
515 // This is a method declaration which will be handled while constructing
516 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000517 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000518
Devang Patel89543712011-08-15 17:24:54 +0000519 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000520
521 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000522 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000523
524 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +0000525 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +0000526
Bill Wendling2b128d72009-05-20 23:19:06 +0000527 // Expose as global.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000528 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000529
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000530 SPMap[N] = TheCU;
Devang Patel0751a282009-06-26 01:49:18 +0000531 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000532}
533
Devang Patel07bb9ee2011-08-15 23:47:24 +0000534/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
535/// as llvm.dbg.enum and llvm.dbg.ty
536void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000537 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
538 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
539 const MDNode *N = NMD->getOperand(i);
540 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
541 constructSubprogramDIE(CU, N);
542 }
543
544 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
545 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
546 const MDNode *N = NMD->getOperand(i);
547 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000548 CU->createGlobalVariableDIE(N);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000549 }
550
Devang Patel07bb9ee2011-08-15 23:47:24 +0000551 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
552 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
553 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000554 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
555 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000556 }
557
558 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
559 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
560 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000561 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
562 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000563 }
564}
565
566/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
567/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
568bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
569 DebugInfoFinder DbgFinder;
570 DbgFinder.processModule(*M);
571
572 bool HasDebugInfo = false;
573 // Scan all the compile-units to see if there are any marked as the main
574 // unit. If not, we do not generate debug info.
575 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
576 E = DbgFinder.compile_unit_end(); I != E; ++I) {
577 if (DICompileUnit(*I).isMain()) {
578 HasDebugInfo = true;
579 break;
580 }
581 }
582 if (!HasDebugInfo) return false;
583
584 // Create all the compile unit DIEs.
585 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
586 E = DbgFinder.compile_unit_end(); I != E; ++I)
587 constructCompileUnit(*I);
588
589 // Create DIEs for each global variable.
590 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
591 E = DbgFinder.global_variable_end(); I != E; ++I) {
592 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000593 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000594 CU->createGlobalVariableDIE(N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000595 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000596
Devang Patel07bb9ee2011-08-15 23:47:24 +0000597 // Create DIEs for each subprogram.
598 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
599 E = DbgFinder.subprogram_end(); I != E; ++I) {
600 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000601 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
602 constructSubprogramDIE(CU, N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000603 }
604
605 return HasDebugInfo;
606}
607
Devang Patel930143b2009-11-21 02:48:08 +0000608/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000609/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky019d2552011-07-29 03:49:23 +0000610/// This is invoked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +0000611void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +0000612 if (DisableDebugInfoPrinting)
613 return;
614
Nick Lewycky019d2552011-07-29 03:49:23 +0000615 // If module has named metadata anchors then use them, otherwise scan the
616 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000617 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
618 if (CU_Nodes) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000619 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
620 DICompileUnit CUNode(CU_Nodes->getOperand(i));
621 CompileUnit *CU = constructCompileUnit(CUNode);
622 DIArray GVs = CUNode.getGlobalVariables();
623 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000624 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000625 DIArray SPs = CUNode.getSubprograms();
626 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
627 constructSubprogramDIE(CU, SPs.getElement(i));
628 DIArray EnumTypes = CUNode.getEnumTypes();
629 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
630 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
631 DIArray RetainedTypes = CUNode.getRetainedTypes();
632 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
633 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
634 }
Devang Patel07bb9ee2011-08-15 23:47:24 +0000635 } else if (!collectLegacyDebugInfo(M))
636 return;
Devang Patele02e5852011-05-03 16:45:22 +0000637
Devang Patel07bb9ee2011-08-15 23:47:24 +0000638 collectInfoFromNamedMDNodes(M);
Devang Patele02e5852011-05-03 16:45:22 +0000639
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000640 // Tell MMI that we have debug info.
641 MMI->setDebugInfoAvailability(true);
Devang Patele02e5852011-05-03 16:45:22 +0000642
Chris Lattnerd442aa32010-04-04 23:17:54 +0000643 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000644 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000645
Bill Wendling2b128d72009-05-20 23:19:06 +0000646 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +0000647 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +0000648}
649
Devang Patel930143b2009-11-21 02:48:08 +0000650/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +0000651///
Devang Patel930143b2009-11-21 02:48:08 +0000652void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000653 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +0000654 const Module *M = MMI->getModule();
Devang Patel7e623022011-08-10 20:55:27 +0000655 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +0000656
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000657 // Collect info for variables that were optimized out.
658 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
659 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
660 DICompileUnit TheCU(CU_Nodes->getOperand(i));
661 DIArray Subprograms = TheCU.getSubprograms();
662 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
663 DISubprogram SP(Subprograms.getElement(i));
664 if (ProcessedSPNodes.count(SP) != 0) continue;
665 if (!SP.Verify()) continue;
666 if (!SP.isDefinition()) continue;
Devang Patel59e27c52011-08-19 23:28:12 +0000667 DIArray Variables = SP.getVariables();
668 if (Variables.getNumElements() == 0) continue;
669
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000670 LexicalScope *Scope =
671 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
672 DeadFnScopeMap[SP] = Scope;
673
674 // Construct subprogram DIE and add variables DIEs.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000675 CompileUnit *SPCU = CUMap.lookup(TheCU);
676 assert (SPCU && "Unable to find Compile Unit!");
677 constructSubprogramDIE(SPCU, SP);
678 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel59e27c52011-08-19 23:28:12 +0000679 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
680 DIVariable DV(Variables.getElement(vi));
681 if (!DV.Verify()) continue;
682 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000683 if (DIE *VariableDIE =
Devang Patel59e27c52011-08-19 23:28:12 +0000684 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000685 ScopeDIE->addChild(VariableDIE);
686 }
Devang Patelf3b2db62010-06-28 18:25:03 +0000687 }
688 }
689 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000690
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000691 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
692 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
693 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
694 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +0000695 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000696 }
697
Devang Patel89543712011-08-15 17:24:54 +0000698 // Emit DW_AT_containing_type attribute to connect types with their
699 // vtable holding type.
700 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
701 CUE = CUMap.end(); CUI != CUE; ++CUI) {
702 CompileUnit *TheCU = CUI->second;
703 TheCU->constructContainingTypeDIEs();
Devang Pateleb57c592009-12-03 19:11:07 +0000704 }
705
Bill Wendling2b128d72009-05-20 23:19:06 +0000706 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000707 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000708 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000709 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000710 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +0000711
712 // End text sections.
713 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000714 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +0000715 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +0000716 }
717
Bill Wendling2b128d72009-05-20 23:19:06 +0000718 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +0000719 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +0000720
721 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +0000722 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000723
724 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +0000725 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +0000726
Bill Wendling2b128d72009-05-20 23:19:06 +0000727 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +0000728 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +0000729
Devang Patel04d2f2d2009-11-24 01:14:22 +0000730 // Emit info into a debug pubtypes section.
731 emitDebugPubTypes();
732
Bill Wendling2b128d72009-05-20 23:19:06 +0000733 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +0000734 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +0000735
736 // Emit info into a debug aranges section.
737 EmitDebugARanges();
738
739 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +0000740 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +0000741
742 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +0000743 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000744
745 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +0000746 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000747
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000748 // Emit info into a debug str section.
749 emitDebugStr();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000750
Devang Pateld0701282010-08-02 17:32:15 +0000751 // clean up.
752 DeleteContainerSeconds(DeadFnScopeMap);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000753 SPMap.clear();
Devang Patel1a0df9a2010-05-10 22:49:55 +0000754 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
755 E = CUMap.end(); I != E; ++I)
756 delete I->second;
757 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +0000758}
759
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000760/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +0000761DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +0000762 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +0000763 LLVMContext &Ctx = DV->getContext();
764 // More then one inlined variable corresponds to one abstract variable.
765 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000766 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000767 if (AbsDbgVariable)
768 return AbsDbgVariable;
769
Devang Patel7e623022011-08-10 20:55:27 +0000770 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000771 if (!Scope)
772 return NULL;
773
Devang Patel99819b52011-08-15 19:01:20 +0000774 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patel7e623022011-08-10 20:55:27 +0000775 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000776 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000777 return AbsDbgVariable;
778}
779
Nick Lewycky019d2552011-07-29 03:49:23 +0000780/// addCurrentFnArgument - If Var is a current function argument then add
781/// it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +0000782bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +0000783 DbgVariable *Var, LexicalScope *Scope) {
784 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000785 return false;
786 DIVariable DV = Var->getVariable();
787 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
788 return false;
789 unsigned ArgNo = DV.getArgNumber();
790 if (ArgNo == 0)
791 return false;
792
Devang Patel4ab660b2011-03-03 20:02:02 +0000793 size_t Size = CurrentFnArguments.size();
794 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +0000795 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +0000796 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +0000797 // arguments does the function have at source level.
798 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +0000799 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +0000800 CurrentFnArguments[ArgNo - 1] = Var;
801 return true;
802}
803
Devang Patel490c8ab2010-05-20 19:57:06 +0000804/// collectVariableInfoFromMMITable - Collect variable information from
805/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000806void
Nick Lewycky019d2552011-07-29 03:49:23 +0000807DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patel490c8ab2010-05-20 19:57:06 +0000808 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +0000809 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
810 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
811 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +0000812 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000813 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +0000814 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +0000815 DIVariable DV(Var);
816 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000817
Devang Patel7e623022011-08-10 20:55:27 +0000818 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000819
Devang Patelcdb7d442009-11-10 23:20:04 +0000820 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +0000821 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +0000822 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000823
Devang Patele1c53f22010-05-20 16:36:41 +0000824 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel99819b52011-08-15 19:01:20 +0000825 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patel3e4a9652011-08-15 21:24:36 +0000826 RegVar->setFrameIndex(VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +0000827 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +0000828 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +0000829 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +0000830 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +0000831 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000832}
Devang Patela3e9c9c2010-03-15 18:33:46 +0000833
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000834/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +0000835/// DBG_VALUE instruction, is in a defined reg.
836static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
837 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000838 return MI->getNumOperands() == 3 &&
839 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
840 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000841}
842
Nick Lewycky019d2552011-07-29 03:49:23 +0000843/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel2442a892011-07-08 17:09:57 +0000844/// at MI.
845static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
846 const MCSymbol *FLabel,
847 const MCSymbol *SLabel,
848 const MachineInstr *MI) {
849 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
850
851 if (MI->getNumOperands() != 3) {
852 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
853 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
854 }
855 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
856 MachineLocation MLoc;
857 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
858 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
859 }
860 if (MI->getOperand(0).isImm())
861 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
862 if (MI->getOperand(0).isFPImm())
863 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
864 if (MI->getOperand(0).isCImm())
865 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
866
867 assert (0 && "Unexpected 3 operand DBG_VALUE instruction!");
868 return DotDebugLocEntry();
869}
870
Devang Patel7e623022011-08-10 20:55:27 +0000871/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000872void
Devang Patel5c0f85c2010-06-25 22:07:34 +0000873DwarfDebug::collectVariableInfo(const MachineFunction *MF,
874 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000875
Devang Patel490c8ab2010-05-20 19:57:06 +0000876 /// collection info from MMI table.
877 collectVariableInfoFromMMITable(MF, Processed);
878
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000879 for (SmallVectorImpl<const MDNode*>::const_iterator
880 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
881 ++UVI) {
882 const MDNode *Var = *UVI;
883 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +0000884 continue;
885
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000886 // History contains relevant DBG_VALUE instructions for Var and instructions
887 // clobbering it.
888 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
889 if (History.empty())
890 continue;
891 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +0000892
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000893 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +0000894 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +0000895 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
896 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +0000897 Scope = LScopes.getCurrentFunctionScope();
Devang Patel8fb9fd62011-07-20 22:18:50 +0000898 else {
899 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patel7e623022011-08-10 20:55:27 +0000900 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel8fb9fd62011-07-20 22:18:50 +0000901 else {
902 if (MDNode *IA = DV.getInlinedAt())
Devang Patel7e623022011-08-10 20:55:27 +0000903 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel8fb9fd62011-07-20 22:18:50 +0000904 else
Devang Patel7e623022011-08-10 20:55:27 +0000905 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel8fb9fd62011-07-20 22:18:50 +0000906 }
907 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000908 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +0000909 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +0000910 continue;
911
912 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000913 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +0000914 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
915 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel6c622ef2011-03-01 22:58:55 +0000916 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +0000917 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +0000918 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +0000919 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000920
921 // Simple ranges that are fully coalesced.
922 if (History.size() <= 1 || (History.size() == 2 &&
923 MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +0000924 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +0000925 continue;
926 }
927
928 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000929 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000930
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000931 for (SmallVectorImpl<const MachineInstr*>::const_iterator
932 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
933 const MachineInstr *Begin = *HI;
934 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +0000935
Devang Patele7181b52011-06-01 23:00:17 +0000936 // Check if DBG_VALUE is truncating a range.
937 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
938 && !Begin->getOperand(0).getReg())
939 continue;
940
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000941 // Compute the range for a register location.
942 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
943 const MCSymbol *SLabel = 0;
944
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000945 if (HI + 1 == HE)
946 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000947 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000948 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000949 else {
950 const MachineInstr *End = HI[1];
Devang Patel53b050a2011-07-07 21:44:42 +0000951 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
952 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000953 if (End->isDebugValue())
954 SLabel = getLabelBeforeInsn(End);
955 else {
956 // End is a normal instruction clobbering the range.
957 SLabel = getLabelAfterInsn(End);
958 assert(SLabel && "Forgot label after clobber instruction");
959 ++HI;
960 }
961 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000962
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000963 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel2442a892011-07-08 17:09:57 +0000964 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +0000965 }
966 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +0000967 }
Devang Patele0a94bf2010-05-14 21:01:35 +0000968
969 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +0000970 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
971 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
972 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
973 DIVariable DV(Variables.getElement(i));
974 if (!DV || !DV.Verify() || !Processed.insert(DV))
975 continue;
976 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
977 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patele0a94bf2010-05-14 21:01:35 +0000978 }
Devang Patel9fc11702010-05-25 23:40:22 +0000979}
Devang Patele0a94bf2010-05-14 21:01:35 +0000980
Devang Patel9fc11702010-05-25 23:40:22 +0000981/// getLabelBeforeInsn - Return Label preceding the instruction.
982const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000983 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
984 assert(Label && "Didn't insert label before instruction");
985 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +0000986}
987
988/// getLabelAfterInsn - Return Label immediately following the instruction.
989const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000990 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +0000991}
992
Devang Patelb5694e72010-10-26 17:49:02 +0000993/// beginInstruction - Process beginning of an instruction.
994void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +0000995 // Check if source location changes, but ignore DBG_VALUE locations.
996 if (!MI->isDebugValue()) {
997 DebugLoc DL = MI->getDebugLoc();
998 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel34a66202011-05-11 19:22:19 +0000999 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001000 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001001 if (DL == PrologEndLoc) {
1002 Flags |= DWARF2_FLAG_PROLOGUE_END;
1003 PrologEndLoc = DebugLoc();
1004 }
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001005 if (!DL.isUnknown()) {
1006 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001007 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001008 } else
Devang Patel34a66202011-05-11 19:22:19 +00001009 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001010 }
Devang Patel9fc11702010-05-25 23:40:22 +00001011 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001012
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001013 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001014 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1015 LabelsBeforeInsn.find(MI);
1016
1017 // No label needed.
1018 if (I == LabelsBeforeInsn.end())
1019 return;
1020
1021 // Label already assigned.
1022 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001023 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001024
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001025 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001026 PrevLabel = MMI->getContext().CreateTempSymbol();
1027 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001028 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001029 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001030}
1031
Devang Patelb5694e72010-10-26 17:49:02 +00001032/// endInstruction - Process end of an instruction.
1033void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001034 // Don't create a new label after DBG_VALUE instructions.
1035 // They don't generate code.
1036 if (!MI->isDebugValue())
1037 PrevLabel = 0;
1038
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001039 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1040 LabelsAfterInsn.find(MI);
1041
1042 // No label needed.
1043 if (I == LabelsAfterInsn.end())
1044 return;
1045
1046 // Label already assigned.
1047 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001048 return;
1049
1050 // We need a label after this instruction.
1051 if (!PrevLabel) {
1052 PrevLabel = MMI->getContext().CreateTempSymbol();
1053 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001054 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001055 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001056}
1057
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001058/// identifyScopeMarkers() -
Devang Patel784077e2011-08-10 23:58:09 +00001059/// Each LexicalScope has first instruction and last instruction to mark
1060/// beginning and end of a scope respectively. Create an inverse map that list
1061/// scopes starts (and ends) with an instruction. One instruction may start (or
1062/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001063void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001064 SmallVector<LexicalScope *, 4> WorkList;
1065 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001066 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001067 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001068
Devang Patel7e623022011-08-10 20:55:27 +00001069 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001070 if (!Children.empty())
Devang Patel7e623022011-08-10 20:55:27 +00001071 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001072 SE = Children.end(); SI != SE; ++SI)
1073 WorkList.push_back(*SI);
1074
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001075 if (S->isAbstractScope())
1076 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001077
Devang Patel7e623022011-08-10 20:55:27 +00001078 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001079 if (Ranges.empty())
1080 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001081 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +00001082 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001083 assert(RI->first && "InsnRange does not have first instruction!");
1084 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001085 requestLabelBeforeInsn(RI->first);
1086 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001087 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001088 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001089}
1090
Devang Patel589845d2011-05-09 22:14:49 +00001091/// getScopeNode - Get MDNode for DebugLoc's scope.
1092static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1093 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1094 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1095 return DL.getScope(Ctx);
1096}
1097
Devang Patel34a66202011-05-11 19:22:19 +00001098/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1099/// line number info for the function.
1100static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1101 const MDNode *Scope = getScopeNode(DL, Ctx);
1102 DISubprogram SP = getDISubprogram(Scope);
1103 if (SP.Verify())
1104 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1105 return DebugLoc();
1106}
1107
Devang Patel930143b2009-11-21 02:48:08 +00001108/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00001109/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00001110void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001111 if (!MMI->hasDebugInfo()) return;
Devang Patel7e623022011-08-10 20:55:27 +00001112 LScopes.initialize(*MF);
1113 if (LScopes.empty()) return;
1114 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001115
Devang Patel6c74a872010-04-27 19:46:33 +00001116 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1117 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001118 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001119 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001120
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001121 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1122
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001123 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001124 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1125 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1126
Devang Patel002d54d2010-05-26 19:37:24 +00001127 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001128 I != E; ++I) {
1129 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001130 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1131 II != IE; ++II) {
1132 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001133
Devang Patel002d54d2010-05-26 19:37:24 +00001134 if (MI->isDebugValue()) {
Devang Patel002d54d2010-05-26 19:37:24 +00001135 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001136
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001137 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001138 const MDNode *Var =
1139 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001140
1141 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001142 if (isDbgValueInDefinedReg(MI))
1143 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1144
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001145 // Check the history of this variable.
1146 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1147 if (History.empty()) {
1148 UserVariables.push_back(Var);
1149 // The first mention of a function argument gets the FunctionBeginSym
1150 // label, so arguments are visible when breaking at function entry.
1151 DIVariable DV(Var);
1152 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1153 DISubprogram(getDISubprogram(DV.getContext()))
1154 .describes(MF->getFunction()))
1155 LabelsBeforeInsn[MI] = FunctionBeginSym;
1156 } else {
1157 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1158 const MachineInstr *Prev = History.back();
1159 if (Prev->isDebugValue()) {
1160 // Coalesce identical entries at the end of History.
1161 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001162 Prev->isIdenticalTo(History[History.size() - 2])) {
1163 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1164 << "\t" << *Prev
1165 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001166 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001167 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001168
1169 // Terminate old register assignments that don't reach MI;
1170 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1171 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1172 isDbgValueInDefinedReg(Prev)) {
1173 // Previous register assignment needs to terminate at the end of
1174 // its basic block.
1175 MachineBasicBlock::const_iterator LastMI =
1176 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001177 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001178 // Drop DBG_VALUE for empty range.
Devang Patelb7a328e2011-07-07 00:14:27 +00001179 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1180 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001181 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001182 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001183 else {
1184 // Terminate after LastMI.
1185 History.push_back(LastMI);
1186 }
1187 }
1188 }
1189 }
1190 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001191 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001192 // Not a DBG_VALUE instruction.
1193 if (!MI->isLabel())
1194 AtBlockEntry = false;
1195
Devang Patel34a66202011-05-11 19:22:19 +00001196 // First known non DBG_VALUE location marks beginning of function
1197 // body.
1198 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1199 PrologEndLoc = MI->getDebugLoc();
1200
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001201 // Check if the instruction clobbers any registers with debug vars.
1202 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1203 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1204 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1205 continue;
1206 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1207 unsigned Reg = *AI; ++AI) {
1208 const MDNode *Var = LiveUserVar[Reg];
1209 if (!Var)
1210 continue;
1211 // Reg is now clobbered.
1212 LiveUserVar[Reg] = 0;
1213
1214 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001215 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1216 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001217 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001218 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1219 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001220 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001221 const MachineInstr *Prev = History.back();
1222 // Sanity-check: Register assignments are terminated at the end of
1223 // their block.
1224 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1225 continue;
1226 // Is the variable still in Reg?
1227 if (!isDbgValueInDefinedReg(Prev) ||
1228 Prev->getOperand(0).getReg() != Reg)
1229 continue;
1230 // Var is clobbered. Make sure the next instruction gets a label.
1231 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001232 }
1233 }
Devang Patel002d54d2010-05-26 19:37:24 +00001234 }
Devang Patel002d54d2010-05-26 19:37:24 +00001235 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001236 }
1237
1238 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1239 I != E; ++I) {
1240 SmallVectorImpl<const MachineInstr*> &History = I->second;
1241 if (History.empty())
1242 continue;
1243
1244 // Make sure the final register assignments are terminated.
1245 const MachineInstr *Prev = History.back();
1246 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1247 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel784077e2011-08-10 23:58:09 +00001248 MachineBasicBlock::const_iterator LastMI =
1249 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001250 if (LastMI == PrevMBB->end())
1251 // Drop DBG_VALUE for empty range.
1252 History.pop_back();
1253 else {
1254 // Terminate after LastMI.
1255 History.push_back(LastMI);
1256 }
1257 }
1258 // Request labels for the full history.
1259 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1260 const MachineInstr *MI = History[i];
1261 if (MI->isDebugValue())
1262 requestLabelBeforeInsn(MI);
1263 else
1264 requestLabelAfterInsn(MI);
1265 }
1266 }
Devang Patel002d54d2010-05-26 19:37:24 +00001267
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001268 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001269 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001270
1271 // Record beginning of function.
1272 if (!PrologEndLoc.isUnknown()) {
1273 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1274 MF->getFunction()->getContext());
1275 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1276 FnStartDL.getScope(MF->getFunction()->getContext()),
1277 DWARF2_FLAG_IS_STMT);
1278 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001279}
1280
Devang Patel7e623022011-08-10 20:55:27 +00001281void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1282// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1283 ScopeVariables[LS].push_back(Var);
1284// Vars.push_back(Var);
1285}
1286
Devang Patel930143b2009-11-21 02:48:08 +00001287/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00001288///
Chris Lattner76555b52010-01-26 23:18:02 +00001289void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patel7e623022011-08-10 20:55:27 +00001290 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00001291
Devang Patel7e623022011-08-10 20:55:27 +00001292 // Define end label for subprogram.
1293 FunctionEndSym = Asm->GetTempSymbol("func_end",
1294 Asm->getFunctionNumber());
1295 // Assumes in correct section after the entry point.
1296 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1297
1298 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1299 collectVariableInfo(MF, ProcessedVars);
1300
Devang Patel3acc70e2011-08-15 22:04:40 +00001301 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001302 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
1303 assert (TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001304
Devang Patel7e623022011-08-10 20:55:27 +00001305 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001306 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1307 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1308 LexicalScope *AScope = AList[i];
1309 DISubprogram SP(AScope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +00001310 if (SP.Verify()) {
1311 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001312 DIArray Variables = SP.getVariables();
1313 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1314 DIVariable DV(Variables.getElement(i));
1315 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1316 continue;
1317 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1318 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001319 }
1320 }
Devang Patel44403472011-08-12 18:10:19 +00001321 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Patel3acc70e2011-08-15 22:04:40 +00001322 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001323 }
Devang Patel7e623022011-08-10 20:55:27 +00001324
Devang Patel3acc70e2011-08-15 22:04:40 +00001325 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patel7e623022011-08-10 20:55:27 +00001326
Devang Patel3acc70e2011-08-15 22:04:40 +00001327 if (!DisableFramePointerElim(*MF))
Devang Patel784077e2011-08-10 23:58:09 +00001328 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1329 dwarf::DW_FORM_flag, 1);
Devang Patel3acc70e2011-08-15 22:04:40 +00001330
Devang Patel7e623022011-08-10 20:55:27 +00001331 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1332 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00001333
Bill Wendling2b128d72009-05-20 23:19:06 +00001334 // Clear debug info
Devang Patel7e623022011-08-10 20:55:27 +00001335 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1336 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1337 DeleteContainerPointers(I->second);
1338 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001339 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001340 UserVariables.clear();
1341 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001342 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001343 LabelsBeforeInsn.clear();
1344 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001345 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001346}
1347
Chris Lattnerba35a672010-03-09 04:54:43 +00001348/// recordSourceLine - Register a source line with debug info. Returns the
1349/// unique label that was emitted and which provides correspondence to
1350/// the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001351void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1352 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001353 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001354 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001355 unsigned Src = 1;
1356 if (S) {
1357 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001358
Dan Gohman50849c62010-05-05 23:41:32 +00001359 if (Scope.isCompileUnit()) {
1360 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001361 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001362 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001363 } else if (Scope.isFile()) {
1364 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001365 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001366 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001367 } else if (Scope.isSubprogram()) {
1368 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001369 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001370 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001371 } else if (Scope.isLexicalBlockFile()) {
1372 DILexicalBlockFile DBF(S);
1373 Fn = DBF.getFilename();
1374 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001375 } else if (Scope.isLexicalBlock()) {
1376 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001377 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001378 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001379 } else
1380 assert(0 && "Unexpected scope info");
1381
Devang Patele01b75c2011-03-24 20:30:50 +00001382 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00001383 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001384 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001385}
1386
Bill Wendling806535f2009-05-20 23:22:40 +00001387//===----------------------------------------------------------------------===//
1388// Emit Methods
1389//===----------------------------------------------------------------------===//
1390
Devang Patel930143b2009-11-21 02:48:08 +00001391/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00001392///
Jim Grosbach00e9c612009-11-22 19:20:36 +00001393unsigned
1394DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00001395 // Get the children.
1396 const std::vector<DIE *> &Children = Die->getChildren();
1397
1398 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001399 if (!Last && !Children.empty())
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001400 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling480ff322009-05-20 23:21:38 +00001401
1402 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001403 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001404
1405 // Get the abbreviation for this DIE.
1406 unsigned AbbrevNumber = Die->getAbbrevNumber();
1407 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1408
1409 // Set DIE offset
1410 Die->setOffset(Offset);
1411
1412 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001413 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001414
1415 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1416 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1417
1418 // Size the DIE attribute values.
1419 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1420 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001421 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001422
1423 // Size the DIE children if any.
1424 if (!Children.empty()) {
1425 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1426 "Children flag not set");
1427
1428 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001429 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00001430
1431 // End of children marker.
1432 Offset += sizeof(int8_t);
1433 }
1434
1435 Die->setSize(Offset - Die->getOffset());
1436 return Offset;
1437}
1438
Devang Patel930143b2009-11-21 02:48:08 +00001439/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00001440///
Devang Patel930143b2009-11-21 02:48:08 +00001441void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001442 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1443 E = CUMap.end(); I != E; ++I) {
1444 // Compute size of compile unit header.
Devang Patel28dce702011-04-12 23:10:47 +00001445 unsigned Offset =
Devang Patel1a0df9a2010-05-10 22:49:55 +00001446 sizeof(int32_t) + // Length of Compilation Unit Info
1447 sizeof(int16_t) + // DWARF version number
1448 sizeof(int32_t) + // Offset Into Abbrev. Section
1449 sizeof(int8_t); // Pointer Size (in bytes)
1450 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001451 }
Bill Wendling480ff322009-05-20 23:21:38 +00001452}
1453
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001454/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1455/// temporary label to it if SymbolStem is specified.
Chris Lattner6629ca92010-04-04 22:59:04 +00001456static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001457 const char *SymbolStem = 0) {
Chris Lattner6629ca92010-04-04 22:59:04 +00001458 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001459 if (!SymbolStem) return 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001460
Chris Lattner6629ca92010-04-04 22:59:04 +00001461 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1462 Asm->OutStreamer.EmitLabel(TmpSym);
1463 return TmpSym;
1464}
1465
1466/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1467/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00001468void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001469 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001470
Bill Wendling480ff322009-05-20 23:21:38 +00001471 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001472 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001473 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001474 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001475 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001476 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001477
Chris Lattner6629ca92010-04-04 22:59:04 +00001478 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001479 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001480
Devang Patel4a213872010-08-24 00:06:12 +00001481 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001482 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1483 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1484 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001485 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001486 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00001487 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1488 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001489
Devang Patel9fc11702010-05-25 23:40:22 +00001490 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1491 "section_debug_loc");
1492
Chris Lattner6629ca92010-04-04 22:59:04 +00001493 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00001494 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001495}
1496
Nick Lewycky019d2552011-07-29 03:49:23 +00001497/// emitDIE - Recursively emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00001498///
Devang Patel930143b2009-11-21 02:48:08 +00001499void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001500 // Get the abbreviation for this DIE.
1501 unsigned AbbrevNumber = Die->getAbbrevNumber();
1502 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1503
Bill Wendling480ff322009-05-20 23:21:38 +00001504 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001505 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00001506 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1507 Twine::utohexstr(Die->getOffset()) + ":0x" +
1508 Twine::utohexstr(Die->getSize()) + " " +
1509 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00001510 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001511
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001512 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00001513 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1514
1515 // Emit the DIE attribute values.
1516 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1517 unsigned Attr = AbbrevData[i].getAttribute();
1518 unsigned Form = AbbrevData[i].getForm();
1519 assert(Form && "Too many attributes for DIE (check abbreviation)");
1520
Chris Lattner7bde8c02010-04-04 18:52:31 +00001521 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001522 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001523
Bill Wendling480ff322009-05-20 23:21:38 +00001524 switch (Attr) {
1525 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00001526 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00001527 break;
1528 case dwarf::DW_AT_abstract_origin: {
1529 DIEEntry *E = cast<DIEEntry>(Values[i]);
1530 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001531 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00001532 Asm->EmitInt32(Addr);
1533 break;
1534 }
Devang Patel12563b32010-04-16 23:33:45 +00001535 case dwarf::DW_AT_ranges: {
1536 // DW_AT_range Value encodes offset in debug_range section.
1537 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00001538
1539 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1540 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1541 V->getValue(),
1542 4);
1543 } else {
1544 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1545 V->getValue(),
1546 DwarfDebugRangeSectionSym,
1547 4);
1548 }
Devang Patel12563b32010-04-16 23:33:45 +00001549 break;
1550 }
Devang Patel9fc11702010-05-25 23:40:22 +00001551 case dwarf::DW_AT_location: {
Devang Pateld8994442011-08-15 21:43:21 +00001552 if (DIELabel *L = dyn_cast<DIELabel>(Values[i]))
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001553 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Pateld8994442011-08-15 21:43:21 +00001554 else
Devang Patel9fc11702010-05-25 23:40:22 +00001555 Values[i]->EmitValue(Asm, Form);
1556 break;
1557 }
Devang Patela1bd5a12010-09-29 19:08:08 +00001558 case dwarf::DW_AT_accessibility: {
1559 if (Asm->isVerbose()) {
1560 DIEInteger *V = cast<DIEInteger>(Values[i]);
1561 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1562 }
1563 Values[i]->EmitValue(Asm, Form);
1564 break;
1565 }
Bill Wendling480ff322009-05-20 23:21:38 +00001566 default:
1567 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001568 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001569 break;
1570 }
Bill Wendling480ff322009-05-20 23:21:38 +00001571 }
1572
1573 // Emit the DIE children if any.
1574 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1575 const std::vector<DIE *> &Children = Die->getChildren();
1576
1577 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001578 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00001579
Chris Lattner7bde8c02010-04-04 18:52:31 +00001580 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00001581 Asm->OutStreamer.AddComment("End Of Children Mark");
1582 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001583 }
1584}
1585
Devang Patel9ccfb642009-12-09 18:24:21 +00001586/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00001587///
Devang Patel9ccfb642009-12-09 18:24:21 +00001588void DwarfDebug::emitDebugInfo() {
1589 // Start debug info section.
1590 Asm->OutStreamer.SwitchSection(
1591 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001592 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1593 E = CUMap.end(); I != E; ++I) {
1594 CompileUnit *TheCU = I->second;
1595 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001596
Devang Patel1a0df9a2010-05-10 22:49:55 +00001597 // Emit the compile units header.
1598 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1599 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001600
Devang Patel1a0df9a2010-05-10 22:49:55 +00001601 // Emit size of content not including length itself
1602 unsigned ContentSize = Die->getSize() +
1603 sizeof(int16_t) + // DWARF version number
1604 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00001605 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001606
Devang Patel1a0df9a2010-05-10 22:49:55 +00001607 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1608 Asm->EmitInt32(ContentSize);
1609 Asm->OutStreamer.AddComment("DWARF version number");
1610 Asm->EmitInt16(dwarf::DWARF_VERSION);
1611 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1612 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1613 DwarfAbbrevSectionSym);
1614 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1615 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001616
Devang Patel1a0df9a2010-05-10 22:49:55 +00001617 emitDIE(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001618 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1619 }
Bill Wendling480ff322009-05-20 23:21:38 +00001620}
1621
Devang Patel930143b2009-11-21 02:48:08 +00001622/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00001623///
Devang Patel930143b2009-11-21 02:48:08 +00001624void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00001625 // Check to see if it is worth the effort.
1626 if (!Abbreviations.empty()) {
1627 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001628 Asm->OutStreamer.SwitchSection(
1629 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001630
Chris Lattnera179b522010-04-04 19:25:43 +00001631 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00001632
1633 // For each abbrevation.
1634 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1635 // Get abbreviation data
1636 const DIEAbbrev *Abbrev = Abbreviations[i];
1637
1638 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00001639 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00001640
1641 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001642 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00001643 }
1644
1645 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00001646 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00001647
Chris Lattnera179b522010-04-04 19:25:43 +00001648 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00001649 }
1650}
1651
Devang Patel930143b2009-11-21 02:48:08 +00001652/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00001653/// the line matrix.
1654///
Devang Patel930143b2009-11-21 02:48:08 +00001655void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00001656 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00001657 Asm->OutStreamer.AddComment("Extended Op");
1658 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001659
Chris Lattner566cae92010-03-09 23:52:58 +00001660 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00001661 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00001662 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1663 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1664
1665 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00001666
Chris Lattnera179b522010-04-04 19:25:43 +00001667 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00001668 Asm->getTargetData().getPointerSize(),
1669 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00001670
1671 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00001672 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1673 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001674 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00001675 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00001676}
1677
Devang Patel9ccfb642009-12-09 18:24:21 +00001678/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1679///
1680void DwarfDebug::emitDebugPubNames() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001681 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1682 E = CUMap.end(); I != E; ++I) {
1683 CompileUnit *TheCU = I->second;
1684 // Start the dwarf pubnames section.
1685 Asm->OutStreamer.SwitchSection(
1686 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001687
Devang Patel1a0df9a2010-05-10 22:49:55 +00001688 Asm->OutStreamer.AddComment("Length of Public Names Info");
1689 Asm->EmitLabelDifference(
1690 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1691 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001692
Devang Patel1a0df9a2010-05-10 22:49:55 +00001693 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1694 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001695
Devang Patel1a0df9a2010-05-10 22:49:55 +00001696 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001697 Asm->EmitInt16(dwarf::DWARF_VERSION);
1698
Devang Patel1a0df9a2010-05-10 22:49:55 +00001699 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001700 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00001701 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001702
Devang Patel1a0df9a2010-05-10 22:49:55 +00001703 Asm->OutStreamer.AddComment("Compilation Unit Length");
1704 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1705 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1706 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001707
Devang Patel1a0df9a2010-05-10 22:49:55 +00001708 const StringMap<DIE*> &Globals = TheCU->getGlobals();
1709 for (StringMap<DIE*>::const_iterator
1710 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1711 const char *Name = GI->getKeyData();
1712 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001713
Devang Patel1a0df9a2010-05-10 22:49:55 +00001714 Asm->OutStreamer.AddComment("DIE offset");
1715 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001716
Devang Patel1a0df9a2010-05-10 22:49:55 +00001717 if (Asm->isVerbose())
1718 Asm->OutStreamer.AddComment("External Name");
1719 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
1720 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001721
Devang Patel1a0df9a2010-05-10 22:49:55 +00001722 Asm->OutStreamer.AddComment("End Mark");
1723 Asm->EmitInt32(0);
1724 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky019d2552011-07-29 03:49:23 +00001725 TheCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00001726 }
Bill Wendling480ff322009-05-20 23:21:38 +00001727}
1728
Devang Patel04d2f2d2009-11-24 01:14:22 +00001729void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001730 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1731 E = CUMap.end(); I != E; ++I) {
1732 CompileUnit *TheCU = I->second;
1733 // Start the dwarf pubnames section.
1734 Asm->OutStreamer.SwitchSection(
1735 Asm->getObjFileLowering().getDwarfPubTypesSection());
1736 Asm->OutStreamer.AddComment("Length of Public Types Info");
1737 Asm->EmitLabelDifference(
1738 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1739 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001740
Devang Patel1a0df9a2010-05-10 22:49:55 +00001741 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1742 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001743
Devang Patel1a0df9a2010-05-10 22:49:55 +00001744 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1745 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001746
Devang Patel1a0df9a2010-05-10 22:49:55 +00001747 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1748 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1749 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001750
Devang Patel1a0df9a2010-05-10 22:49:55 +00001751 Asm->OutStreamer.AddComment("Compilation Unit Length");
1752 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1753 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1754 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001755
Devang Patel1a0df9a2010-05-10 22:49:55 +00001756 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1757 for (StringMap<DIE*>::const_iterator
1758 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1759 const char *Name = GI->getKeyData();
Nick Lewycky019d2552011-07-29 03:49:23 +00001760 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001761
Devang Patel1a0df9a2010-05-10 22:49:55 +00001762 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1763 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001764
Devang Patel1a0df9a2010-05-10 22:49:55 +00001765 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
1766 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1767 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001768
Devang Patel1a0df9a2010-05-10 22:49:55 +00001769 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001770 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001771 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1772 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00001773 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001774}
1775
Devang Patel930143b2009-11-21 02:48:08 +00001776/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00001777///
Devang Patel930143b2009-11-21 02:48:08 +00001778void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00001779 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00001780 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001781
Chris Lattner3d72a672010-03-09 23:38:23 +00001782 // Start the dwarf str section.
1783 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001784 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001785
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001786 // Get all of the string pool entries and put them in an array by their ID so
1787 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001788 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001789 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001790
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001791 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1792 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1793 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001794
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001795 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001796
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001797 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00001798 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001799 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001800
Chris Lattner3d72a672010-03-09 23:38:23 +00001801 // Emit the string itself.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001802 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00001803 }
1804}
1805
Devang Patel930143b2009-11-21 02:48:08 +00001806/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00001807///
Devang Patel930143b2009-11-21 02:48:08 +00001808void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001809 if (DotDebugLocEntries.empty())
1810 return;
1811
Devang Patel116a9d72011-02-04 22:57:18 +00001812 for (SmallVector<DotDebugLocEntry, 4>::iterator
1813 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
1814 I != E; ++I) {
1815 DotDebugLocEntry &Entry = *I;
1816 if (I + 1 != DotDebugLocEntries.end())
1817 Entry.Merge(I+1);
1818 }
1819
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001820 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001821 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00001822 Asm->getObjFileLowering().getDwarfLocSection());
1823 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001824 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
1825 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001826 for (SmallVector<DotDebugLocEntry, 4>::iterator
1827 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00001828 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00001829 DotDebugLocEntry &Entry = *I;
1830 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00001831 if (Entry.isEmpty()) {
1832 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
1833 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001834 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00001835 } else {
1836 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
1837 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patel3e021532011-04-28 02:22:40 +00001838 DIVariable DV(Entry.Variable);
Rafael Espindolad23bfb82011-05-27 22:05:41 +00001839 Asm->OutStreamer.AddComment("Loc expr size");
1840 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
1841 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
1842 Asm->EmitLabelDifference(end, begin, 2);
1843 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00001844 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00001845 DIBasicType BTy(DV.getType());
1846 if (BTy.Verify() &&
1847 (BTy.getEncoding() == dwarf::DW_ATE_signed
1848 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
1849 Asm->OutStreamer.AddComment("DW_OP_consts");
1850 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00001851 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00001852 } else {
1853 Asm->OutStreamer.AddComment("DW_OP_constu");
1854 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00001855 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00001856 }
Devang Pateled9fd452011-07-08 16:49:43 +00001857 } else if (Entry.isLocation()) {
1858 if (!DV.hasComplexAddress())
1859 // Regular entry.
Devang Patel3e021532011-04-28 02:22:40 +00001860 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Pateled9fd452011-07-08 16:49:43 +00001861 else {
1862 // Complex address entry.
1863 unsigned N = DV.getNumAddrElements();
1864 unsigned i = 0;
1865 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
1866 if (Entry.Loc.getOffset()) {
1867 i = 2;
1868 Asm->EmitDwarfRegOp(Entry.Loc);
1869 Asm->OutStreamer.AddComment("DW_OP_deref");
1870 Asm->EmitInt8(dwarf::DW_OP_deref);
1871 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
1872 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1873 Asm->EmitSLEB128(DV.getAddrElement(1));
1874 } else {
1875 // If first address element is OpPlus then emit
1876 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
1877 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
1878 Asm->EmitDwarfRegOp(Loc);
1879 i = 2;
1880 }
1881 } else {
1882 Asm->EmitDwarfRegOp(Entry.Loc);
1883 }
1884
1885 // Emit remaining complex address elements.
1886 for (; i < N; ++i) {
1887 uint64_t Element = DV.getAddrElement(i);
1888 if (Element == DIBuilder::OpPlus) {
1889 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1890 Asm->EmitULEB128(DV.getAddrElement(++i));
1891 } else if (Element == DIBuilder::OpDeref)
1892 Asm->EmitInt8(dwarf::DW_OP_deref);
1893 else llvm_unreachable("unknown Opcode found in complex address");
1894 }
Devang Patel3e021532011-04-28 02:22:40 +00001895 }
Devang Patel3e021532011-04-28 02:22:40 +00001896 }
Devang Pateled9fd452011-07-08 16:49:43 +00001897 // else ... ignore constant fp. There is not any good way to
1898 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00001899 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00001900 }
1901 }
Bill Wendling480ff322009-05-20 23:21:38 +00001902}
1903
1904/// EmitDebugARanges - Emit visible names into a debug aranges section.
1905///
1906void DwarfDebug::EmitDebugARanges() {
1907 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001908 Asm->OutStreamer.SwitchSection(
1909 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001910}
1911
Devang Patel930143b2009-11-21 02:48:08 +00001912/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00001913///
Devang Patel930143b2009-11-21 02:48:08 +00001914void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00001915 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001916 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00001917 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00001918 unsigned char Size = Asm->getTargetData().getPointerSize();
1919 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001920 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00001921 I != E; ++I) {
1922 if (*I)
1923 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00001924 else
Devang Patel6c74a872010-04-27 19:46:33 +00001925 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00001926 }
Bill Wendling480ff322009-05-20 23:21:38 +00001927}
1928
Devang Patel930143b2009-11-21 02:48:08 +00001929/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00001930///
Devang Patel930143b2009-11-21 02:48:08 +00001931void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001932 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00001933 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00001934 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001935 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001936 }
1937}
1938
Devang Patel930143b2009-11-21 02:48:08 +00001939/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00001940/// Section Header:
1941/// 1. length of section
1942/// 2. Dwarf version number
1943/// 3. address size.
1944///
1945/// Entries (one "entry" for each function that was inlined):
1946///
1947/// 1. offset into __debug_str section for MIPS linkage name, if exists;
1948/// otherwise offset into __debug_str for regular function name.
1949/// 2. offset into __debug_str section for regular function name.
1950/// 3. an unsigned LEB128 number indicating the number of distinct inlining
1951/// instances for the function.
1952///
1953/// The rest of the entry consists of a {die_offset, low_pc} pair for each
1954/// inlined instance; the die_offset points to the inlined_subroutine die in the
1955/// __debug_info section, and the low_pc is the starting address for the
1956/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00001957void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00001958 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00001959 return;
1960
Devang Patel1a0df9a2010-05-10 22:49:55 +00001961 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00001962 return;
1963
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001964 Asm->OutStreamer.SwitchSection(
1965 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001966
Chris Lattner566cae92010-03-09 23:52:58 +00001967 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00001968 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
1969 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00001970
Chris Lattnera179b522010-04-04 19:25:43 +00001971 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00001972
Chris Lattner566cae92010-03-09 23:52:58 +00001973 Asm->OutStreamer.AddComment("Dwarf Version");
1974 Asm->EmitInt16(dwarf::DWARF_VERSION);
1975 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00001976 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00001977
Devang Patel32cc43c2010-05-07 20:54:48 +00001978 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001979 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00001980
Devang Patel32cc43c2010-05-07 20:54:48 +00001981 const MDNode *Node = *I;
1982 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00001983 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001984 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00001985 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00001986 StringRef LName = SP.getLinkageName();
1987 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00001988
Chris Lattner566cae92010-03-09 23:52:58 +00001989 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00001990 if (LName.empty()) {
1991 Asm->OutStreamer.EmitBytes(Name, 0);
1992 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001993 } else
Chris Lattner70a4fce2010-04-04 23:25:33 +00001994 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
1995 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001996
Chris Lattner566cae92010-03-09 23:52:58 +00001997 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00001998 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00001999 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002000
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002001 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002002 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002003 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002004 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002005
Chris Lattner7bde8c02010-04-04 18:52:31 +00002006 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002007 Asm->OutStreamer.EmitSymbolValue(LI->first,
2008 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00002009 }
2010 }
2011
Chris Lattnera179b522010-04-04 19:25:43 +00002012 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002013}