blob: ab52251eb85dbd40da6f30e01faadc6a604d211d [file] [log] [blame]
Bill Wendling2f921f82009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattnerb14490d2010-03-09 00:39:24 +000013
Devang Patel80ae3492009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000016#include "DIE.h"
Devang Patel5eb43192011-04-12 17:40:32 +000017#include "DwarfCompileUnit.h"
Bill Wendling30346342010-04-05 22:59:21 +000018#include "llvm/Constants.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000019#include "llvm/Module.h"
Devang Patel2d9e5322011-01-20 00:02:16 +000020#include "llvm/Instructions.h"
David Greene829b3e82009-08-19 21:52:55 +000021#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000023#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000024#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000025#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000026#include "llvm/MC/MCSymbol.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000027#include "llvm/Target/TargetData.h"
Anton Korobeynikov2f931282011-01-10 12:39:04 +000028#include "llvm/Target/TargetFrameLowering.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000029#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner21dc46e2010-04-04 18:06:11 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel61880932010-04-19 19:14:02 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000033#include "llvm/Analysis/DebugInfo.h"
Devang Patela5d93242011-02-24 18:49:30 +000034#include "llvm/Analysis/DIBuilder.h"
Devang Patela86114b2010-10-25 20:45:32 +000035#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000036#include "llvm/ADT/STLExtras.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000037#include "llvm/ADT/StringExtras.h"
Devang Patel6c74a872010-04-27 19:46:33 +000038#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000039#include "llvm/Support/Debug.h"
40#include "llvm/Support/ErrorHandling.h"
Devang Patel1973df22010-01-26 21:39:14 +000041#include "llvm/Support/ValueHandle.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000042#include "llvm/Support/FormattedStream.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000043#include "llvm/Support/Timer.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000044#include "llvm/Support/Path.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000045using namespace llvm;
46
Jim Grosbacha8683bb2010-07-21 21:21:52 +000047static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel30265c42010-07-07 20:12:52 +000048 cl::Hidden,
Devang Patel6c74a872010-04-27 19:46:33 +000049 cl::desc("Disable debug info printing"));
50
Dan Gohman7421ae42010-05-07 01:08:53 +000051static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000052 cl::desc("Make an absence of debug location information explicit."),
Dan Gohman7421ae42010-05-07 01:08:53 +000053 cl::init(false));
54
Bill Wendlingfcc14142010-04-07 09:28:04 +000055namespace {
56 const char *DWARFGroupName = "DWARF Emission";
57 const char *DbgTimerName = "DWARF Debug Writer";
58} // end anonymous namespace
59
Bill Wendling2f921f82009-05-15 09:23:25 +000060//===----------------------------------------------------------------------===//
61
62/// Configuration values for initial hash set sizes (log2).
63///
Bill Wendling2f921f82009-05-15 09:23:25 +000064static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +000065
66namespace llvm {
67
Nick Lewycky019d2552011-07-29 03:49:23 +000068DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +000069 DIType Ty = Var.getType();
70 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
71 // addresses instead.
72 if (Var.isBlockByrefVariable()) {
73 /* Byref variables, in Blocks, are declared by the programmer as
74 "SomeType VarName;", but the compiler creates a
75 __Block_byref_x_VarName struct, and gives the variable VarName
76 either the struct, or a pointer to the struct, as its type. This
77 is necessary for various behind-the-scenes things the compiler
78 needs to do with by-reference variables in blocks.
79
80 However, as far as the original *programmer* is concerned, the
81 variable should still have type 'SomeType', as originally declared.
82
83 The following function dives into the __Block_byref_x_VarName
84 struct to find the original type of the variable. This will be
85 passed back to the code generating the type for the Debug
86 Information Entry for the variable 'VarName'. 'VarName' will then
87 have the original type 'SomeType' in its debug information.
88
89 The original type 'SomeType' will be the type of the field named
90 'VarName' inside the __Block_byref_x_VarName struct.
91
92 NOTE: In order for this to not completely fail on the debugger
93 side, the Debug Information Entry for the variable VarName needs to
94 have a DW_AT_location that tells the debugger how to unwind through
95 the pointers and __Block_byref_x_VarName struct to find the actual
96 value of the variable. The function addBlockByrefType does this. */
97 DIType subType = Ty;
98 unsigned tag = Ty.getTag();
99
100 if (tag == dwarf::DW_TAG_pointer_type) {
101 DIDerivedType DTy = DIDerivedType(Ty);
102 subType = DTy.getTypeDerivedFrom();
103 }
104
105 DICompositeType blockStruct = DICompositeType(subType);
106 DIArray Elements = blockStruct.getTypeArray();
107
108 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
109 DIDescriptor Element = Elements.getElement(i);
110 DIDerivedType DT = DIDerivedType(Element);
111 if (getName() == DT.getName())
112 return (DT.getTypeDerivedFrom());
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000113 }
114 return Ty;
115 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000116 return Ty;
117}
Bill Wendling2f921f82009-05-15 09:23:25 +0000118
Chris Lattnerf5d06362010-04-05 04:09:20 +0000119} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000120
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000121DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel1a0df9a2010-05-10 22:49:55 +0000122 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000123 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel7e623022011-08-10 20:55:27 +0000124 PrevLabel(NULL) {
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000125 NextStringPoolNumber = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000126
Rafael Espindolaa7160962011-05-06 14:56:22 +0000127 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattnere58b5472010-04-04 23:10:38 +0000128 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000129 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000130 FunctionBeginSym = FunctionEndSym = 0;
Dan Gohman6e681a52010-06-18 15:56:31 +0000131 {
132 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
133 beginModule(M);
Torok Edwinf8dba242010-04-07 10:44:46 +0000134 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000135}
136DwarfDebug::~DwarfDebug() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000137}
138
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000139MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
140 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
141 if (Entry.first) return Entry.first;
142
143 Entry.second = NextStringPoolNumber++;
Chris Lattnera179b522010-04-04 19:25:43 +0000144 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000145}
146
147
Devang Patel930143b2009-11-21 02:48:08 +0000148/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000149///
Devang Patel930143b2009-11-21 02:48:08 +0000150void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000151 // Profile the node so that we can make it unique.
152 FoldingSetNodeID ID;
153 Abbrev.Profile(ID);
154
155 // Check the set for priors.
156 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
157
158 // If it's newly added.
159 if (InSet == &Abbrev) {
160 // Add to abbreviation list.
161 Abbreviations.push_back(&Abbrev);
162
163 // Assign the vector position + 1 as its number.
164 Abbrev.setNumber(Abbreviations.size());
165 } else {
166 // Assign existing abbreviation number.
167 Abbrev.setNumber(InSet->getNumber());
168 }
169}
170
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000171/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel43ef34d2010-01-05 01:46:14 +0000172/// printer to not emit usual symbol prefix before the symbol name is used then
173/// return linkage name after skipping this special LLVM prefix.
174static StringRef getRealLinkageName(StringRef LinkageName) {
175 char One = '\1';
176 if (LinkageName.startswith(StringRef(&One, 1)))
177 return LinkageName.substr(1);
178 return LinkageName;
179}
180
Jim Grosbach042483e2009-11-21 23:12:12 +0000181/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +0000182/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
183/// If there are global variables in this scope then create and insert
184/// DIEs for these variables.
Devang Patel32cc43c2010-05-07 20:54:48 +0000185DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000186 CompileUnit *SPCU = getCompileUnit(SPNode);
187 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +0000188
Chris Lattner3a383cb2010-04-05 00:13:49 +0000189 assert(SPDie && "Unable to find subprogram DIE!");
190 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000191
Devang Patel1d6bbd42011-04-22 23:10:17 +0000192 DISubprogram SPDecl = SP.getFunctionDeclaration();
193 if (SPDecl.isSubprogram())
194 // Refer function declaration directly.
Devang Patelf20c4f72011-04-12 22:53:02 +0000195 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Devang Patel89543712011-08-15 17:24:54 +0000196 SPCU->getOrCreateSubprogramDIE(SPDecl));
Devang Patel1d6bbd42011-04-22 23:10:17 +0000197 else {
198 // There is not any need to generate specification DIE for a function
199 // defined at compile unit level. If a function is defined inside another
200 // function then gdb prefers the definition at top level and but does not
201 // expect specification DIE in parent function. So avoid creating
202 // specification DIE for a function defined inside a function.
203 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
204 !SP.getContext().isFile() &&
205 !isSubprogramContext(SP.getContext())) {
206 SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
207
208 // Add arguments.
209 DICompositeType SPTy = SP.getType();
210 DIArray Args = SPTy.getTypeArray();
211 unsigned SPTag = SPTy.getTag();
212 if (SPTag == dwarf::DW_TAG_subroutine_type)
213 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
214 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
215 DIType ATy = DIType(DIType(Args.getElement(i)));
216 SPCU->addType(Arg, ATy);
217 if (ATy.isArtificial())
218 SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
219 SPDie->addChild(Arg);
220 }
221 DIE *SPDeclDie = SPDie;
222 SPDie = new DIE(dwarf::DW_TAG_subprogram);
223 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
224 SPDeclDie);
225 SPCU->addDie(SPDie);
226 }
Chris Lattner3a383cb2010-04-05 00:13:49 +0000227 }
Devang Patela37a95e2010-07-07 22:20:57 +0000228 // Pick up abstract subprogram DIE.
229 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
230 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf20c4f72011-04-12 22:53:02 +0000231 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
232 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patela37a95e2010-07-07 22:20:57 +0000233 SPCU->addDie(SPDie);
234 }
235
Devang Patelf20c4f72011-04-12 22:53:02 +0000236 SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
237 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
238 SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
239 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattner3a383cb2010-04-05 00:13:49 +0000240 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
241 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000242 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000243
Chris Lattner3a383cb2010-04-05 00:13:49 +0000244 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000245}
246
Jim Grosbach042483e2009-11-21 23:12:12 +0000247/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +0000248/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
Devang Patel7e623022011-08-10 20:55:27 +0000249DIE *DwarfDebug::constructLexicalScopeDIE(LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000250
251 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
252 if (Scope->isAbstractScope())
253 return ScopeDIE;
254
Devang Patel7e623022011-08-10 20:55:27 +0000255 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +0000256 if (Ranges.empty())
257 return 0;
258
Devang Patelf20c4f72011-04-12 22:53:02 +0000259 CompileUnit *TheCU = getCompileUnit(Scope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +0000260 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel6c74a872010-04-27 19:46:33 +0000261 if (Ranges.size() > 1) {
262 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000263 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +0000264 // DW_AT_ranges appropriately.
Devang Patelf20c4f72011-04-12 22:53:02 +0000265 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000266 DebugRangeSymbols.size()
267 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000268 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +0000269 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000270 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
271 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000272 }
273 DebugRangeSymbols.push_back(NULL);
274 DebugRangeSymbols.push_back(NULL);
275 return ScopeDIE;
276 }
277
Devang Patel9fc11702010-05-25 23:40:22 +0000278 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
279 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000280
Devang Patel9fc11702010-05-25 23:40:22 +0000281 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000282
Chris Lattnere13c3722010-03-09 01:58:53 +0000283 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
284 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000285
Devang Patelf20c4f72011-04-12 22:53:02 +0000286 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
287 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000288
289 return ScopeDIE;
290}
291
Devang Patel930143b2009-11-21 02:48:08 +0000292/// constructInlinedScopeDIE - This scope represents inlined body of
293/// a function. Construct DIE to represent this concrete inlined copy
294/// of the function.
Devang Patel7e623022011-08-10 20:55:27 +0000295DIE *DwarfDebug::constructInlinedScopeDIE(LexicalScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +0000296
Devang Patel7e623022011-08-10 20:55:27 +0000297 const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000298 assert (Ranges.empty() == false
Devang Patel7e623022011-08-10 20:55:27 +0000299 && "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000300
Devang Patelf098ce22011-07-27 00:34:13 +0000301 if (!Scope->getScopeNode())
302 return NULL;
303 DIScope DS(Scope->getScopeNode());
304 DISubprogram InlinedSP = getDISubprogram(DS);
305 CompileUnit *TheCU = getCompileUnit(InlinedSP);
306 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
307 if (!OriginDIE) {
308 DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
309 return NULL;
310 }
311
Devang Patel7e623022011-08-10 20:55:27 +0000312 SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +0000313 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
314 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +0000315
Devang Patel4c6bd662010-07-08 22:39:20 +0000316 if (StartLabel == 0 || EndLabel == 0) {
Nick Lewycky019d2552011-07-29 03:49:23 +0000317 assert (0 && "Unexpected Start and End labels for a inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000318 return 0;
319 }
Chris Lattnere13c3722010-03-09 01:58:53 +0000320 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000321 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +0000322 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +0000323 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +0000324
Devang Patel73bc1722011-05-05 17:54:26 +0000325 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patelf20c4f72011-04-12 22:53:02 +0000326 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
327 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000328
Devang Patelf098ce22011-07-27 00:34:13 +0000329 if (Ranges.size() > 1) {
330 // .debug_range section has not been laid out yet. Emit offset in
331 // .debug_range as a uint, size 4, for now. emitDIE will handle
332 // DW_AT_ranges appropriately.
333 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Devang Patel784077e2011-08-10 23:58:09 +0000334 DebugRangeSymbols.size()
335 * Asm->getTargetData().getPointerSize());
Devang Patel7e623022011-08-10 20:55:27 +0000336 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patelf098ce22011-07-27 00:34:13 +0000337 RE = Ranges.end(); RI != RE; ++RI) {
338 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
339 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
340 }
341 DebugRangeSymbols.push_back(NULL);
342 DebugRangeSymbols.push_back(NULL);
343 } else {
Devang Patel784077e2011-08-10 23:58:09 +0000344 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
345 StartLabel);
346 TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
347 EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000348 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000349
350 InlinedSubprogramDIEs.insert(OriginDIE);
351
352 // Track the start label for this inlined function.
Devang Patelf098ce22011-07-27 00:34:13 +0000353 //.debug_inlined section specification does not clearly state how
354 // to emit inlined scope that is split into multiple instruction ranges.
355 // For now, use first instruction range and emit low_pc/high_pc pair and
356 // corresponding .debug_inlined section entry for this pair.
Devang Patel32cc43c2010-05-07 20:54:48 +0000357 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000358 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000359
360 if (I == InlineInfo.end()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000361 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach00e9c612009-11-22 19:20:36 +0000362 ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000363 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000364 } else
Chris Lattner085b6522010-03-09 00:31:02 +0000365 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000366
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000367 DILocation DL(Scope->getInlinedAt());
Devang Patelf20c4f72011-04-12 22:53:02 +0000368 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
369 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000370
371 return ScopeDIE;
372}
373
Devang Patel930143b2009-11-21 02:48:08 +0000374/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel7e623022011-08-10 20:55:27 +0000375DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, LexicalScope *Scope) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000376 StringRef Name = DV->getName();
Devang Patel2d9caf92009-11-25 17:36:49 +0000377 if (Name.empty())
Devang Patel97f99fa2009-11-13 02:25:26 +0000378 return NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000379
Devang Patel6e4d2c92011-08-15 18:35:42 +0000380 // Translate tag to proper Dwarf tag.
381 unsigned Tag = DV->getTag();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000382
383 // Define variable debug information entry.
384 DIE *VariableDie = new DIE(Tag);
Devang Patel2606f4d2011-04-25 23:05:21 +0000385 CompileUnit *VariableCU = getCompileUnit(DV->getVariable());
Devang Patel99819b52011-08-15 19:01:20 +0000386 DbgVariable *AbsVar = DV->getAbstractVariable();
387 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Devang Patele1c53f22010-05-20 16:36:41 +0000388 if (AbsDIE)
Devang Patel2606f4d2011-04-25 23:05:21 +0000389 VariableCU->addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel99819b52011-08-15 19:01:20 +0000390 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000391 else {
Devang Patel99819b52011-08-15 19:01:20 +0000392 VariableCU->addString(VariableDie, dwarf::DW_AT_name,
393 dwarf::DW_FORM_string, Name);
Devang Patel2606f4d2011-04-25 23:05:21 +0000394 VariableCU->addSourceLine(VariableDie, DV->getVariable());
Devang Patel2606f4d2011-04-25 23:05:21 +0000395 VariableCU->addType(VariableDie, DV->getType());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000396 }
397
Devang Pateld7d80aa2011-08-15 18:40:16 +0000398 if (DV->isArtificial())
399 VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
Devang Patel2606f4d2011-04-25 23:05:21 +0000400 dwarf::DW_FORM_flag, 1);
Devang Patel9fc11702010-05-25 23:40:22 +0000401
402 if (Scope->isAbstractScope()) {
403 DV->setDIE(VariableDie);
404 return VariableDie;
405 }
406
407 // Add variable address.
408
409 unsigned Offset = DV->getDotDebugLocOffset();
410 if (Offset != ~0U) {
Devang Patel784077e2011-08-10 23:58:09 +0000411 VariableCU->addLabel(VariableDie, dwarf::DW_AT_location,
412 dwarf::DW_FORM_data4,
413 Asm->GetTempSymbol("debug_loc", Offset));
Devang Patel9fc11702010-05-25 23:40:22 +0000414 DV->setDIE(VariableDie);
415 UseDotDebugLocEntry.insert(VariableDie);
416 return VariableDie;
417 }
418
419 // Check if variable is described by a DBG_VALUE instruction.
420 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
421 DbgVariableToDbgInstMap.find(DV);
422 if (DVI != DbgVariableToDbgInstMap.end()) {
423 const MachineInstr *DVInsn = DVI->second;
Devang Patel9fc11702010-05-25 23:40:22 +0000424 bool updated = false;
Devang Patel9fc11702010-05-25 23:40:22 +0000425 if (DVInsn->getNumOperands() == 3) {
Devang Patel86ec8b32010-08-31 22:22:42 +0000426 if (DVInsn->getOperand(0).isReg()) {
427 const MachineOperand RegOp = DVInsn->getOperand(0);
428 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
429 if (DVInsn->getOperand(1).isImm() &&
430 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
Devang Patel77dc5412011-04-27 22:45:24 +0000431 unsigned FrameReg = 0;
432 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
433 int Offset =
434 TFI->getFrameIndexReference(*Asm->MF,
435 DVInsn->getOperand(1).getImm(),
436 FrameReg);
437 MachineLocation Location(FrameReg, Offset);
438 VariableCU->addVariableAddress(DV, VariableDie, Location);
439
440 } else if (RegOp.getReg())
441 VariableCU->addVariableAddress(DV, VariableDie,
442 MachineLocation(RegOp.getReg()));
443 updated = true;
Devang Patel86ec8b32010-08-31 22:22:42 +0000444 }
Devang Patel9fc11702010-05-25 23:40:22 +0000445 else if (DVInsn->getOperand(0).isImm())
Devang Patel3c6aed22011-05-27 16:45:18 +0000446 updated =
447 VariableCU->addConstantValue(VariableDie, DVInsn->getOperand(0),
448 DV->getType());
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000449 else if (DVInsn->getOperand(0).isFPImm())
450 updated =
Devang Patel2606f4d2011-04-25 23:05:21 +0000451 VariableCU->addConstantFPValue(VariableDie, DVInsn->getOperand(0));
Devang Patelf071d722011-06-24 20:46:11 +0000452 else if (DVInsn->getOperand(0).isCImm())
453 updated =
454 VariableCU->addConstantValue(VariableDie,
455 DVInsn->getOperand(0).getCImm(),
Devang Pateldfd6ec32011-08-15 17:57:41 +0000456 DV->getType().isUnsignedDIType());
Devang Patel9fc11702010-05-25 23:40:22 +0000457 } else {
Devang Patel77dc5412011-04-27 22:45:24 +0000458 VariableCU->addVariableAddress(DV, VariableDie,
459 Asm->getDebugValueLocation(DVInsn));
460 updated = true;
Devang Patel9fc11702010-05-25 23:40:22 +0000461 }
462 if (!updated) {
463 // If variableDie is not updated then DBG_VALUE instruction does not
464 // have valid variable info.
465 delete VariableDie;
466 return NULL;
467 }
468 DV->setDIE(VariableDie);
469 return VariableDie;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000470 }
Devang Patel9fc11702010-05-25 23:40:22 +0000471
472 // .. else use frame index, if available.
Devang Patel9fc11702010-05-25 23:40:22 +0000473 int FI = 0;
Devang Patel77dc5412011-04-27 22:45:24 +0000474 if (findVariableFrameIndex(DV, &FI)) {
475 unsigned FrameReg = 0;
476 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
477 int Offset =
478 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
479 MachineLocation Location(FrameReg, Offset);
480 VariableCU->addVariableAddress(DV, VariableDie, Location);
481 }
482
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000483 DV->setDIE(VariableDie);
484 return VariableDie;
485
486}
Devang Patel930143b2009-11-21 02:48:08 +0000487
488/// constructScopeDIE - Construct a DIE for this scope.
Devang Patel7e623022011-08-10 20:55:27 +0000489DIE *DwarfDebug::constructScopeDIE(LexicalScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000490 if (!Scope || !Scope->getScopeNode())
491 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000492
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000493 SmallVector <DIE *, 8> Children;
Devang Patel6c622ef2011-03-01 22:58:55 +0000494
495 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000496 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000497 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
498 if (DbgVariable *ArgDV = CurrentFnArguments[i])
499 if (DIE *Arg = constructVariableDIE(ArgDV, Scope))
500 Children.push_back(Arg);
501
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000502 // Collect lexical scope childrens first.
Devang Patel7e623022011-08-10 20:55:27 +0000503 const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000504 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
505 if (DIE *Variable = constructVariableDIE(Variables[i], Scope))
506 Children.push_back(Variable);
Devang Patel7e623022011-08-10 20:55:27 +0000507 const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000508 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
509 if (DIE *Nested = constructScopeDIE(Scopes[j]))
510 Children.push_back(Nested);
Devang Patel3b548aa2010-03-08 20:52:55 +0000511 DIScope DS(Scope->getScopeNode());
512 DIE *ScopeDIE = NULL;
513 if (Scope->getInlinedAt())
514 ScopeDIE = constructInlinedScopeDIE(Scope);
515 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000516 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000517 if (Scope->isAbstractScope()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000518 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000519 // Note down abstract DIE.
520 if (ScopeDIE)
521 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
522 }
Devang Patel3b548aa2010-03-08 20:52:55 +0000523 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000524 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3b548aa2010-03-08 20:52:55 +0000525 }
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000526 else {
527 // There is no need to emit empty lexical block DIE.
528 if (Children.empty())
529 return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +0000530 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000531 }
532
Devang Patel23b2ae62010-03-29 22:59:58 +0000533 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000534
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000535 // Add children
536 for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
537 E = Children.end(); I != E; ++I)
538 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000539
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000540 if (DS.isSubprogram())
Devang Patelf20c4f72011-04-12 22:53:02 +0000541 getCompileUnit(DS)->addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000542
Devang Patel04d2f2d2009-11-24 01:14:22 +0000543 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000544}
545
Bill Wendling2b128d72009-05-20 23:19:06 +0000546/// GetOrCreateSourceID - Look up the source id with the given directory and
547/// source file names. If none currently exists, create a new id and insert it
548/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
549/// maps as well.
Devang Patel498877d2010-07-24 00:53:22 +0000550
Devang Patele01b75c2011-03-24 20:30:50 +0000551unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
552 StringRef DirName) {
Devang Patel871d0b12010-09-16 20:57:49 +0000553 // If FE did not provide a file name, then assume stdin.
554 if (FileName.empty())
Devang Patele01b75c2011-03-24 20:30:50 +0000555 return GetOrCreateSourceID("<stdin>", StringRef());
556
557 // MCStream expects full path name as filename.
Benjamin Kramer440c3b72011-06-05 14:36:47 +0000558 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
559 SmallString<128> FullPathName = DirName;
560 sys::path::append(FullPathName, FileName);
Devang Patele01b75c2011-03-24 20:30:50 +0000561 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
562 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
563 }
Devang Patel871d0b12010-09-16 20:57:49 +0000564
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000565 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
566 if (Entry.getValue())
567 return Entry.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000568
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000569 unsigned SrcId = SourceIdMap.size();
570 Entry.setValue(SrcId);
Bill Wendling2b128d72009-05-20 23:19:06 +0000571
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000572 // Print out a .file directive to specify files for .loc directives.
Devang Patele01b75c2011-03-24 20:30:50 +0000573 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
Bill Wendling2b128d72009-05-20 23:19:06 +0000574
575 return SrcId;
576}
577
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000578/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel1a0df9a2010-05-10 22:49:55 +0000579/// metadata node with tag DW_TAG_compile_unit.
Devang Patel32cc43c2010-05-07 20:54:48 +0000580void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000581 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +0000582 StringRef FN = DIUnit.getFilename();
583 StringRef Dir = DIUnit.getDirectory();
Devang Patele01b75c2011-03-24 20:30:50 +0000584 unsigned ID = GetOrCreateSourceID(FN, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000585
586 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelf20c4f72011-04-12 22:53:02 +0000587 CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
588 NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
589 DIUnit.getProducer());
590 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
591 DIUnit.getLanguage());
592 NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patelbd798ce2010-04-26 22:54:28 +0000593 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
594 // simplifies debug range entries.
Devang Patelf20c4f72011-04-12 22:53:02 +0000595 NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +0000596 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +0000597 // compile unit in debug_line section.
Rafael Espindola2fe0ee12011-05-10 20:35:05 +0000598 if(Asm->MAI->doesDwarfRequireRelocationForSectionOffset())
Rafael Espindolaa7558912011-05-04 17:44:06 +0000599 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Devang Patelf20c4f72011-04-12 22:53:02 +0000600 Asm->GetTempSymbol("section_line"));
Devang Patelea636392010-08-31 23:50:19 +0000601 else
Devang Patelf20c4f72011-04-12 22:53:02 +0000602 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +0000603
Devang Patel2d9caf92009-11-25 17:36:49 +0000604 if (!Dir.empty())
Devang Patelf20c4f72011-04-12 22:53:02 +0000605 NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +0000606 if (DIUnit.isOptimized())
Devang Patelf20c4f72011-04-12 22:53:02 +0000607 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +0000608
Devang Patel2d9caf92009-11-25 17:36:49 +0000609 StringRef Flags = DIUnit.getFlags();
610 if (!Flags.empty())
Devang Patel784077e2011-08-10 23:58:09 +0000611 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string,
612 Flags);
Devang Patelf20c4f72011-04-12 22:53:02 +0000613
Bill Wendling2b128d72009-05-20 23:19:06 +0000614 unsigned RVer = DIUnit.getRunTimeVersion();
615 if (RVer)
Devang Patelf20c4f72011-04-12 22:53:02 +0000616 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +0000617 dwarf::DW_FORM_data1, RVer);
618
Devang Patel1a0df9a2010-05-10 22:49:55 +0000619 if (!FirstCU)
620 FirstCU = NewCU;
621 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendling2b128d72009-05-20 23:19:06 +0000622}
623
Nick Lewycky019d2552011-07-29 03:49:23 +0000624/// getCompileUnit - Get CompileUnit DIE.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000625CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
626 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
627 DIDescriptor D(N);
628 const MDNode *CUNode = NULL;
629 if (D.isCompileUnit())
630 CUNode = N;
631 else if (D.isSubprogram())
632 CUNode = DISubprogram(N).getCompileUnit();
633 else if (D.isType())
634 CUNode = DIType(N).getCompileUnit();
635 else if (D.isGlobalVariable())
636 CUNode = DIGlobalVariable(N).getCompileUnit();
637 else if (D.isVariable())
638 CUNode = DIVariable(N).getCompileUnit();
639 else if (D.isNameSpace())
640 CUNode = DINameSpace(N).getCompileUnit();
641 else if (D.isFile())
642 CUNode = DIFile(N).getCompileUnit();
643 else
644 return FirstCU;
645
646 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
647 = CUMap.find(CUNode);
648 if (I == CUMap.end())
649 return FirstCU;
650 return I->second;
651}
652
Devang Patel1a0df9a2010-05-10 22:49:55 +0000653/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +0000654void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel469c12d22010-08-10 01:37:23 +0000655 DIGlobalVariable GV(N);
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000656
Devang Patelf5d53602009-09-04 23:59:07 +0000657 // If debug information is malformed then ignore it.
Devang Patel469c12d22010-08-10 01:37:23 +0000658 if (GV.Verify() == false)
Devang Patelf5d53602009-09-04 23:59:07 +0000659 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000660
661 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000662 CompileUnit *TheCU = getCompileUnit(N);
Devang Pateldfd6ec32011-08-15 17:57:41 +0000663 TheCU->createGlobalVariableDIE(N);
Devang Patel0751a282009-06-26 01:49:18 +0000664 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000665}
666
Devang Patel1a0df9a2010-05-10 22:49:55 +0000667/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +0000668void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +0000669 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000670
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000671 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000672 CompileUnit *TheCU = getCompileUnit(N);
673 if (TheCU->getDIE(N))
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000674 return;
675
Bill Wendling2b128d72009-05-20 23:19:06 +0000676 if (!SP.isDefinition())
677 // This is a method declaration which will be handled while constructing
678 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000679 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000680
Devang Patel89543712011-08-15 17:24:54 +0000681 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000682
683 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000684 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +0000685
686 // Add to context owner.
Devang Patelf20c4f72011-04-12 22:53:02 +0000687 TheCU->addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +0000688
Bill Wendling2b128d72009-05-20 23:19:06 +0000689 // Expose as global.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000690 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000691
Devang Patel0751a282009-06-26 01:49:18 +0000692 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000693}
694
Devang Patel930143b2009-11-21 02:48:08 +0000695/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000696/// content. Create global DIEs and emit initial debug info sections.
Nick Lewycky019d2552011-07-29 03:49:23 +0000697/// This is invoked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +0000698void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +0000699 if (DisableDebugInfoPrinting)
700 return;
701
Nick Lewycky019d2552011-07-29 03:49:23 +0000702 // If module has named metadata anchors then use them, otherwise scan the
703 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000704 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
705 if (CU_Nodes) {
Devang Patelc981f622011-05-04 16:34:02 +0000706
707 NamedMDNode *GV_Nodes = M->getNamedMetadata("llvm.dbg.gv");
708 NamedMDNode *SP_Nodes = M->getNamedMetadata("llvm.dbg.sp");
709 if (!GV_Nodes && !SP_Nodes)
710 // If there are not any global variables or any functions then
711 // there is not any debug info in this module.
Devang Patele02e5852011-05-03 16:45:22 +0000712 return;
713
Devang Patelc981f622011-05-04 16:34:02 +0000714 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
715 constructCompileUnit(CU_Nodes->getOperand(i));
Devang Patele02e5852011-05-03 16:45:22 +0000716
Devang Patelc981f622011-05-04 16:34:02 +0000717 if (GV_Nodes)
718 for (unsigned i = 0, e = GV_Nodes->getNumOperands(); i != e; ++i)
719 constructGlobalVariableDIE(GV_Nodes->getOperand(i));
720
721 if (SP_Nodes)
722 for (unsigned i = 0, e = SP_Nodes->getNumOperands(); i != e; ++i)
723 constructSubprogramDIE(SP_Nodes->getOperand(i));
Devang Patele02e5852011-05-03 16:45:22 +0000724
725 } else {
726
727 DebugInfoFinder DbgFinder;
728 DbgFinder.processModule(*M);
729
Devang Patelc981f622011-05-04 16:34:02 +0000730 bool HasDebugInfo = false;
Nick Lewycky019d2552011-07-29 03:49:23 +0000731 // Scan all the compile-units to see if there are any marked as the main
732 // unit. If not, we do not generate debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000733 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
734 E = DbgFinder.compile_unit_end(); I != E; ++I) {
735 if (DICompileUnit(*I).isMain()) {
736 HasDebugInfo = true;
737 break;
738 }
739 }
Devang Patelc981f622011-05-04 16:34:02 +0000740 if (!HasDebugInfo) return;
Devang Patele02e5852011-05-03 16:45:22 +0000741
742 // Create all the compile unit DIEs.
743 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
744 E = DbgFinder.compile_unit_end(); I != E; ++I)
745 constructCompileUnit(*I);
746
747 // Create DIEs for each global variable.
748 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
749 E = DbgFinder.global_variable_end(); I != E; ++I)
750 constructGlobalVariableDIE(*I);
751
752 // Create DIEs for each subprogram.
753 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
754 E = DbgFinder.subprogram_end(); I != E; ++I)
755 constructSubprogramDIE(*I);
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000756 }
Devang Patele02e5852011-05-03 16:45:22 +0000757
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000758 // Tell MMI that we have debug info.
759 MMI->setDebugInfoAvailability(true);
Devang Patele02e5852011-05-03 16:45:22 +0000760
Chris Lattnerd442aa32010-04-04 23:17:54 +0000761 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000762 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000763
Devang Patel8e06a5e2010-08-10 20:01:20 +0000764 //getOrCreateTypeDIE
765 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
Devang Patelf20c4f72011-04-12 22:53:02 +0000766 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
767 DIType Ty(NMD->getOperand(i));
768 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
769 }
Devang Patel8e06a5e2010-08-10 20:01:20 +0000770
Devang Patel7a554812010-09-28 18:08:20 +0000771 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
Devang Patelf20c4f72011-04-12 22:53:02 +0000772 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
773 DIType Ty(NMD->getOperand(i));
774 getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
775 }
Devang Patel7a554812010-09-28 18:08:20 +0000776
Bill Wendling2b128d72009-05-20 23:19:06 +0000777 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +0000778 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +0000779}
780
Devang Patel930143b2009-11-21 02:48:08 +0000781/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +0000782///
Devang Patel930143b2009-11-21 02:48:08 +0000783void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000784 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +0000785 const Module *M = MMI->getModule();
Devang Patel7e623022011-08-10 20:55:27 +0000786 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +0000787 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
788 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
789 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
790 DISubprogram SP(AllSPs->getOperand(SI));
791 if (!SP.Verify()) continue;
792
793 // Collect info for variables that were optimized out.
Devang Patel18efced2010-07-19 17:53:55 +0000794 if (!SP.isDefinition()) continue;
Devang Patelf3b2db62010-06-28 18:25:03 +0000795 StringRef FName = SP.getLinkageName();
796 if (FName.empty())
797 FName = SP.getName();
Devang Patel364bf042010-11-10 22:19:21 +0000798 NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
Devang Patelf3b2db62010-06-28 18:25:03 +0000799 if (!NMD) continue;
800 unsigned E = NMD->getNumOperands();
801 if (!E) continue;
Devang Patel784077e2011-08-10 23:58:09 +0000802 LexicalScope *Scope = new LexicalScope(NULL, DIDescriptor(SP), NULL,
803 false);
Devang Pateld0701282010-08-02 17:32:15 +0000804 DeadFnScopeMap[SP] = Scope;
Benjamin Kramerfa7e6a52011-08-11 18:39:28 +0000805 SmallVector<DbgVariable, 8> Variables;
Devang Patelf3b2db62010-06-28 18:25:03 +0000806 for (unsigned I = 0; I != E; ++I) {
807 DIVariable DV(NMD->getOperand(I));
808 if (!DV.Verify()) continue;
Devang Patel99819b52011-08-15 19:01:20 +0000809 Variables.push_back(DbgVariable(DV, NULL));
Devang Patelf3b2db62010-06-28 18:25:03 +0000810 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000811
Devang Patelf3b2db62010-06-28 18:25:03 +0000812 // Construct subprogram DIE and add variables DIEs.
813 constructSubprogramDIE(SP);
814 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patelf3b2db62010-06-28 18:25:03 +0000815 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Benjamin Kramerfa7e6a52011-08-11 18:39:28 +0000816 if (DIE *VariableDIE = constructVariableDIE(&Variables[i], Scope))
Devang Patelf3b2db62010-06-28 18:25:03 +0000817 ScopeDIE->addChild(VariableDIE);
818 }
819 }
820 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000821
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000822 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
823 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
824 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
825 DIE *ISP = *AI;
Devang Patelf20c4f72011-04-12 22:53:02 +0000826 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000827 }
828
Devang Patel89543712011-08-15 17:24:54 +0000829 // Emit DW_AT_containing_type attribute to connect types with their
830 // vtable holding type.
831 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
832 CUE = CUMap.end(); CUI != CUE; ++CUI) {
833 CompileUnit *TheCU = CUI->second;
834 TheCU->constructContainingTypeDIEs();
Devang Pateleb57c592009-12-03 19:11:07 +0000835 }
836
Bill Wendling2b128d72009-05-20 23:19:06 +0000837 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000838 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000839 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000840 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +0000841 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +0000842
843 // End text sections.
844 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000845 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +0000846 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +0000847 }
848
Bill Wendling2b128d72009-05-20 23:19:06 +0000849 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +0000850 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +0000851
852 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +0000853 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000854
855 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +0000856 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +0000857
Bill Wendling2b128d72009-05-20 23:19:06 +0000858 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +0000859 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +0000860
Devang Patel04d2f2d2009-11-24 01:14:22 +0000861 // Emit info into a debug pubtypes section.
862 emitDebugPubTypes();
863
Bill Wendling2b128d72009-05-20 23:19:06 +0000864 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +0000865 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +0000866
867 // Emit info into a debug aranges section.
868 EmitDebugARanges();
869
870 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +0000871 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +0000872
873 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +0000874 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000875
876 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +0000877 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000878
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000879 // Emit info into a debug str section.
880 emitDebugStr();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000881
Devang Pateld0701282010-08-02 17:32:15 +0000882 // clean up.
883 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000884 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
885 E = CUMap.end(); I != E; ++I)
886 delete I->second;
887 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +0000888}
889
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000890/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +0000891DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +0000892 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +0000893 LLVMContext &Ctx = DV->getContext();
894 // More then one inlined variable corresponds to one abstract variable.
895 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000896 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000897 if (AbsDbgVariable)
898 return AbsDbgVariable;
899
Devang Patel7e623022011-08-10 20:55:27 +0000900 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000901 if (!Scope)
902 return NULL;
903
Devang Patel99819b52011-08-15 19:01:20 +0000904 AbsDbgVariable = new DbgVariable(Var, NULL);
Devang Patel7e623022011-08-10 20:55:27 +0000905 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000906 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000907 return AbsDbgVariable;
908}
909
Nick Lewycky019d2552011-07-29 03:49:23 +0000910/// addCurrentFnArgument - If Var is a current function argument then add
911/// it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +0000912bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +0000913 DbgVariable *Var, LexicalScope *Scope) {
914 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000915 return false;
916 DIVariable DV = Var->getVariable();
917 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
918 return false;
919 unsigned ArgNo = DV.getArgNumber();
920 if (ArgNo == 0)
921 return false;
922
Devang Patel4ab660b2011-03-03 20:02:02 +0000923 size_t Size = CurrentFnArguments.size();
924 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +0000925 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +0000926 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +0000927 // arguments does the function have at source level.
928 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +0000929 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +0000930 CurrentFnArguments[ArgNo - 1] = Var;
931 return true;
932}
933
Devang Patel490c8ab2010-05-20 19:57:06 +0000934/// collectVariableInfoFromMMITable - Collect variable information from
935/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000936void
Nick Lewycky019d2552011-07-29 03:49:23 +0000937DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patel490c8ab2010-05-20 19:57:06 +0000938 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +0000939 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
940 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
941 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +0000942 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000943 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +0000944 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +0000945 DIVariable DV(Var);
946 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000947
Devang Patel7e623022011-08-10 20:55:27 +0000948 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000949
Devang Patelcdb7d442009-11-10 23:20:04 +0000950 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +0000951 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +0000952 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000953
Devang Patele1c53f22010-05-20 16:36:41 +0000954 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Devang Patel99819b52011-08-15 19:01:20 +0000955 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable);
Devang Patele1c53f22010-05-20 16:36:41 +0000956 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +0000957 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +0000958 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +0000959 if (AbsDbgVariable)
Devang Patele1c53f22010-05-20 16:36:41 +0000960 recordVariableFrameIndex(AbsDbgVariable, VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +0000961 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000962}
Devang Patela3e9c9c2010-03-15 18:33:46 +0000963
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000964/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +0000965/// DBG_VALUE instruction, is in a defined reg.
966static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
967 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000968 return MI->getNumOperands() == 3 &&
969 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
970 MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000971}
972
Nick Lewycky019d2552011-07-29 03:49:23 +0000973/// getDebugLocEntry - Get .debug_loc entry for the instruction range starting
Devang Patel2442a892011-07-08 17:09:57 +0000974/// at MI.
975static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
976 const MCSymbol *FLabel,
977 const MCSymbol *SLabel,
978 const MachineInstr *MI) {
979 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
980
981 if (MI->getNumOperands() != 3) {
982 MachineLocation MLoc = Asm->getDebugValueLocation(MI);
983 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
984 }
985 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) {
986 MachineLocation MLoc;
987 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
988 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
989 }
990 if (MI->getOperand(0).isImm())
991 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
992 if (MI->getOperand(0).isFPImm())
993 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
994 if (MI->getOperand(0).isCImm())
995 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
996
997 assert (0 && "Unexpected 3 operand DBG_VALUE instruction!");
998 return DotDebugLocEntry();
999}
1000
Devang Patel7e623022011-08-10 20:55:27 +00001001/// collectVariableInfo - Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001002void
Devang Patel5c0f85c2010-06-25 22:07:34 +00001003DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1004 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001005
Devang Patel490c8ab2010-05-20 19:57:06 +00001006 /// collection info from MMI table.
1007 collectVariableInfoFromMMITable(MF, Processed);
1008
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001009 for (SmallVectorImpl<const MDNode*>::const_iterator
1010 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1011 ++UVI) {
1012 const MDNode *Var = *UVI;
1013 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001014 continue;
1015
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001016 // History contains relevant DBG_VALUE instructions for Var and instructions
1017 // clobbering it.
1018 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1019 if (History.empty())
1020 continue;
1021 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001022
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001023 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001024 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001025 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1026 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001027 Scope = LScopes.getCurrentFunctionScope();
Devang Patel8fb9fd62011-07-20 22:18:50 +00001028 else {
1029 if (DV.getVersion() <= LLVMDebugVersion9)
Devang Patel7e623022011-08-10 20:55:27 +00001030 Scope = LScopes.findLexicalScope(MInsn->getDebugLoc());
Devang Patel8fb9fd62011-07-20 22:18:50 +00001031 else {
1032 if (MDNode *IA = DV.getInlinedAt())
Devang Patel7e623022011-08-10 20:55:27 +00001033 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001034 else
Devang Patel7e623022011-08-10 20:55:27 +00001035 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel8fb9fd62011-07-20 22:18:50 +00001036 }
1037 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001038 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001039 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001040 continue;
1041
1042 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001043 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001044 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1045 DbgVariable *RegVar = new DbgVariable(DV, AbsVar);
Devang Patel6c622ef2011-03-01 22:58:55 +00001046 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001047 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001048 if (AbsVar)
Devang Patelfbd6c452010-05-21 00:10:20 +00001049 DbgVariableToDbgInstMap[AbsVar] = MInsn;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001050
1051 // Simple ranges that are fully coalesced.
1052 if (History.size() <= 1 || (History.size() == 2 &&
1053 MInsn->isIdenticalTo(History.back()))) {
Devang Patel9fc11702010-05-25 23:40:22 +00001054 DbgVariableToDbgInstMap[RegVar] = MInsn;
1055 continue;
1056 }
1057
1058 // handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001059 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001060
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001061 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1062 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1063 const MachineInstr *Begin = *HI;
1064 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001065
Devang Patele7181b52011-06-01 23:00:17 +00001066 // Check if DBG_VALUE is truncating a range.
1067 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1068 && !Begin->getOperand(0).getReg())
1069 continue;
1070
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001071 // Compute the range for a register location.
1072 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1073 const MCSymbol *SLabel = 0;
1074
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001075 if (HI + 1 == HE)
1076 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001077 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001078 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001079 else {
1080 const MachineInstr *End = HI[1];
Devang Patel53b050a2011-07-07 21:44:42 +00001081 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
1082 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001083 if (End->isDebugValue())
1084 SLabel = getLabelBeforeInsn(End);
1085 else {
1086 // End is a normal instruction clobbering the range.
1087 SLabel = getLabelAfterInsn(End);
1088 assert(SLabel && "Forgot label after clobber instruction");
1089 ++HI;
1090 }
1091 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001092
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001093 // The value is valid until the next DBG_VALUE or clobber.
Devang Patel2442a892011-07-08 17:09:57 +00001094 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001095 }
1096 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001097 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001098
1099 // Collect info for variables that were optimized out.
Devang Patelad517352010-06-22 01:01:58 +00001100 const Function *F = MF->getFunction();
Devang Patel364bf042010-11-10 22:19:21 +00001101 if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
Devang Patele0a94bf2010-05-14 21:01:35 +00001102 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00001103 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001104 if (!DV || !Processed.insert(DV))
Devang Patele0a94bf2010-05-14 21:01:35 +00001105 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001106 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Devang Patel99819b52011-08-15 19:01:20 +00001107 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patele0a94bf2010-05-14 21:01:35 +00001108 }
1109 }
Devang Patel9fc11702010-05-25 23:40:22 +00001110}
Devang Patele0a94bf2010-05-14 21:01:35 +00001111
Devang Patel9fc11702010-05-25 23:40:22 +00001112/// getLabelBeforeInsn - Return Label preceding the instruction.
1113const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001114 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1115 assert(Label && "Didn't insert label before instruction");
1116 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001117}
1118
1119/// getLabelAfterInsn - Return Label immediately following the instruction.
1120const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001121 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001122}
1123
Devang Patelb5694e72010-10-26 17:49:02 +00001124/// beginInstruction - Process beginning of an instruction.
1125void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001126 // Check if source location changes, but ignore DBG_VALUE locations.
1127 if (!MI->isDebugValue()) {
1128 DebugLoc DL = MI->getDebugLoc();
1129 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Devang Patel34a66202011-05-11 19:22:19 +00001130 unsigned Flags = DWARF2_FLAG_IS_STMT;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001131 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001132 if (DL == PrologEndLoc) {
1133 Flags |= DWARF2_FLAG_PROLOGUE_END;
1134 PrologEndLoc = DebugLoc();
1135 }
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001136 if (!DL.isUnknown()) {
1137 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001138 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001139 } else
Devang Patel34a66202011-05-11 19:22:19 +00001140 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001141 }
Devang Patel9fc11702010-05-25 23:40:22 +00001142 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001143
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001144 // Insert labels where requested.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001145 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1146 LabelsBeforeInsn.find(MI);
1147
1148 // No label needed.
1149 if (I == LabelsBeforeInsn.end())
1150 return;
1151
1152 // Label already assigned.
1153 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001154 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001155
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001156 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001157 PrevLabel = MMI->getContext().CreateTempSymbol();
1158 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001159 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001160 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001161}
1162
Devang Patelb5694e72010-10-26 17:49:02 +00001163/// endInstruction - Process end of an instruction.
1164void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001165 // Don't create a new label after DBG_VALUE instructions.
1166 // They don't generate code.
1167 if (!MI->isDebugValue())
1168 PrevLabel = 0;
1169
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001170 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1171 LabelsAfterInsn.find(MI);
1172
1173 // No label needed.
1174 if (I == LabelsAfterInsn.end())
1175 return;
1176
1177 // Label already assigned.
1178 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001179 return;
1180
1181 // We need a label after this instruction.
1182 if (!PrevLabel) {
1183 PrevLabel = MMI->getContext().CreateTempSymbol();
1184 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001185 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001186 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001187}
1188
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001189/// identifyScopeMarkers() -
Devang Patel784077e2011-08-10 23:58:09 +00001190/// Each LexicalScope has first instruction and last instruction to mark
1191/// beginning and end of a scope respectively. Create an inverse map that list
1192/// scopes starts (and ends) with an instruction. One instruction may start (or
1193/// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001194void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001195 SmallVector<LexicalScope *, 4> WorkList;
1196 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001197 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001198 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001199
Devang Patel7e623022011-08-10 20:55:27 +00001200 const SmallVector<LexicalScope *, 4> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001201 if (!Children.empty())
Devang Patel7e623022011-08-10 20:55:27 +00001202 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00001203 SE = Children.end(); SI != SE; ++SI)
1204 WorkList.push_back(*SI);
1205
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001206 if (S->isAbstractScope())
1207 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001208
Devang Patel7e623022011-08-10 20:55:27 +00001209 const SmallVector<InsnRange, 4> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001210 if (Ranges.empty())
1211 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001212 for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(),
Devang Patel6c74a872010-04-27 19:46:33 +00001213 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001214 assert(RI->first && "InsnRange does not have first instruction!");
1215 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001216 requestLabelBeforeInsn(RI->first);
1217 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001218 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001219 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001220}
1221
Devang Patel589845d2011-05-09 22:14:49 +00001222/// getScopeNode - Get MDNode for DebugLoc's scope.
1223static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1224 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1225 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1226 return DL.getScope(Ctx);
1227}
1228
Devang Patel34a66202011-05-11 19:22:19 +00001229/// getFnDebugLoc - Walk up the scope chain of given debug loc and find
1230/// line number info for the function.
1231static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1232 const MDNode *Scope = getScopeNode(DL, Ctx);
1233 DISubprogram SP = getDISubprogram(Scope);
1234 if (SP.Verify())
1235 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1236 return DebugLoc();
1237}
1238
Devang Patel930143b2009-11-21 02:48:08 +00001239/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00001240/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00001241void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00001242 if (!MMI->hasDebugInfo()) return;
Devang Patel7e623022011-08-10 20:55:27 +00001243 LScopes.initialize(*MF);
1244 if (LScopes.empty()) return;
1245 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001246
Devang Patel6c74a872010-04-27 19:46:33 +00001247 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1248 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001249 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001250 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001251
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001252 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1253
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001254 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001255 /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1256 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1257
Devang Patel002d54d2010-05-26 19:37:24 +00001258 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001259 I != E; ++I) {
1260 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001261 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1262 II != IE; ++II) {
1263 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001264
Devang Patel002d54d2010-05-26 19:37:24 +00001265 if (MI->isDebugValue()) {
Devang Patel002d54d2010-05-26 19:37:24 +00001266 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001267
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001268 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001269 const MDNode *Var =
1270 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001271
1272 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001273 if (isDbgValueInDefinedReg(MI))
1274 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1275
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001276 // Check the history of this variable.
1277 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1278 if (History.empty()) {
1279 UserVariables.push_back(Var);
1280 // The first mention of a function argument gets the FunctionBeginSym
1281 // label, so arguments are visible when breaking at function entry.
1282 DIVariable DV(Var);
1283 if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1284 DISubprogram(getDISubprogram(DV.getContext()))
1285 .describes(MF->getFunction()))
1286 LabelsBeforeInsn[MI] = FunctionBeginSym;
1287 } else {
1288 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1289 const MachineInstr *Prev = History.back();
1290 if (Prev->isDebugValue()) {
1291 // Coalesce identical entries at the end of History.
1292 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001293 Prev->isIdenticalTo(History[History.size() - 2])) {
1294 DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n"
1295 << "\t" << *Prev
1296 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001297 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001298 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001299
1300 // Terminate old register assignments that don't reach MI;
1301 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1302 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1303 isDbgValueInDefinedReg(Prev)) {
1304 // Previous register assignment needs to terminate at the end of
1305 // its basic block.
1306 MachineBasicBlock::const_iterator LastMI =
1307 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001308 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001309 // Drop DBG_VALUE for empty range.
Devang Patelb7a328e2011-07-07 00:14:27 +00001310 DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n"
1311 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001312 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001313 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001314 else {
1315 // Terminate after LastMI.
1316 History.push_back(LastMI);
1317 }
1318 }
1319 }
1320 }
1321 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001322 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001323 // Not a DBG_VALUE instruction.
1324 if (!MI->isLabel())
1325 AtBlockEntry = false;
1326
Devang Patel34a66202011-05-11 19:22:19 +00001327 // First known non DBG_VALUE location marks beginning of function
1328 // body.
1329 if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown())
1330 PrologEndLoc = MI->getDebugLoc();
1331
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001332 // Check if the instruction clobbers any registers with debug vars.
1333 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1334 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1335 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1336 continue;
1337 for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1338 unsigned Reg = *AI; ++AI) {
1339 const MDNode *Var = LiveUserVar[Reg];
1340 if (!Var)
1341 continue;
1342 // Reg is now clobbered.
1343 LiveUserVar[Reg] = 0;
1344
1345 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001346 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1347 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001348 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001349 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1350 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001351 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001352 const MachineInstr *Prev = History.back();
1353 // Sanity-check: Register assignments are terminated at the end of
1354 // their block.
1355 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1356 continue;
1357 // Is the variable still in Reg?
1358 if (!isDbgValueInDefinedReg(Prev) ||
1359 Prev->getOperand(0).getReg() != Reg)
1360 continue;
1361 // Var is clobbered. Make sure the next instruction gets a label.
1362 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001363 }
1364 }
Devang Patel002d54d2010-05-26 19:37:24 +00001365 }
Devang Patel002d54d2010-05-26 19:37:24 +00001366 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001367 }
1368
1369 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1370 I != E; ++I) {
1371 SmallVectorImpl<const MachineInstr*> &History = I->second;
1372 if (History.empty())
1373 continue;
1374
1375 // Make sure the final register assignments are terminated.
1376 const MachineInstr *Prev = History.back();
1377 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1378 const MachineBasicBlock *PrevMBB = Prev->getParent();
Devang Patel784077e2011-08-10 23:58:09 +00001379 MachineBasicBlock::const_iterator LastMI =
1380 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001381 if (LastMI == PrevMBB->end())
1382 // Drop DBG_VALUE for empty range.
1383 History.pop_back();
1384 else {
1385 // Terminate after LastMI.
1386 History.push_back(LastMI);
1387 }
1388 }
1389 // Request labels for the full history.
1390 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1391 const MachineInstr *MI = History[i];
1392 if (MI->isDebugValue())
1393 requestLabelBeforeInsn(MI);
1394 else
1395 requestLabelAfterInsn(MI);
1396 }
1397 }
Devang Patel002d54d2010-05-26 19:37:24 +00001398
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001399 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001400 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001401
1402 // Record beginning of function.
1403 if (!PrologEndLoc.isUnknown()) {
1404 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1405 MF->getFunction()->getContext());
1406 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1407 FnStartDL.getScope(MF->getFunction()->getContext()),
1408 DWARF2_FLAG_IS_STMT);
1409 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001410}
1411
Devang Patel7e623022011-08-10 20:55:27 +00001412void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1413// SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS);
1414 ScopeVariables[LS].push_back(Var);
1415// Vars.push_back(Var);
1416}
1417
Devang Patel930143b2009-11-21 02:48:08 +00001418/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00001419///
Chris Lattner76555b52010-01-26 23:18:02 +00001420void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patel7e623022011-08-10 20:55:27 +00001421 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00001422
Devang Patel7e623022011-08-10 20:55:27 +00001423 // Define end label for subprogram.
1424 FunctionEndSym = Asm->GetTempSymbol("func_end",
1425 Asm->getFunctionNumber());
1426 // Assumes in correct section after the entry point.
1427 Asm->OutStreamer.EmitLabel(FunctionEndSym);
1428
1429 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1430 collectVariableInfo(MF, ProcessedVars);
1431
1432 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001433 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1434 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1435 LexicalScope *AScope = AList[i];
1436 DISubprogram SP(AScope->getScopeNode());
Devang Patel7e623022011-08-10 20:55:27 +00001437 if (SP.Verify()) {
1438 // Collect info for variables that were optimized out.
1439 StringRef FName = SP.getLinkageName();
1440 if (FName.empty())
1441 FName = SP.getName();
1442 if (NamedMDNode *NMD =
1443 getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
1444 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00001445 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001446 if (!DV || !ProcessedVars.insert(DV))
1447 continue;
Devang Patel7e623022011-08-10 20:55:27 +00001448 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Devang Patel99819b52011-08-15 19:01:20 +00001449 addScopeVariable(Scope, new DbgVariable(DV, NULL));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001450 }
1451 }
1452 }
Devang Patel44403472011-08-12 18:10:19 +00001453 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
1454 constructScopeDIE(AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001455 }
Devang Patel7e623022011-08-10 20:55:27 +00001456
1457 DIE *CurFnDIE = constructScopeDIE(LScopes.getCurrentFunctionScope());
1458
1459 if (!DisableFramePointerElim(*MF)) {
1460 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel784077e2011-08-10 23:58:09 +00001461 CompileUnit *TheCU = getCompileUnit(FnScope->getScopeNode());
1462 TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
1463 dwarf::DW_FORM_flag, 1);
Devang Patel7e623022011-08-10 20:55:27 +00001464 }
1465 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
1466 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00001467
Bill Wendling2b128d72009-05-20 23:19:06 +00001468 // Clear debug info
Devang Patel7e623022011-08-10 20:55:27 +00001469 for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator
1470 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1471 DeleteContainerPointers(I->second);
1472 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001473 DeleteContainerPointers(CurrentFnArguments);
Devang Patele1c53f22010-05-20 16:36:41 +00001474 DbgVariableToFrameIndexMap.clear();
Devang Patele1c53f22010-05-20 16:36:41 +00001475 DbgVariableToDbgInstMap.clear();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001476 UserVariables.clear();
1477 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001478 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001479 LabelsBeforeInsn.clear();
1480 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001481 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001482}
1483
Devang Patele1c53f22010-05-20 16:36:41 +00001484/// recordVariableFrameIndex - Record a variable's index.
1485void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
1486 assert (V && "Invalid DbgVariable!");
1487 DbgVariableToFrameIndexMap[V] = Index;
1488}
1489
1490/// findVariableFrameIndex - Return true if frame index for the variable
1491/// is found. Update FI to hold value of the index.
1492bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
1493 assert (V && "Invalid DbgVariable!");
1494 DenseMap<const DbgVariable *, int>::iterator I =
1495 DbgVariableToFrameIndexMap.find(V);
1496 if (I == DbgVariableToFrameIndexMap.end())
1497 return false;
1498 *FI = I->second;
1499 return true;
1500}
1501
Chris Lattnerba35a672010-03-09 04:54:43 +00001502/// recordSourceLine - Register a source line with debug info. Returns the
1503/// unique label that was emitted and which provides correspondence to
1504/// the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001505void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1506 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001507 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001508 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001509 unsigned Src = 1;
1510 if (S) {
1511 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001512
Dan Gohman50849c62010-05-05 23:41:32 +00001513 if (Scope.isCompileUnit()) {
1514 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001515 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001516 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001517 } else if (Scope.isFile()) {
1518 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001519 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001520 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001521 } else if (Scope.isSubprogram()) {
1522 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001523 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001524 Dir = SP.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001525 } else if (Scope.isLexicalBlock()) {
1526 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001527 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001528 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001529 } else
1530 assert(0 && "Unexpected scope info");
1531
Devang Patele01b75c2011-03-24 20:30:50 +00001532 Src = GetOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00001533 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001534 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001535}
1536
Bill Wendling806535f2009-05-20 23:22:40 +00001537//===----------------------------------------------------------------------===//
1538// Emit Methods
1539//===----------------------------------------------------------------------===//
1540
Devang Patel930143b2009-11-21 02:48:08 +00001541/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00001542///
Jim Grosbach00e9c612009-11-22 19:20:36 +00001543unsigned
1544DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00001545 // Get the children.
1546 const std::vector<DIE *> &Children = Die->getChildren();
1547
1548 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001549 if (!Last && !Children.empty())
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001550 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling480ff322009-05-20 23:21:38 +00001551
1552 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001553 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001554
1555 // Get the abbreviation for this DIE.
1556 unsigned AbbrevNumber = Die->getAbbrevNumber();
1557 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1558
1559 // Set DIE offset
1560 Die->setOffset(Offset);
1561
1562 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001563 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001564
1565 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1566 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1567
1568 // Size the DIE attribute values.
1569 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1570 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001571 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001572
1573 // Size the DIE children if any.
1574 if (!Children.empty()) {
1575 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1576 "Children flag not set");
1577
1578 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001579 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00001580
1581 // End of children marker.
1582 Offset += sizeof(int8_t);
1583 }
1584
1585 Die->setSize(Offset - Die->getOffset());
1586 return Offset;
1587}
1588
Devang Patel930143b2009-11-21 02:48:08 +00001589/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00001590///
Devang Patel930143b2009-11-21 02:48:08 +00001591void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001592 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1593 E = CUMap.end(); I != E; ++I) {
1594 // Compute size of compile unit header.
Devang Patel28dce702011-04-12 23:10:47 +00001595 unsigned Offset =
Devang Patel1a0df9a2010-05-10 22:49:55 +00001596 sizeof(int32_t) + // Length of Compilation Unit Info
1597 sizeof(int16_t) + // DWARF version number
1598 sizeof(int32_t) + // Offset Into Abbrev. Section
1599 sizeof(int8_t); // Pointer Size (in bytes)
1600 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001601 }
Bill Wendling480ff322009-05-20 23:21:38 +00001602}
1603
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001604/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
1605/// temporary label to it if SymbolStem is specified.
Chris Lattner6629ca92010-04-04 22:59:04 +00001606static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001607 const char *SymbolStem = 0) {
Chris Lattner6629ca92010-04-04 22:59:04 +00001608 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001609 if (!SymbolStem) return 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001610
Chris Lattner6629ca92010-04-04 22:59:04 +00001611 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
1612 Asm->OutStreamer.EmitLabel(TmpSym);
1613 return TmpSym;
1614}
1615
1616/// EmitSectionLabels - Emit initial Dwarf sections with a label at
1617/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00001618void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001619 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001620
Bill Wendling480ff322009-05-20 23:21:38 +00001621 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001622 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001623 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001624 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001625 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001626 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001627
Chris Lattner6629ca92010-04-04 22:59:04 +00001628 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001629 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001630
Devang Patel4a213872010-08-24 00:06:12 +00001631 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00001632 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
1633 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1634 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001635 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00001636 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00001637 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
1638 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001639
Devang Patel9fc11702010-05-25 23:40:22 +00001640 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
1641 "section_debug_loc");
1642
Chris Lattner6629ca92010-04-04 22:59:04 +00001643 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00001644 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001645}
1646
Nick Lewycky019d2552011-07-29 03:49:23 +00001647/// emitDIE - Recursively emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00001648///
Devang Patel930143b2009-11-21 02:48:08 +00001649void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001650 // Get the abbreviation for this DIE.
1651 unsigned AbbrevNumber = Die->getAbbrevNumber();
1652 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1653
Bill Wendling480ff322009-05-20 23:21:38 +00001654 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001655 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00001656 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
1657 Twine::utohexstr(Die->getOffset()) + ":0x" +
1658 Twine::utohexstr(Die->getSize()) + " " +
1659 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00001660 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001661
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00001662 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00001663 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
1664
1665 // Emit the DIE attribute values.
1666 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1667 unsigned Attr = AbbrevData[i].getAttribute();
1668 unsigned Form = AbbrevData[i].getForm();
1669 assert(Form && "Too many attributes for DIE (check abbreviation)");
1670
Chris Lattner7bde8c02010-04-04 18:52:31 +00001671 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001672 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001673
Bill Wendling480ff322009-05-20 23:21:38 +00001674 switch (Attr) {
1675 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00001676 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00001677 break;
1678 case dwarf::DW_AT_abstract_origin: {
1679 DIEEntry *E = cast<DIEEntry>(Values[i]);
1680 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001681 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00001682 Asm->EmitInt32(Addr);
1683 break;
1684 }
Devang Patel12563b32010-04-16 23:33:45 +00001685 case dwarf::DW_AT_ranges: {
1686 // DW_AT_range Value encodes offset in debug_range section.
1687 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00001688
1689 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
1690 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
1691 V->getValue(),
1692 4);
1693 } else {
1694 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
1695 V->getValue(),
1696 DwarfDebugRangeSectionSym,
1697 4);
1698 }
Devang Patel12563b32010-04-16 23:33:45 +00001699 break;
1700 }
Devang Patel9fc11702010-05-25 23:40:22 +00001701 case dwarf::DW_AT_location: {
1702 if (UseDotDebugLocEntry.count(Die) != 0) {
1703 DIELabel *L = cast<DIELabel>(Values[i]);
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001704 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Devang Patel9fc11702010-05-25 23:40:22 +00001705 } else
1706 Values[i]->EmitValue(Asm, Form);
1707 break;
1708 }
Devang Patela1bd5a12010-09-29 19:08:08 +00001709 case dwarf::DW_AT_accessibility: {
1710 if (Asm->isVerbose()) {
1711 DIEInteger *V = cast<DIEInteger>(Values[i]);
1712 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
1713 }
1714 Values[i]->EmitValue(Asm, Form);
1715 break;
1716 }
Bill Wendling480ff322009-05-20 23:21:38 +00001717 default:
1718 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001719 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001720 break;
1721 }
Bill Wendling480ff322009-05-20 23:21:38 +00001722 }
1723
1724 // Emit the DIE children if any.
1725 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
1726 const std::vector<DIE *> &Children = Die->getChildren();
1727
1728 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00001729 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00001730
Chris Lattner7bde8c02010-04-04 18:52:31 +00001731 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00001732 Asm->OutStreamer.AddComment("End Of Children Mark");
1733 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001734 }
1735}
1736
Devang Patel9ccfb642009-12-09 18:24:21 +00001737/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00001738///
Devang Patel9ccfb642009-12-09 18:24:21 +00001739void DwarfDebug::emitDebugInfo() {
1740 // Start debug info section.
1741 Asm->OutStreamer.SwitchSection(
1742 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001743 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1744 E = CUMap.end(); I != E; ++I) {
1745 CompileUnit *TheCU = I->second;
1746 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001747
Devang Patel1a0df9a2010-05-10 22:49:55 +00001748 // Emit the compile units header.
1749 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
1750 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001751
Devang Patel1a0df9a2010-05-10 22:49:55 +00001752 // Emit size of content not including length itself
1753 unsigned ContentSize = Die->getSize() +
1754 sizeof(int16_t) + // DWARF version number
1755 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patele141234942011-04-13 19:41:17 +00001756 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001757
Devang Patel1a0df9a2010-05-10 22:49:55 +00001758 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
1759 Asm->EmitInt32(ContentSize);
1760 Asm->OutStreamer.AddComment("DWARF version number");
1761 Asm->EmitInt16(dwarf::DWARF_VERSION);
1762 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1763 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
1764 DwarfAbbrevSectionSym);
1765 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1766 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001767
Devang Patel1a0df9a2010-05-10 22:49:55 +00001768 emitDIE(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001769 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
1770 }
Bill Wendling480ff322009-05-20 23:21:38 +00001771}
1772
Devang Patel930143b2009-11-21 02:48:08 +00001773/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00001774///
Devang Patel930143b2009-11-21 02:48:08 +00001775void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00001776 // Check to see if it is worth the effort.
1777 if (!Abbreviations.empty()) {
1778 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001779 Asm->OutStreamer.SwitchSection(
1780 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001781
Chris Lattnera179b522010-04-04 19:25:43 +00001782 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00001783
1784 // For each abbrevation.
1785 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1786 // Get abbreviation data
1787 const DIEAbbrev *Abbrev = Abbreviations[i];
1788
1789 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00001790 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00001791
1792 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001793 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00001794 }
1795
1796 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00001797 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00001798
Chris Lattnera179b522010-04-04 19:25:43 +00001799 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00001800 }
1801}
1802
Devang Patel930143b2009-11-21 02:48:08 +00001803/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00001804/// the line matrix.
1805///
Devang Patel930143b2009-11-21 02:48:08 +00001806void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00001807 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00001808 Asm->OutStreamer.AddComment("Extended Op");
1809 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001810
Chris Lattner566cae92010-03-09 23:52:58 +00001811 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00001812 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00001813 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1814 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1815
1816 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00001817
Chris Lattnera179b522010-04-04 19:25:43 +00001818 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00001819 Asm->getTargetData().getPointerSize(),
1820 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00001821
1822 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00001823 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1824 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001825 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00001826 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00001827}
1828
Devang Patel9ccfb642009-12-09 18:24:21 +00001829/// emitDebugPubNames - Emit visible names into a debug pubnames section.
1830///
1831void DwarfDebug::emitDebugPubNames() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001832 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1833 E = CUMap.end(); I != E; ++I) {
1834 CompileUnit *TheCU = I->second;
1835 // Start the dwarf pubnames section.
1836 Asm->OutStreamer.SwitchSection(
1837 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001838
Devang Patel1a0df9a2010-05-10 22:49:55 +00001839 Asm->OutStreamer.AddComment("Length of Public Names Info");
1840 Asm->EmitLabelDifference(
1841 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
1842 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001843
Devang Patel1a0df9a2010-05-10 22:49:55 +00001844 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
1845 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001846
Devang Patel1a0df9a2010-05-10 22:49:55 +00001847 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001848 Asm->EmitInt16(dwarf::DWARF_VERSION);
1849
Devang Patel1a0df9a2010-05-10 22:49:55 +00001850 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001851 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00001852 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001853
Devang Patel1a0df9a2010-05-10 22:49:55 +00001854 Asm->OutStreamer.AddComment("Compilation Unit Length");
1855 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1856 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1857 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001858
Devang Patel1a0df9a2010-05-10 22:49:55 +00001859 const StringMap<DIE*> &Globals = TheCU->getGlobals();
1860 for (StringMap<DIE*>::const_iterator
1861 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1862 const char *Name = GI->getKeyData();
1863 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001864
Devang Patel1a0df9a2010-05-10 22:49:55 +00001865 Asm->OutStreamer.AddComment("DIE offset");
1866 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001867
Devang Patel1a0df9a2010-05-10 22:49:55 +00001868 if (Asm->isVerbose())
1869 Asm->OutStreamer.AddComment("External Name");
1870 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
1871 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001872
Devang Patel1a0df9a2010-05-10 22:49:55 +00001873 Asm->OutStreamer.AddComment("End Mark");
1874 Asm->EmitInt32(0);
1875 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
Nick Lewycky019d2552011-07-29 03:49:23 +00001876 TheCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00001877 }
Bill Wendling480ff322009-05-20 23:21:38 +00001878}
1879
Devang Patel04d2f2d2009-11-24 01:14:22 +00001880void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001881 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1882 E = CUMap.end(); I != E; ++I) {
1883 CompileUnit *TheCU = I->second;
1884 // Start the dwarf pubnames section.
1885 Asm->OutStreamer.SwitchSection(
1886 Asm->getObjFileLowering().getDwarfPubTypesSection());
1887 Asm->OutStreamer.AddComment("Length of Public Types Info");
1888 Asm->EmitLabelDifference(
1889 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
1890 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001891
Devang Patel1a0df9a2010-05-10 22:49:55 +00001892 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
1893 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001894
Devang Patel1a0df9a2010-05-10 22:49:55 +00001895 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
1896 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001897
Devang Patel1a0df9a2010-05-10 22:49:55 +00001898 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
1899 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
1900 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001901
Devang Patel1a0df9a2010-05-10 22:49:55 +00001902 Asm->OutStreamer.AddComment("Compilation Unit Length");
1903 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
1904 Asm->GetTempSymbol("info_begin", TheCU->getID()),
1905 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001906
Devang Patel1a0df9a2010-05-10 22:49:55 +00001907 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
1908 for (StringMap<DIE*>::const_iterator
1909 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
1910 const char *Name = GI->getKeyData();
Nick Lewycky019d2552011-07-29 03:49:23 +00001911 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001912
Devang Patel1a0df9a2010-05-10 22:49:55 +00001913 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
1914 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001915
Devang Patel1a0df9a2010-05-10 22:49:55 +00001916 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
1917 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
1918 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001919
Devang Patel1a0df9a2010-05-10 22:49:55 +00001920 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001921 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001922 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
1923 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00001924 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001925}
1926
Devang Patel930143b2009-11-21 02:48:08 +00001927/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00001928///
Devang Patel930143b2009-11-21 02:48:08 +00001929void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00001930 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00001931 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001932
Chris Lattner3d72a672010-03-09 23:38:23 +00001933 // Start the dwarf str section.
1934 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001935 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00001936
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001937 // Get all of the string pool entries and put them in an array by their ID so
1938 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001939 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001940 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001941
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001942 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
1943 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
1944 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001945
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001946 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001947
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001948 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00001949 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001950 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001951
Chris Lattner3d72a672010-03-09 23:38:23 +00001952 // Emit the string itself.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00001953 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00001954 }
1955}
1956
Devang Patel930143b2009-11-21 02:48:08 +00001957/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00001958///
Devang Patel930143b2009-11-21 02:48:08 +00001959void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001960 if (DotDebugLocEntries.empty())
1961 return;
1962
Devang Patel116a9d72011-02-04 22:57:18 +00001963 for (SmallVector<DotDebugLocEntry, 4>::iterator
1964 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
1965 I != E; ++I) {
1966 DotDebugLocEntry &Entry = *I;
1967 if (I + 1 != DotDebugLocEntries.end())
1968 Entry.Merge(I+1);
1969 }
1970
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001971 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001972 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00001973 Asm->getObjFileLowering().getDwarfLocSection());
1974 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001975 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
1976 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001977 for (SmallVector<DotDebugLocEntry, 4>::iterator
1978 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00001979 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00001980 DotDebugLocEntry &Entry = *I;
1981 if (Entry.isMerged()) continue;
Devang Patel9fc11702010-05-25 23:40:22 +00001982 if (Entry.isEmpty()) {
1983 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
1984 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00001985 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00001986 } else {
1987 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
1988 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
Devang Patel3e021532011-04-28 02:22:40 +00001989 DIVariable DV(Entry.Variable);
Rafael Espindolad23bfb82011-05-27 22:05:41 +00001990 Asm->OutStreamer.AddComment("Loc expr size");
1991 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
1992 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
1993 Asm->EmitLabelDifference(end, begin, 2);
1994 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00001995 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00001996 DIBasicType BTy(DV.getType());
1997 if (BTy.Verify() &&
1998 (BTy.getEncoding() == dwarf::DW_ATE_signed
1999 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2000 Asm->OutStreamer.AddComment("DW_OP_consts");
2001 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002002 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002003 } else {
2004 Asm->OutStreamer.AddComment("DW_OP_constu");
2005 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002006 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002007 }
Devang Pateled9fd452011-07-08 16:49:43 +00002008 } else if (Entry.isLocation()) {
2009 if (!DV.hasComplexAddress())
2010 // Regular entry.
Devang Patel3e021532011-04-28 02:22:40 +00002011 Asm->EmitDwarfRegOp(Entry.Loc);
Devang Pateled9fd452011-07-08 16:49:43 +00002012 else {
2013 // Complex address entry.
2014 unsigned N = DV.getNumAddrElements();
2015 unsigned i = 0;
2016 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2017 if (Entry.Loc.getOffset()) {
2018 i = 2;
2019 Asm->EmitDwarfRegOp(Entry.Loc);
2020 Asm->OutStreamer.AddComment("DW_OP_deref");
2021 Asm->EmitInt8(dwarf::DW_OP_deref);
2022 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2023 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2024 Asm->EmitSLEB128(DV.getAddrElement(1));
2025 } else {
2026 // If first address element is OpPlus then emit
2027 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2028 MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2029 Asm->EmitDwarfRegOp(Loc);
2030 i = 2;
2031 }
2032 } else {
2033 Asm->EmitDwarfRegOp(Entry.Loc);
2034 }
2035
2036 // Emit remaining complex address elements.
2037 for (; i < N; ++i) {
2038 uint64_t Element = DV.getAddrElement(i);
2039 if (Element == DIBuilder::OpPlus) {
2040 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2041 Asm->EmitULEB128(DV.getAddrElement(++i));
2042 } else if (Element == DIBuilder::OpDeref)
2043 Asm->EmitInt8(dwarf::DW_OP_deref);
2044 else llvm_unreachable("unknown Opcode found in complex address");
2045 }
Devang Patel3e021532011-04-28 02:22:40 +00002046 }
Devang Patel3e021532011-04-28 02:22:40 +00002047 }
Devang Pateled9fd452011-07-08 16:49:43 +00002048 // else ... ignore constant fp. There is not any good way to
2049 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002050 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002051 }
2052 }
Bill Wendling480ff322009-05-20 23:21:38 +00002053}
2054
2055/// EmitDebugARanges - Emit visible names into a debug aranges section.
2056///
2057void DwarfDebug::EmitDebugARanges() {
2058 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002059 Asm->OutStreamer.SwitchSection(
2060 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002061}
2062
Devang Patel930143b2009-11-21 02:48:08 +00002063/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00002064///
Devang Patel930143b2009-11-21 02:48:08 +00002065void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002066 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002067 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00002068 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00002069 unsigned char Size = Asm->getTargetData().getPointerSize();
2070 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002071 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00002072 I != E; ++I) {
2073 if (*I)
2074 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00002075 else
Devang Patel6c74a872010-04-27 19:46:33 +00002076 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00002077 }
Bill Wendling480ff322009-05-20 23:21:38 +00002078}
2079
Devang Patel930143b2009-11-21 02:48:08 +00002080/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00002081///
Devang Patel930143b2009-11-21 02:48:08 +00002082void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002083 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002084 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002085 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002086 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002087 }
2088}
2089
Devang Patel930143b2009-11-21 02:48:08 +00002090/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00002091/// Section Header:
2092/// 1. length of section
2093/// 2. Dwarf version number
2094/// 3. address size.
2095///
2096/// Entries (one "entry" for each function that was inlined):
2097///
2098/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2099/// otherwise offset into __debug_str for regular function name.
2100/// 2. offset into __debug_str section for regular function name.
2101/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2102/// instances for the function.
2103///
2104/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2105/// inlined instance; the die_offset points to the inlined_subroutine die in the
2106/// __debug_info section, and the low_pc is the starting address for the
2107/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002108void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00002109 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002110 return;
2111
Devang Patel1a0df9a2010-05-10 22:49:55 +00002112 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002113 return;
2114
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002115 Asm->OutStreamer.SwitchSection(
2116 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002117
Chris Lattner566cae92010-03-09 23:52:58 +00002118 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00002119 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2120 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00002121
Chris Lattnera179b522010-04-04 19:25:43 +00002122 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002123
Chris Lattner566cae92010-03-09 23:52:58 +00002124 Asm->OutStreamer.AddComment("Dwarf Version");
2125 Asm->EmitInt16(dwarf::DWARF_VERSION);
2126 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002127 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002128
Devang Patel32cc43c2010-05-07 20:54:48 +00002129 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002130 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002131
Devang Patel32cc43c2010-05-07 20:54:48 +00002132 const MDNode *Node = *I;
2133 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00002134 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002135 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002136 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002137 StringRef LName = SP.getLinkageName();
2138 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002139
Chris Lattner566cae92010-03-09 23:52:58 +00002140 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002141 if (LName.empty()) {
2142 Asm->OutStreamer.EmitBytes(Name, 0);
2143 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002144 } else
Chris Lattner70a4fce2010-04-04 23:25:33 +00002145 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2146 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002147
Chris Lattner566cae92010-03-09 23:52:58 +00002148 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00002149 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00002150 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002151
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002152 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002153 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00002154 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00002155 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002156
Chris Lattner7bde8c02010-04-04 18:52:31 +00002157 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00002158 Asm->OutStreamer.EmitSymbolValue(LI->first,
2159 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00002160 }
2161 }
2162
Chris Lattnera179b522010-04-04 19:25:43 +00002163 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002164}