blob: b022c430ac640408753a1787cdebd6e0cc055ea1 [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
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000139MCSymbol *DwarfDebug::getStringPool() {
140 return Asm->GetTempSymbol("section_str");
141}
142
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000143MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
144 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
145 if (Entry.first) return Entry.first;
146
147 Entry.second = NextStringPoolNumber++;
Chris Lattnera179b522010-04-04 19:25:43 +0000148 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000149}
150
151
Devang Patel930143b2009-11-21 02:48:08 +0000152/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000153///
Devang Patel930143b2009-11-21 02:48:08 +0000154void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000155 // Profile the node so that we can make it unique.
156 FoldingSetNodeID ID;
157 Abbrev.Profile(ID);
158
159 // Check the set for priors.
160 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
161
162 // If it's newly added.
163 if (InSet == &Abbrev) {
164 // Add to abbreviation list.
165 Abbreviations.push_back(&Abbrev);
166
167 // Assign the vector position + 1 as its number.
168 Abbrev.setNumber(Abbreviations.size());
169 } else {
170 // Assign existing abbreviation number.
171 Abbrev.setNumber(InSet->getNumber());
172 }
173}
174
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000175/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel43ef34d2010-01-05 01:46:14 +0000176/// printer to not emit usual symbol prefix before the symbol name is used then
177/// return linkage name after skipping this special LLVM prefix.
178static StringRef getRealLinkageName(StringRef LinkageName) {
179 char One = '\1';
180 if (LinkageName.startswith(StringRef(&One, 1)))
181 return LinkageName.substr(1);
182 return LinkageName;
183}
184
Jim Grosbach042483e2009-11-21 23:12:12 +0000185/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +0000186/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
187/// If there are global variables in this scope then create and insert
188/// DIEs for these variables.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000189DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
190 const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000191 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +0000192
Chris Lattner3a383cb2010-04-05 00:13:49 +0000193 assert(SPDie && "Unable to find subprogram DIE!");
194 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000195
Devang Patel1d6bbd42011-04-22 23:10:17 +0000196 DISubprogram SPDecl = SP.getFunctionDeclaration();
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000197 if (!SPDecl.isSubprogram()) {
Devang Patel1d6bbd42011-04-22 23:10:17 +0000198 // 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())) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000206 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel1d6bbd42011-04-22 23:10:17 +0000207
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 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
252 if (Scope->isAbstractScope())
253 return ScopeDIE;
254
Devang Patel7e623022011-08-10 20:55:27 +0000255 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +0000256 if (Ranges.empty())
257 return 0;
258
Devang Patel7e623022011-08-10 20:55:27 +0000259 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel6c74a872010-04-27 19:46:33 +0000260 if (Ranges.size() > 1) {
261 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000262 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000263 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000264 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000265 DebugRangeSymbols.size()
266 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000267 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +0000268 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000269 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
270 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000271 }
272 DebugRangeSymbols.push_back(NULL);
273 DebugRangeSymbols.push_back(NULL);
274 return ScopeDIE;
275 }
276
Devang Patel9fc11702010-05-25 23:40:22 +0000277 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
278 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000279
Devang Patel9fc11702010-05-25 23:40:22 +0000280 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000281
Chris Lattnere13c3722010-03-09 01:58:53 +0000282 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
283 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000284
Devang Patelf20c4f72011-04-12 22:53:02 +0000285 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
286 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000287
288 return ScopeDIE;
289}
290
Devang Patel930143b2009-11-21 02:48:08 +0000291/// constructInlinedScopeDIE - This scope represents inlined body of
292/// a function. Construct DIE to represent this concrete inlined copy
293/// of the function.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000294DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
295 LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +0000296 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000297 assert(Ranges.empty() == false &&
298 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000299
Devang Patelf098ce22011-07-27 00:34:13 +0000300 if (!Scope->getScopeNode())
301 return NULL;
302 DIScope DS(Scope->getScopeNode());
303 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patelf098ce22011-07-27 00:34:13 +0000304 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
305 if (!OriginDIE) {
306 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
307 return NULL;
308 }
309
Devang Patel7e623022011-08-10 20:55:27 +0000310 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +0000311 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
312 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000313
Devang Patel4c6bd662010-07-08 22:39:20 +0000314 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000315 assert(0 && "Unexpected Start and End labels for a inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000316 return 0;
317 }
Chris Lattnere13c3722010-03-09 01:58:53 +0000318 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000319 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +0000320 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000321 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000322
Devang Patel73bc1722011-05-05 17:54:26 +0000323 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patelf20c4f72011-04-12 22:53:02 +0000324 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
325 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000326
Devang Patelf098ce22011-07-27 00:34:13 +0000327 if (Ranges.size() > 1) {
328 // .debug_range section has not been laid out yet. Emit offset in
329 // .debug_range as a uint, size 4, for now. emitDIE will handle
330 // DW_AT_ranges appropriately.
331 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000332 DebugRangeSymbols.size()
333 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000334 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patelf098ce22011-07-27 00:34:13 +0000335 RE = Ranges.end(); RI != RE; ++RI) {
336 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
337 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
338 }
339 DebugRangeSymbols.push_back(NULL);
340 DebugRangeSymbols.push_back(NULL);
341 } else {
Devang Patel784077e2011-08-10 23:58:09 +0000342 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
343 StartLabel);
344 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
345 EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000346 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000347
348 InlinedSubprogramDIEs.insert(OriginDIE);
349
350 // Track the start label for this inlined function.
Devang Patelf098ce22011-07-27 00:34:13 +0000351 //.debug_inlined section specification does not clearly state how
352 // to emit inlined scope that is split into multiple instruction ranges.
353 // For now, use first instruction range and emit low_pc/high_pc pair and
354 // corresponding .debug_inlined section entry for this pair.
Devang Patel32cc43c2010-05-07 20:54:48 +0000355 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000356 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000357
358 if (I == InlineInfo.end()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000359 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000360 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000361 } else
Chris Lattner085b6522010-03-09 00:31:02 +0000362 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000363
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000364 DILocation DL(Scope->getInlinedAt());
Devang Patelf20c4f72011-04-12 22:53:02 +0000365 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
366 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000367
368 return ScopeDIE;
369}
370
Devang Patel930143b2009-11-21 02:48:08 +0000371/// constructScopeDIE - Construct a DIE for this scope.
Devang Patel3acc70e2011-08-15 22:04:40 +0000372DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000373 if (!Scope || !Scope->getScopeNode())
374 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000375
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000376 SmallVector<DIE *, 8> Children;
Devang Patel6c622ef2011-03-01 22:58:55 +0000377
378 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000379 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000380 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
381 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Patel3acc70e2011-08-15 22:04:40 +0000382 if (DIE *Arg =
383 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel6c622ef2011-03-01 22:58:55 +0000384 Children.push_back(Arg);
385
Eric Christopherf84354b2011-10-03 15:49:16 +0000386 // Collect lexical scope children first.
Devang Patel7e623022011-08-10 20:55:27 +0000387 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000388 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Patel3acc70e2011-08-15 22:04:40 +0000389 if (DIE *Variable =
390 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000391 Children.push_back(Variable);
Devang Patel7e623022011-08-10 20:55:27 +0000392 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000393 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000394 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000395 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000396 DIScope DS(Scope->getScopeNode());
397 DIE *ScopeDIE = NULL;
398 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000399 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000400 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000401 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000402 if (Scope->isAbstractScope()) {
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000403 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000404 // Note down abstract DIE.
405 if (ScopeDIE)
406 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
407 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000408 else
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000409 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000410 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000411 else {
412 // There is no need to emit empty lexical block DIE.
413 if (Children.empty())
414 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000415 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000416 }
417
Devang Patel23b2ae62010-03-29 22:59:58 +0000418 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000419
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000420 // Add children
421 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
422 E = Children.end(); I != E; ++I)
423 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000424
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000425 if (DS.isSubprogram())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000426 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000427
Devang Patel04d2f2d2009-11-24 01:14:22 +0000428 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000429}
430
Bill Wendling2b128d72009-05-20 23:19:06 +0000431/// GetOrCreateSourceID - Look up the source id with the given directory and
432/// source file names. If none currently exists, create a new id and insert it
433/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
434/// maps as well.
Devang Patele01b75c2011-03-24 20:30:50 +0000435unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
436 StringRef DirName) {
Devang Patel871d0b12010-09-16 20:57:49 +0000437 // If FE did not provide a file name, then assume stdin.
438 if (FileName.empty())
Devang Patele01b75c2011-03-24 20:30:50 +0000439 return GetOrCreateSourceID("<stdin>", StringRef());
440
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000441 // TODO: this might not belong here. See if we can factor this better.
442 if (DirName == CompilationDir)
443 DirName = "";
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();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000469 CompilationDir = DIUnit.getDirectory();
470 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
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);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000474 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000475 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
476 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000477 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Devang Patelbd798ce2010-04-26 22:54:28 +0000478 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
479 // simplifies debug range entries.
Devang Patelf20c4f72011-04-12 22:53:02 +0000480 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +0000481 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +0000482 // compile unit in debug_line section.
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000483 if (Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindolaa7558912011-05-04 17:44:06 +0000484 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patelf20c4f72011-04-12 22:53:02 +0000485 Asm->GetTempSymbol("section_line"));
Devang Patelea636392010-08-31 23:50:19 +0000486 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000487 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +0000488
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000489 if (!CompilationDir.empty())
490 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000491 if (DIUnit.isOptimized())
Devang Patelf20c4f72011-04-12 22:53:02 +0000492 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +0000493
Devang Patel2d9caf92009-11-25 17:36:49 +0000494 StringRef Flags = DIUnit.getFlags();
495 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000496 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patelf20c4f72011-04-12 22:53:02 +0000497
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000498 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000499 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000500 dwarf::DW_FORM_data1, RVer);
501
Devang Patel1a0df9a2010-05-10 22:49:55 +0000502 if (!FirstCU)
503 FirstCU = NewCU;
504 CUMap.insert(std::make_pair(N, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000505 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000506}
507
Devang Patel1a0df9a2010-05-10 22:49:55 +0000508/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel1f4f98d2011-08-15 23:36:40 +0000509void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
510 const MDNode *N) {
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000511 CompileUnit *&CURef = SPMap[N];
512 if (CURef)
513 return;
514 CURef = TheCU;
515
Devang Patel80ae3492009-08-28 23:24:31 +0000516 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000517 if (!SP.isDefinition())
518 // This is a method declaration which will be handled while constructing
519 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000520 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000521
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000522 DISubprogram SPDecl = SP.getFunctionDeclaration();
523 DIE *DeclDie = NULL;
524 if (SPDecl.isSubprogram()) {
525 DeclDie = TheCU->getOrCreateSubprogramDIE(SPDecl);
526 }
527
Devang Patel89543712011-08-15 17:24:54 +0000528 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000529
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000530 if (DeclDie) {
531 // Refer function declaration directly.
532 TheCU->addDIEEntry(SubprogramDie, dwarf::DW_AT_specification,
533 dwarf::DW_FORM_ref4, DeclDie);
534 }
535
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000536 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000537 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000538
539 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +0000540 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +0000541
Bill Wendling2b128d72009-05-20 23:19:06 +0000542 // Expose as global.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000543 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000544
Devang Patel0751a282009-06-26 01:49:18 +0000545 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000546}
547
Devang Patel07bb9ee2011-08-15 23:47:24 +0000548/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
549/// as llvm.dbg.enum and llvm.dbg.ty
550void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000551 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
552 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
553 const MDNode *N = NMD->getOperand(i);
554 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
555 constructSubprogramDIE(CU, N);
556 }
557
558 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
559 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
560 const MDNode *N = NMD->getOperand(i);
561 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000562 CU->createGlobalVariableDIE(N);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000563 }
564
Devang Patel07bb9ee2011-08-15 23:47:24 +0000565 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
566 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
567 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000568 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
569 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000570 }
571
572 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
573 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
574 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000575 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
576 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000577 }
578}
579
580/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
581/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
582bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
583 DebugInfoFinder DbgFinder;
584 DbgFinder.processModule(*M);
585
586 bool HasDebugInfo = false;
587 // Scan all the compile-units to see if there are any marked as the main
588 // unit. If not, we do not generate debug info.
589 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
590 E = DbgFinder.compile_unit_end(); I != E; ++I) {
591 if (DICompileUnit(*I).isMain()) {
592 HasDebugInfo = true;
593 break;
594 }
595 }
596 if (!HasDebugInfo) return false;
597
598 // Create all the compile unit DIEs.
599 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
600 E = DbgFinder.compile_unit_end(); I != E; ++I)
601 constructCompileUnit(*I);
602
603 // Create DIEs for each global variable.
604 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
605 E = DbgFinder.global_variable_end(); I != E; ++I) {
606 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000607 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000608 CU->createGlobalVariableDIE(N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000609 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000610
Devang Patel07bb9ee2011-08-15 23:47:24 +0000611 // Create DIEs for each subprogram.
612 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
613 E = DbgFinder.subprogram_end(); I != E; ++I) {
614 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000615 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
616 constructSubprogramDIE(CU, N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000617 }
618
619 return HasDebugInfo;
620}
621
Devang Patel930143b2009-11-21 02:48:08 +0000622/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000623/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky019d2552011-07-29 03:49:23 +0000624/// This is invoked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +0000625void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +0000626 if (DisableDebugInfoPrinting)
627 return;
628
Nick Lewycky019d2552011-07-29 03:49:23 +0000629 // If module has named metadata anchors then use them, otherwise scan the
630 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000631 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
632 if (CU_Nodes) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000633 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
634 DICompileUnit CUNode(CU_Nodes->getOperand(i));
635 CompileUnit *CU = constructCompileUnit(CUNode);
636 DIArray GVs = CUNode.getGlobalVariables();
637 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000638 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000639 DIArray SPs = CUNode.getSubprograms();
640 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
641 constructSubprogramDIE(CU, SPs.getElement(i));
642 DIArray EnumTypes = CUNode.getEnumTypes();
643 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
644 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
645 DIArray RetainedTypes = CUNode.getRetainedTypes();
646 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
647 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
648 }
Devang Patel07bb9ee2011-08-15 23:47:24 +0000649 } else if (!collectLegacyDebugInfo(M))
650 return;
Devang Patele02e5852011-05-03 16:45:22 +0000651
Devang Patel07bb9ee2011-08-15 23:47:24 +0000652 collectInfoFromNamedMDNodes(M);
Devang Patele02e5852011-05-03 16:45:22 +0000653
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000654 // Tell MMI that we have debug info.
655 MMI->setDebugInfoAvailability(true);
Devang Patele02e5852011-05-03 16:45:22 +0000656
Chris Lattnerd442aa32010-04-04 23:17:54 +0000657 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000658 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000659
Bill Wendling2b128d72009-05-20 23:19:06 +0000660 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +0000661 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +0000662}
663
Devang Patel930143b2009-11-21 02:48:08 +0000664/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +0000665///
Devang Patel930143b2009-11-21 02:48:08 +0000666void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000667 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +0000668 const Module *M = MMI->getModule();
Devang Patel7e623022011-08-10 20:55:27 +0000669 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +0000670
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000671 // Collect info for variables that were optimized out.
672 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
673 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
674 DICompileUnit TheCU(CU_Nodes->getOperand(i));
675 DIArray Subprograms = TheCU.getSubprograms();
676 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
677 DISubprogram SP(Subprograms.getElement(i));
678 if (ProcessedSPNodes.count(SP) != 0) continue;
679 if (!SP.Verify()) continue;
680 if (!SP.isDefinition()) continue;
Devang Patel59e27c52011-08-19 23:28:12 +0000681 DIArray Variables = SP.getVariables();
682 if (Variables.getNumElements() == 0) continue;
683
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000684 LexicalScope *Scope =
685 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
686 DeadFnScopeMap[SP] = Scope;
687
688 // Construct subprogram DIE and add variables DIEs.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000689 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000690 assert(SPCU && "Unable to find Compile Unit!");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000691 constructSubprogramDIE(SPCU, SP);
692 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel59e27c52011-08-19 23:28:12 +0000693 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
694 DIVariable DV(Variables.getElement(vi));
695 if (!DV.Verify()) continue;
696 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000697 if (DIE *VariableDIE =
Devang Patel59e27c52011-08-19 23:28:12 +0000698 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000699 ScopeDIE->addChild(VariableDIE);
700 }
Devang Patelf3b2db62010-06-28 18:25:03 +0000701 }
702 }
703 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000704
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000705 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
706 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
707 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
708 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +0000709 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000710 }
711
Devang Patel89543712011-08-15 17:24:54 +0000712 // Emit DW_AT_containing_type attribute to connect types with their
713 // vtable holding type.
714 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
715 CUE = CUMap.end(); CUI != CUE; ++CUI) {
716 CompileUnit *TheCU = CUI->second;
717 TheCU->constructContainingTypeDIEs();
Devang Pateleb57c592009-12-03 19:11:07 +0000718 }
719
Bill Wendling2b128d72009-05-20 23:19:06 +0000720 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000721 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000722 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000723 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000724 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +0000725
726 // End text sections.
727 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000728 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +0000729 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +0000730 }
731
Bill Wendling2b128d72009-05-20 23:19:06 +0000732 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +0000733 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +0000734
735 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +0000736 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000737
738 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +0000739 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +0000740
Bill Wendling2b128d72009-05-20 23:19:06 +0000741 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +0000742 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +0000743
Devang Patel04d2f2d2009-11-24 01:14:22 +0000744 // Emit info into a debug pubtypes section.
745 emitDebugPubTypes();
746
Bill Wendling2b128d72009-05-20 23:19:06 +0000747 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +0000748 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +0000749
750 // Emit info into a debug aranges section.
751 EmitDebugARanges();
752
753 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +0000754 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +0000755
756 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +0000757 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000758
759 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +0000760 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000761
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000762 // Emit info into a debug str section.
763 emitDebugStr();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000764
Devang Pateld0701282010-08-02 17:32:15 +0000765 // clean up.
766 DeleteContainerSeconds(DeadFnScopeMap);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000767 SPMap.clear();
Devang Patel1a0df9a2010-05-10 22:49:55 +0000768 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
769 E = CUMap.end(); I != E; ++I)
770 delete I->second;
771 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +0000772}
773
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000774/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +0000775DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +0000776 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +0000777 LLVMContext &Ctx = DV->getContext();
778 // More then one inlined variable corresponds to one abstract variable.
779 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000780 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000781 if (AbsDbgVariable)
782 return AbsDbgVariable;
783
Devang Patel7e623022011-08-10 20:55:27 +0000784 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000785 if (!Scope)
786 return NULL;
787
Devang Patel99819b52011-08-15 19:01:20 +0000788 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patel7e623022011-08-10 20:55:27 +0000789 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000790 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000791 return AbsDbgVariable;
792}
793
Nick Lewycky019d2552011-07-29 03:49:23 +0000794/// addCurrentFnArgument - If Var is a current function argument then add
795/// it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +0000796bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +0000797 DbgVariable *Var, LexicalScope *Scope) {
798 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000799 return false;
800 DIVariable DV = Var->getVariable();
801 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
802 return false;
803 unsigned ArgNo = DV.getArgNumber();
804 if (ArgNo == 0)
805 return false;
806
Devang Patel4ab660b2011-03-03 20:02:02 +0000807 size_t Size = CurrentFnArguments.size();
808 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +0000809 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +0000810 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +0000811 // arguments does the function have at source level.
812 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +0000813 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +0000814 CurrentFnArguments[ArgNo - 1] = Var;
815 return true;
816}
817
Devang Patel490c8ab2010-05-20 19:57:06 +0000818/// collectVariableInfoFromMMITable - Collect variable information from
819/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000820void
Nick Lewycky019d2552011-07-29 03:49:23 +0000821DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patel490c8ab2010-05-20 19:57:06 +0000822 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +0000823 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
824 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
825 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +0000826 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000827 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +0000828 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +0000829 DIVariable DV(Var);
830 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000831
Devang Patel7e623022011-08-10 20:55:27 +0000832 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000833
Devang Patelcdb7d442009-11-10 23:20:04 +0000834 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +0000835 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +0000836 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000837
Devang Patele1c53f22010-05-20 16:36:41 +0000838 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel99819b52011-08-15 19:01:20 +0000839 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patel3e4a9652011-08-15 21:24:36 +0000840 RegVar->setFrameIndex(VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +0000841 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +0000842 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +0000843 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +0000844 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +0000845 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000846}
Devang Patela3e9c9c2010-03-15 18:33:46 +0000847
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000848/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +0000849/// DBG_VALUE instruction, is in a defined reg.
850static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000851 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000852 return MI->getNumOperands() == 3 &&
853 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
854 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000855}
856
Nick Lewycky019d2552011-07-29 03:49:23 +0000857/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel2442a892011-07-08 17:09:57 +0000858/// at MI.
859static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
860 const MCSymbol *FLabel,
861 const MCSymbol *SLabel,
862 const MachineInstr *MI) {
863 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
864
865 if (MI->getNumOperands() != 3) {
866 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
867 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
868 }
869 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
870 MachineLocation MLoc;
871 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
872 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
873 }
874 if (MI->getOperand(0).isImm())
875 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
876 if (MI->getOperand(0).isFPImm())
877 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
878 if (MI->getOperand(0).isCImm())
879 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
880
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000881 assert(0 && "Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +0000882 return DotDebugLocEntry();
883}
884
Devang Patel7e623022011-08-10 20:55:27 +0000885/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000886void
Devang Patel5c0f85c2010-06-25 22:07:34 +0000887DwarfDebug::collectVariableInfo(const MachineFunction *MF,
888 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000889
Devang Patel490c8ab2010-05-20 19:57:06 +0000890 /// collection info from MMI table.
891 collectVariableInfoFromMMITable(MF, Processed);
892
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000893 for (SmallVectorImpl<const MDNode*>::const_iterator
894 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
895 ++UVI) {
896 const MDNode *Var = *UVI;
897 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +0000898 continue;
899
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000900 // History contains relevant DBG_VALUE instructions for Var and instructions
901 // clobbering it.
902 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
903 if (History.empty())
904 continue;
905 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +0000906
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000907 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +0000908 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +0000909 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
910 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +0000911 Scope = LScopes.getCurrentFunctionScope();
Devang Patel8fb9fd62011-07-20 22:18:50 +0000912 else {
913 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patel7e623022011-08-10 20:55:27 +0000914 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel8fb9fd62011-07-20 22:18:50 +0000915 else {
916 if (MDNode *IA = DV.getInlinedAt())
Devang Patel7e623022011-08-10 20:55:27 +0000917 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel8fb9fd62011-07-20 22:18:50 +0000918 else
Devang Patel7e623022011-08-10 20:55:27 +0000919 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel8fb9fd62011-07-20 22:18:50 +0000920 }
921 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000922 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +0000923 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +0000924 continue;
925
926 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000927 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +0000928 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
929 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel6c622ef2011-03-01 22:58:55 +0000930 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +0000931 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +0000932 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +0000933 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000934
935 // Simple ranges that are fully coalesced.
936 if (History.size() <= 1 || (History.size() == 2 &&
937 MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +0000938 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +0000939 continue;
940 }
941
942 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000943 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000944
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000945 for (SmallVectorImpl<const MachineInstr*>::const_iterator
946 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
947 const MachineInstr *Begin = *HI;
948 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +0000949
Devang Patele7181b52011-06-01 23:00:17 +0000950 // Check if DBG_VALUE is truncating a range.
951 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
952 && !Begin->getOperand(0).getReg())
953 continue;
954
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000955 // Compute the range for a register location.
956 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
957 const MCSymbol *SLabel = 0;
958
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000959 if (HI + 1 == HE)
960 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000961 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000962 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000963 else {
964 const MachineInstr *End = HI[1];
Devang Patel53b050a2011-07-07 21:44:42 +0000965 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
966 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000967 if (End->isDebugValue())
968 SLabel = getLabelBeforeInsn(End);
969 else {
970 // End is a normal instruction clobbering the range.
971 SLabel = getLabelAfterInsn(End);
972 assert(SLabel && "Forgot label after clobber instruction");
973 ++HI;
974 }
975 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000976
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000977 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel2442a892011-07-08 17:09:57 +0000978 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +0000979 }
980 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +0000981 }
Devang Patele0a94bf2010-05-14 21:01:35 +0000982
983 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +0000984 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
985 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
986 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
987 DIVariable DV(Variables.getElement(i));
988 if (!DV || !DV.Verify() || !Processed.insert(DV))
989 continue;
990 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
991 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patele0a94bf2010-05-14 21:01:35 +0000992 }
Devang Patel9fc11702010-05-25 23:40:22 +0000993}
Devang Patele0a94bf2010-05-14 21:01:35 +0000994
Devang Patel9fc11702010-05-25 23:40:22 +0000995/// getLabelBeforeInsn - Return Label preceding the instruction.
996const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000997 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
998 assert(Label && "Didn't insert label before instruction");
999 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001000}
1001
1002/// getLabelAfterInsn - Return Label immediately following the instruction.
1003const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001004 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001005}
1006
Devang Patelb5694e72010-10-26 17:49:02 +00001007/// beginInstruction - Process beginning of an instruction.
1008void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001009 // Check if source location changes, but ignore DBG_VALUE locations.
1010 if (!MI->isDebugValue()) {
1011 DebugLoc DL = MI->getDebugLoc();
1012 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel34a66202011-05-11 19:22:19 +00001013 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001014 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001015 if (DL == PrologEndLoc) {
1016 Flags |= DWARF2_FLAG_PROLOGUE_END;
1017 PrologEndLoc = DebugLoc();
1018 }
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001019 if (!DL.isUnknown()) {
1020 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001021 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001022 } else
Devang Patel34a66202011-05-11 19:22:19 +00001023 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001024 }
Devang Patel9fc11702010-05-25 23:40:22 +00001025 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001026
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001027 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001028 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1029 LabelsBeforeInsn.find(MI);
1030
1031 // No label needed.
1032 if (I == LabelsBeforeInsn.end())
1033 return;
1034
1035 // Label already assigned.
1036 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001037 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001038
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001039 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001040 PrevLabel = MMI->getContext().CreateTempSymbol();
1041 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001042 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001043 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001044}
1045
Devang Patelb5694e72010-10-26 17:49:02 +00001046/// endInstruction - Process end of an instruction.
1047void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001048 // Don't create a new label after DBG_VALUE instructions.
1049 // They don't generate code.
1050 if (!MI->isDebugValue())
1051 PrevLabel = 0;
1052
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001053 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1054 LabelsAfterInsn.find(MI);
1055
1056 // No label needed.
1057 if (I == LabelsAfterInsn.end())
1058 return;
1059
1060 // Label already assigned.
1061 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001062 return;
1063
1064 // We need a label after this instruction.
1065 if (!PrevLabel) {
1066 PrevLabel = MMI->getContext().CreateTempSymbol();
1067 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001068 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001069 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001070}
1071
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001072/// identifyScopeMarkers() -
Devang Patel784077e2011-08-10 23:58:09 +00001073/// Each LexicalScope has first instruction and last instruction to mark
1074/// beginning and end of a scope respectively. Create an inverse map that list
1075/// scopes starts (and ends) with an instruction. One instruction may start (or
1076/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001077void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001078 SmallVector<LexicalScope *, 4> WorkList;
1079 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001080 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001081 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001082
Devang Patel7e623022011-08-10 20:55:27 +00001083 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001084 if (!Children.empty())
Devang Patel7e623022011-08-10 20:55:27 +00001085 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001086 SE = Children.end(); SI != SE; ++SI)
1087 WorkList.push_back(*SI);
1088
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001089 if (S->isAbstractScope())
1090 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001091
Devang Patel7e623022011-08-10 20:55:27 +00001092 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001093 if (Ranges.empty())
1094 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001095 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +00001096 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001097 assert(RI->first && "InsnRange does not have first instruction!");
1098 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001099 requestLabelBeforeInsn(RI->first);
1100 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001101 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001102 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001103}
1104
Devang Patel589845d2011-05-09 22:14:49 +00001105/// getScopeNode - Get MDNode for DebugLoc's scope.
1106static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1107 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1108 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1109 return DL.getScope(Ctx);
1110}
1111
Devang Patel34a66202011-05-11 19:22:19 +00001112/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1113/// line number info for the function.
1114static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1115 const MDNode *Scope = getScopeNode(DL, Ctx);
1116 DISubprogram SP = getDISubprogram(Scope);
1117 if (SP.Verify())
1118 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1119 return DebugLoc();
1120}
1121
Devang Patel930143b2009-11-21 02:48:08 +00001122/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00001123/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00001124void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001125 if (!MMI->hasDebugInfo()) return;
Devang Patel7e623022011-08-10 20:55:27 +00001126 LScopes.initialize(*MF);
1127 if (LScopes.empty()) return;
1128 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001129
Devang Patel6c74a872010-04-27 19:46:33 +00001130 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1131 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001132 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001133 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001134
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001135 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1136
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001137 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001138 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1139 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1140
Devang Patel002d54d2010-05-26 19:37:24 +00001141 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001142 I != E; ++I) {
1143 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001144 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1145 II != IE; ++II) {
1146 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001147
Devang Patel002d54d2010-05-26 19:37:24 +00001148 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001149 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001150
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001151 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001152 const MDNode *Var =
1153 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001154
1155 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001156 if (isDbgValueInDefinedReg(MI))
1157 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1158
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001159 // Check the history of this variable.
1160 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1161 if (History.empty()) {
1162 UserVariables.push_back(Var);
1163 // The first mention of a function argument gets the FunctionBeginSym
1164 // label, so arguments are visible when breaking at function entry.
1165 DIVariable DV(Var);
1166 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1167 DISubprogram(getDISubprogram(DV.getContext()))
1168 .describes(MF->getFunction()))
1169 LabelsBeforeInsn[MI] = FunctionBeginSym;
1170 } else {
1171 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1172 const MachineInstr *Prev = History.back();
1173 if (Prev->isDebugValue()) {
1174 // Coalesce identical entries at the end of History.
1175 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001176 Prev->isIdenticalTo(History[History.size() - 2])) {
1177 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1178 << "\t" << *Prev
1179 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001180 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001181 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001182
1183 // Terminate old register assignments that don't reach MI;
1184 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1185 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1186 isDbgValueInDefinedReg(Prev)) {
1187 // Previous register assignment needs to terminate at the end of
1188 // its basic block.
1189 MachineBasicBlock::const_iterator LastMI =
1190 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001191 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001192 // Drop DBG_VALUE for empty range.
Devang Patelb7a328e2011-07-07 00:14:27 +00001193 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1194 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001195 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001196 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001197 else {
1198 // Terminate after LastMI.
1199 History.push_back(LastMI);
1200 }
1201 }
1202 }
1203 }
1204 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001205 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001206 // Not a DBG_VALUE instruction.
1207 if (!MI->isLabel())
1208 AtBlockEntry = false;
1209
Devang Patel34a66202011-05-11 19:22:19 +00001210 // First known non DBG_VALUE location marks beginning of function
1211 // body.
1212 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1213 PrologEndLoc = MI->getDebugLoc();
1214
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001215 // Check if the instruction clobbers any registers with debug vars.
1216 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1217 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1218 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1219 continue;
1220 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1221 unsigned Reg = *AI; ++AI) {
1222 const MDNode *Var = LiveUserVar[Reg];
1223 if (!Var)
1224 continue;
1225 // Reg is now clobbered.
1226 LiveUserVar[Reg] = 0;
1227
1228 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001229 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1230 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001231 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001232 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1233 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001234 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001235 const MachineInstr *Prev = History.back();
1236 // Sanity-check: Register assignments are terminated at the end of
1237 // their block.
1238 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1239 continue;
1240 // Is the variable still in Reg?
1241 if (!isDbgValueInDefinedReg(Prev) ||
1242 Prev->getOperand(0).getReg() != Reg)
1243 continue;
1244 // Var is clobbered. Make sure the next instruction gets a label.
1245 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001246 }
1247 }
Devang Patel002d54d2010-05-26 19:37:24 +00001248 }
Devang Patel002d54d2010-05-26 19:37:24 +00001249 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001250 }
1251
1252 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1253 I != E; ++I) {
1254 SmallVectorImpl<const MachineInstr*> &History = I->second;
1255 if (History.empty())
1256 continue;
1257
1258 // Make sure the final register assignments are terminated.
1259 const MachineInstr *Prev = History.back();
1260 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1261 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel784077e2011-08-10 23:58:09 +00001262 MachineBasicBlock::const_iterator LastMI =
1263 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001264 if (LastMI == PrevMBB->end())
1265 // Drop DBG_VALUE for empty range.
1266 History.pop_back();
1267 else {
1268 // Terminate after LastMI.
1269 History.push_back(LastMI);
1270 }
1271 }
1272 // Request labels for the full history.
1273 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1274 const MachineInstr *MI = History[i];
1275 if (MI->isDebugValue())
1276 requestLabelBeforeInsn(MI);
1277 else
1278 requestLabelAfterInsn(MI);
1279 }
1280 }
Devang Patel002d54d2010-05-26 19:37:24 +00001281
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001282 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001283 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001284
1285 // Record beginning of function.
1286 if (!PrologEndLoc.isUnknown()) {
1287 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1288 MF->getFunction()->getContext());
1289 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1290 FnStartDL.getScope(MF->getFunction()->getContext()),
1291 DWARF2_FLAG_IS_STMT);
1292 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001293}
1294
Devang Patel7e623022011-08-10 20:55:27 +00001295void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1296// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1297 ScopeVariables[LS].push_back(Var);
1298// Vars.push_back(Var);
1299}
1300
Devang Patel930143b2009-11-21 02:48:08 +00001301/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00001302///
Chris Lattner76555b52010-01-26 23:18:02 +00001303void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patel7e623022011-08-10 20:55:27 +00001304 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00001305
Devang Patel7e623022011-08-10 20:55:27 +00001306 // Define end label for subprogram.
1307 FunctionEndSym = Asm->GetTempSymbol("func_end",
1308 Asm->getFunctionNumber());
1309 // Assumes in correct section after the entry point.
1310 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1311
1312 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1313 collectVariableInfo(MF, ProcessedVars);
1314
Devang Patel3acc70e2011-08-15 22:04:40 +00001315 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001316 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001317 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001318
Devang Patel7e623022011-08-10 20:55:27 +00001319 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001320 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1321 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1322 LexicalScope *AScope = AList[i];
1323 DISubprogram SP(AScope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +00001324 if (SP.Verify()) {
1325 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001326 DIArray Variables = SP.getVariables();
1327 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1328 DIVariable DV(Variables.getElement(i));
1329 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1330 continue;
1331 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1332 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001333 }
1334 }
Devang Patel44403472011-08-12 18:10:19 +00001335 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Patel3acc70e2011-08-15 22:04:40 +00001336 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001337 }
Devang Patel7e623022011-08-10 20:55:27 +00001338
Devang Patel3acc70e2011-08-15 22:04:40 +00001339 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patel7e623022011-08-10 20:55:27 +00001340
Devang Patel3acc70e2011-08-15 22:04:40 +00001341 if (!DisableFramePointerElim(*MF))
Devang Patel784077e2011-08-10 23:58:09 +00001342 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1343 dwarf::DW_FORM_flag, 1);
Devang Patel3acc70e2011-08-15 22:04:40 +00001344
Devang Patel7e623022011-08-10 20:55:27 +00001345 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1346 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00001347
Bill Wendling2b128d72009-05-20 23:19:06 +00001348 // Clear debug info
Devang Patel7e623022011-08-10 20:55:27 +00001349 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1350 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1351 DeleteContainerPointers(I->second);
1352 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001353 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001354 UserVariables.clear();
1355 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001356 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001357 LabelsBeforeInsn.clear();
1358 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001359 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001360}
1361
Chris Lattnerba35a672010-03-09 04:54:43 +00001362/// recordSourceLine - Register a source line with debug info. Returns the
1363/// unique label that was emitted and which provides correspondence to
1364/// the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001365void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1366 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001367 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001368 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001369 unsigned Src = 1;
1370 if (S) {
1371 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001372
Dan Gohman50849c62010-05-05 23:41:32 +00001373 if (Scope.isCompileUnit()) {
1374 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001375 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001376 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001377 } else if (Scope.isFile()) {
1378 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001379 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001380 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001381 } else if (Scope.isSubprogram()) {
1382 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001383 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001384 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001385 } else if (Scope.isLexicalBlockFile()) {
1386 DILexicalBlockFile DBF(S);
1387 Fn = DBF.getFilename();
1388 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001389 } else if (Scope.isLexicalBlock()) {
1390 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001391 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001392 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001393 } else
1394 assert(0 && "Unexpected scope info");
1395
Devang Patele01b75c2011-03-24 20:30:50 +00001396 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00001397 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001398 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001399}
1400
Bill Wendling806535f2009-05-20 23:22:40 +00001401//===----------------------------------------------------------------------===//
1402// Emit Methods
1403//===----------------------------------------------------------------------===//
1404
Devang Patel930143b2009-11-21 02:48:08 +00001405/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00001406///
Jim Grosbach00e9c612009-11-22 19:20:36 +00001407unsigned
1408DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00001409 // Get the children.
1410 const std::vector<DIE *> &Children = Die->getChildren();
1411
1412 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001413 if (!Last && !Children.empty())
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001414 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling480ff322009-05-20 23:21:38 +00001415
1416 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001417 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001418
1419 // Get the abbreviation for this DIE.
1420 unsigned AbbrevNumber = Die->getAbbrevNumber();
1421 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1422
1423 // Set DIE offset
1424 Die->setOffset(Offset);
1425
1426 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001427 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001428
1429 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1430 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1431
1432 // Size the DIE attribute values.
1433 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1434 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001435 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001436
1437 // Size the DIE children if any.
1438 if (!Children.empty()) {
1439 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1440 "Children flag not set");
1441
1442 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001443 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00001444
1445 // End of children marker.
1446 Offset += sizeof(int8_t);
1447 }
1448
1449 Die->setSize(Offset - Die->getOffset());
1450 return Offset;
1451}
1452
Devang Patel930143b2009-11-21 02:48:08 +00001453/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00001454///
Devang Patel930143b2009-11-21 02:48:08 +00001455void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001456 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1457 E = CUMap.end(); I != E; ++I) {
1458 // Compute size of compile unit header.
Devang Patel28dce702011-04-12 23:10:47 +00001459 unsigned Offset =
Devang Patel1a0df9a2010-05-10 22:49:55 +00001460 sizeof(int32_t) + // Length of Compilation Unit Info
1461 sizeof(int16_t) + // DWARF version number
1462 sizeof(int32_t) + // Offset Into Abbrev. Section
1463 sizeof(int8_t); // Pointer Size (in bytes)
1464 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001465 }
Bill Wendling480ff322009-05-20 23:21:38 +00001466}
1467
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001468/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1469/// temporary label to it if SymbolStem is specified.
Chris Lattner6629ca92010-04-04 22:59:04 +00001470static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001471 const char *SymbolStem = 0) {
Chris Lattner6629ca92010-04-04 22:59:04 +00001472 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001473 if (!SymbolStem) return 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001474
Chris Lattner6629ca92010-04-04 22:59:04 +00001475 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1476 Asm->OutStreamer.EmitLabel(TmpSym);
1477 return TmpSym;
1478}
1479
1480/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1481/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00001482void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001483 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001484
Bill Wendling480ff322009-05-20 23:21:38 +00001485 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001486 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001487 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001488 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001489 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001490 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001491
Chris Lattner6629ca92010-04-04 22:59:04 +00001492 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001493 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001494
Devang Patel4a213872010-08-24 00:06:12 +00001495 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001496 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1497 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1498 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001499 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001500 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00001501 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1502 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001503
Devang Patel9fc11702010-05-25 23:40:22 +00001504 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1505 "section_debug_loc");
1506
Chris Lattner6629ca92010-04-04 22:59:04 +00001507 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00001508 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001509}
1510
Nick Lewycky019d2552011-07-29 03:49:23 +00001511/// emitDIE - Recursively emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00001512///
Devang Patel930143b2009-11-21 02:48:08 +00001513void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001514 // Get the abbreviation for this DIE.
1515 unsigned AbbrevNumber = Die->getAbbrevNumber();
1516 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1517
Bill Wendling480ff322009-05-20 23:21:38 +00001518 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001519 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00001520 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1521 Twine::utohexstr(Die->getOffset()) + ":0x" +
1522 Twine::utohexstr(Die->getSize()) + " " +
1523 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00001524 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001525
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001526 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00001527 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1528
1529 // Emit the DIE attribute values.
1530 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1531 unsigned Attr = AbbrevData[i].getAttribute();
1532 unsigned Form = AbbrevData[i].getForm();
1533 assert(Form && "Too many attributes for DIE (check abbreviation)");
1534
Chris Lattner7bde8c02010-04-04 18:52:31 +00001535 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001536 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001537
Bill Wendling480ff322009-05-20 23:21:38 +00001538 switch (Attr) {
1539 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00001540 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00001541 break;
1542 case dwarf::DW_AT_abstract_origin: {
1543 DIEEntry *E = cast<DIEEntry>(Values[i]);
1544 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001545 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00001546 Asm->EmitInt32(Addr);
1547 break;
1548 }
Devang Patel12563b32010-04-16 23:33:45 +00001549 case dwarf::DW_AT_ranges: {
1550 // DW_AT_range Value encodes offset in debug_range section.
1551 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00001552
1553 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1554 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1555 V->getValue(),
1556 4);
1557 } else {
1558 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1559 V->getValue(),
1560 DwarfDebugRangeSectionSym,
1561 4);
1562 }
Devang Patel12563b32010-04-16 23:33:45 +00001563 break;
1564 }
Devang Patel9fc11702010-05-25 23:40:22 +00001565 case dwarf::DW_AT_location: {
Devang Pateld8994442011-08-15 21:43:21 +00001566 if (DIELabel *L = dyn_cast<DIELabel>(Values[i]))
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001567 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Pateld8994442011-08-15 21:43:21 +00001568 else
Devang Patel9fc11702010-05-25 23:40:22 +00001569 Values[i]->EmitValue(Asm, Form);
1570 break;
1571 }
Devang Patela1bd5a12010-09-29 19:08:08 +00001572 case dwarf::DW_AT_accessibility: {
1573 if (Asm->isVerbose()) {
1574 DIEInteger *V = cast<DIEInteger>(Values[i]);
1575 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1576 }
1577 Values[i]->EmitValue(Asm, Form);
1578 break;
1579 }
Bill Wendling480ff322009-05-20 23:21:38 +00001580 default:
1581 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001582 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001583 break;
1584 }
Bill Wendling480ff322009-05-20 23:21:38 +00001585 }
1586
1587 // Emit the DIE children if any.
1588 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1589 const std::vector<DIE *> &Children = Die->getChildren();
1590
1591 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001592 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00001593
Chris Lattner7bde8c02010-04-04 18:52:31 +00001594 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00001595 Asm->OutStreamer.AddComment("End Of Children Mark");
1596 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001597 }
1598}
1599
Devang Patel9ccfb642009-12-09 18:24:21 +00001600/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00001601///
Devang Patel9ccfb642009-12-09 18:24:21 +00001602void DwarfDebug::emitDebugInfo() {
1603 // Start debug info section.
1604 Asm->OutStreamer.SwitchSection(
1605 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001606 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1607 E = CUMap.end(); I != E; ++I) {
1608 CompileUnit *TheCU = I->second;
1609 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001610
Devang Patel1a0df9a2010-05-10 22:49:55 +00001611 // Emit the compile units header.
1612 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1613 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001614
Devang Patel1a0df9a2010-05-10 22:49:55 +00001615 // Emit size of content not including length itself
1616 unsigned ContentSize = Die->getSize() +
1617 sizeof(int16_t) + // DWARF version number
1618 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00001619 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001620
Devang Patel1a0df9a2010-05-10 22:49:55 +00001621 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1622 Asm->EmitInt32(ContentSize);
1623 Asm->OutStreamer.AddComment("DWARF version number");
1624 Asm->EmitInt16(dwarf::DWARF_VERSION);
1625 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1626 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1627 DwarfAbbrevSectionSym);
1628 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1629 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001630
Devang Patel1a0df9a2010-05-10 22:49:55 +00001631 emitDIE(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001632 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1633 }
Bill Wendling480ff322009-05-20 23:21:38 +00001634}
1635
Devang Patel930143b2009-11-21 02:48:08 +00001636/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00001637///
Devang Patel930143b2009-11-21 02:48:08 +00001638void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00001639 // Check to see if it is worth the effort.
1640 if (!Abbreviations.empty()) {
1641 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001642 Asm->OutStreamer.SwitchSection(
1643 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001644
Chris Lattnera179b522010-04-04 19:25:43 +00001645 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00001646
1647 // For each abbrevation.
1648 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1649 // Get abbreviation data
1650 const DIEAbbrev *Abbrev = Abbreviations[i];
1651
1652 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00001653 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00001654
1655 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001656 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00001657 }
1658
1659 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00001660 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00001661
Chris Lattnera179b522010-04-04 19:25:43 +00001662 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00001663 }
1664}
1665
Devang Patel930143b2009-11-21 02:48:08 +00001666/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00001667/// the line matrix.
1668///
Devang Patel930143b2009-11-21 02:48:08 +00001669void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00001670 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00001671 Asm->OutStreamer.AddComment("Extended Op");
1672 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001673
Chris Lattner566cae92010-03-09 23:52:58 +00001674 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00001675 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00001676 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1677 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1678
1679 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00001680
Chris Lattnera179b522010-04-04 19:25:43 +00001681 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00001682 Asm->getTargetData().getPointerSize(),
1683 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00001684
1685 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00001686 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1687 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001688 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00001689 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00001690}
1691
Devang Patel9ccfb642009-12-09 18:24:21 +00001692/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1693///
1694void DwarfDebug::emitDebugPubNames() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001695 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1696 E = CUMap.end(); I != E; ++I) {
1697 CompileUnit *TheCU = I->second;
1698 // Start the dwarf pubnames section.
1699 Asm->OutStreamer.SwitchSection(
1700 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001701
Devang Patel1a0df9a2010-05-10 22:49:55 +00001702 Asm->OutStreamer.AddComment("Length of Public Names Info");
1703 Asm->EmitLabelDifference(
1704 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1705 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001706
Devang Patel1a0df9a2010-05-10 22:49:55 +00001707 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1708 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001709
Devang Patel1a0df9a2010-05-10 22:49:55 +00001710 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001711 Asm->EmitInt16(dwarf::DWARF_VERSION);
1712
Devang Patel1a0df9a2010-05-10 22:49:55 +00001713 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001714 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00001715 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001716
Devang Patel1a0df9a2010-05-10 22:49:55 +00001717 Asm->OutStreamer.AddComment("Compilation Unit Length");
1718 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1719 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1720 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001721
Devang Patel1a0df9a2010-05-10 22:49:55 +00001722 const StringMap<DIE*> &Globals = TheCU->getGlobals();
1723 for (StringMap<DIE*>::const_iterator
1724 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1725 const char *Name = GI->getKeyData();
1726 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001727
Devang Patel1a0df9a2010-05-10 22:49:55 +00001728 Asm->OutStreamer.AddComment("DIE offset");
1729 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001730
Devang Patel1a0df9a2010-05-10 22:49:55 +00001731 if (Asm->isVerbose())
1732 Asm->OutStreamer.AddComment("External Name");
1733 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
1734 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001735
Devang Patel1a0df9a2010-05-10 22:49:55 +00001736 Asm->OutStreamer.AddComment("End Mark");
1737 Asm->EmitInt32(0);
1738 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky019d2552011-07-29 03:49:23 +00001739 TheCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00001740 }
Bill Wendling480ff322009-05-20 23:21:38 +00001741}
1742
Devang Patel04d2f2d2009-11-24 01:14:22 +00001743void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001744 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1745 E = CUMap.end(); I != E; ++I) {
1746 CompileUnit *TheCU = I->second;
1747 // Start the dwarf pubnames section.
1748 Asm->OutStreamer.SwitchSection(
1749 Asm->getObjFileLowering().getDwarfPubTypesSection());
1750 Asm->OutStreamer.AddComment("Length of Public Types Info");
1751 Asm->EmitLabelDifference(
1752 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1753 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001754
Devang Patel1a0df9a2010-05-10 22:49:55 +00001755 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1756 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001757
Devang Patel1a0df9a2010-05-10 22:49:55 +00001758 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1759 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001760
Devang Patel1a0df9a2010-05-10 22:49:55 +00001761 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1762 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1763 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001764
Devang Patel1a0df9a2010-05-10 22:49:55 +00001765 Asm->OutStreamer.AddComment("Compilation Unit Length");
1766 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1767 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1768 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001769
Devang Patel1a0df9a2010-05-10 22:49:55 +00001770 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1771 for (StringMap<DIE*>::const_iterator
1772 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1773 const char *Name = GI->getKeyData();
Nick Lewycky019d2552011-07-29 03:49:23 +00001774 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001775
Devang Patel1a0df9a2010-05-10 22:49:55 +00001776 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1777 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001778
Devang Patel1a0df9a2010-05-10 22:49:55 +00001779 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
1780 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1781 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001782
Devang Patel1a0df9a2010-05-10 22:49:55 +00001783 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001784 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001785 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1786 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00001787 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001788}
1789
Devang Patel930143b2009-11-21 02:48:08 +00001790/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00001791///
Devang Patel930143b2009-11-21 02:48:08 +00001792void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00001793 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00001794 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001795
Chris Lattner3d72a672010-03-09 23:38:23 +00001796 // Start the dwarf str section.
1797 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001798 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001799
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001800 // Get all of the string pool entries and put them in an array by their ID so
1801 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001802 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001803 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001804
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001805 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1806 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1807 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001808
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001809 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001810
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001811 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00001812 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001813 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001814
Chris Lattner3d72a672010-03-09 23:38:23 +00001815 // Emit the string itself.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001816 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001817 Asm->OutStreamer.EmitZeros(1, 0);
Bill Wendling480ff322009-05-20 23:21:38 +00001818 }
1819}
1820
Devang Patel930143b2009-11-21 02:48:08 +00001821/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00001822///
Devang Patel930143b2009-11-21 02:48:08 +00001823void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001824 if (DotDebugLocEntries.empty())
1825 return;
1826
Devang Patel116a9d72011-02-04 22:57:18 +00001827 for (SmallVector<DotDebugLocEntry, 4>::iterator
1828 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
1829 I != E; ++I) {
1830 DotDebugLocEntry &Entry = *I;
1831 if (I + 1 != DotDebugLocEntries.end())
1832 Entry.Merge(I+1);
1833 }
1834
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001835 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001836 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00001837 Asm->getObjFileLowering().getDwarfLocSection());
1838 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001839 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
1840 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001841 for (SmallVector<DotDebugLocEntry, 4>::iterator
1842 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00001843 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00001844 DotDebugLocEntry &Entry = *I;
1845 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00001846 if (Entry.isEmpty()) {
1847 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
1848 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001849 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00001850 } else {
1851 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
1852 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patel3e021532011-04-28 02:22:40 +00001853 DIVariable DV(Entry.Variable);
Rafael Espindolad23bfb82011-05-27 22:05:41 +00001854 Asm->OutStreamer.AddComment("Loc expr size");
1855 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
1856 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
1857 Asm->EmitLabelDifference(end, begin, 2);
1858 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00001859 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00001860 DIBasicType BTy(DV.getType());
1861 if (BTy.Verify() &&
1862 (BTy.getEncoding() == dwarf::DW_ATE_signed
1863 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
1864 Asm->OutStreamer.AddComment("DW_OP_consts");
1865 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00001866 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00001867 } else {
1868 Asm->OutStreamer.AddComment("DW_OP_constu");
1869 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00001870 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00001871 }
Devang Pateled9fd452011-07-08 16:49:43 +00001872 } else if (Entry.isLocation()) {
1873 if (!DV.hasComplexAddress())
1874 // Regular entry.
Devang Patel3e021532011-04-28 02:22:40 +00001875 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Pateled9fd452011-07-08 16:49:43 +00001876 else {
1877 // Complex address entry.
1878 unsigned N = DV.getNumAddrElements();
1879 unsigned i = 0;
1880 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
1881 if (Entry.Loc.getOffset()) {
1882 i = 2;
1883 Asm->EmitDwarfRegOp(Entry.Loc);
1884 Asm->OutStreamer.AddComment("DW_OP_deref");
1885 Asm->EmitInt8(dwarf::DW_OP_deref);
1886 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
1887 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1888 Asm->EmitSLEB128(DV.getAddrElement(1));
1889 } else {
1890 // If first address element is OpPlus then emit
1891 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
1892 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
1893 Asm->EmitDwarfRegOp(Loc);
1894 i = 2;
1895 }
1896 } else {
1897 Asm->EmitDwarfRegOp(Entry.Loc);
1898 }
1899
1900 // Emit remaining complex address elements.
1901 for (; i < N; ++i) {
1902 uint64_t Element = DV.getAddrElement(i);
1903 if (Element == DIBuilder::OpPlus) {
1904 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
1905 Asm->EmitULEB128(DV.getAddrElement(++i));
1906 } else if (Element == DIBuilder::OpDeref)
1907 Asm->EmitInt8(dwarf::DW_OP_deref);
1908 else llvm_unreachable("unknown Opcode found in complex address");
1909 }
Devang Patel3e021532011-04-28 02:22:40 +00001910 }
Devang Patel3e021532011-04-28 02:22:40 +00001911 }
Devang Pateled9fd452011-07-08 16:49:43 +00001912 // else ... ignore constant fp. There is not any good way to
1913 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00001914 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00001915 }
1916 }
Bill Wendling480ff322009-05-20 23:21:38 +00001917}
1918
1919/// EmitDebugARanges - Emit visible names into a debug aranges section.
1920///
1921void DwarfDebug::EmitDebugARanges() {
1922 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001923 Asm->OutStreamer.SwitchSection(
1924 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001925}
1926
Devang Patel930143b2009-11-21 02:48:08 +00001927/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00001928///
Devang Patel930143b2009-11-21 02:48:08 +00001929void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00001930 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001931 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00001932 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00001933 unsigned char Size = Asm->getTargetData().getPointerSize();
1934 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001935 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00001936 I != E; ++I) {
1937 if (*I)
1938 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00001939 else
Devang Patel6c74a872010-04-27 19:46:33 +00001940 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00001941 }
Bill Wendling480ff322009-05-20 23:21:38 +00001942}
1943
Devang Patel930143b2009-11-21 02:48:08 +00001944/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00001945///
Devang Patel930143b2009-11-21 02:48:08 +00001946void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001947 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00001948 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00001949 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001950 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001951 }
1952}
1953
Devang Patel930143b2009-11-21 02:48:08 +00001954/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00001955/// Section Header:
1956/// 1. length of section
1957/// 2. Dwarf version number
1958/// 3. address size.
1959///
1960/// Entries (one "entry" for each function that was inlined):
1961///
1962/// 1. offset into __debug_str section for MIPS linkage name, if exists;
1963/// otherwise offset into __debug_str for regular function name.
1964/// 2. offset into __debug_str section for regular function name.
1965/// 3. an unsigned LEB128 number indicating the number of distinct inlining
1966/// instances for the function.
1967///
1968/// The rest of the entry consists of a {die_offset, low_pc} pair for each
1969/// inlined instance; the die_offset points to the inlined_subroutine die in the
1970/// __debug_info section, and the low_pc is the starting address for the
1971/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00001972void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00001973 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00001974 return;
1975
Devang Patel1a0df9a2010-05-10 22:49:55 +00001976 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00001977 return;
1978
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001979 Asm->OutStreamer.SwitchSection(
1980 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001981
Chris Lattner566cae92010-03-09 23:52:58 +00001982 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00001983 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
1984 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00001985
Chris Lattnera179b522010-04-04 19:25:43 +00001986 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00001987
Chris Lattner566cae92010-03-09 23:52:58 +00001988 Asm->OutStreamer.AddComment("Dwarf Version");
1989 Asm->EmitInt16(dwarf::DWARF_VERSION);
1990 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00001991 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00001992
Devang Patel32cc43c2010-05-07 20:54:48 +00001993 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001994 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00001995
Devang Patel32cc43c2010-05-07 20:54:48 +00001996 const MDNode *Node = *I;
1997 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00001998 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001999 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002000 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002001 StringRef LName = SP.getLinkageName();
2002 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002003
Chris Lattner566cae92010-03-09 23:52:58 +00002004 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002005 if (LName.empty()) {
2006 Asm->OutStreamer.EmitBytes(Name, 0);
2007 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002008 } else
Chris Lattner70a4fce2010-04-04 23:25:33 +00002009 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2010 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002011
Chris Lattner566cae92010-03-09 23:52:58 +00002012 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00002013 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00002014 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002015
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002016 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002017 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002018 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002019 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002020
Chris Lattner7bde8c02010-04-04 18:52:31 +00002021 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002022 Asm->OutStreamer.EmitSymbolValue(LI->first,
2023 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00002024 }
2025 }
2026
Chris Lattnera179b522010-04-04 19:25:43 +00002027 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002028}