blob: 06782423fe3d83015b51eb0e427f2aa6675fa226 [file] [log] [blame]
Bill Wendling0310d762009-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 Lattner6cde3e62010-03-09 00:39:24 +000013
Devang Patele4b27562009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000016#include "DIE.h"
Bill Wendling57fbba42010-04-05 22:59:21 +000017#include "llvm/Constants.h"
Bill Wendling0310d762009-05-15 09:23:25 +000018#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000019#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000022#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattner45111d12010-01-16 21:57:06 +000025#include "llvm/Target/Mangler.h"
Bill Wendling0310d762009-05-15 09:23:25 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000030#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000032#include "llvm/Analysis/DebugInfo.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000033#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000034#include "llvm/ADT/StringExtras.h"
Devang Patel9cdb4102010-04-21 16:32:19 +000035#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000038#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000039#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000040#include "llvm/Support/Timer.h"
41#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000042using namespace llvm;
43
Devang Patel9cdb4102010-04-21 16:32:19 +000044static cl::opt<bool> PrintDbgScope("print-dbgscope", cl::Hidden,
45 cl::desc("Print DbgScope information for each machine instruction"));
46
Devang Patel708e4742010-04-21 19:08:53 +000047static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
48 cl::desc("Disable debug info printing"));
49
Bill Wendling5f017e82010-04-07 09:28:04 +000050namespace {
51 const char *DWARFGroupName = "DWARF Emission";
52 const char *DbgTimerName = "DWARF Debug Writer";
53} // end anonymous namespace
54
Bill Wendling0310d762009-05-15 09:23:25 +000055//===----------------------------------------------------------------------===//
56
57/// Configuration values for initial hash set sizes (log2).
58///
Bill Wendling0310d762009-05-15 09:23:25 +000059static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000060
61namespace llvm {
62
63//===----------------------------------------------------------------------===//
64/// CompileUnit - This dwarf writer support class manages information associate
65/// with a source file.
Nick Lewycky5f9843f2009-11-17 08:11:44 +000066class CompileUnit {
Bill Wendling0310d762009-05-15 09:23:25 +000067 /// ID - File identifier for source.
68 ///
69 unsigned ID;
70
71 /// Die - Compile unit debug information entry.
72 ///
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000073 const OwningPtr<DIE> CUDie;
Bill Wendling0310d762009-05-15 09:23:25 +000074
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +000075 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel6f01d9c2009-11-21 00:31:03 +000076 DIE *IndexTyDie;
77
Bill Wendling0310d762009-05-15 09:23:25 +000078 /// GVToDieMap - Tracks the mapping of unit level debug informaton
79 /// variables to debug information entries.
Devang Patele4b27562009-08-28 23:24:31 +000080 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel622b0262010-01-19 06:19:05 +000081 DenseMap<MDNode *, DIE *> GVToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000082
83 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
84 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patele4b27562009-08-28 23:24:31 +000085 /// FIXME : Rename
Devang Patel622b0262010-01-19 06:19:05 +000086 DenseMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000087
88 /// Globals - A map of globally visible named entities for this unit.
89 ///
90 StringMap<DIE*> Globals;
91
Devang Patel193f7202009-11-24 01:14:22 +000092 /// GlobalTypes - A map of globally visible types for this unit.
93 ///
94 StringMap<DIE*> GlobalTypes;
95
Bill Wendling0310d762009-05-15 09:23:25 +000096public:
97 CompileUnit(unsigned I, DIE *D)
Devang Patel2c4ceb12009-11-21 02:48:08 +000098 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +000099
100 // Accessors.
Devang Patel193f7202009-11-24 01:14:22 +0000101 unsigned getID() const { return ID; }
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +0000102 DIE* getCUDie() const { return CUDie.get(); }
Devang Patel193f7202009-11-24 01:14:22 +0000103 const StringMap<DIE*> &getGlobals() const { return Globals; }
104 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling0310d762009-05-15 09:23:25 +0000105
106 /// hasContent - Return true if this compile unit has something to write out.
107 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000108 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000109
Devang Patel2c4ceb12009-11-21 02:48:08 +0000110 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000111 ///
Benjamin Kramerbbb88db2010-03-31 20:15:45 +0000112 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +0000113
Devang Patel193f7202009-11-24 01:14:22 +0000114 /// addGlobalType - Add a new global type to the compile unit.
115 ///
Benjamin Kramerbbb88db2010-03-31 20:15:45 +0000116 void addGlobalType(StringRef Name, DIE *Die) {
Devang Patel193f7202009-11-24 01:14:22 +0000117 GlobalTypes[Name] = Die;
118 }
119
Devang Patel017d1212009-11-20 21:37:22 +0000120 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling0310d762009-05-15 09:23:25 +0000121 /// specified debug variable.
Devang Patel017d1212009-11-20 21:37:22 +0000122 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000123
Devang Patel017d1212009-11-20 21:37:22 +0000124 /// insertDIE - Insert DIE into the map.
125 void insertDIE(MDNode *N, DIE *D) {
126 GVToDieMap.insert(std::make_pair(N, D));
127 }
Bill Wendling0310d762009-05-15 09:23:25 +0000128
Devang Patel017d1212009-11-20 21:37:22 +0000129 /// getDIEEntry - Returns the debug information entry for the speciefied
130 /// debug variable.
Devang Patel6404e4e2009-12-15 19:16:48 +0000131 DIEEntry *getDIEEntry(MDNode *N) {
Devang Patel622b0262010-01-19 06:19:05 +0000132 DenseMap<MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
Devang Patel6404e4e2009-12-15 19:16:48 +0000133 if (I == GVToDIEEntryMap.end())
134 return NULL;
135 return I->second;
136 }
Devang Patel017d1212009-11-20 21:37:22 +0000137
138 /// insertDIEEntry - Insert debug information entry into the map.
139 void insertDIEEntry(MDNode *N, DIEEntry *E) {
140 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000141 }
142
Devang Patel2c4ceb12009-11-21 02:48:08 +0000143 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000144 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000145 void addDie(DIE *Buffer) {
146 this->CUDie->addChild(Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000147 }
Devang Patel6f01d9c2009-11-21 00:31:03 +0000148
149 // getIndexTyDie - Get an anonymous type for index type.
150 DIE *getIndexTyDie() {
151 return IndexTyDie;
152 }
153
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000154 // setIndexTyDie - Set D as anonymous type for index which can be reused
155 // later.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000156 void setIndexTyDie(DIE *D) {
157 IndexTyDie = D;
158 }
159
Bill Wendling0310d762009-05-15 09:23:25 +0000160};
161
162//===----------------------------------------------------------------------===//
163/// DbgVariable - This class is used to track local variable information.
164///
Devang Patelf76a3d62009-11-16 21:53:40 +0000165class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000166 DIVariable Var; // Variable Descriptor.
167 unsigned FrameIndex; // Variable frame index.
Devang Patel90a48ad2010-03-15 18:33:46 +0000168 const MachineInstr *DbgValueMInsn; // DBG_VALUE
Devang Patelaead63c2010-03-29 22:59:58 +0000169 // DbgValueLabel - DBG_VALUE is effective from this label.
170 MCSymbol *DbgValueLabel;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000171 DbgVariable *const AbstractVar; // Abstract variable for this variable.
Devang Patel53bb5c92009-11-10 23:06:00 +0000172 DIE *TheDIE;
Bill Wendling0310d762009-05-15 09:23:25 +0000173public:
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000174 // AbsVar may be NULL.
175 DbgVariable(DIVariable V, unsigned I, DbgVariable *AbsVar)
Devang Patelaead63c2010-03-29 22:59:58 +0000176 : Var(V), FrameIndex(I), DbgValueMInsn(0),
177 DbgValueLabel(0), AbstractVar(AbsVar), TheDIE(0) {}
Devang Patel90a48ad2010-03-15 18:33:46 +0000178 DbgVariable(DIVariable V, const MachineInstr *MI, DbgVariable *AbsVar)
Devang Patelaead63c2010-03-29 22:59:58 +0000179 : Var(V), FrameIndex(0), DbgValueMInsn(MI), DbgValueLabel(0),
180 AbstractVar(AbsVar), TheDIE(0)
Devang Patel90a48ad2010-03-15 18:33:46 +0000181 {}
Bill Wendling0310d762009-05-15 09:23:25 +0000182
183 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000184 DIVariable getVariable() const { return Var; }
185 unsigned getFrameIndex() const { return FrameIndex; }
Devang Patel90a48ad2010-03-15 18:33:46 +0000186 const MachineInstr *getDbgValue() const { return DbgValueMInsn; }
Devang Patelaead63c2010-03-29 22:59:58 +0000187 MCSymbol *getDbgValueLabel() const { return DbgValueLabel; }
188 void setDbgValueLabel(MCSymbol *L) { DbgValueLabel = L; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000189 DbgVariable *getAbstractVariable() const { return AbstractVar; }
190 void setDIE(DIE *D) { TheDIE = D; }
191 DIE *getDIE() const { return TheDIE; }
Bill Wendling0310d762009-05-15 09:23:25 +0000192};
193
194//===----------------------------------------------------------------------===//
Devang Patel9cdb4102010-04-21 16:32:19 +0000195/// DbgRange - This is used to track range of instructions with identical
196/// debug info scope.
197///
198typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
199
200//===----------------------------------------------------------------------===//
Bill Wendling0310d762009-05-15 09:23:25 +0000201/// DbgScope - This class is used to track scope information.
202///
Devang Patelf76a3d62009-11-16 21:53:40 +0000203class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000204 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000205 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel3139fcf2010-01-26 21:39:14 +0000206 // Location at which this scope is inlined.
207 AssertingVH<MDNode> InlinedAtLocation;
Devang Patel53bb5c92009-11-10 23:06:00 +0000208 bool AbstractScope; // Abstract Scope
Devang Pateld38dd112009-10-01 18:25:23 +0000209 const MachineInstr *LastInsn; // Last instruction of this scope.
210 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Patel9cdb4102010-04-21 16:32:19 +0000211 unsigned DFSIn, DFSOut;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000212 // Scopes defined in scope. Contents not owned.
213 SmallVector<DbgScope *, 4> Scopes;
214 // Variables declared in scope. Contents owned.
215 SmallVector<DbgVariable *, 8> Variables;
Devang Patel9cdb4102010-04-21 16:32:19 +0000216 SmallVector<DbgRange, 4> Ranges;
Owen Anderson04c05f72009-06-24 22:53:20 +0000217 // Private state for dump()
218 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000219public:
Devang Patelc90aefe2009-10-14 21:08:09 +0000220 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000221 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Patel9cdb4102010-04-21 16:32:19 +0000222 LastInsn(0), FirstInsn(0),
223 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000224 virtual ~DbgScope();
225
226 // Accessors.
227 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000228 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000229 DIDescriptor getDesc() const { return Desc; }
Chris Lattnerbc733f52010-03-13 02:17:42 +0000230 MDNode *getInlinedAt() const { return InlinedAtLocation; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000231 MDNode *getScopeNode() const { return Desc.getNode(); }
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000232 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
233 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel9cdb4102010-04-21 16:32:19 +0000234 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
235
236 /// openInsnRange - This scope covers instruction range starting from MI.
237 void openInsnRange(const MachineInstr *MI) {
238 if (!FirstInsn)
239 FirstInsn = MI;
240
241 if (Parent)
242 Parent->openInsnRange(MI);
243 }
244
245 /// extendInsnRange - Extend the current instruction range covered by
246 /// this scope.
247 void extendInsnRange(const MachineInstr *MI) {
248 assert (FirstInsn && "MI Range is not open!");
249 LastInsn = MI;
250 if (Parent)
251 Parent->extendInsnRange(MI);
252 }
253
254 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
255 /// until now. This is used when a new scope is encountered while walking
256 /// machine instructions.
257 void closeInsnRange(DbgScope *NewScope = NULL) {
258 assert (LastInsn && "Last insn missing!");
259 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
260 FirstInsn = NULL;
261 LastInsn = NULL;
262 // If Parent dominates NewScope then do not close Parent's instruction
263 // range.
264 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
265 Parent->closeInsnRange(NewScope);
266 }
267
Devang Patel53bb5c92009-11-10 23:06:00 +0000268 void setAbstractScope() { AbstractScope = true; }
269 bool isAbstractScope() const { return AbstractScope; }
Devang Patel9cdb4102010-04-21 16:32:19 +0000270
271 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
272 unsigned getDFSOut() const { return DFSOut; }
273 void setDFSOut(unsigned O) { DFSOut = O; }
274 unsigned getDFSIn() const { return DFSIn; }
275 void setDFSIn(unsigned I) { DFSIn = I; }
276 bool dominates(const DbgScope *S) {
277 if (S == this)
278 return true;
279 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
280 return true;
281 return false;
282 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000283
Devang Patel2c4ceb12009-11-21 02:48:08 +0000284 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000285 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000286 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000287
Devang Patel2c4ceb12009-11-21 02:48:08 +0000288 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000289 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000290 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000291
Bill Wendling0310d762009-05-15 09:23:25 +0000292#ifndef NDEBUG
293 void dump() const;
294#endif
295};
Devang Patel9cdb4102010-04-21 16:32:19 +0000296
Chris Lattnerea761862010-04-05 04:09:20 +0000297} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000298
299#ifndef NDEBUG
300void DbgScope::dump() const {
David Greenef83adbc2009-12-24 00:31:35 +0000301 raw_ostream &err = dbgs();
Chris Lattnerc281de12009-08-23 00:51:00 +0000302 err.indent(IndentLevel);
Devang Patel53bb5c92009-11-10 23:06:00 +0000303 MDNode *N = Desc.getNode();
304 N->dump();
Devang Patel53bb5c92009-11-10 23:06:00 +0000305 if (AbstractScope)
306 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000307
308 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000309 if (!Scopes.empty())
310 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000311 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
312 if (Scopes[i] != this)
313 Scopes[i]->dump();
314
315 IndentLevel -= 2;
316}
317#endif
318
Bill Wendling0310d762009-05-15 09:23:25 +0000319DbgScope::~DbgScope() {
Bill Wendling0310d762009-05-15 09:23:25 +0000320 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
321 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000322}
323
Chris Lattner49cd6642010-04-05 05:11:15 +0000324DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Chris Lattnerd38fee82010-04-05 00:13:49 +0000325 : Asm(A), MMI(Asm->MMI), ModuleCU(0),
Chris Lattner994cb122010-04-05 03:52:55 +0000326 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelf2548ca2010-04-16 23:33:45 +0000327 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000328 NextStringPoolNumber = 0;
Chris Lattner9c69e285532010-04-04 22:59:04 +0000329
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000330 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
331 DwarfStrSectionSym = TextSectionSym = 0;
Devang Patel9cdb4102010-04-21 16:32:19 +0000332 DwarfDebugRangeSectionSym = 0;
Devang Patel3547a882010-04-22 18:39:21 +0000333 FunctionBeginSym = 0;
Torok Edwin9c421072010-04-07 10:44:46 +0000334 if (TimePassesIsEnabled) {
335 NamedRegionTimer T(DbgTimerName, DWARFGroupName);
336 beginModule(M);
337 } else {
338 beginModule(M);
339 }
Bill Wendling0310d762009-05-15 09:23:25 +0000340}
341DwarfDebug::~DwarfDebug() {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000342 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
343 DIEBlocks[j]->~DIEBlock();
Bill Wendling0310d762009-05-15 09:23:25 +0000344}
345
Chris Lattnerbc733f52010-03-13 02:17:42 +0000346MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
347 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
348 if (Entry.first) return Entry.first;
349
350 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000351 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000352}
353
354
Devang Patel2c4ceb12009-11-21 02:48:08 +0000355/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000356///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000357void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000358 // Profile the node so that we can make it unique.
359 FoldingSetNodeID ID;
360 Abbrev.Profile(ID);
361
362 // Check the set for priors.
363 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
364
365 // If it's newly added.
366 if (InSet == &Abbrev) {
367 // Add to abbreviation list.
368 Abbreviations.push_back(&Abbrev);
369
370 // Assign the vector position + 1 as its number.
371 Abbrev.setNumber(Abbreviations.size());
372 } else {
373 // Assign existing abbreviation number.
374 Abbrev.setNumber(InSet->getNumber());
375 }
376}
377
Devang Patel2c4ceb12009-11-21 02:48:08 +0000378/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000379/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000380DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000381 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000382 return Value;
383}
384
Devang Patel2c4ceb12009-11-21 02:48:08 +0000385/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000386///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000387void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000388 unsigned Form, uint64_t Integer) {
389 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000390 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000391 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000392}
393
Devang Patel2c4ceb12009-11-21 02:48:08 +0000394/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000395///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000396void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000397 unsigned Form, int64_t Integer) {
398 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000399 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000400 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000401}
402
Devang Patel69f57b12009-12-02 15:25:16 +0000403/// addString - Add a string attribute data and value. DIEString only
404/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000405void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattner4cf202b2010-01-23 03:11:46 +0000406 StringRef String) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000407 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000408 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000409}
410
Devang Patel2c4ceb12009-11-21 02:48:08 +0000411/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000412///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000413void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000414 const MCSymbol *Label) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000415 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000416 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000417}
418
Devang Patel2c4ceb12009-11-21 02:48:08 +0000419/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000420///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000421void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000422 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000423 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000424 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000425}
426
Chris Lattner74e41f92010-04-05 05:24:55 +0000427/// addDIEEntry - Add a DIE attribute data and value.
428///
429void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
430 DIE *Entry) {
431 Die->addValue(Attribute, Form, createDIEEntry(Entry));
432}
433
434
Devang Patel2c4ceb12009-11-21 02:48:08 +0000435/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000436///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000437void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000438 DIEBlock *Block) {
Chris Lattnera37d5382010-04-05 00:18:22 +0000439 Block->ComputeSize(Asm);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000440 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000441 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000442}
443
Devang Patel2c4ceb12009-11-21 02:48:08 +0000444/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000445/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000446void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendling0310d762009-05-15 09:23:25 +0000447 // If there is no compile unit specified, don't add a line #.
Devang Patel3c91b052010-03-08 20:52:55 +0000448 if (!V->getCompileUnit().Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000449 return;
450
451 unsigned Line = V->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000452 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
453 V->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000454 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000455 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
456 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000457}
458
Devang Patel2c4ceb12009-11-21 02:48:08 +0000459/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000460/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000461void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendling0310d762009-05-15 09:23:25 +0000462 // If there is no compile unit specified, don't add a line #.
Devang Patel3c91b052010-03-08 20:52:55 +0000463 if (!G->getCompileUnit().Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000464 return;
465
466 unsigned Line = G->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000467 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
468 G->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000469 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000470 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
471 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000472}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000473
Devang Patel2c4ceb12009-11-21 02:48:08 +0000474/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000475/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000476void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel82dfc0c2009-08-31 22:47:13 +0000477 // If there is no compile unit specified, don't add a line #.
Devang Patel3c91b052010-03-08 20:52:55 +0000478 if (!SP->getCompileUnit().Verify())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000479 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000480 // If the line number is 0, don't add it.
481 if (SP->getLineNumber() == 0)
482 return;
483
Devang Patel82dfc0c2009-08-31 22:47:13 +0000484 unsigned Line = SP->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000485 if (!SP->getContext().Verify())
486 return;
Devang Patel9bb59a22010-03-24 21:30:35 +0000487 unsigned FileID = GetOrCreateSourceID(SP->getDirectory(),
488 SP->getFilename());
Devang Patel82dfc0c2009-08-31 22:47:13 +0000489 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000490 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
491 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000492}
493
Devang Patel2c4ceb12009-11-21 02:48:08 +0000494/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000495/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000496void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendling0310d762009-05-15 09:23:25 +0000497 // If there is no compile unit specified, don't add a line #.
498 DICompileUnit CU = Ty->getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000499 if (!CU.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000500 return;
501
502 unsigned Line = Ty->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000503 if (!Ty->getContext().Verify())
504 return;
505 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
506 Ty->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000507 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000508 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
509 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000510}
511
Devang Patel6404e4e2009-12-15 19:16:48 +0000512/// addSourceLine - Add location information to specified debug information
513/// entry.
514void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
515 // If there is no compile unit specified, don't add a line #.
Devang Patel3c91b052010-03-08 20:52:55 +0000516 if (!NS->getCompileUnit().Verify())
Devang Patel6404e4e2009-12-15 19:16:48 +0000517 return;
518
519 unsigned Line = NS->getLineNumber();
520 StringRef FN = NS->getFilename();
521 StringRef Dir = NS->getDirectory();
522
523 unsigned FileID = GetOrCreateSourceID(Dir, FN);
524 assert(FileID && "Invalid file id");
525 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
526 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
527}
528
Caroline Ticedc8f6042009-08-31 21:19:37 +0000529/* Byref variables, in Blocks, are declared by the programmer as
530 "SomeType VarName;", but the compiler creates a
531 __Block_byref_x_VarName struct, and gives the variable VarName
532 either the struct, or a pointer to the struct, as its type. This
533 is necessary for various behind-the-scenes things the compiler
534 needs to do with by-reference variables in blocks.
535
536 However, as far as the original *programmer* is concerned, the
537 variable should still have type 'SomeType', as originally declared.
538
539 The following function dives into the __Block_byref_x_VarName
540 struct to find the original type of the variable. This will be
541 passed back to the code generating the type for the Debug
542 Information Entry for the variable 'VarName'. 'VarName' will then
543 have the original type 'SomeType' in its debug information.
544
545 The original type 'SomeType' will be the type of the field named
546 'VarName' inside the __Block_byref_x_VarName struct.
547
548 NOTE: In order for this to not completely fail on the debugger
549 side, the Debug Information Entry for the variable VarName needs to
550 have a DW_AT_location that tells the debugger how to unwind through
551 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patel2c4ceb12009-11-21 02:48:08 +0000552 value of the variable. The function addBlockByrefType does this. */
Caroline Ticedc8f6042009-08-31 21:19:37 +0000553
554/// Find the type the programmer originally declared the variable to be
555/// and return that type.
556///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000557DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000558
559 DIType subType = Ty;
560 unsigned tag = Ty.getTag();
561
562 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000563 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000564 subType = DTy.getTypeDerivedFrom();
565 }
566
567 DICompositeType blockStruct = DICompositeType(subType.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000568 DIArray Elements = blockStruct.getTypeArray();
569
Caroline Ticedc8f6042009-08-31 21:19:37 +0000570 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
571 DIDescriptor Element = Elements.getElement(i);
572 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000573 if (Name == DT.getName())
Caroline Ticedc8f6042009-08-31 21:19:37 +0000574 return (DT.getTypeDerivedFrom());
575 }
576
577 return Ty;
578}
579
Devang Patel2c4ceb12009-11-21 02:48:08 +0000580/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000581/// and generate the DWARF information necessary to find the actual variable
582/// given the extra address information encoded in the DIVariable, starting from
583/// the starting location. Add the DWARF information to the die.
584///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000585void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000586 unsigned Attribute,
587 const MachineLocation &Location) {
588 const DIVariable &VD = DV->getVariable();
589 DIType Ty = VD.getType();
590
591 // Decode the original location, and use that as the start of the byref
592 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000593 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000594 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000595 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000596
597 if (Location.isReg()) {
598 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000599 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000600 } else {
601 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000602 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
603 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000604 }
605 } else {
606 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000607 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000608 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000609 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
610 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000611 }
612
Devang Patel2c4ceb12009-11-21 02:48:08 +0000613 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000614 }
615
616 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
617 uint64_t Element = VD.getAddrElement(i);
618
619 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000620 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
621 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000622 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000623 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000624 } else llvm_unreachable("unknown DIFactory Opcode");
625 }
626
627 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000628 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000629}
630
Caroline Ticedc8f6042009-08-31 21:19:37 +0000631/* Byref variables, in Blocks, are declared by the programmer as "SomeType
632 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
633 gives the variable VarName either the struct, or a pointer to the struct, as
634 its type. This is necessary for various behind-the-scenes things the
635 compiler needs to do with by-reference variables in Blocks.
636
637 However, as far as the original *programmer* is concerned, the variable
638 should still have type 'SomeType', as originally declared.
639
Devang Patel2c4ceb12009-11-21 02:48:08 +0000640 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000641 struct to find the original type of the variable, which is then assigned to
642 the variable's Debug Information Entry as its real type. So far, so good.
643 However now the debugger will expect the variable VarName to have the type
644 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000645 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000646 pointers and struct to find the actual variable of type SomeType.
647
648 The following function does just that. We start by getting
649 the "normal" location for the variable. This will be the location
650 of either the struct __Block_byref_x_VarName or the pointer to the
651 struct __Block_byref_x_VarName.
652
653 The struct will look something like:
654
655 struct __Block_byref_x_VarName {
656 ... <various fields>
657 struct __Block_byref_x_VarName *forwarding;
658 ... <various other fields>
659 SomeType VarName;
660 ... <maybe more fields>
661 };
662
663 If we are given the struct directly (as our starting point) we
664 need to tell the debugger to:
665
666 1). Add the offset of the forwarding field.
667
Dan Gohmanf451cb82010-02-10 16:03:48 +0000668 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000669 struct to use (the real one may have been copied onto the heap).
670
671 3). Add the offset for the field VarName, to find the actual variable.
672
673 If we started with a pointer to the struct, then we need to
674 dereference that pointer first, before the other steps.
675 Translating this into DWARF ops, we will need to append the following
676 to the current location description for the variable:
677
678 DW_OP_deref -- optional, if we start with a pointer
679 DW_OP_plus_uconst <forward_fld_offset>
680 DW_OP_deref
681 DW_OP_plus_uconst <varName_fld_offset>
682
683 That is what this function does. */
684
Devang Patel2c4ceb12009-11-21 02:48:08 +0000685/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000686/// provided, and generate the DWARF information necessary to find the
687/// actual Block variable (navigating the Block struct) based on the
688/// starting location. Add the DWARF information to the die. For
689/// more information, read large comment just above here.
690///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000691void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000692 unsigned Attribute,
693 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000694 const DIVariable &VD = DV->getVariable();
695 DIType Ty = VD.getType();
696 DIType TmpTy = Ty;
697 unsigned Tag = Ty.getTag();
698 bool isPointer = false;
699
Devang Patel65dbc902009-11-25 17:36:49 +0000700 StringRef varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000701
702 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000703 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000704 TmpTy = DTy.getTypeDerivedFrom();
705 isPointer = true;
706 }
707
708 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
709
Daniel Dunbar00564992009-09-19 20:40:14 +0000710 // Find the __forwarding field and the variable field in the __Block_byref
711 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000712 DIArray Fields = blockStruct.getTypeArray();
713 DIDescriptor varField = DIDescriptor();
714 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000715
Daniel Dunbar00564992009-09-19 20:40:14 +0000716 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
717 DIDescriptor Element = Fields.getElement(i);
718 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000719 StringRef fieldName = DT.getName();
720 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000721 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000722 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000723 varField = Element;
724 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000725
Daniel Dunbar00564992009-09-19 20:40:14 +0000726 // Get the offsets for the forwarding field and the variable field.
Chris Lattner1d65ba72010-03-31 06:06:37 +0000727 unsigned forwardingFieldOffset =
Daniel Dunbar00564992009-09-19 20:40:14 +0000728 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
Chris Lattner1d65ba72010-03-31 06:06:37 +0000729 unsigned varFieldOffset =
Daniel Dunbar00564992009-09-19 20:40:14 +0000730 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000731
Mike Stump7e3720d2009-09-24 23:21:26 +0000732 // Decode the original location, and use that as the start of the byref
733 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000734 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar00564992009-09-19 20:40:14 +0000735 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000736 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000737
Daniel Dunbar00564992009-09-19 20:40:14 +0000738 if (Location.isReg()) {
739 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000740 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000741 else {
742 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000743 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
744 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000745 }
746 } else {
747 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000748 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000749 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000750 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
751 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000752 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000753
Devang Patel2c4ceb12009-11-21 02:48:08 +0000754 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000755 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000756
Mike Stump7e3720d2009-09-24 23:21:26 +0000757 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000758 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000759 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000760 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000761
Daniel Dunbar00564992009-09-19 20:40:14 +0000762 // Next add the offset for the '__forwarding' field:
763 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
764 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000765 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000766 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
767 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000768 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000769
Daniel Dunbar00564992009-09-19 20:40:14 +0000770 // Now dereference the __forwarding field to get to the real __Block_byref
771 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000772 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000773
Daniel Dunbar00564992009-09-19 20:40:14 +0000774 // Now that we've got the real __Block_byref... struct, add the offset
775 // for the variable's field to get to the location of the actual variable:
776 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000777 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000778 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
779 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000780 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000781
Daniel Dunbar00564992009-09-19 20:40:14 +0000782 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000783 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000784}
785
Devang Patel2c4ceb12009-11-21 02:48:08 +0000786/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000787/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000788void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000789 const MachineLocation &Location) {
Chris Lattnerd38fee82010-04-05 00:13:49 +0000790 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling0310d762009-05-15 09:23:25 +0000791 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000792 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bill Wendling0310d762009-05-15 09:23:25 +0000793
794 if (Location.isReg()) {
795 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000796 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000797 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000798 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
799 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000800 }
801 } else {
802 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000803 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000804 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000805 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
806 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000807 }
808
Devang Patel2c4ceb12009-11-21 02:48:08 +0000809 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000810 }
811
Devang Patel2c4ceb12009-11-21 02:48:08 +0000812 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000813}
814
Devang Patelc366f832009-12-10 19:14:49 +0000815/// addToContextOwner - Add Die into the list of its context owner's children.
816void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3c91b052010-03-08 20:52:55 +0000817 if (Context.isType()) {
Devang Patelc366f832009-12-10 19:14:49 +0000818 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
819 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000820 } else if (Context.isNameSpace()) {
821 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context.getNode()));
822 ContextDIE->addChild(Die);
Devang Patelc366f832009-12-10 19:14:49 +0000823 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
824 ContextDIE->addChild(Die);
825 else
826 ModuleCU->addDie(Die);
827}
828
Devang Patel16ced732009-12-10 18:05:33 +0000829/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
830/// given DIType.
831DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
832 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
833 if (TyDIE)
834 return TyDIE;
835
836 // Create new type.
837 TyDIE = new DIE(dwarf::DW_TAG_base_type);
838 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
839 if (Ty.isBasicType())
840 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
841 else if (Ty.isCompositeType())
842 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
843 else {
844 assert(Ty.isDerivedType() && "Unknown kind of DIType");
845 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
846 }
847
Devang Patelc366f832009-12-10 19:14:49 +0000848 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000849 return TyDIE;
850}
851
Devang Patel2c4ceb12009-11-21 02:48:08 +0000852/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000853void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel3c91b052010-03-08 20:52:55 +0000854 if (!Ty.isValid())
Bill Wendling0310d762009-05-15 09:23:25 +0000855 return;
856
857 // Check for pre-existence.
Devang Patel8a241142009-12-09 18:24:21 +0000858 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000859 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000860 if (Entry) {
861 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000862 return;
863 }
864
Bill Wendling0310d762009-05-15 09:23:25 +0000865 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000866 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000867
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000868 // Set up proxy.
869 Entry = createDIEEntry(Buffer);
870 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
871
Devang Patel2c4ceb12009-11-21 02:48:08 +0000872 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000873}
874
Devang Patel2c4ceb12009-11-21 02:48:08 +0000875/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000876void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000877 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000878 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000879 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000880 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000881 BTy.getEncoding());
882
883 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000884 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000885 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000886 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000887 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000888}
889
Devang Patel2c4ceb12009-11-21 02:48:08 +0000890/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000891void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000892 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000893 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000894 uint64_t Size = DTy.getSizeInBits() >> 3;
895 unsigned Tag = DTy.getTag();
896
897 // FIXME - Workaround for templates.
898 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
899
900 Buffer.setTag(Tag);
901
902 // Map to main type, void will not have a type.
903 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +0000904 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000905
906 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000907 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000908 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000909
910 // Add size if non-zero (derived types might be zero-sized.)
911 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000912 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000913
914 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +0000915 if (!DTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000916 addSourceLine(&Buffer, &DTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000917}
918
Devang Patel2c4ceb12009-11-21 02:48:08 +0000919/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +0000920void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000921 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000922 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000923
924 uint64_t Size = CTy.getSizeInBits() >> 3;
925 unsigned Tag = CTy.getTag();
926 Buffer.setTag(Tag);
927
928 switch (Tag) {
929 case dwarf::DW_TAG_vector_type:
930 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +0000931 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000932 break;
933 case dwarf::DW_TAG_enumeration_type: {
934 DIArray Elements = CTy.getTypeArray();
935
936 // Add enumerators to enumeration type.
937 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
938 DIE *ElemDie = NULL;
Devang Patel3c91b052010-03-08 20:52:55 +0000939 DIDescriptor Enum(Elements.getElement(i).getNode());
940 if (Enum.isEnumerator()) {
941 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000942 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +0000943 }
Bill Wendling0310d762009-05-15 09:23:25 +0000944 }
945 }
946 break;
947 case dwarf::DW_TAG_subroutine_type: {
948 // Add return type.
949 DIArray Elements = CTy.getTypeArray();
950 DIDescriptor RTy = Elements.getElement(0);
Devang Patel8a241142009-12-09 18:24:21 +0000951 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000952
953 // Add prototype flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000954 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000955
956 // Add arguments.
957 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
958 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
959 DIDescriptor Ty = Elements.getElement(i);
Devang Patel8a241142009-12-09 18:24:21 +0000960 addType(Arg, DIType(Ty.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000961 Buffer.addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +0000962 }
963 }
964 break;
965 case dwarf::DW_TAG_structure_type:
966 case dwarf::DW_TAG_union_type:
967 case dwarf::DW_TAG_class_type: {
968 // Add elements to structure type.
969 DIArray Elements = CTy.getTypeArray();
970
971 // A forward struct declared type may not have elements available.
Devang Patel3c91b052010-03-08 20:52:55 +0000972 unsigned N = Elements.getNumElements();
973 if (N == 0)
Bill Wendling0310d762009-05-15 09:23:25 +0000974 break;
975
976 // Add elements to structure type.
Devang Patel3c91b052010-03-08 20:52:55 +0000977 for (unsigned i = 0; i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +0000978 DIDescriptor Element = Elements.getElement(i);
979 DIE *ElemDie = NULL;
Devang Patel3c91b052010-03-08 20:52:55 +0000980 if (Element.isSubprogram())
Devang Patelffe966c2009-12-14 16:18:45 +0000981 ElemDie = createSubprogramDIE(DISubprogram(Element.getNode()));
Devang Patel3c91b052010-03-08 20:52:55 +0000982 else if (Element.isVariable()) {
Devang Patel1ee0cb92010-01-30 01:08:30 +0000983 DIVariable DV(Element.getNode());
984 ElemDie = new DIE(dwarf::DW_TAG_variable);
985 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
986 DV.getName());
987 addType(ElemDie, DV.getType());
988 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
989 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
990 addSourceLine(ElemDie, &DV);
Devang Patel3c91b052010-03-08 20:52:55 +0000991 } else if (Element.isDerivedType())
Devang Patel8a241142009-12-09 18:24:21 +0000992 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patel3c91b052010-03-08 20:52:55 +0000993 else
994 continue;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000995 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +0000996 }
997
Devang Patela1ba2692009-08-27 23:51:51 +0000998 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000999 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001000
1001 unsigned RLang = CTy.getRunTimeLang();
1002 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001003 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +00001004 dwarf::DW_FORM_data1, RLang);
Devang Patelb5544992010-01-26 21:16:06 +00001005
1006 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel3c91b052010-03-08 20:52:55 +00001007 if (DIDescriptor(ContainingType.getNode()).isCompositeType())
Devang Patelb5544992010-01-26 21:16:06 +00001008 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
1009 getOrCreateTypeDIE(DIType(ContainingType.getNode())));
Bill Wendling0310d762009-05-15 09:23:25 +00001010 break;
1011 }
1012 default:
1013 break;
1014 }
1015
1016 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +00001017 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001018 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001019
Devang Patel8cbb6122010-01-29 18:34:58 +00001020 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendling0310d762009-05-15 09:23:25 +00001021 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1022 // Add size if non-zero (derived types might be zero-sized.)
1023 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001024 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001025 else {
1026 // Add zero size if it is not a forward declaration.
1027 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001028 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001029 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001030 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +00001031 }
1032
1033 // Add source line info if available.
1034 if (!CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001035 addSourceLine(&Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001036 }
1037}
1038
Devang Patel2c4ceb12009-11-21 02:48:08 +00001039/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1040void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +00001041 int64_t L = SR.getLo();
1042 int64_t H = SR.getHi();
1043 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1044
Devang Patel2c4ceb12009-11-21 02:48:08 +00001045 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +00001046 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001047 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +00001048 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001049
Devang Patel2c4ceb12009-11-21 02:48:08 +00001050 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +00001051}
1052
Devang Patel2c4ceb12009-11-21 02:48:08 +00001053/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001054void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +00001055 DICompositeType *CTy) {
1056 Buffer.setTag(dwarf::DW_TAG_array_type);
1057 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001058 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001059
1060 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +00001061 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001062 DIArray Elements = CTy->getTypeArray();
1063
Devang Patel6f01d9c2009-11-21 00:31:03 +00001064 // Get an anonymous type for index type.
Devang Patel8a241142009-12-09 18:24:21 +00001065 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +00001066 if (!IdxTy) {
1067 // Construct an anonymous type for index type.
1068 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001069 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1070 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001071 dwarf::DW_ATE_signed);
Devang Patel8a241142009-12-09 18:24:21 +00001072 ModuleCU->addDie(IdxTy);
1073 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001074 }
Bill Wendling0310d762009-05-15 09:23:25 +00001075
1076 // Add subranges to array type.
1077 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1078 DIDescriptor Element = Elements.getElement(i);
1079 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001080 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001081 }
1082}
1083
Devang Patel2c4ceb12009-11-21 02:48:08 +00001084/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3c91b052010-03-08 20:52:55 +00001085DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001086 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3c91b052010-03-08 20:52:55 +00001087 StringRef Name = ETy.getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001088 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3c91b052010-03-08 20:52:55 +00001089 int64_t Value = ETy.getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001090 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001091 return Enumerator;
1092}
1093
Devang Patel351ca332010-01-05 01:46:14 +00001094/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1095/// printer to not emit usual symbol prefix before the symbol name is used then
1096/// return linkage name after skipping this special LLVM prefix.
1097static StringRef getRealLinkageName(StringRef LinkageName) {
1098 char One = '\1';
1099 if (LinkageName.startswith(StringRef(&One, 1)))
1100 return LinkageName.substr(1);
1101 return LinkageName;
1102}
1103
Devang Patel2c4ceb12009-11-21 02:48:08 +00001104/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patel8a241142009-12-09 18:24:21 +00001105DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001106 // If the global variable was optmized out then no need to create debug info
1107 // entry.
Devang Patel84c73e92009-11-06 17:58:12 +00001108 if (!GV.getGlobal()) return NULL;
Devang Patel65dbc902009-11-25 17:36:49 +00001109 if (GV.getDisplayName().empty()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001110
Bill Wendling0310d762009-05-15 09:23:25 +00001111 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001112 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001113 GV.getDisplayName());
1114
Devang Patel65dbc902009-11-25 17:36:49 +00001115 StringRef LinkageName = GV.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001116 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001117 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001118 getRealLinkageName(LinkageName));
1119
Devang Patel8a241142009-12-09 18:24:21 +00001120 addType(GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001121 if (!GV.isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001122 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1123 addSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001124
Bill Wendling0310d762009-05-15 09:23:25 +00001125 return GVDie;
1126}
1127
Devang Patel2c4ceb12009-11-21 02:48:08 +00001128/// createMemberDIE - Create new member DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001129DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001130 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001131 StringRef Name = DT.getName();
1132 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001133 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel65dbc902009-11-25 17:36:49 +00001134
Devang Patel8a241142009-12-09 18:24:21 +00001135 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001136
Devang Patel2c4ceb12009-11-21 02:48:08 +00001137 addSourceLine(MemberDie, &DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001138
Benjamin Kramer345ef342010-03-31 19:34:01 +00001139 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001140 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001141
Bill Wendling0310d762009-05-15 09:23:25 +00001142 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001143 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001144
1145 if (Size != FieldSize) {
1146 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001147 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1148 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001149
1150 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling0310d762009-05-15 09:23:25 +00001151 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1152 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer3d594fd2010-01-07 17:50:57 +00001153 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling0310d762009-05-15 09:23:25 +00001154 Offset -= FieldOffset;
1155
1156 // Maybe we need to work from the other end.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001157 if (Asm->getTargetData().isLittleEndian())
1158 Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001159 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001160
Devang Patel33db5082009-11-04 22:06:12 +00001161 // Here WD_AT_data_member_location points to the anonymous
1162 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001163 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001164
1165 } else
1166 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001167 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001168
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001169 if (DT.getTag() == dwarf::DW_TAG_inheritance
1170 && DT.isVirtual()) {
1171
1172 // For C++, virtual base classes are not at fixed offset. Use following
1173 // expression to extract appropriate offset from vtable.
1174 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1175
Benjamin Kramer345ef342010-03-31 19:34:01 +00001176 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001177 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1178 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1179 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1180 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1181 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1182 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1183 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1184
1185 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1186 VBaseLocationDie);
1187 } else
1188 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001189
1190 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001191 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001192 dwarf::DW_ACCESS_protected);
1193 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001194 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001195 dwarf::DW_ACCESS_private);
Devang Patel5d11eb02009-12-03 19:11:07 +00001196 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1197 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1198 dwarf::DW_ACCESS_public);
1199 if (DT.isVirtual())
1200 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1201 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001202 return MemberDie;
1203}
1204
Devang Patelffe966c2009-12-14 16:18:45 +00001205/// createSubprogramDIE - Create new DIE using SP.
1206DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
1207 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1208 if (SPDie)
1209 return SPDie;
1210
1211 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1eac3e72010-03-02 17:58:15 +00001212 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +00001213 if (!SP.getName().empty())
1214 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001215
Devang Patel65dbc902009-11-25 17:36:49 +00001216 StringRef LinkageName = SP.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001217 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001218 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001219 getRealLinkageName(LinkageName));
1220
Devang Patel2c4ceb12009-11-21 02:48:08 +00001221 addSourceLine(SPDie, &SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001222
Bill Wendling0310d762009-05-15 09:23:25 +00001223 // Add prototyped tag, if C or ObjC.
1224 unsigned Lang = SP.getCompileUnit().getLanguage();
1225 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1226 Lang == dwarf::DW_LANG_ObjC)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001227 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001228
1229 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001230 DICompositeType SPTy = SP.getType();
1231 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001232 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001233
Devang Patel3c91b052010-03-08 20:52:55 +00001234 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001235 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001236 else
Devang Patel8a241142009-12-09 18:24:21 +00001237 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001238
Devang Patel5d11eb02009-12-03 19:11:07 +00001239 unsigned VK = SP.getVirtuality();
1240 if (VK) {
1241 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001242 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel5d11eb02009-12-03 19:11:07 +00001243 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1244 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1245 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel622b0262010-01-19 06:19:05 +00001246 ContainingTypeMap.insert(std::make_pair(SPDie,
1247 SP.getContainingType().getNode()));
Devang Patel5d11eb02009-12-03 19:11:07 +00001248 }
1249
Devang Patelffe966c2009-12-14 16:18:45 +00001250 if (MakeDecl || !SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001251 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001252
1253 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001254 // be handled while processing variables.
1255 DICompositeType SPTy = SP.getType();
1256 DIArray Args = SPTy.getTypeArray();
1257 unsigned SPTag = SPTy.getTag();
1258
Bill Wendling0310d762009-05-15 09:23:25 +00001259 if (SPTag == dwarf::DW_TAG_subroutine_type)
1260 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1261 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelb4645642010-02-06 01:02:37 +00001262 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1263 addType(Arg, ATy);
1264 if (ATy.isArtificial())
1265 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001266 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001267 }
1268 }
1269
Devang Patel4e0d19d2010-02-03 19:57:19 +00001270 if (SP.isArtificial())
1271 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1272
Bill Wendling0310d762009-05-15 09:23:25 +00001273 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001274 ModuleCU->insertDIE(SP.getNode(), SPDie);
Devang Patel2a4a3b72010-04-19 19:14:02 +00001275
Evan Chenge5667632010-04-21 03:18:23 +00001276 if (!DisableFramePointerElim(*Asm->MF))
Devang Patel2a4a3b72010-04-19 19:14:02 +00001277 addUInt(SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr, dwarf::DW_FORM_flag, 1);
1278
Bill Wendling0310d762009-05-15 09:23:25 +00001279 return SPDie;
1280}
1281
Devang Patel53bb5c92009-11-10 23:06:00 +00001282DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
Chris Lattnered7a77b2010-03-31 05:36:29 +00001283 assert(N && "Invalid Scope encoding!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001284
1285 DbgScope *AScope = AbstractScopes.lookup(N);
1286 if (AScope)
1287 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001288
Devang Patel53bb5c92009-11-10 23:06:00 +00001289 DbgScope *Parent = NULL;
1290
1291 DIDescriptor Scope(N);
1292 if (Scope.isLexicalBlock()) {
1293 DILexicalBlock DB(N);
1294 DIDescriptor ParentDesc = DB.getContext();
Devang Patel3c91b052010-03-08 20:52:55 +00001295 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001296 }
1297
1298 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1299
1300 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001301 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001302 AScope->setAbstractScope();
1303 AbstractScopes[N] = AScope;
1304 if (DIDescriptor(N).isSubprogram())
1305 AbstractScopesList.push_back(AScope);
1306 return AScope;
1307}
Devang Patelaf9e8472009-10-01 20:31:14 +00001308
Devang Patel5f094002010-04-06 23:53:48 +00001309/// isSubprogramContext - Return true if Context is either a subprogram
1310/// or another context nested inside a subprogram.
Dan Gohmanb3579832010-04-15 17:08:50 +00001311static bool isSubprogramContext(MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +00001312 if (!Context)
1313 return false;
1314 DIDescriptor D(Context);
1315 if (D.isSubprogram())
1316 return true;
1317 if (D.isType())
1318 return isSubprogramContext(DIType(Context).getContext().getNode());
1319 return false;
1320}
1321
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001322/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001323/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1324/// If there are global variables in this scope then create and insert
1325/// DIEs for these variables.
1326DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001327 DIE *SPDie = ModuleCU->getDIE(SPNode);
1328 assert(SPDie && "Unable to find subprogram DIE!");
1329 DISubprogram SP(SPNode);
Chris Lattner206d61e2010-03-13 07:26:18 +00001330
Chris Lattnerd38fee82010-04-05 00:13:49 +00001331 // There is not any need to generate specification DIE for a function
1332 // defined at compile unit level. If a function is defined inside another
1333 // function then gdb prefers the definition at top level and but does not
1334 // expect specification DIE in parent function. So avoid creating
1335 // specification DIE for a function defined inside a function.
1336 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Devang Patel5f094002010-04-06 23:53:48 +00001337 !SP.getContext().isFile() &&
1338 !isSubprogramContext(SP.getContext().getNode())) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001339 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1340
1341 // Add arguments.
1342 DICompositeType SPTy = SP.getType();
1343 DIArray Args = SPTy.getTypeArray();
1344 unsigned SPTag = SPTy.getTag();
1345 if (SPTag == dwarf::DW_TAG_subroutine_type)
1346 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1347 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1348 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1349 addType(Arg, ATy);
1350 if (ATy.isArtificial())
1351 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1352 SPDie->addChild(Arg);
1353 }
1354 DIE *SPDeclDie = SPDie;
1355 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1356 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1357 SPDeclDie);
1358 ModuleCU->addDie(SPDie);
1359 }
1360
1361 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1362 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1363 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1364 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1365 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1366 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1367 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +00001368
Chris Lattnerd38fee82010-04-05 00:13:49 +00001369 if (!DISubprogram(SPNode).isLocalToUnit())
1370 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1371
1372 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +00001373}
1374
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001375/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001376/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1377DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel9cdb4102010-04-21 16:32:19 +00001378
1379 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1380 if (Scope->isAbstractScope())
1381 return ScopeDIE;
1382
1383 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1384 if (Ranges.empty())
1385 return 0;
1386
Devang Patel18ee3bb2010-04-22 20:52:00 +00001387 bool MarkFunctionBegin = false;
1388 if (FunctionBeginSym &&
1389 Asm->MF->getFunction()->isWeakForLinker())
1390 MarkFunctionBegin = true;
1391
Devang Patel9cdb4102010-04-21 16:32:19 +00001392 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1393 if (Ranges.size() > 1) {
1394 // .debug_range section has not been laid out yet. Emit offset in
1395 // .debug_range as a uint, size 4, for now. emitDIE will handle
1396 // DW_AT_ranges appropriately.
1397 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1398 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1399 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1400 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel18ee3bb2010-04-22 20:52:00 +00001401 MCSymbol *Sym = LabelsBeforeInsn.lookup(RI->first);
1402 if (MarkFunctionBegin)
1403 WeakDebugRangeSymbols.insert(std::make_pair(Sym, FunctionBeginSym));
1404 DebugRangeSymbols.push_back(Sym);
1405
1406 Sym = LabelsAfterInsn.lookup(RI->second);
1407 if (MarkFunctionBegin)
1408 WeakDebugRangeSymbols.insert(std::make_pair(Sym, FunctionBeginSym));
1409 DebugRangeSymbols.push_back(Sym);
Devang Patel9cdb4102010-04-21 16:32:19 +00001410 }
1411 DebugRangeSymbols.push_back(NULL);
1412 DebugRangeSymbols.push_back(NULL);
1413 return ScopeDIE;
1414 }
1415
Devang Patelf1dabde2010-04-22 18:43:35 +00001416 MCSymbol *Start = LabelsBeforeInsn.lookup(RI->first);
1417 MCSymbol *End = LabelsAfterInsn.lookup(RI->second);
Devang Patel9cdb4102010-04-21 16:32:19 +00001418
Devang Patelaead63c2010-03-29 22:59:58 +00001419 if (Start == 0 || End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +00001420
Chris Lattnerb7db7332010-03-09 01:58:53 +00001421 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1422 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattnera34ec2292010-03-09 01:51:43 +00001423
Devang Patelbc655ea2010-04-22 18:28:58 +00001424 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1425 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +00001426
1427 return ScopeDIE;
1428}
1429
Devang Patel2c4ceb12009-11-21 02:48:08 +00001430/// constructInlinedScopeDIE - This scope represents inlined body of
1431/// a function. Construct DIE to represent this concrete inlined copy
1432/// of the function.
1433DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel9cdb4102010-04-21 16:32:19 +00001434
1435 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1436 assert (Ranges.empty() == false
1437 && "DbgScope does not have instruction markers!");
1438
1439 // FIXME : .debug_inlined section specification does not clearly state how
1440 // to emit inlined scope that is split into multiple instruction ranges.
1441 // For now, use first instruction range and emit low_pc/high_pc pair and
1442 // corresponding .debug_inlined section entry for this pair.
1443 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patelf1dabde2010-04-22 18:43:35 +00001444 MCSymbol *StartLabel = LabelsBeforeInsn.lookup(RI->first);
1445 MCSymbol *EndLabel = LabelsAfterInsn.lookup(RI->second);
Devang Patel9cdb4102010-04-21 16:32:19 +00001446
1447 if (StartLabel == 0 || EndLabel == 0) {
1448 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1449 return 0;
1450 }
Chris Lattnerb7db7332010-03-09 01:58:53 +00001451 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001452 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +00001453 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001454 "Invalid end label for an inlined scope!");
Devang Patel9cdb4102010-04-21 16:32:19 +00001455
Devang Patel3c91b052010-03-08 20:52:55 +00001456 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +00001457 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001458 DIScope DS(Scope->getScopeNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001459 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1460
1461 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001462 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Chris Lattnered7a77b2010-03-31 05:36:29 +00001463 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001464 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001465 dwarf::DW_FORM_ref4, OriginDIE);
1466
Chris Lattner6ed0f902010-03-09 00:31:02 +00001467 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnerb7db7332010-03-09 01:58:53 +00001468 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel53bb5c92009-11-10 23:06:00 +00001469
1470 InlinedSubprogramDIEs.insert(OriginDIE);
1471
1472 // Track the start label for this inlined function.
Devang Patel622b0262010-01-19 06:19:05 +00001473 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel53bb5c92009-11-10 23:06:00 +00001474 I = InlineInfo.find(InlinedSP.getNode());
1475
1476 if (I == InlineInfo.end()) {
Chris Lattner6ed0f902010-03-09 00:31:02 +00001477 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001478 ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001479 InlinedSPNodes.push_back(InlinedSP.getNode());
1480 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +00001481 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001482
Devang Patel53bb5c92009-11-10 23:06:00 +00001483 DILocation DL(Scope->getInlinedAt());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001484 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1485 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001486
1487 return ScopeDIE;
1488}
1489
Devang Patel2c4ceb12009-11-21 02:48:08 +00001490
1491/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001492DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001493 // Get the descriptor.
1494 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001495 StringRef Name = VD.getName();
1496 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001497 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001498
1499 // Translate tag to proper Dwarf tag. The result variable is dropped for
1500 // now.
1501 unsigned Tag;
1502 switch (VD.getTag()) {
1503 case dwarf::DW_TAG_return_variable:
1504 return NULL;
1505 case dwarf::DW_TAG_arg_variable:
1506 Tag = dwarf::DW_TAG_formal_parameter;
1507 break;
1508 case dwarf::DW_TAG_auto_variable: // fall thru
1509 default:
1510 Tag = dwarf::DW_TAG_variable;
1511 break;
1512 }
1513
1514 // Define variable debug information entry.
1515 DIE *VariableDie = new DIE(Tag);
1516
1517
1518 DIE *AbsDIE = NULL;
1519 if (DbgVariable *AV = DV->getAbstractVariable())
1520 AbsDIE = AV->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001521
Devang Patel53bb5c92009-11-10 23:06:00 +00001522 if (AbsDIE) {
1523 DIScope DS(Scope->getScopeNode());
1524 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001525 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc0326792009-11-11 03:09:50 +00001526 (void) OriginSPDIE;
Chris Lattnered7a77b2010-03-31 05:36:29 +00001527 assert(OriginSPDIE && "Unable to find Origin DIE for the SP!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001528 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
Chris Lattnered7a77b2010-03-31 05:36:29 +00001529 assert(AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001530 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001531 dwarf::DW_FORM_ref4, AbsDIE);
1532 }
1533 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001534 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1535 addSourceLine(VariableDie, &VD);
Devang Patel53bb5c92009-11-10 23:06:00 +00001536
1537 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001538 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001539 // addresses instead.
1540 if (VD.isBlockByrefVariable())
Devang Patel8a241142009-12-09 18:24:21 +00001541 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel53bb5c92009-11-10 23:06:00 +00001542 else
Devang Patel8a241142009-12-09 18:24:21 +00001543 addType(VariableDie, VD.getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001544 }
1545
1546 // Add variable address.
1547 if (!Scope->isAbstractScope()) {
Devang Patel90a48ad2010-03-15 18:33:46 +00001548 // Check if variable is described by DBG_VALUE instruction.
1549 if (const MachineInstr *DbgValueInsn = DV->getDbgValue()) {
1550 if (DbgValueInsn->getNumOperands() == 3) {
1551 // FIXME : Handle getNumOperands != 3
1552 if (DbgValueInsn->getOperand(0).getType()
1553 == MachineOperand::MO_Register
1554 && DbgValueInsn->getOperand(0).getReg()) {
1555 MachineLocation Location;
1556 Location.set(DbgValueInsn->getOperand(0).getReg());
1557 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patelaead63c2010-03-29 22:59:58 +00001558 if (MCSymbol *VS = DV->getDbgValueLabel())
1559 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1560 VS);
Devang Patel90a48ad2010-03-15 18:33:46 +00001561 } else if (DbgValueInsn->getOperand(0).getType() ==
1562 MachineOperand::MO_Immediate) {
Benjamin Kramer345ef342010-03-31 19:34:01 +00001563 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel90a48ad2010-03-15 18:33:46 +00001564 unsigned Imm = DbgValueInsn->getOperand(0).getImm();
1565 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
1566 addBlock(VariableDie, dwarf::DW_AT_const_value, 0, Block);
Devang Patelaead63c2010-03-29 22:59:58 +00001567 if (MCSymbol *VS = DV->getDbgValueLabel())
1568 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1569 VS);
Bill Wendling57fbba42010-04-05 22:59:21 +00001570 } else if (DbgValueInsn->getOperand(0).getType() ==
1571 MachineOperand::MO_FPImmediate) {
1572 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1573 APFloat FPImm = DbgValueInsn->getOperand(0).getFPImm()->getValueAPF();
1574
1575 // Get the raw data form of the floating point.
1576 const APInt FltVal = FPImm.bitcastToAPInt();
1577 const char *FltPtr = (const char*)FltVal.getRawData();
1578
John McCall795ee9d2010-04-06 23:35:53 +00001579 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Bill Wendling57fbba42010-04-05 22:59:21 +00001580 bool LittleEndian = Asm->getTargetData().isLittleEndian();
1581 int Incr = (LittleEndian ? 1 : -1);
1582 int Start = (LittleEndian ? 0 : NumBytes - 1);
1583 int Stop = (LittleEndian ? NumBytes : -1);
1584
1585 // Output the constant to DWARF one byte at a time.
1586 for (; Start != Stop; Start += Incr)
1587 addUInt(Block, 0, dwarf::DW_FORM_data1,
1588 (unsigned char)0xFF & FltPtr[Start]);
1589
1590 addBlock(VariableDie, dwarf::DW_AT_const_value, 0, Block);
1591
1592 if (MCSymbol *VS = DV->getDbgValueLabel())
1593 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1594 VS);
Devang Patel90a48ad2010-03-15 18:33:46 +00001595 } else {
1596 //FIXME : Handle other operand types.
1597 delete VariableDie;
1598 return NULL;
1599 }
1600 }
1601 } else {
1602 MachineLocation Location;
1603 unsigned FrameReg;
Chris Lattnerd38fee82010-04-05 00:13:49 +00001604 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1605 int Offset = RI->getFrameIndexReference(*Asm->MF, DV->getFrameIndex(),
Chris Lattner1d65ba72010-03-31 06:06:37 +00001606 FrameReg);
Devang Patel90a48ad2010-03-15 18:33:46 +00001607 Location.set(FrameReg, Offset);
1608
1609 if (VD.hasComplexAddress())
1610 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1611 else if (VD.isBlockByrefVariable())
1612 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1613 else
1614 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1615 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001616 }
Devang Patelb4645642010-02-06 01:02:37 +00001617
1618 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1619 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel53bb5c92009-11-10 23:06:00 +00001620 DV->setDIE(VariableDie);
1621 return VariableDie;
1622
1623}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001624
Devang Patel193f7202009-11-24 01:14:22 +00001625void DwarfDebug::addPubTypes(DISubprogram SP) {
1626 DICompositeType SPTy = SP.getType();
1627 unsigned SPTag = SPTy.getTag();
1628 if (SPTag != dwarf::DW_TAG_subroutine_type)
1629 return;
1630
1631 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +00001632 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1633 DIType ATy(Args.getElement(i).getNode());
Devang Patel3c91b052010-03-08 20:52:55 +00001634 if (!ATy.isValid())
Devang Patel193f7202009-11-24 01:14:22 +00001635 continue;
1636 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel50d80e32010-04-13 20:35:04 +00001637 if (DIDescriptor(CATy.getNode()).Verify() && !CATy.getName().empty()
1638 && !CATy.isForwardDecl()) {
Devang Patel193f7202009-11-24 01:14:22 +00001639 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1640 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1641 }
1642 }
1643}
1644
Devang Patel2c4ceb12009-11-21 02:48:08 +00001645/// constructScopeDIE - Construct a DIE for this scope.
1646DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +00001647 if (!Scope || !Scope->getScopeNode())
1648 return NULL;
1649
1650 DIScope DS(Scope->getScopeNode());
1651 DIE *ScopeDIE = NULL;
1652 if (Scope->getInlinedAt())
1653 ScopeDIE = constructInlinedScopeDIE(Scope);
1654 else if (DS.isSubprogram()) {
1655 if (Scope->isAbstractScope())
1656 ScopeDIE = ModuleCU->getDIE(DS.getNode());
1657 else
1658 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
1659 }
Devang Patelaead63c2010-03-29 22:59:58 +00001660 else
Devang Patel3c91b052010-03-08 20:52:55 +00001661 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patelaead63c2010-03-29 22:59:58 +00001662 if (!ScopeDIE) return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001663
Devang Patel53bb5c92009-11-10 23:06:00 +00001664 // Add variables to scope.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001665 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel53bb5c92009-11-10 23:06:00 +00001666 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001667 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001668 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001669 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001670 }
1671
1672 // Add nested scopes.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001673 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel53bb5c92009-11-10 23:06:00 +00001674 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1675 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001676 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001677 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001678 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001679 }
Devang Patel193f7202009-11-24 01:14:22 +00001680
1681 if (DS.isSubprogram())
1682 addPubTypes(DISubprogram(DS.getNode()));
1683
1684 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001685}
1686
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001687/// GetOrCreateSourceID - Look up the source id with the given directory and
1688/// source file names. If none currently exists, create a new id and insert it
1689/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1690/// maps as well.
Chris Lattner1d65ba72010-03-31 06:06:37 +00001691unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001692 unsigned DId;
1693 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1694 if (DI != DirectoryIdMap.end()) {
1695 DId = DI->getValue();
1696 } else {
1697 DId = DirectoryNames.size() + 1;
1698 DirectoryIdMap[DirName] = DId;
1699 DirectoryNames.push_back(DirName);
1700 }
1701
1702 unsigned FId;
1703 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1704 if (FI != SourceFileIdMap.end()) {
1705 FId = FI->getValue();
1706 } else {
1707 FId = SourceFileNames.size() + 1;
1708 SourceFileIdMap[FileName] = FId;
1709 SourceFileNames.push_back(FileName);
1710 }
1711
1712 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1713 SourceIdMap.find(std::make_pair(DId, FId));
1714 if (SI != SourceIdMap.end())
1715 return SI->second;
1716
1717 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1718 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1719 SourceIds.push_back(std::make_pair(DId, FId));
1720
1721 return SrcId;
1722}
1723
Devang Patel6404e4e2009-12-15 19:16:48 +00001724/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1725DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
1726 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1727 if (NDie)
1728 return NDie;
1729 NDie = new DIE(dwarf::DW_TAG_namespace);
1730 ModuleCU->insertDIE(NS.getNode(), NDie);
1731 if (!NS.getName().empty())
1732 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1733 addSourceLine(NDie, &NS);
1734 addToContextOwner(NDie, NS.getContext());
1735 return NDie;
1736}
1737
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +00001738void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001739 DICompileUnit DIUnit(N);
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +00001740 // Use first compile unit marked as isMain as the compile unit for this
1741 // module.
1742 if (ModuleCU || !DIUnit.isMain())
1743 return;
Devang Patel65dbc902009-11-25 17:36:49 +00001744 StringRef FN = DIUnit.getFilename();
1745 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001746 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001747
1748 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001749 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001750 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001751 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001752 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001753 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Chris Lattner9c69e285532010-04-04 22:59:04 +00001754 addLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, TextSectionSym);
Devang Patel4a602ca2010-03-22 23:11:36 +00001755 addLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnerc0215722010-04-04 19:25:43 +00001756 Asm->GetTempSymbol("text_end"));
Devang Patel4a602ca2010-03-22 23:11:36 +00001757 // DW_AT_stmt_list is a offset of line number information for this
1758 // compile unit in debug_line section. It is always zero when only one
1759 // compile unit is emitted in one object file.
1760 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001761
Devang Patel65dbc902009-11-25 17:36:49 +00001762 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001763 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001764 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001765 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001766
Devang Patel65dbc902009-11-25 17:36:49 +00001767 StringRef Flags = DIUnit.getFlags();
1768 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001769 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001770
1771 unsigned RVer = DIUnit.getRunTimeVersion();
1772 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001773 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001774 dwarf::DW_FORM_data1, RVer);
1775
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +00001776 assert(!ModuleCU &&
1777 "ModuleCU assigned since the top of constructCompileUnit");
1778 ModuleCU = new CompileUnit(ID, Die);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001779}
1780
Devang Patel2c4ceb12009-11-21 02:48:08 +00001781void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001782 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001783
Devang Patel905cf5e2009-09-04 23:59:07 +00001784 // If debug information is malformed then ignore it.
1785 if (DI_GV.Verify() == false)
1786 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001787
1788 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001789 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel13e16b62009-06-26 01:49:18 +00001790 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001791
Devang Patel8a241142009-12-09 18:24:21 +00001792 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Pateledb45632009-12-10 23:25:41 +00001793 if (!VariableDie)
1794 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001795
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001796 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001797 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001798
1799 // Add to context owner.
Devang Patelc9b16cc2010-01-15 01:12:22 +00001800 DIDescriptor GVContext = DI_GV.getContext();
1801 // Do not create specification DIE if context is either compile unit
1802 // or a subprogram.
Chris Lattner1d65ba72010-03-31 06:06:37 +00001803 if (DI_GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel5f094002010-04-06 23:53:48 +00001804 !GVContext.isFile() &&
1805 !isSubprogramContext(GVContext.getNode())) {
Devang Patel6404e4e2009-12-15 19:16:48 +00001806 // Create specification DIE.
1807 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1808 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1809 dwarf::DW_FORM_ref4, VariableDie);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001810 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel6404e4e2009-12-15 19:16:48 +00001811 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner4faf59a2010-03-08 22:31:46 +00001812 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattnerdeb0cba2010-03-12 21:09:07 +00001813 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel6404e4e2009-12-15 19:16:48 +00001814 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel8581e012010-02-09 01:58:33 +00001815 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel6404e4e2009-12-15 19:16:48 +00001816 ModuleCU->addDie(VariableSpecDIE);
1817 } else {
Benjamin Kramer345ef342010-03-31 19:34:01 +00001818 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel6404e4e2009-12-15 19:16:48 +00001819 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner4faf59a2010-03-08 22:31:46 +00001820 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattnerdeb0cba2010-03-12 21:09:07 +00001821 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel6404e4e2009-12-15 19:16:48 +00001822 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1823 }
Devang Patelc9b16cc2010-01-15 01:12:22 +00001824 addToContextOwner(VariableDie, GVContext);
Devang Patelc366f832009-12-10 19:14:49 +00001825
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001826 // Expose as global. FIXME - need to check external flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001827 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel193f7202009-11-24 01:14:22 +00001828
1829 DIType GTy = DI_GV.getType();
Devang Patel50d80e32010-04-13 20:35:04 +00001830 if (GTy.isCompositeType() && !GTy.getName().empty()
1831 && !GTy.isForwardDecl()) {
Devang Patel193f7202009-11-24 01:14:22 +00001832 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
Chris Lattnered7a77b2010-03-31 05:36:29 +00001833 assert(Entry && "Missing global type!");
Devang Patel193f7202009-11-24 01:14:22 +00001834 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1835 }
Devang Patel13e16b62009-06-26 01:49:18 +00001836 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001837}
1838
Devang Patel2c4ceb12009-11-21 02:48:08 +00001839void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001840 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001841
Stuart Hastings639336e2010-04-06 21:38:29 +00001842 // Check for pre-existence.
1843 if (ModuleCU->getDIE(N))
1844 return;
1845
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001846 if (!SP.isDefinition())
1847 // This is a method declaration which will be handled while constructing
1848 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001849 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001850
Stuart Hastings639336e2010-04-06 21:38:29 +00001851 DIE *SubprogramDie = createSubprogramDIE(SP);
1852
1853 // Add to map.
1854 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001855
1856 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00001857 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00001858
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001859 // Expose as global.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001860 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001861
Devang Patel13e16b62009-06-26 01:49:18 +00001862 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001863}
1864
Devang Patel2c4ceb12009-11-21 02:48:08 +00001865/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001866/// content. Create global DIEs and emit initial debug info sections.
1867/// This is inovked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +00001868void DwarfDebug::beginModule(Module *M) {
Devang Patel708e4742010-04-21 19:08:53 +00001869 if (DisableDebugInfoPrinting)
1870 return;
1871
Devang Patel78ab9e22009-07-30 18:56:46 +00001872 DebugInfoFinder DbgFinder;
1873 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001874
Chris Lattnerd850ac72010-04-05 02:19:28 +00001875 bool HasDebugInfo = false;
1876
1877 // Scan all the compile-units to see if there are any marked as the main unit.
1878 // if not, we do not generate debug info.
1879 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1880 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1881 if (DICompileUnit(*I).isMain()) {
1882 HasDebugInfo = true;
1883 break;
1884 }
1885 }
1886
1887 if (!HasDebugInfo) return;
1888
1889 // Tell MMI that we have debug info.
1890 MMI->setDebugInfoAvailability(true);
1891
Chris Lattnerbe15beb2010-04-04 23:17:54 +00001892 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +00001893 EmitSectionLabels();
Chris Lattnerbe15beb2010-04-04 23:17:54 +00001894
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001895 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001896 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1897 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001898 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001899
Devang Patel53bb5c92009-11-10 23:06:00 +00001900 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001901 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1902 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001903 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00001904
Devang Patelc366f832009-12-10 19:14:49 +00001905 // Create DIEs for each global variable.
1906 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1907 E = DbgFinder.global_variable_end(); I != E; ++I)
1908 constructGlobalVariableDIE(*I);
1909
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001910 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001911 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001912
1913 // Print out .file directives to specify files for .loc directives. These are
1914 // printed out early so that they precede any .loc directives.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001915 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001916 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1917 // Remember source id starts at 1.
1918 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001919 // FIXME: don't use sys::path for this! This should not depend on the
1920 // host.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001921 sys::Path FullPath(getSourceDirectoryName(Id.first));
1922 bool AppendOk =
1923 FullPath.appendComponent(getSourceFileName(Id.second));
1924 assert(AppendOk && "Could not append filename to directory!");
1925 AppendOk = false;
Chris Lattnera6594fc2010-01-25 18:58:59 +00001926 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001927 }
1928 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001929}
1930
Devang Patel2c4ceb12009-11-21 02:48:08 +00001931/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001932///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001933void DwarfDebug::endModule() {
Bill Wendling5f017e82010-04-07 09:28:04 +00001934 if (!ModuleCU) return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001935
Devang Patel53bb5c92009-11-10 23:06:00 +00001936 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1937 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1938 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1939 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001940 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00001941 }
1942
Devang Patel622b0262010-01-19 06:19:05 +00001943 for (DenseMap<DIE *, MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00001944 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1945 DIE *SPDie = CI->first;
1946 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1947 if (!N) continue;
1948 DIE *NDie = ModuleCU->getDIE(N);
1949 if (!NDie) continue;
1950 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00001951 }
1952
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001953 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001954 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00001955 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001956 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00001957 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001958
1959 // End text sections.
1960 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001961 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +00001962 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001963 }
1964
1965 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001966 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001967
1968 // Emit function debug frame information
1969 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1970 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001971 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001972
1973 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001974 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001975
1976 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00001977 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001978
1979 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001980 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001981
1982 // Emit source line correspondence into a debug line section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001983 emitDebugLines();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001984
1985 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001986 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001987
Devang Patel193f7202009-11-24 01:14:22 +00001988 // Emit info into a debug pubtypes section.
1989 emitDebugPubTypes();
1990
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001991 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001992 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001993
1994 // Emit info into a debug aranges section.
1995 EmitDebugARanges();
1996
1997 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001998 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001999
2000 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002001 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002002
2003 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002004 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002005
Chris Lattnerbc733f52010-03-13 02:17:42 +00002006 // Emit info into a debug str section.
2007 emitDebugStr();
2008
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +00002009 delete ModuleCU;
2010 ModuleCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002011}
2012
Devang Patel53bb5c92009-11-10 23:06:00 +00002013/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002014DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2015 unsigned FrameIdx,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002016 DebugLoc ScopeLoc) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002017
2018 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
2019 if (AbsDbgVariable)
2020 return AbsDbgVariable;
2021
Chris Lattnerde4845c2010-04-02 19:42:39 +00002022 LLVMContext &Ctx = Var.getNode()->getContext();
2023 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00002024 if (!Scope)
2025 return NULL;
2026
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002027 AbsDbgVariable = new DbgVariable(Var, FrameIdx,
2028 NULL /* No more-abstract variable*/);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002029 Scope->addVariable(AbsDbgVariable);
Devang Patel53bb5c92009-11-10 23:06:00 +00002030 AbstractVariables[Var.getNode()] = AbsDbgVariable;
2031 return AbsDbgVariable;
2032}
2033
Devang Patel90a48ad2010-03-15 18:33:46 +00002034/// findAbstractVariable - Find abstract variable, if any, associated with Var.
2035/// FIXME : Refactor findAbstractVariable.
2036DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2037 const MachineInstr *MI,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002038 DebugLoc ScopeLoc) {
Devang Patel90a48ad2010-03-15 18:33:46 +00002039
2040 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
2041 if (AbsDbgVariable)
2042 return AbsDbgVariable;
2043
Chris Lattnerde4845c2010-04-02 19:42:39 +00002044 LLVMContext &Ctx = Var.getNode()->getContext();
2045 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel90a48ad2010-03-15 18:33:46 +00002046 if (!Scope)
2047 return NULL;
2048
Devang Patelaead63c2010-03-29 22:59:58 +00002049 AbsDbgVariable = new DbgVariable(Var, MI,
Devang Patel90a48ad2010-03-15 18:33:46 +00002050 NULL /* No more-abstract variable*/);
2051 Scope->addVariable(AbsDbgVariable);
2052 AbstractVariables[Var.getNode()] = AbsDbgVariable;
Devang Patelaead63c2010-03-29 22:59:58 +00002053 DbgValueStartMap[MI] = AbsDbgVariable;
Devang Patel90a48ad2010-03-15 18:33:46 +00002054 return AbsDbgVariable;
2055}
2056
Devang Patel2c4ceb12009-11-21 02:48:08 +00002057/// collectVariableInfo - Populate DbgScope entries with variables' info.
2058void DwarfDebug::collectVariableInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002059 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002060
Devang Patele717faa2009-10-06 01:26:37 +00002061 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2062 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2063 VE = VMap.end(); VI != VE; ++VI) {
Devang Patelbc5201f2010-01-22 22:52:10 +00002064 MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00002065 if (!Var) continue;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002066 DIVariable DV(Var);
2067 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002068
Chris Lattnerde4845c2010-04-02 19:42:39 +00002069 DbgScope *Scope = 0;
2070 if (MDNode *IA = VP.second.getInlinedAt(Ctx))
2071 Scope = ConcreteScopes.lookup(IA);
2072 if (Scope == 0)
2073 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
2074
Devang Patelfb0ee432009-11-10 23:20:04 +00002075 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002076 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00002077 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00002078
Chris Lattnerde4845c2010-04-02 19:42:39 +00002079 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, VP.second);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002080 DbgVariable *RegVar = new DbgVariable(DV, VP.first, AbsDbgVariable);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002081 Scope->addVariable(RegVar);
Devang Patele717faa2009-10-06 01:26:37 +00002082 }
Devang Patel90a48ad2010-03-15 18:33:46 +00002083
2084 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002085 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel90a48ad2010-03-15 18:33:46 +00002086 I != E; ++I) {
2087 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2088 II != IE; ++II) {
2089 const MachineInstr *MInsn = II;
Chris Lattner14d750d2010-03-31 05:39:57 +00002090 if (!MInsn->isDebugValue())
Devang Patel90a48ad2010-03-15 18:33:46 +00002091 continue;
Devang Patelaead63c2010-03-29 22:59:58 +00002092
Devang Patel90a48ad2010-03-15 18:33:46 +00002093 // FIXME : Lift this restriction.
2094 if (MInsn->getNumOperands() != 3)
2095 continue;
Dan Gohman82d5eaf2010-04-17 16:43:55 +00002096 DIVariable DV(
2097 const_cast<MDNode *>(MInsn->getOperand(MInsn->getNumOperands() - 1)
2098 .getMetadata()));
Devang Patel90a48ad2010-03-15 18:33:46 +00002099 if (DV.getTag() == dwarf::DW_TAG_arg_variable) {
2100 // FIXME Handle inlined subroutine arguments.
2101 DbgVariable *ArgVar = new DbgVariable(DV, MInsn, NULL);
2102 CurrentFnDbgScope->addVariable(ArgVar);
Devang Patelaead63c2010-03-29 22:59:58 +00002103 DbgValueStartMap[MInsn] = ArgVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00002104 continue;
2105 }
2106
2107 DebugLoc DL = MInsn->getDebugLoc();
2108 if (DL.isUnknown()) continue;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002109 DbgScope *Scope = 0;
2110 if (MDNode *IA = DL.getInlinedAt(Ctx))
2111 Scope = ConcreteScopes.lookup(IA);
2112 if (Scope == 0)
2113 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2114
Devang Patel90a48ad2010-03-15 18:33:46 +00002115 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002116 if (Scope == 0)
Devang Patel90a48ad2010-03-15 18:33:46 +00002117 continue;
2118
Chris Lattnerde4845c2010-04-02 19:42:39 +00002119 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, MInsn, DL);
Devang Patel90a48ad2010-03-15 18:33:46 +00002120 DbgVariable *RegVar = new DbgVariable(DV, MInsn, AbsDbgVariable);
Devang Patelaead63c2010-03-29 22:59:58 +00002121 DbgValueStartMap[MInsn] = RegVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00002122 Scope->addVariable(RegVar);
2123 }
2124 }
Devang Patele717faa2009-10-06 01:26:37 +00002125}
2126
Devang Patel553881b2010-03-29 17:20:31 +00002127/// beginScope - Process beginning of a scope.
2128void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patel553881b2010-03-29 17:20:31 +00002129 // Check location.
2130 DebugLoc DL = MI->getDebugLoc();
2131 if (DL.isUnknown())
2132 return;
Devang Patel553881b2010-03-29 17:20:31 +00002133
Chris Lattnerd38fee82010-04-05 00:13:49 +00002134 MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Chris Lattnerde4845c2010-04-02 19:42:39 +00002135
2136 // FIXME: Should only verify each scope once!
2137 if (!DIScope(Scope).Verify())
Devang Patel553881b2010-03-29 17:20:31 +00002138 return;
Devang Patel553881b2010-03-29 17:20:31 +00002139
Devang Patelaead63c2010-03-29 22:59:58 +00002140 // DBG_VALUE instruction establishes new value.
Chris Lattner14d750d2010-03-31 05:39:57 +00002141 if (MI->isDebugValue()) {
Devang Patelaead63c2010-03-29 22:59:58 +00002142 DenseMap<const MachineInstr *, DbgVariable *>::iterator DI
2143 = DbgValueStartMap.find(MI);
2144 if (DI != DbgValueStartMap.end()) {
Devang Patel9cdb4102010-04-21 16:32:19 +00002145 MCSymbol *Label = NULL;
2146 if (DL == PrevInstLoc)
2147 Label = PrevLabel;
2148 else {
2149 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2150 PrevInstLoc = DL;
2151 PrevLabel = Label;
2152 }
Devang Patelaead63c2010-03-29 22:59:58 +00002153 DI->second->setDbgValueLabel(Label);
2154 }
Devang Patel7ed63112010-03-30 18:07:00 +00002155 return;
Devang Patelaead63c2010-03-29 22:59:58 +00002156 }
2157
Devang Patel553881b2010-03-29 17:20:31 +00002158 // Emit a label to indicate location change. This is used for line
Devang Patel9cdb4102010-04-21 16:32:19 +00002159 // table even if this instruction does not start a new scope.
2160 MCSymbol *Label = NULL;
2161 if (DL == PrevInstLoc)
2162 Label = PrevLabel;
2163 else {
2164 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2165 PrevInstLoc = DL;
2166 PrevLabel = Label;
2167 }
Devang Patel553881b2010-03-29 17:20:31 +00002168
Devang Patel1c246352010-04-08 16:50:29 +00002169 // If this instruction begins a scope then note down corresponding label.
2170 if (InsnsBeginScopeSet.count(MI) != 0)
Devang Patelf1dabde2010-04-22 18:43:35 +00002171 LabelsBeforeInsn[MI] = Label;
Devang Patel0d20ac82009-10-06 01:50:42 +00002172}
2173
Devang Patel2c4ceb12009-11-21 02:48:08 +00002174/// endScope - Process end of a scope.
2175void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel1c246352010-04-08 16:50:29 +00002176 if (InsnsEndScopeSet.count(MI) != 0) {
2177 // Emit a label if this instruction ends a scope.
2178 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2179 Asm->OutStreamer.EmitLabel(Label);
Devang Patelf1dabde2010-04-22 18:43:35 +00002180 LabelsAfterInsn[MI] = Label;
Devang Patel1c246352010-04-08 16:50:29 +00002181 }
Devang Patel53bb5c92009-11-10 23:06:00 +00002182}
2183
Devang Patel9cdb4102010-04-21 16:32:19 +00002184/// getOrCreateDbgScope - Create DbgScope for the scope.
2185DbgScope *DwarfDebug::getOrCreateDbgScope(MDNode *Scope, MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002186 if (!InlinedAt) {
2187 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2188 if (WScope)
Devang Patel9cdb4102010-04-21 16:32:19 +00002189 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002190 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2191 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patel9cdb4102010-04-21 16:32:19 +00002192 if (DIDescriptor(Scope).isLexicalBlock()) {
2193 DbgScope *Parent =
2194 getOrCreateDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
2195 WScope->setParent(Parent);
2196 Parent->addScope(WScope);
2197 }
2198
2199 if (!WScope->getParent()) {
2200 StringRef SPName = DISubprogram(Scope).getLinkageName();
2201 if (SPName == Asm->MF->getFunction()->getName())
2202 CurrentFnDbgScope = WScope;
2203 }
2204
2205 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002206 }
2207
2208 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2209 if (WScope)
Devang Patel9cdb4102010-04-21 16:32:19 +00002210 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002211
2212 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2213 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2214 DILocation DL(InlinedAt);
Devang Patel9cdb4102010-04-21 16:32:19 +00002215 DbgScope *Parent =
2216 getOrCreateDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
2217 WScope->setParent(Parent);
2218 Parent->addScope(WScope);
2219
2220 ConcreteScopes[InlinedAt] = WScope;
2221 getOrCreateAbstractScope(Scope);
2222
2223 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00002224}
2225
Devang Patel9cdb4102010-04-21 16:32:19 +00002226/// hasValidLocation - Return true if debug location entry attached with
2227/// machine instruction encodes valid location info.
2228static bool hasValidLocation(LLVMContext &Ctx,
2229 const MachineInstr *MInsn,
2230 MDNode *&Scope, MDNode *&InlinedAt) {
2231 if (MInsn->isDebugValue())
2232 return false;
2233 DebugLoc DL = MInsn->getDebugLoc();
2234 if (DL.isUnknown()) return false;
2235
2236 MDNode *S = DL.getScope(Ctx);
2237
2238 // There is no need to create another DIE for compile unit. For all
2239 // other scopes, create one DbgScope now. This will be translated
2240 // into a scope DIE at the end.
2241 if (DIScope(S).isCompileUnit()) return false;
2242
2243 Scope = S;
2244 InlinedAt = DL.getInlinedAt(Ctx);
2245 return true;
2246}
2247
2248/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2249/// hierarchy.
2250static void calculateDominanceGraph(DbgScope *Scope) {
2251 assert (Scope && "Unable to calculate scop edominance graph!");
2252 SmallVector<DbgScope *, 4> WorkStack;
2253 WorkStack.push_back(Scope);
2254 unsigned Counter = 0;
2255 while (!WorkStack.empty()) {
2256 DbgScope *WS = WorkStack.back();
2257 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2258 bool visitedChildren = false;
2259 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2260 SE = Children.end(); SI != SE; ++SI) {
2261 DbgScope *ChildScope = *SI;
2262 if (!ChildScope->getDFSOut()) {
2263 WorkStack.push_back(ChildScope);
2264 visitedChildren = true;
2265 ChildScope->setDFSIn(++Counter);
2266 break;
2267 }
2268 }
2269 if (!visitedChildren) {
2270 WorkStack.pop_back();
2271 WS->setDFSOut(++Counter);
2272 }
2273 }
2274}
2275
2276/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
2277static
2278void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2279 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2280{
2281#ifndef NDEBUG
2282 unsigned PrevDFSIn = 0;
2283 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2284 I != E; ++I) {
2285 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2286 II != IE; ++II) {
2287 const MachineInstr *MInsn = II;
2288 MDNode *Scope = NULL;
2289 MDNode *InlinedAt = NULL;
2290
2291 // Check if instruction has valid location information.
2292 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2293 dbgs() << " [ ";
2294 if (InlinedAt)
2295 dbgs() << "*";
2296 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
2297 MI2ScopeMap.find(MInsn);
2298 if (DI != MI2ScopeMap.end()) {
2299 DbgScope *S = DI->second;
2300 dbgs() << S->getDFSIn();
2301 PrevDFSIn = S->getDFSIn();
2302 } else
2303 dbgs() << PrevDFSIn;
2304 } else
2305 dbgs() << " [ x" << PrevDFSIn;
2306 dbgs() << " ]";
2307 MInsn->dump();
2308 }
2309 dbgs() << "\n";
2310 }
2311#endif
2312}
Devang Patel2c4ceb12009-11-21 02:48:08 +00002313/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00002314/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00002315bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00002316 // If scope information was extracted using .dbg intrinsics then there is not
2317 // any need to extract these information by scanning each instruction.
2318 if (!DbgScopeMap.empty())
2319 return false;
2320
Devang Patel53bb5c92009-11-10 23:06:00 +00002321 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel9cdb4102010-04-21 16:32:19 +00002322 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2323 SmallVector<DbgRange, 4> MIRanges;
2324 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
2325 MDNode *PrevScope = NULL;
2326 MDNode *PrevInlinedAt = NULL;
2327 const MachineInstr *RangeBeginMI = NULL;
2328 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002329 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00002330 I != E; ++I) {
2331 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2332 II != IE; ++II) {
2333 const MachineInstr *MInsn = II;
Devang Patel9cdb4102010-04-21 16:32:19 +00002334 MDNode *Scope = NULL;
2335 MDNode *InlinedAt = NULL;
2336
2337 // Check if instruction has valid location information.
2338 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2339 PrevMI = MInsn;
2340 continue;
2341 }
Chris Lattnerde4845c2010-04-02 19:42:39 +00002342
Devang Patel9cdb4102010-04-21 16:32:19 +00002343 // If scope has not changed then skip this instruction.
2344 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2345 PrevMI = MInsn;
2346 continue;
2347 }
2348
2349 if (RangeBeginMI) {
2350 // If we have alread seen a beginning of a instruction range and
2351 // current instruction scope does not match scope of first instruction
2352 // in this range then create a new instruction range.
2353 DbgRange R(RangeBeginMI, PrevMI);
2354 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
2355 MIRanges.push_back(R);
2356 }
2357
2358 // This is a beginning of a new instruction range.
2359 RangeBeginMI = MInsn;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002360
Devang Patel9cdb4102010-04-21 16:32:19 +00002361 // Reset previous markers.
2362 PrevMI = MInsn;
2363 PrevScope = Scope;
2364 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00002365 }
2366 }
2367
Devang Patel9cdb4102010-04-21 16:32:19 +00002368 // Create last instruction range.
2369 if (RangeBeginMI && PrevMI && PrevScope) {
2370 DbgRange R(RangeBeginMI, PrevMI);
2371 MIRanges.push_back(R);
2372 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00002373 }
Devang Patel9cdb4102010-04-21 16:32:19 +00002374
Devang Patel344130e2010-01-04 20:44:00 +00002375 if (!CurrentFnDbgScope)
2376 return false;
2377
Devang Patel9cdb4102010-04-21 16:32:19 +00002378 calculateDominanceGraph(CurrentFnDbgScope);
2379 if (PrintDbgScope)
2380 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2381
2382 // Find ranges of instructions covered by each DbgScope;
2383 DbgScope *PrevDbgScope = NULL;
2384 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2385 RE = MIRanges.end(); RI != RE; ++RI) {
2386 const DbgRange &R = *RI;
2387 DbgScope *S = MI2ScopeMap.lookup(R.first);
2388 assert (S && "Lost DbgScope for a machine instruction!");
2389 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2390 PrevDbgScope->closeInsnRange(S);
2391 S->openInsnRange(R.first);
2392 S->extendInsnRange(R.second);
2393 PrevDbgScope = S;
2394 }
2395
2396 if (PrevDbgScope)
2397 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00002398
Devang Patele37b0c62010-04-08 18:43:56 +00002399 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00002400
2401 return !DbgScopeMap.empty();
2402}
2403
Devang Patel9cdb4102010-04-21 16:32:19 +00002404/// identifyScopeMarkers() -
2405/// Each DbgScope has first instruction and last instruction to mark beginning
2406/// and end of a scope respectively. Create an inverse map that list scopes
2407/// starts (and ends) with an instruction. One instruction may start (or end)
2408/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00002409void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00002410 SmallVector<DbgScope *, 4> WorkList;
2411 WorkList.push_back(CurrentFnDbgScope);
2412 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00002413 DbgScope *S = WorkList.pop_back_val();
Devang Patel9cdb4102010-04-21 16:32:19 +00002414
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002415 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patel42aafd72010-01-20 02:05:23 +00002416 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002417 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00002418 SE = Children.end(); SI != SE; ++SI)
2419 WorkList.push_back(*SI);
2420
Devang Patel53bb5c92009-11-10 23:06:00 +00002421 if (S->isAbstractScope())
2422 continue;
Devang Patel9cdb4102010-04-21 16:32:19 +00002423
2424 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2425 if (Ranges.empty())
2426 continue;
2427 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2428 RE = Ranges.end(); RI != RE; ++RI) {
2429 assert(RI->first && "DbgRange does not have first instruction!");
2430 assert(RI->second && "DbgRange does not have second instruction!");
2431 InsnsBeginScopeSet.insert(RI->first);
2432 InsnsEndScopeSet.insert(RI->second);
2433 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002434 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002435}
2436
Dan Gohman084751c2010-04-20 00:37:27 +00002437/// FindFirstDebugLoc - Find the first debug location in the function. This
2438/// is intended to be an approximation for the source position of the
2439/// beginning of the function.
2440static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2441 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2442 I != E; ++I)
2443 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2444 MBBI != MBBE; ++MBBI) {
2445 DebugLoc DL = MBBI->getDebugLoc();
2446 if (!DL.isUnknown())
2447 return DL;
2448 }
2449 return DebugLoc();
2450}
2451
Devang Patel2c4ceb12009-11-21 02:48:08 +00002452/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002453/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00002454void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00002455 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00002456 if (!extractScopeInformation()) return;
Chris Lattnera909d662010-03-29 20:38:20 +00002457
Devang Patel2c4ceb12009-11-21 02:48:08 +00002458 collectVariableInfo();
Devang Patel60b35bd2009-10-06 18:37:31 +00002459
Devang Patel3547a882010-04-22 18:39:21 +00002460 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2461 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002462 // Assumes in correct section after the entry point.
Devang Patel3547a882010-04-22 18:39:21 +00002463 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002464
2465 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2466 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00002467 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002468 if (FDL.isUnknown()) return;
2469
2470 MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
2471
2472 DISubprogram SP = getDISubprogram(Scope);
2473 unsigned Line, Col;
2474 if (SP.Verify()) {
2475 Line = SP.getLineNumber();
2476 Col = 0;
2477 } else {
2478 Line = FDL.getLine();
2479 Col = FDL.getCol();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002480 }
Chris Lattnerde4845c2010-04-02 19:42:39 +00002481
2482 recordSourceLine(Line, Col, Scope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002483}
2484
Devang Patel2c4ceb12009-11-21 02:48:08 +00002485/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002486///
Chris Lattnereec791a2010-01-26 23:18:02 +00002487void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002488 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002489
Devang Patel344130e2010-01-04 20:44:00 +00002490 if (CurrentFnDbgScope) {
2491 // Define end label for subprogram.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002492 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_end",
2493 Asm->getFunctionNumber()));
Devang Patel344130e2010-01-04 20:44:00 +00002494
2495 // Get function line info.
2496 if (!Lines.empty()) {
2497 // Get section line info.
2498 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2499 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2500 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2501 // Append the function info to section info.
2502 SectionLineInfos.insert(SectionLineInfos.end(),
2503 Lines.begin(), Lines.end());
2504 }
2505
2506 // Construct abstract scopes.
2507 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2508 AE = AbstractScopesList.end(); AI != AE; ++AI)
2509 constructScopeDIE(*AI);
2510
2511 constructScopeDIE(CurrentFnDbgScope);
2512
Chris Lattnerd38fee82010-04-05 00:13:49 +00002513 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002514 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002515 }
2516
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002517 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002518 CurrentFnDbgScope = NULL;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002519 DeleteContainerSeconds(DbgScopeMap);
Devang Patel51424712010-04-09 16:04:20 +00002520 InsnsBeginScopeSet.clear();
2521 InsnsEndScopeSet.clear();
Devang Patelaead63c2010-03-29 22:59:58 +00002522 DbgValueStartMap.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002523 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002524 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002525 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002526 AbstractVariables.clear();
Devang Patelf1dabde2010-04-22 18:43:35 +00002527 LabelsBeforeInsn.clear();
2528 LabelsAfterInsn.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002529 Lines.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002530 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002531}
2532
Chris Lattnerc6087842010-03-09 04:54:43 +00002533/// recordSourceLine - Register a source line with debug info. Returns the
2534/// unique label that was emitted and which provides correspondence to
2535/// the source line list.
2536MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, MDNode *S) {
Devang Patel65dbc902009-11-25 17:36:49 +00002537 StringRef Dir;
2538 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002539
2540 DIDescriptor Scope(S);
2541 if (Scope.isCompileUnit()) {
2542 DICompileUnit CU(S);
2543 Dir = CU.getDirectory();
2544 Fn = CU.getFilename();
2545 } else if (Scope.isSubprogram()) {
2546 DISubprogram SP(S);
2547 Dir = SP.getDirectory();
2548 Fn = SP.getFilename();
2549 } else if (Scope.isLexicalBlock()) {
2550 DILexicalBlock DB(S);
2551 Dir = DB.getDirectory();
2552 Fn = DB.getFilename();
2553 } else
Chris Lattner206d61e2010-03-13 07:26:18 +00002554 assert(0 && "Unexpected scope info");
Devang Patelf84548d2009-10-05 18:03:19 +00002555
2556 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Chris Lattner63d78362010-03-14 08:36:50 +00002557 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner25b68c62010-03-14 08:15:55 +00002558 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002559
Chris Lattnerc6087842010-03-09 04:54:43 +00002560 Asm->OutStreamer.EmitLabel(Label);
2561 return Label;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002562}
2563
Bill Wendling829e67b2009-05-20 23:22:40 +00002564//===----------------------------------------------------------------------===//
2565// Emit Methods
2566//===----------------------------------------------------------------------===//
2567
Devang Patel2c4ceb12009-11-21 02:48:08 +00002568/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002569///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002570unsigned
2571DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002572 // Get the children.
2573 const std::vector<DIE *> &Children = Die->getChildren();
2574
2575 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002576 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00002577 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00002578
2579 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002580 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002581
2582 // Get the abbreviation for this DIE.
2583 unsigned AbbrevNumber = Die->getAbbrevNumber();
2584 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2585
2586 // Set DIE offset
2587 Die->setOffset(Offset);
2588
2589 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002590 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002591
2592 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2593 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2594
2595 // Size the DIE attribute values.
2596 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2597 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00002598 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00002599
2600 // Size the DIE children if any.
2601 if (!Children.empty()) {
2602 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2603 "Children flag not set");
2604
2605 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002606 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002607
2608 // End of children marker.
2609 Offset += sizeof(int8_t);
2610 }
2611
2612 Die->setSize(Offset - Die->getOffset());
2613 return Offset;
2614}
2615
Devang Patel2c4ceb12009-11-21 02:48:08 +00002616/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002617///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002618void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002619 // Compute size of compile unit header.
2620 static unsigned Offset =
2621 sizeof(int32_t) + // Length of Compilation Unit Info
2622 sizeof(int16_t) + // DWARF version number
2623 sizeof(int32_t) + // Offset Into Abbrev. Section
2624 sizeof(int8_t); // Pointer Size (in bytes)
2625
Devang Patel2c4ceb12009-11-21 02:48:08 +00002626 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Bill Wendling94d04b82009-05-20 23:21:38 +00002627}
2628
Chris Lattner11b8f302010-04-04 23:02:02 +00002629/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2630/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00002631static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00002632 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002633 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00002634 if (!SymbolStem) return 0;
2635
Chris Lattner9c69e285532010-04-04 22:59:04 +00002636 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2637 Asm->OutStreamer.EmitLabel(TmpSym);
2638 return TmpSym;
2639}
2640
2641/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2642/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00002643void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002644 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002645
Bill Wendling94d04b82009-05-20 23:21:38 +00002646 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002647 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002648 DwarfFrameSectionSym =
2649 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2650 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002651
Chris Lattner9c69e285532010-04-04 22:59:04 +00002652 DwarfInfoSectionSym =
2653 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2654 DwarfAbbrevSectionSym =
2655 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00002656 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Chris Lattner9c69e285532010-04-04 22:59:04 +00002657
2658 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00002659 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002660
Chris Lattner11b8f302010-04-04 23:02:02 +00002661 EmitSectionSym(Asm, TLOF.getDwarfLineSection());
2662 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2663 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2664 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Chris Lattner9c69e285532010-04-04 22:59:04 +00002665 DwarfStrSectionSym =
2666 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00002667 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2668 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00002669
Chris Lattner9c69e285532010-04-04 22:59:04 +00002670 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00002671 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002672}
2673
Devang Patel2c4ceb12009-11-21 02:48:08 +00002674/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002675///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002676void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002677 // Get the abbreviation for this DIE.
2678 unsigned AbbrevNumber = Die->getAbbrevNumber();
2679 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2680
Bill Wendling94d04b82009-05-20 23:21:38 +00002681 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00002682 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00002683 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2684 Twine::utohexstr(Die->getOffset()) + ":0x" +
2685 Twine::utohexstr(Die->getSize()) + " " +
2686 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002687 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002688
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002689 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00002690 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2691
2692 // Emit the DIE attribute values.
2693 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2694 unsigned Attr = AbbrevData[i].getAttribute();
2695 unsigned Form = AbbrevData[i].getForm();
2696 assert(Form && "Too many attributes for DIE (check abbreviation)");
2697
Chris Lattner3f53c832010-04-04 18:52:31 +00002698 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00002699 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2700
Bill Wendling94d04b82009-05-20 23:21:38 +00002701 switch (Attr) {
2702 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002703 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002704 break;
2705 case dwarf::DW_AT_abstract_origin: {
2706 DIEEntry *E = cast<DIEEntry>(Values[i]);
2707 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002708 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002709 Asm->EmitInt32(Addr);
2710 break;
2711 }
Devang Patelf2548ca2010-04-16 23:33:45 +00002712 case dwarf::DW_AT_ranges: {
2713 // DW_AT_range Value encodes offset in debug_range section.
2714 DIEInteger *V = cast<DIEInteger>(Values[i]);
2715 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2716 V->getValue(),
2717 DwarfDebugRangeSectionSym,
2718 4);
2719 break;
2720 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002721 default:
2722 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002723 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00002724 break;
2725 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002726 }
2727
2728 // Emit the DIE children if any.
2729 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2730 const std::vector<DIE *> &Children = Die->getChildren();
2731
2732 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002733 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002734
Chris Lattner3f53c832010-04-04 18:52:31 +00002735 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00002736 Asm->OutStreamer.AddComment("End Of Children Mark");
2737 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002738 }
2739}
2740
Devang Patel8a241142009-12-09 18:24:21 +00002741/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002742///
Devang Patel8a241142009-12-09 18:24:21 +00002743void DwarfDebug::emitDebugInfo() {
2744 // Start debug info section.
2745 Asm->OutStreamer.SwitchSection(
2746 Asm->getObjFileLowering().getDwarfInfoSection());
2747 DIE *Die = ModuleCU->getCUDie();
Bill Wendling94d04b82009-05-20 23:21:38 +00002748
2749 // Emit the compile units header.
Chris Lattnerc0215722010-04-04 19:25:43 +00002750 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2751 ModuleCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00002752
2753 // Emit size of content not including length itself
2754 unsigned ContentSize = Die->getSize() +
2755 sizeof(int16_t) + // DWARF version number
2756 sizeof(int32_t) + // Offset Into Abbrev. Section
2757 sizeof(int8_t) + // Pointer Size (in bytes)
2758 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2759
Chris Lattner233f52b2010-03-09 23:52:58 +00002760 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2761 Asm->EmitInt32(ContentSize);
2762 Asm->OutStreamer.AddComment("DWARF version number");
2763 Asm->EmitInt16(dwarf::DWARF_VERSION);
2764 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Chris Lattner6189ed12010-04-04 23:25:33 +00002765 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2766 DwarfAbbrevSectionSym);
Chris Lattner233f52b2010-03-09 23:52:58 +00002767 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002768 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002769
Devang Patel2c4ceb12009-11-21 02:48:08 +00002770 emitDIE(Die);
Bill Wendling94d04b82009-05-20 23:21:38 +00002771 // FIXME - extra padding for gdb bug.
Chris Lattner233f52b2010-03-09 23:52:58 +00002772 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2773 Asm->EmitInt8(0);
2774 Asm->EmitInt8(0);
2775 Asm->EmitInt8(0);
2776 Asm->EmitInt8(0);
Chris Lattnerc0215722010-04-04 19:25:43 +00002777 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", ModuleCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00002778}
2779
Devang Patel2c4ceb12009-11-21 02:48:08 +00002780/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002781///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002782void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002783 // Check to see if it is worth the effort.
2784 if (!Abbreviations.empty()) {
2785 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002786 Asm->OutStreamer.SwitchSection(
2787 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002788
Chris Lattnerc0215722010-04-04 19:25:43 +00002789 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002790
2791 // For each abbrevation.
2792 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2793 // Get abbreviation data
2794 const DIEAbbrev *Abbrev = Abbreviations[i];
2795
2796 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002797 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00002798
2799 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002800 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00002801 }
2802
2803 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002804 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002805
Chris Lattnerc0215722010-04-04 19:25:43 +00002806 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002807 }
2808}
2809
Devang Patel2c4ceb12009-11-21 02:48:08 +00002810/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002811/// the line matrix.
2812///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002813void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002814 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00002815 Asm->OutStreamer.AddComment("Extended Op");
2816 Asm->EmitInt8(0);
2817
2818 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002819 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00002820 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2821 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2822
2823 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00002824
Chris Lattnerc0215722010-04-04 19:25:43 +00002825 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00002826 Asm->getTargetData().getPointerSize(),
2827 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002828
2829 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00002830 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2831 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002832 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00002833 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002834}
2835
Devang Patel2c4ceb12009-11-21 02:48:08 +00002836/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00002837///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002838void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002839 // If the target is using .loc/.file, the assembler will be emitting the
2840 // .debug_line table automatically.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002841 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002842 return;
2843
2844 // Minimum line delta, thus ranging from -10..(255-10).
2845 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2846 // Maximum line delta, thus ranging from -10..(255-10).
2847 const int MaxLineDelta = 255 + MinLineDelta;
2848
2849 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002850 Asm->OutStreamer.SwitchSection(
2851 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002852
2853 // Construct the section header.
Chris Lattner233f52b2010-03-09 23:52:58 +00002854 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnera6437182010-04-04 19:58:12 +00002855 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
2856 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00002857 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002858
Chris Lattner233f52b2010-03-09 23:52:58 +00002859 Asm->OutStreamer.AddComment("DWARF version number");
2860 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling94d04b82009-05-20 23:21:38 +00002861
Chris Lattner233f52b2010-03-09 23:52:58 +00002862 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnera6437182010-04-04 19:58:12 +00002863 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
2864 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00002865 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002866
Chris Lattner233f52b2010-03-09 23:52:58 +00002867 Asm->OutStreamer.AddComment("Minimum Instruction Length");
2868 Asm->EmitInt8(1);
2869 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
2870 Asm->EmitInt8(1);
2871 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
2872 Asm->EmitInt8(MinLineDelta);
2873 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
2874 Asm->EmitInt8(MaxLineDelta);
2875 Asm->OutStreamer.AddComment("Special Opcode Base");
2876 Asm->EmitInt8(-MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00002877
2878 // Line number standard opcode encodings argument count
Chris Lattner233f52b2010-03-09 23:52:58 +00002879 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
2880 Asm->EmitInt8(0);
2881 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
2882 Asm->EmitInt8(1);
2883 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
2884 Asm->EmitInt8(1);
2885 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
2886 Asm->EmitInt8(1);
2887 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
2888 Asm->EmitInt8(1);
2889 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
2890 Asm->EmitInt8(0);
2891 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
2892 Asm->EmitInt8(0);
2893 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
2894 Asm->EmitInt8(0);
2895 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
2896 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002897
2898 // Emit directories.
2899 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattner4cf202b2010-01-23 03:11:46 +00002900 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner3f53c832010-04-04 18:52:31 +00002901 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002902 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002903 }
2904
Chris Lattner233f52b2010-03-09 23:52:58 +00002905 Asm->OutStreamer.AddComment("End of directories");
2906 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002907
2908 // Emit files.
2909 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2910 // Remember source id starts at 1.
2911 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattner4cf202b2010-01-23 03:11:46 +00002912 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner3f53c832010-04-04 18:52:31 +00002913 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002914 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
2915
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002916 Asm->EmitULEB128(Id.first, "Directory #");
2917 Asm->EmitULEB128(0, "Mod date");
2918 Asm->EmitULEB128(0, "File size");
Bill Wendling94d04b82009-05-20 23:21:38 +00002919 }
2920
Chris Lattner233f52b2010-03-09 23:52:58 +00002921 Asm->OutStreamer.AddComment("End of files");
2922 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002923
Chris Lattnerc0215722010-04-04 19:25:43 +00002924 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002925
2926 // A sequence for each text section.
2927 unsigned SecSrcLinesSize = SectionSourceLines.size();
2928
2929 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2930 // Isolate current sections line info.
2931 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2932
Bill Wendling94d04b82009-05-20 23:21:38 +00002933 // Dwarf assumes we start with first line of first source file.
2934 unsigned Source = 1;
2935 unsigned Line = 1;
2936
2937 // Construct rows of the address, source, line, column matrix.
2938 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2939 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner25b68c62010-03-14 08:15:55 +00002940 MCSymbol *Label = LineInfo.getLabel();
Chris Lattnerb9130602010-03-14 02:20:58 +00002941 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling94d04b82009-05-20 23:21:38 +00002942
Caroline Ticec6f9d622009-09-11 18:25:54 +00002943 if (LineInfo.getLine() == 0) continue;
2944
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002945 if (Asm->isVerbose()) {
Chris Lattner188a87d2010-03-10 01:04:13 +00002946 std::pair<unsigned, unsigned> SrcID =
Bill Wendling94d04b82009-05-20 23:21:38 +00002947 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner188a87d2010-03-10 01:04:13 +00002948 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner49f618a2010-03-10 02:29:31 +00002949 "/" +
2950 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner188a87d2010-03-10 01:04:13 +00002951 ":" + Twine(LineInfo.getLine()));
Bill Wendling94d04b82009-05-20 23:21:38 +00002952 }
2953
2954 // Define the line address.
Chris Lattner233f52b2010-03-09 23:52:58 +00002955 Asm->OutStreamer.AddComment("Extended Op");
2956 Asm->EmitInt8(0);
2957 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002958 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00002959
2960 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2961 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2962
2963 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002964 Asm->OutStreamer.EmitSymbolValue(Label,
2965 Asm->getTargetData().getPointerSize(),
Chris Lattnerb9130602010-03-14 02:20:58 +00002966 0/*AddrSpace*/);
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00002967
Bill Wendling94d04b82009-05-20 23:21:38 +00002968 // If change of source, then switch to the new source.
2969 if (Source != LineInfo.getSourceID()) {
2970 Source = LineInfo.getSourceID();
Chris Lattner233f52b2010-03-09 23:52:58 +00002971 Asm->OutStreamer.AddComment("DW_LNS_set_file");
2972 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002973 Asm->EmitULEB128(Source, "New Source");
Bill Wendling94d04b82009-05-20 23:21:38 +00002974 }
2975
2976 // If change of line.
2977 if (Line != LineInfo.getLine()) {
2978 // Determine offset.
2979 int Offset = LineInfo.getLine() - Line;
2980 int Delta = Offset - MinLineDelta;
2981
2982 // Update line.
2983 Line = LineInfo.getLine();
2984
2985 // If delta is small enough and in range...
2986 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2987 // ... then use fast opcode.
Chris Lattner233f52b2010-03-09 23:52:58 +00002988 Asm->OutStreamer.AddComment("Line Delta");
2989 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00002990 } else {
2991 // ... otherwise use long hand.
Chris Lattner233f52b2010-03-09 23:52:58 +00002992 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling94d04b82009-05-20 23:21:38 +00002993 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002994 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner233f52b2010-03-09 23:52:58 +00002995 Asm->OutStreamer.AddComment("DW_LNS_copy");
2996 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00002997 }
2998 } else {
2999 // Copy the previous row (different address or source)
Chris Lattner233f52b2010-03-09 23:52:58 +00003000 Asm->OutStreamer.AddComment("DW_LNS_copy");
3001 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003002 }
3003 }
3004
Devang Patel2c4ceb12009-11-21 02:48:08 +00003005 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003006 }
3007
3008 if (SecSrcLinesSize == 0)
3009 // Because we're emitting a debug_line section, we still need a line
3010 // table. The linker and friends expect it to exist. If there's nothing to
3011 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003012 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003013
Chris Lattnerc0215722010-04-04 19:25:43 +00003014 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003015}
3016
Devang Patel2c4ceb12009-11-21 02:48:08 +00003017/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003018///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003019void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003020 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003021 return;
3022
Chris Lattnerd38fee82010-04-05 00:13:49 +00003023 int stackGrowth = Asm->getTargetData().getPointerSize();
3024 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3025 TargetFrameInfo::StackGrowsDown)
3026 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00003027
3028 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003029 Asm->OutStreamer.SwitchSection(
3030 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003031
Chris Lattnerc0215722010-04-04 19:25:43 +00003032 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003033 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003034 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3035 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003036
Chris Lattnerc0215722010-04-04 19:25:43 +00003037 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003038 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00003039 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00003040 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00003041 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00003042 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003043 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003044 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3045 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00003046 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003047 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling94d04b82009-05-20 23:21:38 +00003048 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00003049
3050 std::vector<MachineMove> Moves;
3051 RI->getInitialFrameState(Moves);
3052
Chris Lattner02b86b92010-04-04 23:41:46 +00003053 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003054
3055 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattnerc0215722010-04-04 19:25:43 +00003056 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003057}
3058
Devang Patel2c4ceb12009-11-21 02:48:08 +00003059/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00003060/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00003061void DwarfDebug::
3062emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003063 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003064 return;
3065
3066 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003067 Asm->OutStreamer.SwitchSection(
3068 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003069
Chris Lattner233f52b2010-03-09 23:52:58 +00003070 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00003071 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00003072 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00003073 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00003074 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00003075 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003076
Chris Lattner206d61e2010-03-13 07:26:18 +00003077 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00003078
Chris Lattner233f52b2010-03-09 23:52:58 +00003079 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattner6189ed12010-04-04 23:25:33 +00003080 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
3081 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003082
Chris Lattner233f52b2010-03-09 23:52:58 +00003083 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00003084 MCSymbol *FuncBeginSym =
3085 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00003086 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00003087 Asm->getTargetData().getPointerSize(),
3088 0/*AddrSpace*/);
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003089
3090
Chris Lattner233f52b2010-03-09 23:52:58 +00003091 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00003092 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003093 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003094
Chris Lattner02b86b92010-04-04 23:41:46 +00003095 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003096
3097 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattner206d61e2010-03-13 07:26:18 +00003098 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00003099}
3100
Devang Patel8a241142009-12-09 18:24:21 +00003101/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3102///
3103void DwarfDebug::emitDebugPubNames() {
3104 // Start the dwarf pubnames section.
3105 Asm->OutStreamer.SwitchSection(
3106 Asm->getObjFileLowering().getDwarfPubNamesSection());
3107
Chris Lattner233f52b2010-03-09 23:52:58 +00003108 Asm->OutStreamer.AddComment("Length of Public Names Info");
Chris Lattnera6437182010-04-04 19:58:12 +00003109 Asm->EmitLabelDifference(
3110 Asm->GetTempSymbol("pubnames_end", ModuleCU->getID()),
3111 Asm->GetTempSymbol("pubnames_begin", ModuleCU->getID()), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003112
Chris Lattnerc0215722010-04-04 19:25:43 +00003113 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3114 ModuleCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003115
Chris Lattner233f52b2010-03-09 23:52:58 +00003116 Asm->OutStreamer.AddComment("DWARF Version");
3117 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling94d04b82009-05-20 23:21:38 +00003118
Chris Lattner233f52b2010-03-09 23:52:58 +00003119 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Chris Lattner6189ed12010-04-04 23:25:33 +00003120 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3121 DwarfInfoSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003122
Chris Lattner233f52b2010-03-09 23:52:58 +00003123 Asm->OutStreamer.AddComment("Compilation Unit Length");
Chris Lattnera6437182010-04-04 19:58:12 +00003124 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()),
3125 Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3126 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003127
Devang Patel8a241142009-12-09 18:24:21 +00003128 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling94d04b82009-05-20 23:21:38 +00003129 for (StringMap<DIE*>::const_iterator
3130 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3131 const char *Name = GI->getKeyData();
Chris Lattner233f52b2010-03-09 23:52:58 +00003132 DIE *Entity = GI->second;
Bill Wendling94d04b82009-05-20 23:21:38 +00003133
Chris Lattner233f52b2010-03-09 23:52:58 +00003134 Asm->OutStreamer.AddComment("DIE offset");
3135 Asm->EmitInt32(Entity->getOffset());
Chris Lattner4cf202b2010-01-23 03:11:46 +00003136
Chris Lattner3f53c832010-04-04 18:52:31 +00003137 if (Asm->isVerbose())
Chris Lattner4cf202b2010-01-23 03:11:46 +00003138 Asm->OutStreamer.AddComment("External Name");
3139 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003140 }
3141
Chris Lattner233f52b2010-03-09 23:52:58 +00003142 Asm->OutStreamer.AddComment("End Mark");
3143 Asm->EmitInt32(0);
Chris Lattnerc0215722010-04-04 19:25:43 +00003144 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3145 ModuleCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003146}
3147
Devang Patel193f7202009-11-24 01:14:22 +00003148void DwarfDebug::emitDebugPubTypes() {
Devang Patelf3a03762009-11-24 19:18:41 +00003149 // Start the dwarf pubnames section.
3150 Asm->OutStreamer.SwitchSection(
3151 Asm->getObjFileLowering().getDwarfPubTypesSection());
Chris Lattner233f52b2010-03-09 23:52:58 +00003152 Asm->OutStreamer.AddComment("Length of Public Types Info");
Chris Lattnera6437182010-04-04 19:58:12 +00003153 Asm->EmitLabelDifference(
3154 Asm->GetTempSymbol("pubtypes_end", ModuleCU->getID()),
3155 Asm->GetTempSymbol("pubtypes_begin", ModuleCU->getID()), 4);
Devang Patel193f7202009-11-24 01:14:22 +00003156
Chris Lattnerc0215722010-04-04 19:25:43 +00003157 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3158 ModuleCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003159
Chris Lattner3f53c832010-04-04 18:52:31 +00003160 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
Devang Patele3d6d222010-02-02 03:47:27 +00003161 Asm->EmitInt16(dwarf::DWARF_VERSION);
Devang Patel193f7202009-11-24 01:14:22 +00003162
Chris Lattner233f52b2010-03-09 23:52:58 +00003163 Asm->OutStreamer.AddComment("Offset of Compilation ModuleCU Info");
Chris Lattner6189ed12010-04-04 23:25:33 +00003164 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3165 DwarfInfoSectionSym);
Devang Patel193f7202009-11-24 01:14:22 +00003166
Chris Lattner233f52b2010-03-09 23:52:58 +00003167 Asm->OutStreamer.AddComment("Compilation ModuleCU Length");
Chris Lattnera6437182010-04-04 19:58:12 +00003168 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()),
3169 Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3170 4);
Devang Patel193f7202009-11-24 01:14:22 +00003171
3172 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
3173 for (StringMap<DIE*>::const_iterator
3174 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3175 const char *Name = GI->getKeyData();
3176 DIE * Entity = GI->second;
3177
Chris Lattner3f53c832010-04-04 18:52:31 +00003178 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Devang Patele3d6d222010-02-02 03:47:27 +00003179 Asm->EmitInt32(Entity->getOffset());
Chris Lattner4cf202b2010-01-23 03:11:46 +00003180
Chris Lattner3f53c832010-04-04 18:52:31 +00003181 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Devang Patel31acb892010-02-02 03:37:03 +00003182 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
Devang Patel193f7202009-11-24 01:14:22 +00003183 }
3184
Chris Lattner233f52b2010-03-09 23:52:58 +00003185 Asm->OutStreamer.AddComment("End Mark");
3186 Asm->EmitInt32(0);
Chris Lattnerc0215722010-04-04 19:25:43 +00003187 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3188 ModuleCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003189}
3190
Devang Patel2c4ceb12009-11-21 02:48:08 +00003191/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003192///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003193void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003194 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003195 if (StringPool.empty()) return;
3196
3197 // Start the dwarf str section.
3198 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003199 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003200
Chris Lattnerbc733f52010-03-13 02:17:42 +00003201 // Get all of the string pool entries and put them in an array by their ID so
3202 // we can sort them.
3203 SmallVector<std::pair<unsigned,
3204 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3205
3206 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3207 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3208 Entries.push_back(std::make_pair(I->second.second, &*I));
3209
3210 array_pod_sort(Entries.begin(), Entries.end());
3211
3212 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003213 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003214 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003215
3216 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003217 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003218 }
3219}
3220
Devang Patel2c4ceb12009-11-21 02:48:08 +00003221/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003222///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003223void DwarfDebug::emitDebugLoc() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003224 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003225 Asm->OutStreamer.SwitchSection(
3226 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003227}
3228
3229/// EmitDebugARanges - Emit visible names into a debug aranges section.
3230///
3231void DwarfDebug::EmitDebugARanges() {
3232 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003233 Asm->OutStreamer.SwitchSection(
3234 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003235}
3236
Devang Patel2c4ceb12009-11-21 02:48:08 +00003237/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003238///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003239void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003240 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003241 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00003242 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel18ee3bb2010-04-22 20:52:00 +00003243 for (SmallVector<const MCSymbol *, 8>::const_iterator
3244 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
3245 I != E; ++I) {
3246 if (*I) {
3247 const MCSymbol *Begin = TextSectionSym;
3248 // If this symbol is inside linkonce section then use appropriate begin
3249 // marker;
3250 DenseMap<const MCSymbol *, const MCSymbol *>::iterator WI
3251 = WeakDebugRangeSymbols.find(*I);
3252 if (WI != WeakDebugRangeSymbols.end())
3253 Begin = WI->second;
3254
3255 Asm->EmitLabelDifference(*I, Begin,
Devang Patelf2548ca2010-04-16 23:33:45 +00003256 Asm->getTargetData().getPointerSize());
Devang Patel18ee3bb2010-04-22 20:52:00 +00003257 }
Devang Patelf2548ca2010-04-16 23:33:45 +00003258 else
3259 Asm->OutStreamer.EmitIntValue(0, Asm->getTargetData().getPointerSize(),
3260 /*addrspace*/0);
3261 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003262}
3263
Devang Patel2c4ceb12009-11-21 02:48:08 +00003264/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003265///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003266void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003267 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00003268 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003269 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003270 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003271 }
3272}
3273
Devang Patel2c4ceb12009-11-21 02:48:08 +00003274/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00003275/// Section Header:
3276/// 1. length of section
3277/// 2. Dwarf version number
3278/// 3. address size.
3279///
3280/// Entries (one "entry" for each function that was inlined):
3281///
3282/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3283/// otherwise offset into __debug_str for regular function name.
3284/// 2. offset into __debug_str section for regular function name.
3285/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3286/// instances for the function.
3287///
3288/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3289/// inlined instance; the die_offset points to the inlined_subroutine die in the
3290/// __debug_info section, and the low_pc is the starting address for the
3291/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003292void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003293 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003294 return;
3295
Devang Patel1dbc7712009-06-29 20:45:18 +00003296 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00003297 return;
3298
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003299 Asm->OutStreamer.SwitchSection(
3300 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00003301
Chris Lattner233f52b2010-03-09 23:52:58 +00003302 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003303 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3304 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003305
Chris Lattnerc0215722010-04-04 19:25:43 +00003306 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003307
Chris Lattner233f52b2010-03-09 23:52:58 +00003308 Asm->OutStreamer.AddComment("Dwarf Version");
3309 Asm->EmitInt16(dwarf::DWARF_VERSION);
3310 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003311 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003312
Devang Patel53bb5c92009-11-10 23:06:00 +00003313 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
3314 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00003315
Devang Patel53bb5c92009-11-10 23:06:00 +00003316 MDNode *Node = *I;
Devang Patel622b0262010-01-19 06:19:05 +00003317 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003318 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00003319 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00003320 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00003321 StringRef LName = SP.getLinkageName();
3322 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003323
Chris Lattner233f52b2010-03-09 23:52:58 +00003324 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003325 if (LName.empty()) {
3326 Asm->OutStreamer.EmitBytes(Name, 0);
3327 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3328 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00003329 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3330 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00003331
Chris Lattner233f52b2010-03-09 23:52:58 +00003332 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00003333 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003334 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00003335
Devang Patel53bb5c92009-11-10 23:06:00 +00003336 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00003337 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00003338 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00003339 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003340
Chris Lattner3f53c832010-04-04 18:52:31 +00003341 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003342 Asm->OutStreamer.EmitSymbolValue(LI->first,
3343 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003344 }
3345 }
3346
Chris Lattnerc0215722010-04-04 19:25:43 +00003347 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003348}