blob: 8acc8571ddc6c95c7a9658f32534eff06bb0e6fd [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"
Eric Christopher4996c702011-11-07 09:24:32 +000017#include "DwarfAccelTable.h"
Devang Patel5eb43192011-04-12 17:40:32 +000018#include "DwarfCompileUnit.h"
Bill Wendling30346342010-04-05 22:59:21 +000019#include "llvm/Constants.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000020#include "llvm/Module.h"
Devang Patel2d9e5322011-01-20 00:02:16 +000021#include "llvm/Instructions.h"
David Greene829b3e82009-08-19 21:52:55 +000022#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000024#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000025#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000026#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000027#include "llvm/MC/MCSymbol.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000028#include "llvm/Target/TargetData.h"
Anton Korobeynikov2f931282011-01-10 12:39:04 +000029#include "llvm/Target/TargetFrameLowering.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner21dc46e2010-04-04 18:06:11 +000031#include "llvm/Target/TargetMachine.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel61880932010-04-19 19:14:02 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000034#include "llvm/Analysis/DebugInfo.h"
Devang Patela5d93242011-02-24 18:49:30 +000035#include "llvm/Analysis/DIBuilder.h"
Devang Patela86114b2010-10-25 20:45:32 +000036#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000037#include "llvm/ADT/STLExtras.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000038#include "llvm/ADT/StringExtras.h"
Devang Patel6c74a872010-04-27 19:46:33 +000039#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000040#include "llvm/Support/Debug.h"
41#include "llvm/Support/ErrorHandling.h"
Devang Patel1973df22010-01-26 21:39:14 +000042#include "llvm/Support/ValueHandle.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000043#include "llvm/Support/FormattedStream.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000044#include "llvm/Support/Timer.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000045#include "llvm/Support/Path.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000046using namespace llvm;
47
Jim Grosbacha8683bb2010-07-21 21:21:52 +000048static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel30265c42010-07-07 20:12:52 +000049 cl::Hidden,
Devang Patel6c74a872010-04-27 19:46:33 +000050 cl::desc("Disable debug info printing"));
51
Dan Gohman7421ae42010-05-07 01:08:53 +000052static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000053 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman7421ae42010-05-07 01:08:53 +000054 cl::init(false));
55
Eric Christopher4996c702011-11-07 09:24:32 +000056static cl::opt<bool> DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
57 cl::desc("Output prototype dwarf accelerator tables."),
58 cl::init(false));
59
Bill Wendlingfcc14142010-04-07 09:28:04 +000060namespace {
61 const char *DWARFGroupName = "DWARF Emission";
62 const char *DbgTimerName = "DWARF Debug Writer";
63} // end anonymous namespace
64
Bill Wendling2f921f82009-05-15 09:23:25 +000065//===----------------------------------------------------------------------===//
66
67/// Configuration values for initial hash set sizes (log2).
68///
Bill Wendling2f921f82009-05-15 09:23:25 +000069static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +000070
71namespace llvm {
72
Nick Lewycky019d2552011-07-29 03:49:23 +000073DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +000074 DIType Ty = Var.getType();
75 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
76 // addresses instead.
77 if (Var.isBlockByrefVariable()) {
78 /* Byref variables, in Blocks, are declared by the programmer as
79 "SomeType VarName;", but the compiler creates a
80 __Block_byref_x_VarName struct, and gives the variable VarName
81 either the struct, or a pointer to the struct, as its type. This
82 is necessary for various behind-the-scenes things the compiler
83 needs to do with by-reference variables in blocks.
84
85 However, as far as the original *programmer* is concerned, the
86 variable should still have type 'SomeType', as originally declared.
87
88 The following function dives into the __Block_byref_x_VarName
89 struct to find the original type of the variable. This will be
90 passed back to the code generating the type for the Debug
91 Information Entry for the variable 'VarName'. 'VarName' will then
92 have the original type 'SomeType' in its debug information.
93
94 The original type 'SomeType' will be the type of the field named
95 'VarName' inside the __Block_byref_x_VarName struct.
96
97 NOTE: In order for this to not completely fail on the debugger
98 side, the Debug Information Entry for the variable VarName needs to
99 have a DW_AT_location that tells the debugger how to unwind through
100 the pointers and __Block_byref_x_VarName struct to find the actual
101 value of the variable. The function addBlockByrefType does this. */
102 DIType subType = Ty;
103 unsigned tag = Ty.getTag();
104
105 if (tag == dwarf::DW_TAG_pointer_type) {
106 DIDerivedType DTy = DIDerivedType(Ty);
107 subType = DTy.getTypeDerivedFrom();
108 }
109
110 DICompositeType blockStruct = DICompositeType(subType);
111 DIArray Elements = blockStruct.getTypeArray();
112
113 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
114 DIDescriptor Element = Elements.getElement(i);
115 DIDerivedType DT = DIDerivedType(Element);
116 if (getName() == DT.getName())
117 return (DT.getTypeDerivedFrom());
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000118 }
119 return Ty;
120 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000121 return Ty;
122}
Bill Wendling2f921f82009-05-15 09:23:25 +0000123
Chris Lattnerf5d06362010-04-05 04:09:20 +0000124} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000125
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000126DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel1a0df9a2010-05-10 22:49:55 +0000127 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000128 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel7e623022011-08-10 20:55:27 +0000129 PrevLabel(NULL) {
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000130 NextStringPoolNumber = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000131
Rafael Espindolaa7160962011-05-06 14:56:22 +0000132 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattnere58b5472010-04-04 23:10:38 +0000133 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000134 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000135 FunctionBeginSym = FunctionEndSym = 0;
Dan Gohman6e681a52010-06-18 15:56:31 +0000136 {
137 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
138 beginModule(M);
Torok Edwinf8dba242010-04-07 10:44:46 +0000139 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000140}
141DwarfDebug::~DwarfDebug() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000142}
143
Eric Christophera7b61892011-11-07 09:18:38 +0000144/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
145/// temporary label to it if SymbolStem is specified.
146static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
147 const char *SymbolStem = 0) {
148 Asm->OutStreamer.SwitchSection(Section);
149 if (!SymbolStem) return 0;
150
151 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
152 Asm->OutStreamer.EmitLabel(TmpSym);
153 return TmpSym;
154}
155
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000156MCSymbol *DwarfDebug::getStringPool() {
157 return Asm->GetTempSymbol("section_str");
158}
159
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000160MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
161 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
162 if (Entry.first) return Entry.first;
163
164 Entry.second = NextStringPoolNumber++;
Chris Lattnera179b522010-04-04 19:25:43 +0000165 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000166}
167
Eric Christophera7b61892011-11-07 09:18:38 +0000168MCSymbol *DwarfDebug::getDwarfStrSectionSym(void) {
169 if (DwarfStrSectionSym) return DwarfStrSectionSym;
170 DwarfStrSectionSym =
171 EmitSectionSym(Asm, Asm->getObjFileLowering().getDwarfStrSection(),
172 "section_str");
173 return DwarfStrSectionSym;
174}
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000175
Devang Patel930143b2009-11-21 02:48:08 +0000176/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000177///
Devang Patel930143b2009-11-21 02:48:08 +0000178void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000179 // Profile the node so that we can make it unique.
180 FoldingSetNodeID ID;
181 Abbrev.Profile(ID);
182
183 // Check the set for priors.
184 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
185
186 // If it's newly added.
187 if (InSet == &Abbrev) {
188 // Add to abbreviation list.
189 Abbreviations.push_back(&Abbrev);
190
191 // Assign the vector position + 1 as its number.
192 Abbrev.setNumber(Abbreviations.size());
193 } else {
194 // Assign existing abbreviation number.
195 Abbrev.setNumber(InSet->getNumber());
196 }
197}
198
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000199/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel43ef34d2010-01-05 01:46:14 +0000200/// printer to not emit usual symbol prefix before the symbol name is used then
201/// return linkage name after skipping this special LLVM prefix.
202static StringRef getRealLinkageName(StringRef LinkageName) {
203 char One = '\1';
204 if (LinkageName.startswith(StringRef(&One, 1)))
205 return LinkageName.substr(1);
206 return LinkageName;
207}
208
Jim Grosbach042483e2009-11-21 23:12:12 +0000209/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +0000210/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
211/// If there are global variables in this scope then create and insert
212/// DIEs for these variables.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000213DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
214 const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000215 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +0000216
Chris Lattner3a383cb2010-04-05 00:13:49 +0000217 assert(SPDie && "Unable to find subprogram DIE!");
218 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000219
Devang Patel1d6bbd42011-04-22 23:10:17 +0000220 DISubprogram SPDecl = SP.getFunctionDeclaration();
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000221 if (!SPDecl.isSubprogram()) {
Devang Patel1d6bbd42011-04-22 23:10:17 +0000222 // There is not any need to generate specification DIE for a function
223 // defined at compile unit level. If a function is defined inside another
224 // function then gdb prefers the definition at top level and but does not
225 // expect specification DIE in parent function. So avoid creating
226 // specification DIE for a function defined inside a function.
227 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
228 !SP.getContext().isFile() &&
229 !isSubprogramContext(SP.getContext())) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000230 SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel1d6bbd42011-04-22 23:10:17 +0000231
232 // Add arguments.
233 DICompositeType SPTy = SP.getType();
234 DIArray Args = SPTy.getTypeArray();
235 unsigned SPTag = SPTy.getTag();
236 if (SPTag == dwarf::DW_TAG_subroutine_type)
237 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
238 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
239 DIType ATy = DIType(DIType(Args.getElement(i)));
240 SPCU->addType(Arg, ATy);
241 if (ATy.isArtificial())
242 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
243 SPDie->addChild(Arg);
244 }
245 DIE *SPDeclDie = SPDie;
246 SPDie = new DIE(dwarf::DW_TAG_subprogram);
247 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
248 SPDeclDie);
249 SPCU->addDie(SPDie);
250 }
Chris Lattner3a383cb2010-04-05 00:13:49 +0000251 }
Devang Patela37a95e2010-07-07 22:20:57 +0000252 // Pick up abstract subprogram DIE.
253 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
254 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf20c4f72011-04-12 22:53:02 +0000255 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
256 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patela37a95e2010-07-07 22:20:57 +0000257 SPCU->addDie(SPDie);
258 }
259
Devang Patelf20c4f72011-04-12 22:53:02 +0000260 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
261 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
262 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
263 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattner3a383cb2010-04-05 00:13:49 +0000264 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
265 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000266 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000267
Chris Lattner3a383cb2010-04-05 00:13:49 +0000268 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000269}
270
Jim Grosbach042483e2009-11-21 23:12:12 +0000271/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +0000272/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000273DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
274 LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000275 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
276 if (Scope->isAbstractScope())
277 return ScopeDIE;
278
Devang Patel7e623022011-08-10 20:55:27 +0000279 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +0000280 if (Ranges.empty())
281 return 0;
282
Devang Patel7e623022011-08-10 20:55:27 +0000283 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel6c74a872010-04-27 19:46:33 +0000284 if (Ranges.size() > 1) {
285 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000286 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000287 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000288 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000289 DebugRangeSymbols.size()
290 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000291 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +0000292 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000293 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
294 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000295 }
296 DebugRangeSymbols.push_back(NULL);
297 DebugRangeSymbols.push_back(NULL);
298 return ScopeDIE;
299 }
300
Devang Patel9fc11702010-05-25 23:40:22 +0000301 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
302 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000303
Devang Patel9fc11702010-05-25 23:40:22 +0000304 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000305
Chris Lattnere13c3722010-03-09 01:58:53 +0000306 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
307 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000308
Devang Patelf20c4f72011-04-12 22:53:02 +0000309 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
310 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000311
312 return ScopeDIE;
313}
314
Devang Patel930143b2009-11-21 02:48:08 +0000315/// constructInlinedScopeDIE - This scope represents inlined body of
316/// a function. Construct DIE to represent this concrete inlined copy
317/// of the function.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000318DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
319 LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +0000320 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000321 assert(Ranges.empty() == false &&
322 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000323
Devang Patelf098ce22011-07-27 00:34:13 +0000324 if (!Scope->getScopeNode())
325 return NULL;
326 DIScope DS(Scope->getScopeNode());
327 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patelf098ce22011-07-27 00:34:13 +0000328 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
329 if (!OriginDIE) {
330 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
331 return NULL;
332 }
333
Devang Patel7e623022011-08-10 20:55:27 +0000334 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +0000335 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
336 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000337
Devang Patel4c6bd662010-07-08 22:39:20 +0000338 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000339 assert(0 && "Unexpected Start and End labels for a inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000340 return 0;
341 }
Chris Lattnere13c3722010-03-09 01:58:53 +0000342 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000343 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +0000344 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000345 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000346
Devang Patel73bc1722011-05-05 17:54:26 +0000347 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patelf20c4f72011-04-12 22:53:02 +0000348 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
349 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000350
Devang Patelf098ce22011-07-27 00:34:13 +0000351 if (Ranges.size() > 1) {
352 // .debug_range section has not been laid out yet. Emit offset in
353 // .debug_range as a uint, size 4, for now. emitDIE will handle
354 // DW_AT_ranges appropriately.
355 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000356 DebugRangeSymbols.size()
357 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000358 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patelf098ce22011-07-27 00:34:13 +0000359 RE = Ranges.end(); RI != RE; ++RI) {
360 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
361 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
362 }
363 DebugRangeSymbols.push_back(NULL);
364 DebugRangeSymbols.push_back(NULL);
365 } else {
Devang Patel784077e2011-08-10 23:58:09 +0000366 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
367 StartLabel);
368 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
369 EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000370 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000371
372 InlinedSubprogramDIEs.insert(OriginDIE);
373
374 // Track the start label for this inlined function.
Devang Patelf098ce22011-07-27 00:34:13 +0000375 //.debug_inlined section specification does not clearly state how
376 // to emit inlined scope that is split into multiple instruction ranges.
377 // For now, use first instruction range and emit low_pc/high_pc pair and
378 // corresponding .debug_inlined section entry for this pair.
Devang Patel32cc43c2010-05-07 20:54:48 +0000379 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000380 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000381
382 if (I == InlineInfo.end()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000383 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000384 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000385 } else
Chris Lattner085b6522010-03-09 00:31:02 +0000386 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000387
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000388 DILocation DL(Scope->getInlinedAt());
Devang Patelf20c4f72011-04-12 22:53:02 +0000389 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
390 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000391
392 return ScopeDIE;
393}
394
Devang Patel930143b2009-11-21 02:48:08 +0000395/// constructScopeDIE - Construct a DIE for this scope.
Devang Patel3acc70e2011-08-15 22:04:40 +0000396DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000397 if (!Scope || !Scope->getScopeNode())
398 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000399
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000400 SmallVector<DIE *, 8> Children;
Devang Patel6c622ef2011-03-01 22:58:55 +0000401
402 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000403 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000404 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
405 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Devang Patel3acc70e2011-08-15 22:04:40 +0000406 if (DIE *Arg =
407 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope()))
Devang Patel6c622ef2011-03-01 22:58:55 +0000408 Children.push_back(Arg);
409
Eric Christopherf84354b2011-10-03 15:49:16 +0000410 // Collect lexical scope children first.
Devang Patel7e623022011-08-10 20:55:27 +0000411 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000412 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Devang Patel3acc70e2011-08-15 22:04:40 +0000413 if (DIE *Variable =
414 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope()))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000415 Children.push_back(Variable);
Devang Patel7e623022011-08-10 20:55:27 +0000416 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000417 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000418 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000419 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000420 DIScope DS(Scope->getScopeNode());
421 DIE *ScopeDIE = NULL;
422 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000423 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000424 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000425 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000426 if (Scope->isAbstractScope()) {
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000427 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000428 // Note down abstract DIE.
429 if (ScopeDIE)
430 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
431 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000432 else
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000433 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000434 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000435 else {
436 // There is no need to emit empty lexical block DIE.
437 if (Children.empty())
438 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000439 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000440 }
441
Devang Patel23b2ae62010-03-29 22:59:58 +0000442 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000443
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000444 // Add children
445 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
446 E = Children.end(); I != E; ++I)
447 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000448
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000449 if (DS.isSubprogram())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000450 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000451
Eric Christopher4996c702011-11-07 09:24:32 +0000452 if (DS.isSubprogram() && !Scope->isAbstractScope())
453 TheCU->addAccelName(DISubprogram(DS).getName(), ScopeDIE);
454
Devang Patel04d2f2d2009-11-24 01:14:22 +0000455 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000456}
457
Bill Wendling2b128d72009-05-20 23:19:06 +0000458/// GetOrCreateSourceID - Look up the source id with the given directory and
459/// source file names. If none currently exists, create a new id and insert it
460/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
461/// maps as well.
Devang Patele01b75c2011-03-24 20:30:50 +0000462unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
463 StringRef DirName) {
Devang Patel871d0b12010-09-16 20:57:49 +0000464 // If FE did not provide a file name, then assume stdin.
465 if (FileName.empty())
Devang Patele01b75c2011-03-24 20:30:50 +0000466 return GetOrCreateSourceID("<stdin>", StringRef());
467
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000468 // TODO: this might not belong here. See if we can factor this better.
469 if (DirName == CompilationDir)
470 DirName = "";
471
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000472 unsigned SrcId = SourceIdMap.size()+1;
473 std::pair<std::string, std::string> SourceName =
474 std::make_pair(FileName, DirName);
475 std::pair<std::pair<std::string, std::string>, unsigned> Entry =
476 make_pair(SourceName, SrcId);
Devang Patel871d0b12010-09-16 20:57:49 +0000477
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000478 std::map<std::pair<std::string, std::string>, unsigned>::iterator I;
479 bool NewlyInserted;
480 tie(I, NewlyInserted) = SourceIdMap.insert(Entry);
481 if (!NewlyInserted)
482 return I->second;
Bill Wendling2b128d72009-05-20 23:19:06 +0000483
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000484 // Print out a .file directive to specify files for .loc directives.
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000485 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.first.second,
486 Entry.first.first);
Bill Wendling2b128d72009-05-20 23:19:06 +0000487
488 return SrcId;
489}
490
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000491/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel1a0df9a2010-05-10 22:49:55 +0000492/// metadata node with tag DW_TAG_compile_unit.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000493CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000494 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +0000495 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000496 CompilationDir = DIUnit.getDirectory();
497 unsigned ID = GetOrCreateSourceID(FN, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000498
499 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelf20c4f72011-04-12 22:53:02 +0000500 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000501 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000502 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
503 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000504 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Devang Patelbd798ce2010-04-26 22:54:28 +0000505 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
506 // simplifies debug range entries.
Devang Patelf20c4f72011-04-12 22:53:02 +0000507 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +0000508 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +0000509 // compile unit in debug_line section.
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000510 if (Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindolaa7558912011-05-04 17:44:06 +0000511 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patelf20c4f72011-04-12 22:53:02 +0000512 Asm->GetTempSymbol("section_line"));
Devang Patelea636392010-08-31 23:50:19 +0000513 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000514 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +0000515
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000516 if (!CompilationDir.empty())
517 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000518 if (DIUnit.isOptimized())
Devang Patelf20c4f72011-04-12 22:53:02 +0000519 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +0000520
Devang Patel2d9caf92009-11-25 17:36:49 +0000521 StringRef Flags = DIUnit.getFlags();
522 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000523 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Devang Patelf20c4f72011-04-12 22:53:02 +0000524
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000525 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000526 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000527 dwarf::DW_FORM_data1, RVer);
528
Devang Patel1a0df9a2010-05-10 22:49:55 +0000529 if (!FirstCU)
530 FirstCU = NewCU;
531 CUMap.insert(std::make_pair(N, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000532 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000533}
534
Eric Christopher4996c702011-11-07 09:24:32 +0000535static bool isObjCClass(StringRef Name) {
536 return Name[0] == '+' || Name[0] == '-';
537}
538
539static bool hasObjCCategory(StringRef Name) {
540 if (Name[0] != '+' && Name[0] != '-')
541 return false;
542
543 size_t pos = Name.find(')');
544 if (pos != std::string::npos) {
545 if (Name[pos+1] != ' ') return false;
546 return true;
547 }
548
549 return false;
550}
551
552static void getObjCClassCategory(StringRef In, StringRef &Class,
553 StringRef &Category) {
554 if (!hasObjCCategory(In)) {
555 Class = In.slice(In.find('[') + 1, In.find(' '));
556 Category = "";
557 return;
558 }
559
560 Class = In.slice(In.find('[') + 1, In.find('('));
561 Category = In.slice(In.find('[') + 1, In.find(' '));
562 return;
563}
564
Devang Patel1a0df9a2010-05-10 22:49:55 +0000565/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel1f4f98d2011-08-15 23:36:40 +0000566void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
567 const MDNode *N) {
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000568 CompileUnit *&CURef = SPMap[N];
569 if (CURef)
570 return;
571 CURef = TheCU;
572
Devang Patel80ae3492009-08-28 23:24:31 +0000573 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000574 if (!SP.isDefinition())
575 // This is a method declaration which will be handled while constructing
576 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000577 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000578
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000579 DISubprogram SPDecl = SP.getFunctionDeclaration();
580 DIE *DeclDie = NULL;
581 if (SPDecl.isSubprogram()) {
582 DeclDie = TheCU->getOrCreateSubprogramDIE(SPDecl);
583 }
584
Devang Patel89543712011-08-15 17:24:54 +0000585 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000586
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000587 if (DeclDie) {
588 // Refer function declaration directly.
589 TheCU->addDIEEntry(SubprogramDie, dwarf::DW_AT_specification,
590 dwarf::DW_FORM_ref4, DeclDie);
591 }
592
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000593 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000594 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000595
596 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +0000597 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +0000598
Bill Wendling2b128d72009-05-20 23:19:06 +0000599 // Expose as global.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000600 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000601
Eric Christopher4996c702011-11-07 09:24:32 +0000602 // Add to Accel Names
603 TheCU->addAccelName(SP.getName(), SubprogramDie);
604
605 // If this is an Objective-C selector name add it to the ObjC accelerator too.
606 if (isObjCClass(SP.getName())) {
607 StringRef Class, Category;
608 getObjCClassCategory(SP.getName(), Class, Category);
609 TheCU->addAccelObjC(Class, SubprogramDie);
610 if (Category != "")
611 TheCU->addAccelObjC(Category, SubprogramDie);
612 }
613
Devang Patel0751a282009-06-26 01:49:18 +0000614 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000615}
616
Devang Patel07bb9ee2011-08-15 23:47:24 +0000617/// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
618/// as llvm.dbg.enum and llvm.dbg.ty
619void DwarfDebug::collectInfoFromNamedMDNodes(Module *M) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000620 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp"))
621 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
622 const MDNode *N = NMD->getOperand(i);
623 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
624 constructSubprogramDIE(CU, N);
625 }
626
627 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv"))
628 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
629 const MDNode *N = NMD->getOperand(i);
630 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000631 CU->createGlobalVariableDIE(N);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000632 }
633
Devang Patel07bb9ee2011-08-15 23:47:24 +0000634 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
635 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
636 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000637 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
638 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000639 }
640
641 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
642 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
643 DIType Ty(NMD->getOperand(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000644 if (CompileUnit *CU = CUMap.lookup(Ty.getCompileUnit()))
645 CU->getOrCreateTypeDIE(Ty);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000646 }
647}
648
649/// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
650/// FIXME - Remove this when dragon-egg and llvm-gcc switch to DIBuilder.
651bool DwarfDebug::collectLegacyDebugInfo(Module *M) {
652 DebugInfoFinder DbgFinder;
653 DbgFinder.processModule(*M);
654
655 bool HasDebugInfo = false;
656 // Scan all the compile-units to see if there are any marked as the main
657 // unit. If not, we do not generate debug info.
658 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
659 E = DbgFinder.compile_unit_end(); I != E; ++I) {
660 if (DICompileUnit(*I).isMain()) {
661 HasDebugInfo = true;
662 break;
663 }
664 }
665 if (!HasDebugInfo) return false;
666
667 // Create all the compile unit DIEs.
668 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
669 E = DbgFinder.compile_unit_end(); I != E; ++I)
670 constructCompileUnit(*I);
671
672 // Create DIEs for each global variable.
673 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
674 E = DbgFinder.global_variable_end(); I != E; ++I) {
675 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000676 if (CompileUnit *CU = CUMap.lookup(DIGlobalVariable(N).getCompileUnit()))
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000677 CU->createGlobalVariableDIE(N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000678 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000679
Devang Patel07bb9ee2011-08-15 23:47:24 +0000680 // Create DIEs for each subprogram.
681 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
682 E = DbgFinder.subprogram_end(); I != E; ++I) {
683 const MDNode *N = *I;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000684 if (CompileUnit *CU = CUMap.lookup(DISubprogram(N).getCompileUnit()))
685 constructSubprogramDIE(CU, N);
Devang Patel07bb9ee2011-08-15 23:47:24 +0000686 }
687
688 return HasDebugInfo;
689}
690
Devang Patel930143b2009-11-21 02:48:08 +0000691/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000692/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky019d2552011-07-29 03:49:23 +0000693/// This is invoked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +0000694void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +0000695 if (DisableDebugInfoPrinting)
696 return;
697
Nick Lewycky019d2552011-07-29 03:49:23 +0000698 // If module has named metadata anchors then use them, otherwise scan the
699 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000700 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
701 if (CU_Nodes) {
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000702 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
703 DICompileUnit CUNode(CU_Nodes->getOperand(i));
704 CompileUnit *CU = constructCompileUnit(CUNode);
705 DIArray GVs = CUNode.getGlobalVariables();
706 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Devang Patel0ecbcbd2011-08-18 23:17:55 +0000707 CU->createGlobalVariableDIE(GVs.getElement(i));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000708 DIArray SPs = CUNode.getSubprograms();
709 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
710 constructSubprogramDIE(CU, SPs.getElement(i));
711 DIArray EnumTypes = CUNode.getEnumTypes();
712 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
713 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
714 DIArray RetainedTypes = CUNode.getRetainedTypes();
715 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
716 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
717 }
Devang Patel07bb9ee2011-08-15 23:47:24 +0000718 } else if (!collectLegacyDebugInfo(M))
719 return;
Devang Patele02e5852011-05-03 16:45:22 +0000720
Devang Patel07bb9ee2011-08-15 23:47:24 +0000721 collectInfoFromNamedMDNodes(M);
Devang Patele02e5852011-05-03 16:45:22 +0000722
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000723 // Tell MMI that we have debug info.
724 MMI->setDebugInfoAvailability(true);
Devang Patele02e5852011-05-03 16:45:22 +0000725
Chris Lattnerd442aa32010-04-04 23:17:54 +0000726 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000727 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000728
Bill Wendling2b128d72009-05-20 23:19:06 +0000729 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +0000730 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +0000731}
732
Devang Patel930143b2009-11-21 02:48:08 +0000733/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +0000734///
Devang Patel930143b2009-11-21 02:48:08 +0000735void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000736 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +0000737 const Module *M = MMI->getModule();
Devang Patel7e623022011-08-10 20:55:27 +0000738 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +0000739
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000740 // Collect info for variables that were optimized out.
741 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
742 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
743 DICompileUnit TheCU(CU_Nodes->getOperand(i));
744 DIArray Subprograms = TheCU.getSubprograms();
745 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
746 DISubprogram SP(Subprograms.getElement(i));
747 if (ProcessedSPNodes.count(SP) != 0) continue;
748 if (!SP.Verify()) continue;
749 if (!SP.isDefinition()) continue;
Devang Patel59e27c52011-08-19 23:28:12 +0000750 DIArray Variables = SP.getVariables();
751 if (Variables.getNumElements() == 0) continue;
752
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000753 LexicalScope *Scope =
754 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
755 DeadFnScopeMap[SP] = Scope;
756
757 // Construct subprogram DIE and add variables DIEs.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000758 CompileUnit *SPCU = CUMap.lookup(TheCU);
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000759 assert(SPCU && "Unable to find Compile Unit!");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000760 constructSubprogramDIE(SPCU, SP);
761 DIE *ScopeDIE = SPCU->getDIE(SP);
Devang Patel59e27c52011-08-19 23:28:12 +0000762 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
763 DIVariable DV(Variables.getElement(vi));
764 if (!DV.Verify()) continue;
765 DbgVariable *NewVar = new DbgVariable(DV, NULL);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000766 if (DIE *VariableDIE =
Devang Patel59e27c52011-08-19 23:28:12 +0000767 SPCU->constructVariableDIE(NewVar, Scope->isAbstractScope()))
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000768 ScopeDIE->addChild(VariableDIE);
769 }
Devang Patelf3b2db62010-06-28 18:25:03 +0000770 }
771 }
772 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000773
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000774 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
775 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
776 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
777 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +0000778 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000779 }
780
Devang Patel89543712011-08-15 17:24:54 +0000781 // Emit DW_AT_containing_type attribute to connect types with their
782 // vtable holding type.
783 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
784 CUE = CUMap.end(); CUI != CUE; ++CUI) {
785 CompileUnit *TheCU = CUI->second;
786 TheCU->constructContainingTypeDIEs();
Devang Pateleb57c592009-12-03 19:11:07 +0000787 }
788
Bill Wendling2b128d72009-05-20 23:19:06 +0000789 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000790 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000791 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000792 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000793 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +0000794
795 // End text sections.
796 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000797 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +0000798 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +0000799 }
800
Bill Wendling2b128d72009-05-20 23:19:06 +0000801 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +0000802 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +0000803
804 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +0000805 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000806
807 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +0000808 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +0000809
Eric Christopher4996c702011-11-07 09:24:32 +0000810 // Emit info into a dwarf accelerator table sections.
811 if (DwarfAccelTables) {
812 emitAccelNames();
813 emitAccelObjC();
814 emitAccelNamespaces();
815 emitAccelTypes();
816 }
817
Bill Wendling2b128d72009-05-20 23:19:06 +0000818 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +0000819 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +0000820
Devang Patel04d2f2d2009-11-24 01:14:22 +0000821 // Emit info into a debug pubtypes section.
822 emitDebugPubTypes();
823
Bill Wendling2b128d72009-05-20 23:19:06 +0000824 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +0000825 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +0000826
827 // Emit info into a debug aranges section.
828 EmitDebugARanges();
829
830 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +0000831 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +0000832
833 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +0000834 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000835
836 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +0000837 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000838
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000839 // Emit info into a debug str section.
840 emitDebugStr();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000841
Devang Pateld0701282010-08-02 17:32:15 +0000842 // clean up.
843 DeleteContainerSeconds(DeadFnScopeMap);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000844 SPMap.clear();
Devang Patel1a0df9a2010-05-10 22:49:55 +0000845 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
846 E = CUMap.end(); I != E; ++I)
847 delete I->second;
848 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +0000849}
850
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000851/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +0000852DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +0000853 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +0000854 LLVMContext &Ctx = DV->getContext();
855 // More then one inlined variable corresponds to one abstract variable.
856 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000857 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000858 if (AbsDbgVariable)
859 return AbsDbgVariable;
860
Devang Patel7e623022011-08-10 20:55:27 +0000861 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000862 if (!Scope)
863 return NULL;
864
Devang Patel99819b52011-08-15 19:01:20 +0000865 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patel7e623022011-08-10 20:55:27 +0000866 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000867 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000868 return AbsDbgVariable;
869}
870
Nick Lewycky019d2552011-07-29 03:49:23 +0000871/// addCurrentFnArgument - If Var is a current function argument then add
872/// it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +0000873bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +0000874 DbgVariable *Var, LexicalScope *Scope) {
875 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000876 return false;
877 DIVariable DV = Var->getVariable();
878 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
879 return false;
880 unsigned ArgNo = DV.getArgNumber();
881 if (ArgNo == 0)
882 return false;
883
Devang Patel4ab660b2011-03-03 20:02:02 +0000884 size_t Size = CurrentFnArguments.size();
885 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +0000886 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +0000887 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +0000888 // arguments does the function have at source level.
889 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +0000890 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +0000891 CurrentFnArguments[ArgNo - 1] = Var;
892 return true;
893}
894
Devang Patel490c8ab2010-05-20 19:57:06 +0000895/// collectVariableInfoFromMMITable - Collect variable information from
896/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000897void
Nick Lewycky019d2552011-07-29 03:49:23 +0000898DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patel490c8ab2010-05-20 19:57:06 +0000899 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +0000900 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
901 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
902 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +0000903 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000904 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +0000905 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +0000906 DIVariable DV(Var);
907 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000908
Devang Patel7e623022011-08-10 20:55:27 +0000909 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000910
Devang Patelcdb7d442009-11-10 23:20:04 +0000911 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +0000912 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +0000913 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000914
Devang Patele1c53f22010-05-20 16:36:41 +0000915 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel99819b52011-08-15 19:01:20 +0000916 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patel3e4a9652011-08-15 21:24:36 +0000917 RegVar->setFrameIndex(VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +0000918 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +0000919 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +0000920 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +0000921 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +0000922 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000923}
Devang Patela3e9c9c2010-03-15 18:33:46 +0000924
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000925/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +0000926/// DBG_VALUE instruction, is in a defined reg.
927static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000928 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000929 return MI->getNumOperands() == 3 &&
930 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
931 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000932}
933
Nick Lewycky019d2552011-07-29 03:49:23 +0000934/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel2442a892011-07-08 17:09:57 +0000935/// at MI.
936static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
937 const MCSymbol *FLabel,
938 const MCSymbol *SLabel,
939 const MachineInstr *MI) {
940 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
941
942 if (MI->getNumOperands() != 3) {
943 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
944 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
945 }
946 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
947 MachineLocation MLoc;
948 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
949 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
950 }
951 if (MI->getOperand(0).isImm())
952 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
953 if (MI->getOperand(0).isFPImm())
954 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
955 if (MI->getOperand(0).isCImm())
956 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
957
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000958 assert(0 && "Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +0000959 return DotDebugLocEntry();
960}
961
Devang Patel7e623022011-08-10 20:55:27 +0000962/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000963void
Devang Patel5c0f85c2010-06-25 22:07:34 +0000964DwarfDebug::collectVariableInfo(const MachineFunction *MF,
965 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000966
Devang Patel490c8ab2010-05-20 19:57:06 +0000967 /// collection info from MMI table.
968 collectVariableInfoFromMMITable(MF, Processed);
969
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000970 for (SmallVectorImpl<const MDNode*>::const_iterator
971 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
972 ++UVI) {
973 const MDNode *Var = *UVI;
974 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +0000975 continue;
976
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000977 // History contains relevant DBG_VALUE instructions for Var and instructions
978 // clobbering it.
979 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
980 if (History.empty())
981 continue;
982 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +0000983
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000984 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +0000985 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +0000986 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
987 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +0000988 Scope = LScopes.getCurrentFunctionScope();
Devang Patel8fb9fd62011-07-20 22:18:50 +0000989 else {
990 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patel7e623022011-08-10 20:55:27 +0000991 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel8fb9fd62011-07-20 22:18:50 +0000992 else {
993 if (MDNode *IA = DV.getInlinedAt())
Devang Patel7e623022011-08-10 20:55:27 +0000994 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel8fb9fd62011-07-20 22:18:50 +0000995 else
Devang Patel7e623022011-08-10 20:55:27 +0000996 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel8fb9fd62011-07-20 22:18:50 +0000997 }
998 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000999 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001000 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001001 continue;
1002
1003 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001004 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001005 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1006 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel6c622ef2011-03-01 22:58:55 +00001007 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001008 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001009 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001010 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001011
1012 // Simple ranges that are fully coalesced.
1013 if (History.size() <= 1 || (History.size() == 2 &&
1014 MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001015 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001016 continue;
1017 }
1018
1019 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001020 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001021
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001022 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1023 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1024 const MachineInstr *Begin = *HI;
1025 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001026
Devang Patele7181b52011-06-01 23:00:17 +00001027 // Check if DBG_VALUE is truncating a range.
1028 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1029 && !Begin->getOperand(0).getReg())
1030 continue;
1031
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001032 // Compute the range for a register location.
1033 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1034 const MCSymbol *SLabel = 0;
1035
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001036 if (HI + 1 == HE)
1037 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001038 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001039 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001040 else {
1041 const MachineInstr *End = HI[1];
Devang Patel53b050a2011-07-07 21:44:42 +00001042 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1043 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001044 if (End->isDebugValue())
1045 SLabel = getLabelBeforeInsn(End);
1046 else {
1047 // End is a normal instruction clobbering the range.
1048 SLabel = getLabelAfterInsn(End);
1049 assert(SLabel && "Forgot label after clobber instruction");
1050 ++HI;
1051 }
1052 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001053
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001054 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel2442a892011-07-08 17:09:57 +00001055 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001056 }
1057 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001058 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001059
1060 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001061 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1062 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1063 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1064 DIVariable DV(Variables.getElement(i));
1065 if (!DV || !DV.Verify() || !Processed.insert(DV))
1066 continue;
1067 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1068 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patele0a94bf2010-05-14 21:01:35 +00001069 }
Devang Patel9fc11702010-05-25 23:40:22 +00001070}
Devang Patele0a94bf2010-05-14 21:01:35 +00001071
Devang Patel9fc11702010-05-25 23:40:22 +00001072/// getLabelBeforeInsn - Return Label preceding the instruction.
1073const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001074 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1075 assert(Label && "Didn't insert label before instruction");
1076 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001077}
1078
1079/// getLabelAfterInsn - Return Label immediately following the instruction.
1080const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001081 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001082}
1083
Devang Patelb5694e72010-10-26 17:49:02 +00001084/// beginInstruction - Process beginning of an instruction.
1085void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001086 // Check if source location changes, but ignore DBG_VALUE locations.
1087 if (!MI->isDebugValue()) {
1088 DebugLoc DL = MI->getDebugLoc();
1089 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel34a66202011-05-11 19:22:19 +00001090 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001091 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001092 if (DL == PrologEndLoc) {
1093 Flags |= DWARF2_FLAG_PROLOGUE_END;
1094 PrologEndLoc = DebugLoc();
1095 }
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001096 if (!DL.isUnknown()) {
1097 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001098 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001099 } else
Devang Patel34a66202011-05-11 19:22:19 +00001100 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001101 }
Devang Patel9fc11702010-05-25 23:40:22 +00001102 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001103
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001104 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001105 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1106 LabelsBeforeInsn.find(MI);
1107
1108 // No label needed.
1109 if (I == LabelsBeforeInsn.end())
1110 return;
1111
1112 // Label already assigned.
1113 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001114 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001115
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001116 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001117 PrevLabel = MMI->getContext().CreateTempSymbol();
1118 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001119 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001120 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001121}
1122
Devang Patelb5694e72010-10-26 17:49:02 +00001123/// endInstruction - Process end of an instruction.
1124void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001125 // Don't create a new label after DBG_VALUE instructions.
1126 // They don't generate code.
1127 if (!MI->isDebugValue())
1128 PrevLabel = 0;
1129
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001130 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1131 LabelsAfterInsn.find(MI);
1132
1133 // No label needed.
1134 if (I == LabelsAfterInsn.end())
1135 return;
1136
1137 // Label already assigned.
1138 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001139 return;
1140
1141 // We need a label after this instruction.
1142 if (!PrevLabel) {
1143 PrevLabel = MMI->getContext().CreateTempSymbol();
1144 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001145 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001146 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001147}
1148
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001149/// identifyScopeMarkers() -
Devang Patel784077e2011-08-10 23:58:09 +00001150/// Each LexicalScope has first instruction and last instruction to mark
1151/// beginning and end of a scope respectively. Create an inverse map that list
1152/// scopes starts (and ends) with an instruction. One instruction may start (or
1153/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001154void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001155 SmallVector<LexicalScope *, 4> WorkList;
1156 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001157 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001158 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001159
Devang Patel7e623022011-08-10 20:55:27 +00001160 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001161 if (!Children.empty())
Devang Patel7e623022011-08-10 20:55:27 +00001162 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001163 SE = Children.end(); SI != SE; ++SI)
1164 WorkList.push_back(*SI);
1165
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001166 if (S->isAbstractScope())
1167 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001168
Devang Patel7e623022011-08-10 20:55:27 +00001169 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001170 if (Ranges.empty())
1171 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001172 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +00001173 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001174 assert(RI->first && "InsnRange does not have first instruction!");
1175 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001176 requestLabelBeforeInsn(RI->first);
1177 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001178 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001179 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001180}
1181
Devang Patel589845d2011-05-09 22:14:49 +00001182/// getScopeNode - Get MDNode for DebugLoc's scope.
1183static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1184 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1185 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1186 return DL.getScope(Ctx);
1187}
1188
Devang Patel34a66202011-05-11 19:22:19 +00001189/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1190/// line number info for the function.
1191static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1192 const MDNode *Scope = getScopeNode(DL, Ctx);
1193 DISubprogram SP = getDISubprogram(Scope);
1194 if (SP.Verify())
1195 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1196 return DebugLoc();
1197}
1198
Devang Patel930143b2009-11-21 02:48:08 +00001199/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00001200/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00001201void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001202 if (!MMI->hasDebugInfo()) return;
Devang Patel7e623022011-08-10 20:55:27 +00001203 LScopes.initialize(*MF);
1204 if (LScopes.empty()) return;
1205 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001206
Devang Patel6c74a872010-04-27 19:46:33 +00001207 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1208 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001209 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001210 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001211
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001212 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1213
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001214 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001215 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1216 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1217
Devang Patel002d54d2010-05-26 19:37:24 +00001218 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001219 I != E; ++I) {
1220 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001221 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1222 II != IE; ++II) {
1223 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001224
Devang Patel002d54d2010-05-26 19:37:24 +00001225 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001226 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001227
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001228 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001229 const MDNode *Var =
1230 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001231
1232 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001233 if (isDbgValueInDefinedReg(MI))
1234 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1235
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001236 // Check the history of this variable.
1237 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1238 if (History.empty()) {
1239 UserVariables.push_back(Var);
1240 // The first mention of a function argument gets the FunctionBeginSym
1241 // label, so arguments are visible when breaking at function entry.
1242 DIVariable DV(Var);
1243 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1244 DISubprogram(getDISubprogram(DV.getContext()))
1245 .describes(MF->getFunction()))
1246 LabelsBeforeInsn[MI] = FunctionBeginSym;
1247 } else {
1248 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1249 const MachineInstr *Prev = History.back();
1250 if (Prev->isDebugValue()) {
1251 // Coalesce identical entries at the end of History.
1252 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001253 Prev->isIdenticalTo(History[History.size() - 2])) {
1254 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1255 << "\t" << *Prev
1256 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001257 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001258 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001259
1260 // Terminate old register assignments that don't reach MI;
1261 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1262 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1263 isDbgValueInDefinedReg(Prev)) {
1264 // Previous register assignment needs to terminate at the end of
1265 // its basic block.
1266 MachineBasicBlock::const_iterator LastMI =
1267 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001268 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001269 // Drop DBG_VALUE for empty range.
Devang Patelb7a328e2011-07-07 00:14:27 +00001270 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1271 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001272 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001273 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001274 else {
1275 // Terminate after LastMI.
1276 History.push_back(LastMI);
1277 }
1278 }
1279 }
1280 }
1281 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001282 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001283 // Not a DBG_VALUE instruction.
1284 if (!MI->isLabel())
1285 AtBlockEntry = false;
1286
Devang Patel34a66202011-05-11 19:22:19 +00001287 // First known non DBG_VALUE location marks beginning of function
1288 // body.
1289 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1290 PrologEndLoc = MI->getDebugLoc();
1291
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001292 // Check if the instruction clobbers any registers with debug vars.
1293 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1294 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1295 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1296 continue;
1297 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1298 unsigned Reg = *AI; ++AI) {
1299 const MDNode *Var = LiveUserVar[Reg];
1300 if (!Var)
1301 continue;
1302 // Reg is now clobbered.
1303 LiveUserVar[Reg] = 0;
1304
1305 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001306 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1307 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001308 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001309 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1310 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001311 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001312 const MachineInstr *Prev = History.back();
1313 // Sanity-check: Register assignments are terminated at the end of
1314 // their block.
1315 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1316 continue;
1317 // Is the variable still in Reg?
1318 if (!isDbgValueInDefinedReg(Prev) ||
1319 Prev->getOperand(0).getReg() != Reg)
1320 continue;
1321 // Var is clobbered. Make sure the next instruction gets a label.
1322 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001323 }
1324 }
Devang Patel002d54d2010-05-26 19:37:24 +00001325 }
Devang Patel002d54d2010-05-26 19:37:24 +00001326 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001327 }
1328
1329 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1330 I != E; ++I) {
1331 SmallVectorImpl<const MachineInstr*> &History = I->second;
1332 if (History.empty())
1333 continue;
1334
1335 // Make sure the final register assignments are terminated.
1336 const MachineInstr *Prev = History.back();
1337 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1338 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel784077e2011-08-10 23:58:09 +00001339 MachineBasicBlock::const_iterator LastMI =
1340 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001341 if (LastMI == PrevMBB->end())
1342 // Drop DBG_VALUE for empty range.
1343 History.pop_back();
1344 else {
1345 // Terminate after LastMI.
1346 History.push_back(LastMI);
1347 }
1348 }
1349 // Request labels for the full history.
1350 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1351 const MachineInstr *MI = History[i];
1352 if (MI->isDebugValue())
1353 requestLabelBeforeInsn(MI);
1354 else
1355 requestLabelAfterInsn(MI);
1356 }
1357 }
Devang Patel002d54d2010-05-26 19:37:24 +00001358
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001359 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001360 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001361
1362 // Record beginning of function.
1363 if (!PrologEndLoc.isUnknown()) {
1364 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1365 MF->getFunction()->getContext());
1366 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1367 FnStartDL.getScope(MF->getFunction()->getContext()),
1368 DWARF2_FLAG_IS_STMT);
1369 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001370}
1371
Devang Patel7e623022011-08-10 20:55:27 +00001372void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1373// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1374 ScopeVariables[LS].push_back(Var);
1375// Vars.push_back(Var);
1376}
1377
Devang Patel930143b2009-11-21 02:48:08 +00001378/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00001379///
Chris Lattner76555b52010-01-26 23:18:02 +00001380void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patel7e623022011-08-10 20:55:27 +00001381 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00001382
Devang Patel7e623022011-08-10 20:55:27 +00001383 // Define end label for subprogram.
1384 FunctionEndSym = Asm->GetTempSymbol("func_end",
1385 Asm->getFunctionNumber());
1386 // Assumes in correct section after the entry point.
1387 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1388
1389 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1390 collectVariableInfo(MF, ProcessedVars);
1391
Devang Patel3acc70e2011-08-15 22:04:40 +00001392 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001393 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001394 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001395
Devang Patel7e623022011-08-10 20:55:27 +00001396 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001397 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1398 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1399 LexicalScope *AScope = AList[i];
1400 DISubprogram SP(AScope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +00001401 if (SP.Verify()) {
1402 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001403 DIArray Variables = SP.getVariables();
1404 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1405 DIVariable DV(Variables.getElement(i));
1406 if (!DV || !DV.Verify() || !ProcessedVars.insert(DV))
1407 continue;
1408 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1409 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001410 }
1411 }
Devang Patel44403472011-08-12 18:10:19 +00001412 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Devang Patel3acc70e2011-08-15 22:04:40 +00001413 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001414 }
Devang Patel7e623022011-08-10 20:55:27 +00001415
Devang Patel3acc70e2011-08-15 22:04:40 +00001416 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Devang Patel7e623022011-08-10 20:55:27 +00001417
Devang Patel3acc70e2011-08-15 22:04:40 +00001418 if (!DisableFramePointerElim(*MF))
Devang Patel784077e2011-08-10 23:58:09 +00001419 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1420 dwarf::DW_FORM_flag, 1);
Devang Patel3acc70e2011-08-15 22:04:40 +00001421
Devang Patel7e623022011-08-10 20:55:27 +00001422 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1423 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00001424
Bill Wendling2b128d72009-05-20 23:19:06 +00001425 // Clear debug info
Devang Patel7e623022011-08-10 20:55:27 +00001426 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1427 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1428 DeleteContainerPointers(I->second);
1429 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001430 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001431 UserVariables.clear();
1432 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001433 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001434 LabelsBeforeInsn.clear();
1435 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001436 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001437}
1438
Chris Lattnerba35a672010-03-09 04:54:43 +00001439/// recordSourceLine - Register a source line with debug info. Returns the
1440/// unique label that was emitted and which provides correspondence to
1441/// the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001442void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1443 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001444 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001445 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001446 unsigned Src = 1;
1447 if (S) {
1448 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001449
Dan Gohman50849c62010-05-05 23:41:32 +00001450 if (Scope.isCompileUnit()) {
1451 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001452 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001453 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001454 } else if (Scope.isFile()) {
1455 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001456 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001457 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001458 } else if (Scope.isSubprogram()) {
1459 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001460 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001461 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001462 } else if (Scope.isLexicalBlockFile()) {
1463 DILexicalBlockFile DBF(S);
1464 Fn = DBF.getFilename();
1465 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001466 } else if (Scope.isLexicalBlock()) {
1467 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001468 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001469 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001470 } else
1471 assert(0 && "Unexpected scope info");
1472
Devang Patele01b75c2011-03-24 20:30:50 +00001473 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00001474 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001475 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001476}
1477
Bill Wendling806535f2009-05-20 23:22:40 +00001478//===----------------------------------------------------------------------===//
1479// Emit Methods
1480//===----------------------------------------------------------------------===//
1481
Devang Patel930143b2009-11-21 02:48:08 +00001482/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00001483///
Jim Grosbach00e9c612009-11-22 19:20:36 +00001484unsigned
1485DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00001486 // Get the children.
1487 const std::vector<DIE *> &Children = Die->getChildren();
1488
1489 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001490 if (!Last && !Children.empty())
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001491 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling480ff322009-05-20 23:21:38 +00001492
1493 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001494 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001495
1496 // Get the abbreviation for this DIE.
1497 unsigned AbbrevNumber = Die->getAbbrevNumber();
1498 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1499
1500 // Set DIE offset
1501 Die->setOffset(Offset);
1502
1503 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001504 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001505
1506 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1507 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1508
1509 // Size the DIE attribute values.
1510 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1511 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001512 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001513
1514 // Size the DIE children if any.
1515 if (!Children.empty()) {
1516 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1517 "Children flag not set");
1518
1519 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001520 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00001521
1522 // End of children marker.
1523 Offset += sizeof(int8_t);
1524 }
1525
1526 Die->setSize(Offset - Die->getOffset());
1527 return Offset;
1528}
1529
Devang Patel930143b2009-11-21 02:48:08 +00001530/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00001531///
Devang Patel930143b2009-11-21 02:48:08 +00001532void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001533 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1534 E = CUMap.end(); I != E; ++I) {
1535 // Compute size of compile unit header.
Devang Patel28dce702011-04-12 23:10:47 +00001536 unsigned Offset =
Devang Patel1a0df9a2010-05-10 22:49:55 +00001537 sizeof(int32_t) + // Length of Compilation Unit Info
1538 sizeof(int16_t) + // DWARF version number
1539 sizeof(int32_t) + // Offset Into Abbrev. Section
1540 sizeof(int8_t); // Pointer Size (in bytes)
1541 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001542 }
Bill Wendling480ff322009-05-20 23:21:38 +00001543}
1544
Chris Lattner6629ca92010-04-04 22:59:04 +00001545/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1546/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00001547void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001548 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001549
Bill Wendling480ff322009-05-20 23:21:38 +00001550 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001551 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001552 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001553 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001554 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001555 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001556
Chris Lattner6629ca92010-04-04 22:59:04 +00001557 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001558 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001559
Devang Patel4a213872010-08-24 00:06:12 +00001560 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001561 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1562 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1563 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001564 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001565 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00001566 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1567 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001568
Devang Patel9fc11702010-05-25 23:40:22 +00001569 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1570 "section_debug_loc");
1571
Chris Lattner6629ca92010-04-04 22:59:04 +00001572 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00001573 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001574}
1575
Nick Lewycky019d2552011-07-29 03:49:23 +00001576/// emitDIE - Recursively emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00001577///
Devang Patel930143b2009-11-21 02:48:08 +00001578void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001579 // Get the abbreviation for this DIE.
1580 unsigned AbbrevNumber = Die->getAbbrevNumber();
1581 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1582
Bill Wendling480ff322009-05-20 23:21:38 +00001583 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001584 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00001585 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1586 Twine::utohexstr(Die->getOffset()) + ":0x" +
1587 Twine::utohexstr(Die->getSize()) + " " +
1588 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00001589 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001590
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001591 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00001592 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1593
1594 // Emit the DIE attribute values.
1595 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1596 unsigned Attr = AbbrevData[i].getAttribute();
1597 unsigned Form = AbbrevData[i].getForm();
1598 assert(Form && "Too many attributes for DIE (check abbreviation)");
1599
Chris Lattner7bde8c02010-04-04 18:52:31 +00001600 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001601 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001602
Bill Wendling480ff322009-05-20 23:21:38 +00001603 switch (Attr) {
1604 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00001605 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00001606 break;
1607 case dwarf::DW_AT_abstract_origin: {
1608 DIEEntry *E = cast<DIEEntry>(Values[i]);
1609 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001610 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00001611 Asm->EmitInt32(Addr);
1612 break;
1613 }
Devang Patel12563b32010-04-16 23:33:45 +00001614 case dwarf::DW_AT_ranges: {
1615 // DW_AT_range Value encodes offset in debug_range section.
1616 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00001617
1618 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1619 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1620 V->getValue(),
1621 4);
1622 } else {
1623 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1624 V->getValue(),
1625 DwarfDebugRangeSectionSym,
1626 4);
1627 }
Devang Patel12563b32010-04-16 23:33:45 +00001628 break;
1629 }
Devang Patel9fc11702010-05-25 23:40:22 +00001630 case dwarf::DW_AT_location: {
Devang Pateld8994442011-08-15 21:43:21 +00001631 if (DIELabel *L = dyn_cast<DIELabel>(Values[i]))
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001632 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Pateld8994442011-08-15 21:43:21 +00001633 else
Devang Patel9fc11702010-05-25 23:40:22 +00001634 Values[i]->EmitValue(Asm, Form);
1635 break;
1636 }
Devang Patela1bd5a12010-09-29 19:08:08 +00001637 case dwarf::DW_AT_accessibility: {
1638 if (Asm->isVerbose()) {
1639 DIEInteger *V = cast<DIEInteger>(Values[i]);
1640 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1641 }
1642 Values[i]->EmitValue(Asm, Form);
1643 break;
1644 }
Bill Wendling480ff322009-05-20 23:21:38 +00001645 default:
1646 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001647 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001648 break;
1649 }
Bill Wendling480ff322009-05-20 23:21:38 +00001650 }
1651
1652 // Emit the DIE children if any.
1653 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1654 const std::vector<DIE *> &Children = Die->getChildren();
1655
1656 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001657 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00001658
Chris Lattner7bde8c02010-04-04 18:52:31 +00001659 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00001660 Asm->OutStreamer.AddComment("End Of Children Mark");
1661 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001662 }
1663}
1664
Devang Patel9ccfb642009-12-09 18:24:21 +00001665/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00001666///
Devang Patel9ccfb642009-12-09 18:24:21 +00001667void DwarfDebug::emitDebugInfo() {
1668 // Start debug info section.
1669 Asm->OutStreamer.SwitchSection(
1670 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001671 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1672 E = CUMap.end(); I != E; ++I) {
1673 CompileUnit *TheCU = I->second;
1674 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001675
Devang Patel1a0df9a2010-05-10 22:49:55 +00001676 // Emit the compile units header.
1677 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1678 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001679
Devang Patel1a0df9a2010-05-10 22:49:55 +00001680 // Emit size of content not including length itself
1681 unsigned ContentSize = Die->getSize() +
1682 sizeof(int16_t) + // DWARF version number
1683 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00001684 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001685
Devang Patel1a0df9a2010-05-10 22:49:55 +00001686 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1687 Asm->EmitInt32(ContentSize);
1688 Asm->OutStreamer.AddComment("DWARF version number");
1689 Asm->EmitInt16(dwarf::DWARF_VERSION);
1690 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1691 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1692 DwarfAbbrevSectionSym);
1693 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1694 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001695
Devang Patel1a0df9a2010-05-10 22:49:55 +00001696 emitDIE(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001697 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1698 }
Bill Wendling480ff322009-05-20 23:21:38 +00001699}
1700
Devang Patel930143b2009-11-21 02:48:08 +00001701/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00001702///
Devang Patel930143b2009-11-21 02:48:08 +00001703void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00001704 // Check to see if it is worth the effort.
1705 if (!Abbreviations.empty()) {
1706 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001707 Asm->OutStreamer.SwitchSection(
1708 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001709
Chris Lattnera179b522010-04-04 19:25:43 +00001710 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00001711
1712 // For each abbrevation.
1713 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1714 // Get abbreviation data
1715 const DIEAbbrev *Abbrev = Abbreviations[i];
1716
1717 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00001718 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00001719
1720 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001721 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00001722 }
1723
1724 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00001725 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00001726
Chris Lattnera179b522010-04-04 19:25:43 +00001727 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00001728 }
1729}
1730
Devang Patel930143b2009-11-21 02:48:08 +00001731/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00001732/// the line matrix.
1733///
Devang Patel930143b2009-11-21 02:48:08 +00001734void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00001735 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00001736 Asm->OutStreamer.AddComment("Extended Op");
1737 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001738
Chris Lattner566cae92010-03-09 23:52:58 +00001739 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00001740 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00001741 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1742 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1743
1744 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00001745
Chris Lattnera179b522010-04-04 19:25:43 +00001746 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00001747 Asm->getTargetData().getPointerSize(),
1748 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00001749
1750 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00001751 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1752 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001753 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00001754 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00001755}
1756
Eric Christopher4996c702011-11-07 09:24:32 +00001757/// emitAccelNames - Emit visible names into a hashed accelerator table
1758/// section.
1759void DwarfDebug::emitAccelNames() {
1760 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1761 dwarf::DW_FORM_data4));
1762 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1763 E = CUMap.end(); I != E; ++I) {
1764 CompileUnit *TheCU = I->second;
1765 const StringMap<DIE*> &Names = TheCU->getAccelNames();
1766 for (StringMap<DIE*>::const_iterator
1767 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1768 const char *Name = GI->getKeyData();
1769 DIE *Entity = GI->second;
1770 AT.AddName(Name, Entity);
1771 }
1772 }
1773
1774 AT.FinalizeTable(Asm, "Names");
1775 Asm->OutStreamer.SwitchSection(
1776 Asm->getObjFileLowering().getDwarfAccelNamesSection());
1777 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
1778 Asm->OutStreamer.EmitLabel(SectionBegin);
1779
1780 // Emit the full data.
1781 AT.Emit(Asm, SectionBegin, this);
1782}
1783
1784/// emitAccelObjC - Emit objective C classes and categories into a hashed
1785/// accelerator table section.
1786void DwarfDebug::emitAccelObjC() {
1787 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1788 dwarf::DW_FORM_data4));
1789 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1790 E = CUMap.end(); I != E; ++I) {
1791 CompileUnit *TheCU = I->second;
1792 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
1793 for (StringMap<std::vector<DIE*> >::const_iterator
1794 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1795 const char *Name = GI->getKeyData();
1796 std::vector<DIE *> Entities = GI->second;
1797 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
1798 DE = Entities.end(); DI != DE; ++DI)
1799 AT.AddName(Name, (*DI));
1800 }
1801 }
1802
1803 AT.FinalizeTable(Asm, "ObjC");
1804 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1805 .getDwarfAccelObjCSection());
1806 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
1807 Asm->OutStreamer.EmitLabel(SectionBegin);
1808
1809 // Emit the full data.
1810 AT.Emit(Asm, SectionBegin, this);
1811}
1812
1813/// emitAccelNamespace - Emit namespace dies into a hashed accelerator
1814/// table.
1815void DwarfDebug::emitAccelNamespaces() {
1816 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1817 dwarf::DW_FORM_data4));
1818 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1819 E = CUMap.end(); I != E; ++I) {
1820 CompileUnit *TheCU = I->second;
1821 const StringMap<DIE*> &Names = TheCU->getAccelNamespace();
1822 for (StringMap<DIE*>::const_iterator
1823 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1824 const char *Name = GI->getKeyData();
1825 DIE *Entity = GI->second;
1826 AT.AddName(Name, Entity);
1827 }
1828 }
1829
1830 AT.FinalizeTable(Asm, "namespac");
1831 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1832 .getDwarfAccelNamespaceSection());
1833 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
1834 Asm->OutStreamer.EmitLabel(SectionBegin);
1835
1836 // Emit the full data.
1837 AT.Emit(Asm, SectionBegin, this);
1838}
1839
1840/// emitAccelTypes() - Emit type dies into a hashed accelerator table.
1841void DwarfDebug::emitAccelTypes() {
1842 DwarfAccelTable AT(DwarfAccelTable::Atom(DwarfAccelTable::eAtomTypeDIEOffset,
1843 dwarf::DW_FORM_data4));
1844 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1845 E = CUMap.end(); I != E; ++I) {
1846 CompileUnit *TheCU = I->second;
1847 const StringMap<DIE*> &Names = TheCU->getGlobalTypes();
1848 //TODO: TheCU->getAccelTypes();
1849 for (StringMap<DIE*>::const_iterator
1850 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
1851 const char *Name = GI->getKeyData();
1852 DIE *Entity = GI->second;
1853 AT.AddName(Name, Entity);
1854 }
1855 }
1856
1857 AT.FinalizeTable(Asm, "types");
1858 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
1859 .getDwarfAccelTypesSection());
1860 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
1861 Asm->OutStreamer.EmitLabel(SectionBegin);
1862
1863 // Emit the full data.
1864 AT.Emit(Asm, SectionBegin, this);
1865}
1866
Devang Patel9ccfb642009-12-09 18:24:21 +00001867/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1868///
1869void DwarfDebug::emitDebugPubNames() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001870 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1871 E = CUMap.end(); I != E; ++I) {
1872 CompileUnit *TheCU = I->second;
1873 // Start the dwarf pubnames section.
1874 Asm->OutStreamer.SwitchSection(
1875 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001876
Devang Patel1a0df9a2010-05-10 22:49:55 +00001877 Asm->OutStreamer.AddComment("Length of Public Names Info");
1878 Asm->EmitLabelDifference(
1879 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1880 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001881
Devang Patel1a0df9a2010-05-10 22:49:55 +00001882 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1883 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001884
Devang Patel1a0df9a2010-05-10 22:49:55 +00001885 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001886 Asm->EmitInt16(dwarf::DWARF_VERSION);
1887
Devang Patel1a0df9a2010-05-10 22:49:55 +00001888 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001889 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00001890 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001891
Devang Patel1a0df9a2010-05-10 22:49:55 +00001892 Asm->OutStreamer.AddComment("Compilation Unit Length");
1893 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1894 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1895 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001896
Devang Patel1a0df9a2010-05-10 22:49:55 +00001897 const StringMap<DIE*> &Globals = TheCU->getGlobals();
1898 for (StringMap<DIE*>::const_iterator
1899 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1900 const char *Name = GI->getKeyData();
1901 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001902
Devang Patel1a0df9a2010-05-10 22:49:55 +00001903 Asm->OutStreamer.AddComment("DIE offset");
1904 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001905
Devang Patel1a0df9a2010-05-10 22:49:55 +00001906 if (Asm->isVerbose())
1907 Asm->OutStreamer.AddComment("External Name");
1908 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
1909 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001910
Devang Patel1a0df9a2010-05-10 22:49:55 +00001911 Asm->OutStreamer.AddComment("End Mark");
1912 Asm->EmitInt32(0);
1913 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky019d2552011-07-29 03:49:23 +00001914 TheCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00001915 }
Bill Wendling480ff322009-05-20 23:21:38 +00001916}
1917
Devang Patel04d2f2d2009-11-24 01:14:22 +00001918void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001919 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1920 E = CUMap.end(); I != E; ++I) {
1921 CompileUnit *TheCU = I->second;
Eric Christopher6abc9c52011-11-07 09:18:35 +00001922 // Start the dwarf pubtypes section.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001923 Asm->OutStreamer.SwitchSection(
1924 Asm->getObjFileLowering().getDwarfPubTypesSection());
1925 Asm->OutStreamer.AddComment("Length of Public Types Info");
1926 Asm->EmitLabelDifference(
1927 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1928 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001929
Devang Patel1a0df9a2010-05-10 22:49:55 +00001930 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1931 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001932
Devang Patel1a0df9a2010-05-10 22:49:55 +00001933 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1934 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001935
Devang Patel1a0df9a2010-05-10 22:49:55 +00001936 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1937 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1938 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001939
Devang Patel1a0df9a2010-05-10 22:49:55 +00001940 Asm->OutStreamer.AddComment("Compilation Unit Length");
1941 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1942 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1943 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001944
Devang Patel1a0df9a2010-05-10 22:49:55 +00001945 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1946 for (StringMap<DIE*>::const_iterator
1947 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1948 const char *Name = GI->getKeyData();
Nick Lewycky019d2552011-07-29 03:49:23 +00001949 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001950
Devang Patel1a0df9a2010-05-10 22:49:55 +00001951 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1952 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001953
Devang Patel1a0df9a2010-05-10 22:49:55 +00001954 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
1955 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1956 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001957
Devang Patel1a0df9a2010-05-10 22:49:55 +00001958 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001959 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001960 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1961 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00001962 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001963}
1964
Devang Patel930143b2009-11-21 02:48:08 +00001965/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00001966///
Devang Patel930143b2009-11-21 02:48:08 +00001967void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00001968 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00001969 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001970
Chris Lattner3d72a672010-03-09 23:38:23 +00001971 // Start the dwarf str section.
1972 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001973 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001974
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001975 // Get all of the string pool entries and put them in an array by their ID so
1976 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001977 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001978 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001979
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001980 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1981 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1982 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001983
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001984 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001985
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001986 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00001987 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001988 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001989
Chris Lattner3d72a672010-03-09 23:38:23 +00001990 // Emit the string itself.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001991 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001992 Asm->OutStreamer.EmitZeros(1, 0);
Bill Wendling480ff322009-05-20 23:21:38 +00001993 }
1994}
1995
Devang Patel930143b2009-11-21 02:48:08 +00001996/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00001997///
Devang Patel930143b2009-11-21 02:48:08 +00001998void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001999 if (DotDebugLocEntries.empty())
2000 return;
2001
Devang Patel116a9d72011-02-04 22:57:18 +00002002 for (SmallVector<DotDebugLocEntry, 4>::iterator
2003 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2004 I != E; ++I) {
2005 DotDebugLocEntry &Entry = *I;
2006 if (I + 1 != DotDebugLocEntries.end())
2007 Entry.Merge(I+1);
2008 }
2009
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002010 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002011 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00002012 Asm->getObjFileLowering().getDwarfLocSection());
2013 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002014 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2015 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002016 for (SmallVector<DotDebugLocEntry, 4>::iterator
2017 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002018 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002019 DotDebugLocEntry &Entry = *I;
2020 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002021 if (Entry.isEmpty()) {
2022 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2023 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002024 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002025 } else {
2026 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2027 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patel3e021532011-04-28 02:22:40 +00002028 DIVariable DV(Entry.Variable);
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002029 Asm->OutStreamer.AddComment("Loc expr size");
2030 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2031 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2032 Asm->EmitLabelDifference(end, begin, 2);
2033 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002034 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002035 DIBasicType BTy(DV.getType());
2036 if (BTy.Verify() &&
2037 (BTy.getEncoding() == dwarf::DW_ATE_signed
2038 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2039 Asm->OutStreamer.AddComment("DW_OP_consts");
2040 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002041 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002042 } else {
2043 Asm->OutStreamer.AddComment("DW_OP_constu");
2044 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002045 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002046 }
Devang Pateled9fd452011-07-08 16:49:43 +00002047 } else if (Entry.isLocation()) {
2048 if (!DV.hasComplexAddress())
2049 // Regular entry.
Devang Patel3e021532011-04-28 02:22:40 +00002050 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Pateled9fd452011-07-08 16:49:43 +00002051 else {
2052 // Complex address entry.
2053 unsigned N = DV.getNumAddrElements();
2054 unsigned i = 0;
2055 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2056 if (Entry.Loc.getOffset()) {
2057 i = 2;
2058 Asm->EmitDwarfRegOp(Entry.Loc);
2059 Asm->OutStreamer.AddComment("DW_OP_deref");
2060 Asm->EmitInt8(dwarf::DW_OP_deref);
2061 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2062 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2063 Asm->EmitSLEB128(DV.getAddrElement(1));
2064 } else {
2065 // If first address element is OpPlus then emit
2066 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2067 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2068 Asm->EmitDwarfRegOp(Loc);
2069 i = 2;
2070 }
2071 } else {
2072 Asm->EmitDwarfRegOp(Entry.Loc);
2073 }
2074
2075 // Emit remaining complex address elements.
2076 for (; i < N; ++i) {
2077 uint64_t Element = DV.getAddrElement(i);
2078 if (Element == DIBuilder::OpPlus) {
2079 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2080 Asm->EmitULEB128(DV.getAddrElement(++i));
2081 } else if (Element == DIBuilder::OpDeref)
2082 Asm->EmitInt8(dwarf::DW_OP_deref);
2083 else llvm_unreachable("unknown Opcode found in complex address");
2084 }
Devang Patel3e021532011-04-28 02:22:40 +00002085 }
Devang Patel3e021532011-04-28 02:22:40 +00002086 }
Devang Pateled9fd452011-07-08 16:49:43 +00002087 // else ... ignore constant fp. There is not any good way to
2088 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002089 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002090 }
2091 }
Bill Wendling480ff322009-05-20 23:21:38 +00002092}
2093
2094/// EmitDebugARanges - Emit visible names into a debug aranges section.
2095///
2096void DwarfDebug::EmitDebugARanges() {
2097 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002098 Asm->OutStreamer.SwitchSection(
2099 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002100}
2101
Devang Patel930143b2009-11-21 02:48:08 +00002102/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00002103///
Devang Patel930143b2009-11-21 02:48:08 +00002104void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002105 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002106 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00002107 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00002108 unsigned char Size = Asm->getTargetData().getPointerSize();
2109 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002110 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00002111 I != E; ++I) {
2112 if (*I)
2113 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00002114 else
Devang Patel6c74a872010-04-27 19:46:33 +00002115 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00002116 }
Bill Wendling480ff322009-05-20 23:21:38 +00002117}
2118
Devang Patel930143b2009-11-21 02:48:08 +00002119/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00002120///
Devang Patel930143b2009-11-21 02:48:08 +00002121void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002122 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002123 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002124 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002125 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002126 }
2127}
2128
Devang Patel930143b2009-11-21 02:48:08 +00002129/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00002130/// Section Header:
2131/// 1. length of section
2132/// 2. Dwarf version number
2133/// 3. address size.
2134///
2135/// Entries (one "entry" for each function that was inlined):
2136///
2137/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2138/// otherwise offset into __debug_str for regular function name.
2139/// 2. offset into __debug_str section for regular function name.
2140/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2141/// instances for the function.
2142///
2143/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2144/// inlined instance; the die_offset points to the inlined_subroutine die in the
2145/// __debug_info section, and the low_pc is the starting address for the
2146/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002147void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00002148 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002149 return;
2150
Devang Patel1a0df9a2010-05-10 22:49:55 +00002151 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002152 return;
2153
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002154 Asm->OutStreamer.SwitchSection(
2155 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002156
Chris Lattner566cae92010-03-09 23:52:58 +00002157 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002158 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2159 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002160
Chris Lattnera179b522010-04-04 19:25:43 +00002161 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002162
Chris Lattner566cae92010-03-09 23:52:58 +00002163 Asm->OutStreamer.AddComment("Dwarf Version");
2164 Asm->EmitInt16(dwarf::DWARF_VERSION);
2165 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002166 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002167
Devang Patel32cc43c2010-05-07 20:54:48 +00002168 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002169 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002170
Devang Patel32cc43c2010-05-07 20:54:48 +00002171 const MDNode *Node = *I;
2172 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00002173 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002174 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002175 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002176 StringRef LName = SP.getLinkageName();
2177 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002178
Chris Lattner566cae92010-03-09 23:52:58 +00002179 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002180 if (LName.empty()) {
2181 Asm->OutStreamer.EmitBytes(Name, 0);
2182 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002183 } else
Chris Lattner70a4fce2010-04-04 23:25:33 +00002184 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2185 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002186
Chris Lattner566cae92010-03-09 23:52:58 +00002187 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00002188 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00002189 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002190
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002191 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002192 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002193 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002194 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002195
Chris Lattner7bde8c02010-04-04 18:52:31 +00002196 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002197 Asm->OutStreamer.EmitSymbolValue(LI->first,
2198 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00002199 }
2200 }
2201
Chris Lattnera179b522010-04-04 19:25:43 +00002202 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002203}