blob: 7a40303778844b5e21d73cd232d446b83c4a9ff2 [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 Pateleac9c072010-04-27 19:46:33 +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 Pateleac9c072010-04-27 19:46:33 +000044static cl::opt<bool> PrintDbgScope("print-dbgscope", cl::Hidden,
45 cl::desc("Print DbgScope information for each machine instruction"));
46
47static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
48 cl::desc("Disable debug info printing"));
49
Dan Gohman281d65d2010-05-07 01:08:53 +000050static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
51 cl::desc("Make an absense of debug location information explicit."),
52 cl::init(false));
53
Bill Wendling5f017e82010-04-07 09:28:04 +000054namespace {
55 const char *DWARFGroupName = "DWARF Emission";
56 const char *DbgTimerName = "DWARF Debug Writer";
57} // end anonymous namespace
58
Bill Wendling0310d762009-05-15 09:23:25 +000059//===----------------------------------------------------------------------===//
60
61/// Configuration values for initial hash set sizes (log2).
62///
Bill Wendling0310d762009-05-15 09:23:25 +000063static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000064
65namespace llvm {
66
67//===----------------------------------------------------------------------===//
68/// CompileUnit - This dwarf writer support class manages information associate
69/// with a source file.
Nick Lewycky5f9843f2009-11-17 08:11:44 +000070class CompileUnit {
Bill Wendling0310d762009-05-15 09:23:25 +000071 /// ID - File identifier for source.
72 ///
73 unsigned ID;
74
75 /// Die - Compile unit debug information entry.
76 ///
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000077 const OwningPtr<DIE> CUDie;
Bill Wendling0310d762009-05-15 09:23:25 +000078
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +000079 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel6f01d9c2009-11-21 00:31:03 +000080 DIE *IndexTyDie;
81
Bill Wendling0310d762009-05-15 09:23:25 +000082 /// GVToDieMap - Tracks the mapping of unit level debug informaton
83 /// variables to debug information entries.
Devang Patele4b27562009-08-28 23:24:31 +000084 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patele9f8f5e2010-05-07 20:54:48 +000085 DenseMap<const MDNode *, DIE *> GVToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000086
87 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
88 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patele4b27562009-08-28 23:24:31 +000089 /// FIXME : Rename
Devang Patele9f8f5e2010-05-07 20:54:48 +000090 DenseMap<const MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000091
92 /// Globals - A map of globally visible named entities for this unit.
93 ///
94 StringMap<DIE*> Globals;
95
Devang Patel193f7202009-11-24 01:14:22 +000096 /// GlobalTypes - A map of globally visible types for this unit.
97 ///
98 StringMap<DIE*> GlobalTypes;
99
Bill Wendling0310d762009-05-15 09:23:25 +0000100public:
101 CompileUnit(unsigned I, DIE *D)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000102 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000103
104 // Accessors.
Devang Patel193f7202009-11-24 01:14:22 +0000105 unsigned getID() const { return ID; }
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +0000106 DIE* getCUDie() const { return CUDie.get(); }
Devang Patel193f7202009-11-24 01:14:22 +0000107 const StringMap<DIE*> &getGlobals() const { return Globals; }
108 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling0310d762009-05-15 09:23:25 +0000109
110 /// hasContent - Return true if this compile unit has something to write out.
111 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000112 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000113
Devang Patel2c4ceb12009-11-21 02:48:08 +0000114 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000115 ///
Benjamin Kramerbbb88db2010-03-31 20:15:45 +0000116 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +0000117
Devang Patel193f7202009-11-24 01:14:22 +0000118 /// addGlobalType - Add a new global type to the compile unit.
119 ///
Benjamin Kramerbbb88db2010-03-31 20:15:45 +0000120 void addGlobalType(StringRef Name, DIE *Die) {
Devang Patel193f7202009-11-24 01:14:22 +0000121 GlobalTypes[Name] = Die;
122 }
123
Devang Patel017d1212009-11-20 21:37:22 +0000124 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling0310d762009-05-15 09:23:25 +0000125 /// specified debug variable.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000126 DIE *getDIE(const MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000127
Devang Patel017d1212009-11-20 21:37:22 +0000128 /// insertDIE - Insert DIE into the map.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000129 void insertDIE(const MDNode *N, DIE *D) {
Devang Patel017d1212009-11-20 21:37:22 +0000130 GVToDieMap.insert(std::make_pair(N, D));
131 }
Bill Wendling0310d762009-05-15 09:23:25 +0000132
Devang Patel017d1212009-11-20 21:37:22 +0000133 /// getDIEEntry - Returns the debug information entry for the speciefied
134 /// debug variable.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000135 DIEEntry *getDIEEntry(const MDNode *N) {
136 DenseMap<const MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
Devang Patel6404e4e2009-12-15 19:16:48 +0000137 if (I == GVToDIEEntryMap.end())
138 return NULL;
139 return I->second;
140 }
Devang Patel017d1212009-11-20 21:37:22 +0000141
142 /// insertDIEEntry - Insert debug information entry into the map.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000143 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
Devang Patel017d1212009-11-20 21:37:22 +0000144 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000145 }
146
Devang Patel2c4ceb12009-11-21 02:48:08 +0000147 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000148 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000149 void addDie(DIE *Buffer) {
150 this->CUDie->addChild(Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000151 }
Devang Patel6f01d9c2009-11-21 00:31:03 +0000152
153 // getIndexTyDie - Get an anonymous type for index type.
154 DIE *getIndexTyDie() {
155 return IndexTyDie;
156 }
157
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000158 // setIndexTyDie - Set D as anonymous type for index which can be reused
159 // later.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000160 void setIndexTyDie(DIE *D) {
161 IndexTyDie = D;
162 }
163
Bill Wendling0310d762009-05-15 09:23:25 +0000164};
165
166//===----------------------------------------------------------------------===//
167/// DbgVariable - This class is used to track local variable information.
168///
Devang Patelf76a3d62009-11-16 21:53:40 +0000169class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000170 DIVariable Var; // Variable Descriptor.
171 unsigned FrameIndex; // Variable frame index.
Devang Patel90a48ad2010-03-15 18:33:46 +0000172 const MachineInstr *DbgValueMInsn; // DBG_VALUE
Devang Patelaead63c2010-03-29 22:59:58 +0000173 // DbgValueLabel - DBG_VALUE is effective from this label.
174 MCSymbol *DbgValueLabel;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000175 DbgVariable *const AbstractVar; // Abstract variable for this variable.
Devang Patel53bb5c92009-11-10 23:06:00 +0000176 DIE *TheDIE;
Bill Wendling0310d762009-05-15 09:23:25 +0000177public:
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000178 // AbsVar may be NULL.
179 DbgVariable(DIVariable V, unsigned I, DbgVariable *AbsVar)
Devang Patelaead63c2010-03-29 22:59:58 +0000180 : Var(V), FrameIndex(I), DbgValueMInsn(0),
181 DbgValueLabel(0), AbstractVar(AbsVar), TheDIE(0) {}
Devang Patel90a48ad2010-03-15 18:33:46 +0000182 DbgVariable(DIVariable V, const MachineInstr *MI, DbgVariable *AbsVar)
Devang Patelaead63c2010-03-29 22:59:58 +0000183 : Var(V), FrameIndex(0), DbgValueMInsn(MI), DbgValueLabel(0),
184 AbstractVar(AbsVar), TheDIE(0)
Devang Patel90a48ad2010-03-15 18:33:46 +0000185 {}
Bill Wendling0310d762009-05-15 09:23:25 +0000186
187 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000188 DIVariable getVariable() const { return Var; }
189 unsigned getFrameIndex() const { return FrameIndex; }
Devang Patel90a48ad2010-03-15 18:33:46 +0000190 const MachineInstr *getDbgValue() const { return DbgValueMInsn; }
Devang Patelaead63c2010-03-29 22:59:58 +0000191 MCSymbol *getDbgValueLabel() const { return DbgValueLabel; }
192 void setDbgValueLabel(MCSymbol *L) { DbgValueLabel = L; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000193 DbgVariable *getAbstractVariable() const { return AbstractVar; }
194 void setDIE(DIE *D) { TheDIE = D; }
195 DIE *getDIE() const { return TheDIE; }
Bill Wendling0310d762009-05-15 09:23:25 +0000196};
197
198//===----------------------------------------------------------------------===//
Devang Pateleac9c072010-04-27 19:46:33 +0000199/// DbgRange - This is used to track range of instructions with identical
200/// debug info scope.
201///
202typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
203
204//===----------------------------------------------------------------------===//
Bill Wendling0310d762009-05-15 09:23:25 +0000205/// DbgScope - This class is used to track scope information.
206///
Devang Patelf76a3d62009-11-16 21:53:40 +0000207class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000208 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000209 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel3139fcf2010-01-26 21:39:14 +0000210 // Location at which this scope is inlined.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000211 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patel53bb5c92009-11-10 23:06:00 +0000212 bool AbstractScope; // Abstract Scope
Devang Pateld38dd112009-10-01 18:25:23 +0000213 const MachineInstr *LastInsn; // Last instruction of this scope.
214 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Pateleac9c072010-04-27 19:46:33 +0000215 unsigned DFSIn, DFSOut;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000216 // Scopes defined in scope. Contents not owned.
217 SmallVector<DbgScope *, 4> Scopes;
218 // Variables declared in scope. Contents owned.
219 SmallVector<DbgVariable *, 8> Variables;
Devang Pateleac9c072010-04-27 19:46:33 +0000220 SmallVector<DbgRange, 4> Ranges;
Owen Anderson04c05f72009-06-24 22:53:20 +0000221 // Private state for dump()
222 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000223public:
Devang Patele9f8f5e2010-05-07 20:54:48 +0000224 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000225 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Pateleac9c072010-04-27 19:46:33 +0000226 LastInsn(0), FirstInsn(0),
227 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000228 virtual ~DbgScope();
229
230 // Accessors.
231 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000232 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000233 DIDescriptor getDesc() const { return Desc; }
Devang Patele9f8f5e2010-05-07 20:54:48 +0000234 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
235 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000236 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
237 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Pateleac9c072010-04-27 19:46:33 +0000238 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
239
240 /// openInsnRange - This scope covers instruction range starting from MI.
241 void openInsnRange(const MachineInstr *MI) {
242 if (!FirstInsn)
243 FirstInsn = MI;
244
245 if (Parent)
246 Parent->openInsnRange(MI);
247 }
248
249 /// extendInsnRange - Extend the current instruction range covered by
250 /// this scope.
251 void extendInsnRange(const MachineInstr *MI) {
252 assert (FirstInsn && "MI Range is not open!");
253 LastInsn = MI;
254 if (Parent)
255 Parent->extendInsnRange(MI);
256 }
257
258 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
259 /// until now. This is used when a new scope is encountered while walking
260 /// machine instructions.
261 void closeInsnRange(DbgScope *NewScope = NULL) {
262 assert (LastInsn && "Last insn missing!");
263 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
264 FirstInsn = NULL;
265 LastInsn = NULL;
266 // If Parent dominates NewScope then do not close Parent's instruction
267 // range.
268 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
269 Parent->closeInsnRange(NewScope);
270 }
271
Devang Patel53bb5c92009-11-10 23:06:00 +0000272 void setAbstractScope() { AbstractScope = true; }
273 bool isAbstractScope() const { return AbstractScope; }
Devang Pateleac9c072010-04-27 19:46:33 +0000274
275 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
276 unsigned getDFSOut() const { return DFSOut; }
277 void setDFSOut(unsigned O) { DFSOut = O; }
278 unsigned getDFSIn() const { return DFSIn; }
279 void setDFSIn(unsigned I) { DFSIn = I; }
280 bool dominates(const DbgScope *S) {
281 if (S == this)
282 return true;
283 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
284 return true;
285 return false;
286 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000287
Devang Patel2c4ceb12009-11-21 02:48:08 +0000288 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000289 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000290 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000291
Devang Patel2c4ceb12009-11-21 02:48:08 +0000292 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000293 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000294 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000295
Bill Wendling0310d762009-05-15 09:23:25 +0000296#ifndef NDEBUG
297 void dump() const;
298#endif
299};
Devang Pateleac9c072010-04-27 19:46:33 +0000300
Chris Lattnerea761862010-04-05 04:09:20 +0000301} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000302
303#ifndef NDEBUG
304void DbgScope::dump() const {
David Greenef83adbc2009-12-24 00:31:35 +0000305 raw_ostream &err = dbgs();
Chris Lattnerc281de12009-08-23 00:51:00 +0000306 err.indent(IndentLevel);
Devang Patele9f8f5e2010-05-07 20:54:48 +0000307 const MDNode *N = Desc;
Devang Patel53bb5c92009-11-10 23:06:00 +0000308 N->dump();
Devang Patel53bb5c92009-11-10 23:06:00 +0000309 if (AbstractScope)
310 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000311
312 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000313 if (!Scopes.empty())
314 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000315 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
316 if (Scopes[i] != this)
317 Scopes[i]->dump();
318
319 IndentLevel -= 2;
320}
321#endif
322
Bill Wendling0310d762009-05-15 09:23:25 +0000323DbgScope::~DbgScope() {
Bill Wendling0310d762009-05-15 09:23:25 +0000324 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
325 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000326}
327
Chris Lattner49cd6642010-04-05 05:11:15 +0000328DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Chris Lattnerd38fee82010-04-05 00:13:49 +0000329 : Asm(A), MMI(Asm->MMI), ModuleCU(0),
Chris Lattner994cb122010-04-05 03:52:55 +0000330 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelf2548ca2010-04-16 23:33:45 +0000331 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000332 NextStringPoolNumber = 0;
Chris Lattner9c69e285532010-04-04 22:59:04 +0000333
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000334 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
335 DwarfStrSectionSym = TextSectionSym = 0;
Devang Pateleac9c072010-04-27 19:46:33 +0000336 DwarfDebugRangeSectionSym = 0;
337 FunctionBeginSym = 0;
Torok Edwin9c421072010-04-07 10:44:46 +0000338 if (TimePassesIsEnabled) {
339 NamedRegionTimer T(DbgTimerName, DWARFGroupName);
340 beginModule(M);
341 } else {
342 beginModule(M);
343 }
Bill Wendling0310d762009-05-15 09:23:25 +0000344}
345DwarfDebug::~DwarfDebug() {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000346 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
347 DIEBlocks[j]->~DIEBlock();
Bill Wendling0310d762009-05-15 09:23:25 +0000348}
349
Chris Lattnerbc733f52010-03-13 02:17:42 +0000350MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
351 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
352 if (Entry.first) return Entry.first;
353
354 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000355 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000356}
357
358
Devang Patel2c4ceb12009-11-21 02:48:08 +0000359/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000360///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000361void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000362 // Profile the node so that we can make it unique.
363 FoldingSetNodeID ID;
364 Abbrev.Profile(ID);
365
366 // Check the set for priors.
367 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
368
369 // If it's newly added.
370 if (InSet == &Abbrev) {
371 // Add to abbreviation list.
372 Abbreviations.push_back(&Abbrev);
373
374 // Assign the vector position + 1 as its number.
375 Abbrev.setNumber(Abbreviations.size());
376 } else {
377 // Assign existing abbreviation number.
378 Abbrev.setNumber(InSet->getNumber());
379 }
380}
381
Devang Patel2c4ceb12009-11-21 02:48:08 +0000382/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000383/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000384DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000385 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000386 return Value;
387}
388
Devang Patel2c4ceb12009-11-21 02:48:08 +0000389/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000390///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000391void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000392 unsigned Form, uint64_t Integer) {
393 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000394 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000395 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000396}
397
Devang Patel2c4ceb12009-11-21 02:48:08 +0000398/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000399///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000400void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000401 unsigned Form, int64_t Integer) {
402 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000403 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000404 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000405}
406
Devang Patel69f57b12009-12-02 15:25:16 +0000407/// addString - Add a string attribute data and value. DIEString only
408/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000409void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattner4cf202b2010-01-23 03:11:46 +0000410 StringRef String) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000411 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000412 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000413}
414
Devang Patel2c4ceb12009-11-21 02:48:08 +0000415/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000416///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000417void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000418 const MCSymbol *Label) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000419 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000420 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000421}
422
Devang Patel2c4ceb12009-11-21 02:48:08 +0000423/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000424///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000425void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000426 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000427 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000428 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000429}
430
Chris Lattner74e41f92010-04-05 05:24:55 +0000431/// addDIEEntry - Add a DIE attribute data and value.
432///
433void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
434 DIE *Entry) {
435 Die->addValue(Attribute, Form, createDIEEntry(Entry));
436}
437
438
Devang Patel2c4ceb12009-11-21 02:48:08 +0000439/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000440///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000441void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000442 DIEBlock *Block) {
Chris Lattnera37d5382010-04-05 00:18:22 +0000443 Block->ComputeSize(Asm);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000444 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000445 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000446}
447
Devang Patel2c4ceb12009-11-21 02:48:08 +0000448/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000449/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000450void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Devang Patelc665a512010-05-07 23:33:41 +0000451 // Verify variable.
452 if (!V->Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000453 return;
454
455 unsigned Line = V->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000456 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
457 V->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000458 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000459 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
460 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000461}
462
Devang Patel2c4ceb12009-11-21 02:48:08 +0000463/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000464/// entry.
Devang Patela49d8772010-05-07 23:19:07 +0000465void DwarfDebug::addSourceLine(DIE *Die, const DIGlobalVariable *G) {
Devang Patelc665a512010-05-07 23:33:41 +0000466 // Verify global variable.
467 if (!G->Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000468 return;
469
470 unsigned Line = G->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000471 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
472 G->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000473 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000474 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
475 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000476}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000477
Devang Patel2c4ceb12009-11-21 02:48:08 +0000478/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000479/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000480void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patelc665a512010-05-07 23:33:41 +0000481 // Verify subprogram.
482 if (!SP->Verify())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000483 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000484 // If the line number is 0, don't add it.
485 if (SP->getLineNumber() == 0)
486 return;
487
Devang Patel82dfc0c2009-08-31 22:47:13 +0000488 unsigned Line = SP->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000489 if (!SP->getContext().Verify())
490 return;
Devang Patel9bb59a22010-03-24 21:30:35 +0000491 unsigned FileID = GetOrCreateSourceID(SP->getDirectory(),
492 SP->getFilename());
Devang Patel82dfc0c2009-08-31 22:47:13 +0000493 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000494 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
495 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000496}
497
Devang Patel2c4ceb12009-11-21 02:48:08 +0000498/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000499/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000500void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Devang Patelc665a512010-05-07 23:33:41 +0000501 // Verify type.
502 if (!Ty->Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000503 return;
504
505 unsigned Line = Ty->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000506 if (!Ty->getContext().Verify())
507 return;
508 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
509 Ty->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000510 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000511 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
512 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000513}
514
Devang Patel6404e4e2009-12-15 19:16:48 +0000515/// addSourceLine - Add location information to specified debug information
516/// entry.
517void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
Devang Patelc665a512010-05-07 23:33:41 +0000518 // Verify namespace.
519 if (!NS->Verify())
Devang Patel6404e4e2009-12-15 19:16:48 +0000520 return;
521
522 unsigned Line = NS->getLineNumber();
523 StringRef FN = NS->getFilename();
524 StringRef Dir = NS->getDirectory();
525
526 unsigned FileID = GetOrCreateSourceID(Dir, FN);
527 assert(FileID && "Invalid file id");
528 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
529 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
530}
531
Caroline Ticedc8f6042009-08-31 21:19:37 +0000532/* Byref variables, in Blocks, are declared by the programmer as
533 "SomeType VarName;", but the compiler creates a
534 __Block_byref_x_VarName struct, and gives the variable VarName
535 either the struct, or a pointer to the struct, as its type. This
536 is necessary for various behind-the-scenes things the compiler
537 needs to do with by-reference variables in blocks.
538
539 However, as far as the original *programmer* is concerned, the
540 variable should still have type 'SomeType', as originally declared.
541
542 The following function dives into the __Block_byref_x_VarName
543 struct to find the original type of the variable. This will be
544 passed back to the code generating the type for the Debug
545 Information Entry for the variable 'VarName'. 'VarName' will then
546 have the original type 'SomeType' in its debug information.
547
548 The original type 'SomeType' will be the type of the field named
549 'VarName' inside the __Block_byref_x_VarName struct.
550
551 NOTE: In order for this to not completely fail on the debugger
552 side, the Debug Information Entry for the variable VarName needs to
553 have a DW_AT_location that tells the debugger how to unwind through
554 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patel2c4ceb12009-11-21 02:48:08 +0000555 value of the variable. The function addBlockByrefType does this. */
Caroline Ticedc8f6042009-08-31 21:19:37 +0000556
557/// Find the type the programmer originally declared the variable to be
558/// and return that type.
559///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000560DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000561
562 DIType subType = Ty;
563 unsigned tag = Ty.getTag();
564
565 if (tag == dwarf::DW_TAG_pointer_type) {
Devang Patel2db49d72010-05-07 18:11:54 +0000566 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000567 subType = DTy.getTypeDerivedFrom();
568 }
569
Devang Patel2db49d72010-05-07 18:11:54 +0000570 DICompositeType blockStruct = DICompositeType(subType);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000571 DIArray Elements = blockStruct.getTypeArray();
572
Caroline Ticedc8f6042009-08-31 21:19:37 +0000573 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
574 DIDescriptor Element = Elements.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +0000575 DIDerivedType DT = DIDerivedType(Element);
Devang Patel65dbc902009-11-25 17:36:49 +0000576 if (Name == DT.getName())
Caroline Ticedc8f6042009-08-31 21:19:37 +0000577 return (DT.getTypeDerivedFrom());
578 }
579
580 return Ty;
581}
582
Devang Patel2c4ceb12009-11-21 02:48:08 +0000583/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000584/// and generate the DWARF information necessary to find the actual variable
585/// given the extra address information encoded in the DIVariable, starting from
586/// the starting location. Add the DWARF information to the die.
587///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000588void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000589 unsigned Attribute,
590 const MachineLocation &Location) {
591 const DIVariable &VD = DV->getVariable();
592 DIType Ty = VD.getType();
593
594 // Decode the original location, and use that as the start of the byref
595 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000596 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000597 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000598 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000599
600 if (Location.isReg()) {
601 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000602 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000603 } else {
604 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000605 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
606 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000607 }
608 } else {
609 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000610 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000611 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000612 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
613 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000614 }
615
Devang Patel2c4ceb12009-11-21 02:48:08 +0000616 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000617 }
618
619 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
620 uint64_t Element = VD.getAddrElement(i);
621
622 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000623 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
624 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000625 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000626 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000627 } else llvm_unreachable("unknown DIFactory Opcode");
628 }
629
630 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000631 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000632}
633
Caroline Ticedc8f6042009-08-31 21:19:37 +0000634/* Byref variables, in Blocks, are declared by the programmer as "SomeType
635 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
636 gives the variable VarName either the struct, or a pointer to the struct, as
637 its type. This is necessary for various behind-the-scenes things the
638 compiler needs to do with by-reference variables in Blocks.
639
640 However, as far as the original *programmer* is concerned, the variable
641 should still have type 'SomeType', as originally declared.
642
Devang Patel2c4ceb12009-11-21 02:48:08 +0000643 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000644 struct to find the original type of the variable, which is then assigned to
645 the variable's Debug Information Entry as its real type. So far, so good.
646 However now the debugger will expect the variable VarName to have the type
647 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000648 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000649 pointers and struct to find the actual variable of type SomeType.
650
651 The following function does just that. We start by getting
652 the "normal" location for the variable. This will be the location
653 of either the struct __Block_byref_x_VarName or the pointer to the
654 struct __Block_byref_x_VarName.
655
656 The struct will look something like:
657
658 struct __Block_byref_x_VarName {
659 ... <various fields>
660 struct __Block_byref_x_VarName *forwarding;
661 ... <various other fields>
662 SomeType VarName;
663 ... <maybe more fields>
664 };
665
666 If we are given the struct directly (as our starting point) we
667 need to tell the debugger to:
668
669 1). Add the offset of the forwarding field.
670
Dan Gohmanf451cb82010-02-10 16:03:48 +0000671 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000672 struct to use (the real one may have been copied onto the heap).
673
674 3). Add the offset for the field VarName, to find the actual variable.
675
676 If we started with a pointer to the struct, then we need to
677 dereference that pointer first, before the other steps.
678 Translating this into DWARF ops, we will need to append the following
679 to the current location description for the variable:
680
681 DW_OP_deref -- optional, if we start with a pointer
682 DW_OP_plus_uconst <forward_fld_offset>
683 DW_OP_deref
684 DW_OP_plus_uconst <varName_fld_offset>
685
686 That is what this function does. */
687
Devang Patel2c4ceb12009-11-21 02:48:08 +0000688/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000689/// provided, and generate the DWARF information necessary to find the
690/// actual Block variable (navigating the Block struct) based on the
691/// starting location. Add the DWARF information to the die. For
692/// more information, read large comment just above here.
693///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000694void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000695 unsigned Attribute,
696 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000697 const DIVariable &VD = DV->getVariable();
698 DIType Ty = VD.getType();
699 DIType TmpTy = Ty;
700 unsigned Tag = Ty.getTag();
701 bool isPointer = false;
702
Devang Patel65dbc902009-11-25 17:36:49 +0000703 StringRef varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000704
705 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patel2db49d72010-05-07 18:11:54 +0000706 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000707 TmpTy = DTy.getTypeDerivedFrom();
708 isPointer = true;
709 }
710
Devang Patel2db49d72010-05-07 18:11:54 +0000711 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000712
Daniel Dunbar00564992009-09-19 20:40:14 +0000713 // Find the __forwarding field and the variable field in the __Block_byref
714 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000715 DIArray Fields = blockStruct.getTypeArray();
716 DIDescriptor varField = DIDescriptor();
717 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000718
Daniel Dunbar00564992009-09-19 20:40:14 +0000719 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
720 DIDescriptor Element = Fields.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +0000721 DIDerivedType DT = DIDerivedType(Element);
Devang Patel65dbc902009-11-25 17:36:49 +0000722 StringRef fieldName = DT.getName();
723 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000724 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000725 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000726 varField = Element;
727 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000728
Daniel Dunbar00564992009-09-19 20:40:14 +0000729 // Get the offsets for the forwarding field and the variable field.
Chris Lattner1d65ba72010-03-31 06:06:37 +0000730 unsigned forwardingFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000731 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner1d65ba72010-03-31 06:06:37 +0000732 unsigned varFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000733 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000734
Mike Stump7e3720d2009-09-24 23:21:26 +0000735 // Decode the original location, and use that as the start of the byref
736 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000737 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar00564992009-09-19 20:40:14 +0000738 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000739 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000740
Daniel Dunbar00564992009-09-19 20:40:14 +0000741 if (Location.isReg()) {
742 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000743 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000744 else {
745 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000746 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
747 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000748 }
749 } else {
750 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000751 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000752 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000753 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
754 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000755 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000756
Devang Patel2c4ceb12009-11-21 02:48:08 +0000757 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000758 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000759
Mike Stump7e3720d2009-09-24 23:21:26 +0000760 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000761 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000762 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000763 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000764
Daniel Dunbar00564992009-09-19 20:40:14 +0000765 // Next add the offset for the '__forwarding' field:
766 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
767 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000768 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000769 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
770 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000771 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000772
Daniel Dunbar00564992009-09-19 20:40:14 +0000773 // Now dereference the __forwarding field to get to the real __Block_byref
774 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000775 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000776
Daniel Dunbar00564992009-09-19 20:40:14 +0000777 // Now that we've got the real __Block_byref... struct, add the offset
778 // for the variable's field to get to the location of the actual variable:
779 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000780 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000781 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
782 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000783 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000784
Daniel Dunbar00564992009-09-19 20:40:14 +0000785 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000786 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000787}
788
Devang Patel2c4ceb12009-11-21 02:48:08 +0000789/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000790/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000791void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000792 const MachineLocation &Location) {
Chris Lattnerd38fee82010-04-05 00:13:49 +0000793 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling0310d762009-05-15 09:23:25 +0000794 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000795 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bill Wendling0310d762009-05-15 09:23:25 +0000796
797 if (Location.isReg()) {
798 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000799 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000800 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000801 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
802 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000803 }
804 } else {
805 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000806 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000807 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000808 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
809 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000810 }
811
Devang Patel2c4ceb12009-11-21 02:48:08 +0000812 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000813 }
814
Devang Patel2c4ceb12009-11-21 02:48:08 +0000815 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000816}
817
Devang Patela43098d2010-04-28 01:03:09 +0000818/// addRegisterAddress - Add register location entry in variable DIE.
819bool DwarfDebug::addRegisterAddress(DIE *Die, DbgVariable *DV,
820 const MachineOperand &MO) {
821 assert (MO.isReg() && "Invalid machine operand!");
822 if (!MO.getReg())
823 return false;
824 MachineLocation Location;
825 Location.set(MO.getReg());
826 addAddress(Die, dwarf::DW_AT_location, Location);
827 if (MCSymbol *VS = DV->getDbgValueLabel())
828 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
829 return true;
830}
831
832/// addConstantValue - Add constant value entry in variable DIE.
833bool DwarfDebug::addConstantValue(DIE *Die, DbgVariable *DV,
834 const MachineOperand &MO) {
835 assert (MO.isImm() && "Invalid machine operand!");
836 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
837 unsigned Imm = MO.getImm();
838 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
839 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
840 if (MCSymbol *VS = DV->getDbgValueLabel())
841 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
842 return true;
843}
844
845/// addConstantFPValue - Add constant value entry in variable DIE.
846bool DwarfDebug::addConstantFPValue(DIE *Die, DbgVariable *DV,
847 const MachineOperand &MO) {
848 assert (MO.isFPImm() && "Invalid machine operand!");
849 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
850 APFloat FPImm = MO.getFPImm()->getValueAPF();
851
852 // Get the raw data form of the floating point.
853 const APInt FltVal = FPImm.bitcastToAPInt();
854 const char *FltPtr = (const char*)FltVal.getRawData();
855
856 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
857 bool LittleEndian = Asm->getTargetData().isLittleEndian();
858 int Incr = (LittleEndian ? 1 : -1);
859 int Start = (LittleEndian ? 0 : NumBytes - 1);
860 int Stop = (LittleEndian ? NumBytes : -1);
861
862 // Output the constant to DWARF one byte at a time.
863 for (; Start != Stop; Start += Incr)
864 addUInt(Block, 0, dwarf::DW_FORM_data1,
865 (unsigned char)0xFF & FltPtr[Start]);
866
867 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
868
869 if (MCSymbol *VS = DV->getDbgValueLabel())
870 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
871 VS);
872 return true;
873}
874
875
Devang Patelc366f832009-12-10 19:14:49 +0000876/// addToContextOwner - Add Die into the list of its context owner's children.
877void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3c91b052010-03-08 20:52:55 +0000878 if (Context.isType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000879 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patelc366f832009-12-10 19:14:49 +0000880 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000881 } else if (Context.isNameSpace()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000882 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel6404e4e2009-12-15 19:16:48 +0000883 ContextDIE->addChild(Die);
Devang Patel2db49d72010-05-07 18:11:54 +0000884 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context))
Devang Patelc366f832009-12-10 19:14:49 +0000885 ContextDIE->addChild(Die);
886 else
887 ModuleCU->addDie(Die);
888}
889
Devang Patel16ced732009-12-10 18:05:33 +0000890/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
891/// given DIType.
892DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel2db49d72010-05-07 18:11:54 +0000893 DIE *TyDIE = ModuleCU->getDIE(Ty);
Devang Patel16ced732009-12-10 18:05:33 +0000894 if (TyDIE)
895 return TyDIE;
896
897 // Create new type.
898 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2db49d72010-05-07 18:11:54 +0000899 ModuleCU->insertDIE(Ty, TyDIE);
Devang Patel16ced732009-12-10 18:05:33 +0000900 if (Ty.isBasicType())
Devang Patel2db49d72010-05-07 18:11:54 +0000901 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000902 else if (Ty.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +0000903 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000904 else {
905 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel2db49d72010-05-07 18:11:54 +0000906 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000907 }
908
Devang Patelc366f832009-12-10 19:14:49 +0000909 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000910 return TyDIE;
911}
912
Devang Patel2c4ceb12009-11-21 02:48:08 +0000913/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000914void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9c004872010-05-07 21:45:47 +0000915 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000916 return;
917
918 // Check for pre-existence.
Devang Patel2db49d72010-05-07 18:11:54 +0000919 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000920 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000921 if (Entry) {
922 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000923 return;
924 }
925
Bill Wendling0310d762009-05-15 09:23:25 +0000926 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000927 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000928
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000929 // Set up proxy.
930 Entry = createDIEEntry(Buffer);
Devang Patel2db49d72010-05-07 18:11:54 +0000931 ModuleCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000932
Devang Patel2c4ceb12009-11-21 02:48:08 +0000933 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000934}
935
Devang Patel2c4ceb12009-11-21 02:48:08 +0000936/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000937void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000938 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000939 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000940 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000941 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000942 BTy.getEncoding());
943
944 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000945 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000946 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000947 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000948 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000949}
950
Devang Patel2c4ceb12009-11-21 02:48:08 +0000951/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000952void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000953 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000954 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000955 uint64_t Size = DTy.getSizeInBits() >> 3;
956 unsigned Tag = DTy.getTag();
957
958 // FIXME - Workaround for templates.
959 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
960
961 Buffer.setTag(Tag);
962
963 // Map to main type, void will not have a type.
964 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +0000965 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000966
967 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000968 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000969 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000970
971 // Add size if non-zero (derived types might be zero-sized.)
972 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000973 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000974
975 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +0000976 if (!DTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000977 addSourceLine(&Buffer, &DTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000978}
979
Devang Patel2c4ceb12009-11-21 02:48:08 +0000980/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +0000981void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000982 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000983 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000984
985 uint64_t Size = CTy.getSizeInBits() >> 3;
986 unsigned Tag = CTy.getTag();
987 Buffer.setTag(Tag);
988
989 switch (Tag) {
990 case dwarf::DW_TAG_vector_type:
991 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +0000992 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000993 break;
994 case dwarf::DW_TAG_enumeration_type: {
995 DIArray Elements = CTy.getTypeArray();
996
997 // Add enumerators to enumeration type.
998 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
999 DIE *ElemDie = NULL;
Devang Patel2db49d72010-05-07 18:11:54 +00001000 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3c91b052010-03-08 20:52:55 +00001001 if (Enum.isEnumerator()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001002 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001003 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +00001004 }
Bill Wendling0310d762009-05-15 09:23:25 +00001005 }
1006 }
1007 break;
1008 case dwarf::DW_TAG_subroutine_type: {
1009 // Add return type.
1010 DIArray Elements = CTy.getTypeArray();
1011 DIDescriptor RTy = Elements.getElement(0);
Devang Patel2db49d72010-05-07 18:11:54 +00001012 addType(&Buffer, DIType(RTy));
Bill Wendling0310d762009-05-15 09:23:25 +00001013
1014 // Add prototype flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001015 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001016
1017 // Add arguments.
1018 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1019 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1020 DIDescriptor Ty = Elements.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +00001021 addType(Arg, DIType(Ty));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001022 Buffer.addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001023 }
1024 }
1025 break;
1026 case dwarf::DW_TAG_structure_type:
1027 case dwarf::DW_TAG_union_type:
1028 case dwarf::DW_TAG_class_type: {
1029 // Add elements to structure type.
1030 DIArray Elements = CTy.getTypeArray();
1031
1032 // A forward struct declared type may not have elements available.
Devang Patel3c91b052010-03-08 20:52:55 +00001033 unsigned N = Elements.getNumElements();
1034 if (N == 0)
Bill Wendling0310d762009-05-15 09:23:25 +00001035 break;
1036
1037 // Add elements to structure type.
Devang Patel3c91b052010-03-08 20:52:55 +00001038 for (unsigned i = 0; i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001039 DIDescriptor Element = Elements.getElement(i);
1040 DIE *ElemDie = NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001041 if (Element.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001042 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel3c91b052010-03-08 20:52:55 +00001043 else if (Element.isVariable()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001044 DIVariable DV(Element);
Devang Patel1ee0cb92010-01-30 01:08:30 +00001045 ElemDie = new DIE(dwarf::DW_TAG_variable);
1046 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1047 DV.getName());
1048 addType(ElemDie, DV.getType());
1049 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1050 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1051 addSourceLine(ElemDie, &DV);
Devang Patel3c91b052010-03-08 20:52:55 +00001052 } else if (Element.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001053 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3c91b052010-03-08 20:52:55 +00001054 else
1055 continue;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001056 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001057 }
1058
Devang Patela1ba2692009-08-27 23:51:51 +00001059 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001060 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001061
1062 unsigned RLang = CTy.getRunTimeLang();
1063 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001064 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +00001065 dwarf::DW_FORM_data1, RLang);
Devang Patelb5544992010-01-26 21:16:06 +00001066
1067 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel2db49d72010-05-07 18:11:54 +00001068 if (DIDescriptor(ContainingType).isCompositeType())
Devang Patelb5544992010-01-26 21:16:06 +00001069 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patel2db49d72010-05-07 18:11:54 +00001070 getOrCreateTypeDIE(DIType(ContainingType)));
Bill Wendling0310d762009-05-15 09:23:25 +00001071 break;
1072 }
1073 default:
1074 break;
1075 }
1076
1077 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +00001078 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001079 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001080
Devang Patel8cbb6122010-01-29 18:34:58 +00001081 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendling0310d762009-05-15 09:23:25 +00001082 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1083 // Add size if non-zero (derived types might be zero-sized.)
1084 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001085 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001086 else {
1087 // Add zero size if it is not a forward declaration.
1088 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001089 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001090 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001091 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +00001092 }
1093
1094 // Add source line info if available.
1095 if (!CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001096 addSourceLine(&Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001097 }
1098}
1099
Devang Patel2c4ceb12009-11-21 02:48:08 +00001100/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1101void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +00001102 int64_t L = SR.getLo();
1103 int64_t H = SR.getHi();
1104 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1105
Devang Patel2c4ceb12009-11-21 02:48:08 +00001106 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +00001107 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001108 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +00001109 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001110
Devang Patel2c4ceb12009-11-21 02:48:08 +00001111 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +00001112}
1113
Devang Patel2c4ceb12009-11-21 02:48:08 +00001114/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001115void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +00001116 DICompositeType *CTy) {
1117 Buffer.setTag(dwarf::DW_TAG_array_type);
1118 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001119 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001120
1121 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +00001122 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001123 DIArray Elements = CTy->getTypeArray();
1124
Devang Patel6f01d9c2009-11-21 00:31:03 +00001125 // Get an anonymous type for index type.
Devang Patel8a241142009-12-09 18:24:21 +00001126 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +00001127 if (!IdxTy) {
1128 // Construct an anonymous type for index type.
1129 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001130 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1131 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001132 dwarf::DW_ATE_signed);
Devang Patel8a241142009-12-09 18:24:21 +00001133 ModuleCU->addDie(IdxTy);
1134 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001135 }
Bill Wendling0310d762009-05-15 09:23:25 +00001136
1137 // Add subranges to array type.
1138 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1139 DIDescriptor Element = Elements.getElement(i);
1140 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2db49d72010-05-07 18:11:54 +00001141 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001142 }
1143}
1144
Devang Patel2c4ceb12009-11-21 02:48:08 +00001145/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3c91b052010-03-08 20:52:55 +00001146DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001147 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3c91b052010-03-08 20:52:55 +00001148 StringRef Name = ETy.getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001149 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3c91b052010-03-08 20:52:55 +00001150 int64_t Value = ETy.getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001151 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001152 return Enumerator;
1153}
1154
Devang Patel351ca332010-01-05 01:46:14 +00001155/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1156/// printer to not emit usual symbol prefix before the symbol name is used then
1157/// return linkage name after skipping this special LLVM prefix.
1158static StringRef getRealLinkageName(StringRef LinkageName) {
1159 char One = '\1';
1160 if (LinkageName.startswith(StringRef(&One, 1)))
1161 return LinkageName.substr(1);
1162 return LinkageName;
1163}
1164
Devang Patel2c4ceb12009-11-21 02:48:08 +00001165/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patel8a241142009-12-09 18:24:21 +00001166DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001167 // If the global variable was optmized out then no need to create debug info
1168 // entry.
Devang Patel84c73e92009-11-06 17:58:12 +00001169 if (!GV.getGlobal()) return NULL;
Devang Patel65dbc902009-11-25 17:36:49 +00001170 if (GV.getDisplayName().empty()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001171
Bill Wendling0310d762009-05-15 09:23:25 +00001172 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001173 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001174 GV.getDisplayName());
1175
Devang Patel65dbc902009-11-25 17:36:49 +00001176 StringRef LinkageName = GV.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001177 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001178 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001179 getRealLinkageName(LinkageName));
1180
Devang Patel8a241142009-12-09 18:24:21 +00001181 addType(GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001182 if (!GV.isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001183 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1184 addSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001185
Bill Wendling0310d762009-05-15 09:23:25 +00001186 return GVDie;
1187}
1188
Devang Patel2c4ceb12009-11-21 02:48:08 +00001189/// createMemberDIE - Create new member DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001190DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001191 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001192 StringRef Name = DT.getName();
1193 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001194 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel65dbc902009-11-25 17:36:49 +00001195
Devang Patel8a241142009-12-09 18:24:21 +00001196 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001197
Devang Patel2c4ceb12009-11-21 02:48:08 +00001198 addSourceLine(MemberDie, &DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001199
Benjamin Kramer345ef342010-03-31 19:34:01 +00001200 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001201 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001202
Bill Wendling0310d762009-05-15 09:23:25 +00001203 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001204 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001205
1206 if (Size != FieldSize) {
1207 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001208 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1209 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001210
1211 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling0310d762009-05-15 09:23:25 +00001212 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1213 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer3d594fd2010-01-07 17:50:57 +00001214 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling0310d762009-05-15 09:23:25 +00001215 Offset -= FieldOffset;
1216
1217 // Maybe we need to work from the other end.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001218 if (Asm->getTargetData().isLittleEndian())
1219 Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001220 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001221
Devang Patel33db5082009-11-04 22:06:12 +00001222 // Here WD_AT_data_member_location points to the anonymous
1223 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001224 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001225
1226 } else
1227 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001228 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001229
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001230 if (DT.getTag() == dwarf::DW_TAG_inheritance
1231 && DT.isVirtual()) {
1232
1233 // For C++, virtual base classes are not at fixed offset. Use following
1234 // expression to extract appropriate offset from vtable.
1235 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1236
Benjamin Kramer345ef342010-03-31 19:34:01 +00001237 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001238 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1239 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1240 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1241 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1242 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1243 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1244 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1245
1246 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1247 VBaseLocationDie);
1248 } else
1249 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001250
1251 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001252 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001253 dwarf::DW_ACCESS_protected);
1254 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001255 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001256 dwarf::DW_ACCESS_private);
Devang Patel5d11eb02009-12-03 19:11:07 +00001257 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1258 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1259 dwarf::DW_ACCESS_public);
1260 if (DT.isVirtual())
1261 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1262 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001263 return MemberDie;
1264}
1265
Devang Patelffe966c2009-12-14 16:18:45 +00001266/// createSubprogramDIE - Create new DIE using SP.
1267DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
Devang Patel2db49d72010-05-07 18:11:54 +00001268 DIE *SPDie = ModuleCU->getDIE(SP);
Devang Patelffe966c2009-12-14 16:18:45 +00001269 if (SPDie)
1270 return SPDie;
1271
1272 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1eac3e72010-03-02 17:58:15 +00001273 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +00001274 if (!SP.getName().empty())
1275 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001276
Devang Patel65dbc902009-11-25 17:36:49 +00001277 StringRef LinkageName = SP.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001278 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001279 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001280 getRealLinkageName(LinkageName));
1281
Devang Patel2c4ceb12009-11-21 02:48:08 +00001282 addSourceLine(SPDie, &SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001283
Bill Wendling0310d762009-05-15 09:23:25 +00001284 // Add prototyped tag, if C or ObjC.
1285 unsigned Lang = SP.getCompileUnit().getLanguage();
1286 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1287 Lang == dwarf::DW_LANG_ObjC)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001288 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001289
1290 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001291 DICompositeType SPTy = SP.getType();
1292 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001293 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001294
Devang Patel3c91b052010-03-08 20:52:55 +00001295 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001296 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001297 else
Devang Patel2db49d72010-05-07 18:11:54 +00001298 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001299
Devang Patel5d11eb02009-12-03 19:11:07 +00001300 unsigned VK = SP.getVirtuality();
1301 if (VK) {
1302 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001303 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel5d11eb02009-12-03 19:11:07 +00001304 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1305 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1306 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel622b0262010-01-19 06:19:05 +00001307 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel2db49d72010-05-07 18:11:54 +00001308 SP.getContainingType()));
Devang Patel5d11eb02009-12-03 19:11:07 +00001309 }
1310
Devang Patelffe966c2009-12-14 16:18:45 +00001311 if (MakeDecl || !SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001312 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001313
1314 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001315 // be handled while processing variables.
1316 DICompositeType SPTy = SP.getType();
1317 DIArray Args = SPTy.getTypeArray();
1318 unsigned SPTag = SPTy.getTag();
1319
Bill Wendling0310d762009-05-15 09:23:25 +00001320 if (SPTag == dwarf::DW_TAG_subroutine_type)
1321 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1322 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001323 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelb4645642010-02-06 01:02:37 +00001324 addType(Arg, ATy);
1325 if (ATy.isArtificial())
1326 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001327 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001328 }
1329 }
1330
Devang Patel4e0d19d2010-02-03 19:57:19 +00001331 if (SP.isArtificial())
1332 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1333
Devang Patelccff8122010-04-30 19:38:23 +00001334 if (!SP.isLocalToUnit())
1335 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel2a4a3b72010-04-19 19:14:02 +00001336
Devang Patelccff8122010-04-30 19:38:23 +00001337 if (SP.isOptimized())
1338 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1339
Devang Patelccff8122010-04-30 19:38:23 +00001340 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel2db49d72010-05-07 18:11:54 +00001341 ModuleCU->insertDIE(SP, SPDie);
Devang Patelccff8122010-04-30 19:38:23 +00001342
Bill Wendling0310d762009-05-15 09:23:25 +00001343 return SPDie;
1344}
1345
Devang Patele9f8f5e2010-05-07 20:54:48 +00001346DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattnered7a77b2010-03-31 05:36:29 +00001347 assert(N && "Invalid Scope encoding!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001348
1349 DbgScope *AScope = AbstractScopes.lookup(N);
1350 if (AScope)
1351 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001352
Devang Patel53bb5c92009-11-10 23:06:00 +00001353 DbgScope *Parent = NULL;
1354
1355 DIDescriptor Scope(N);
1356 if (Scope.isLexicalBlock()) {
1357 DILexicalBlock DB(N);
1358 DIDescriptor ParentDesc = DB.getContext();
Devang Patel2db49d72010-05-07 18:11:54 +00001359 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel53bb5c92009-11-10 23:06:00 +00001360 }
1361
1362 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1363
1364 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001365 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001366 AScope->setAbstractScope();
1367 AbstractScopes[N] = AScope;
1368 if (DIDescriptor(N).isSubprogram())
1369 AbstractScopesList.push_back(AScope);
1370 return AScope;
1371}
Devang Patelaf9e8472009-10-01 20:31:14 +00001372
Devang Patel5f094002010-04-06 23:53:48 +00001373/// isSubprogramContext - Return true if Context is either a subprogram
1374/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001375static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +00001376 if (!Context)
1377 return false;
1378 DIDescriptor D(Context);
1379 if (D.isSubprogram())
1380 return true;
1381 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001382 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +00001383 return false;
1384}
1385
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001386/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001387/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1388/// If there are global variables in this scope then create and insert
1389/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001390DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001391 DIE *SPDie = ModuleCU->getDIE(SPNode);
1392 assert(SPDie && "Unable to find subprogram DIE!");
1393 DISubprogram SP(SPNode);
Chris Lattner206d61e2010-03-13 07:26:18 +00001394
Chris Lattnerd38fee82010-04-05 00:13:49 +00001395 // There is not any need to generate specification DIE for a function
1396 // defined at compile unit level. If a function is defined inside another
1397 // function then gdb prefers the definition at top level and but does not
1398 // expect specification DIE in parent function. So avoid creating
1399 // specification DIE for a function defined inside a function.
1400 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Devang Patel5f094002010-04-06 23:53:48 +00001401 !SP.getContext().isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +00001402 !isSubprogramContext(SP.getContext())) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001403 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1404
1405 // Add arguments.
1406 DICompositeType SPTy = SP.getType();
1407 DIArray Args = SPTy.getTypeArray();
1408 unsigned SPTag = SPTy.getTag();
1409 if (SPTag == dwarf::DW_TAG_subroutine_type)
1410 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1411 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001412 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattnerd38fee82010-04-05 00:13:49 +00001413 addType(Arg, ATy);
1414 if (ATy.isArtificial())
1415 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1416 SPDie->addChild(Arg);
1417 }
1418 DIE *SPDeclDie = SPDie;
1419 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1420 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1421 SPDeclDie);
1422 ModuleCU->addDie(SPDie);
1423 }
1424
1425 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1426 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1427 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1428 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1429 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1430 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1431 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +00001432
Chris Lattnerd38fee82010-04-05 00:13:49 +00001433 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +00001434}
1435
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001436/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001437/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1438DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001439
1440 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1441 if (Scope->isAbstractScope())
1442 return ScopeDIE;
1443
1444 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1445 if (Ranges.empty())
1446 return 0;
1447
1448 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1449 if (Ranges.size() > 1) {
1450 // .debug_range section has not been laid out yet. Emit offset in
1451 // .debug_range as a uint, size 4, for now. emitDIE will handle
1452 // DW_AT_ranges appropriately.
1453 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1454 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1455 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1456 RE = Ranges.end(); RI != RE; ++RI) {
1457 DebugRangeSymbols.push_back(LabelsBeforeInsn.lookup(RI->first));
1458 DebugRangeSymbols.push_back(LabelsAfterInsn.lookup(RI->second));
1459 }
1460 DebugRangeSymbols.push_back(NULL);
1461 DebugRangeSymbols.push_back(NULL);
1462 return ScopeDIE;
1463 }
1464
1465 MCSymbol *Start = LabelsBeforeInsn.lookup(RI->first);
1466 MCSymbol *End = LabelsAfterInsn.lookup(RI->second);
1467
Devang Patelaead63c2010-03-29 22:59:58 +00001468 if (Start == 0 || End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +00001469
Chris Lattnerb7db7332010-03-09 01:58:53 +00001470 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1471 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattnera34ec2292010-03-09 01:51:43 +00001472
Devang Pateleac9c072010-04-27 19:46:33 +00001473 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1474 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +00001475
1476 return ScopeDIE;
1477}
1478
Devang Patel2c4ceb12009-11-21 02:48:08 +00001479/// constructInlinedScopeDIE - This scope represents inlined body of
1480/// a function. Construct DIE to represent this concrete inlined copy
1481/// of the function.
1482DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001483
1484 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1485 assert (Ranges.empty() == false
1486 && "DbgScope does not have instruction markers!");
1487
1488 // FIXME : .debug_inlined section specification does not clearly state how
1489 // to emit inlined scope that is split into multiple instruction ranges.
1490 // For now, use first instruction range and emit low_pc/high_pc pair and
1491 // corresponding .debug_inlined section entry for this pair.
1492 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1493 MCSymbol *StartLabel = LabelsBeforeInsn.lookup(RI->first);
1494 MCSymbol *EndLabel = LabelsAfterInsn.lookup(RI->second);
1495
1496 if (StartLabel == 0 || EndLabel == 0) {
1497 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1498 return 0;
1499 }
Chris Lattnerb7db7332010-03-09 01:58:53 +00001500 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001501 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +00001502 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001503 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +00001504
Devang Patel3c91b052010-03-08 20:52:55 +00001505 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +00001506 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001507 DIScope DS(Scope->getScopeNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001508 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1509
Devang Patel2db49d72010-05-07 18:11:54 +00001510 DISubprogram InlinedSP = getDISubprogram(DS);
1511 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001512 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001513 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001514 dwarf::DW_FORM_ref4, OriginDIE);
1515
Chris Lattner6ed0f902010-03-09 00:31:02 +00001516 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnerb7db7332010-03-09 01:58:53 +00001517 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel53bb5c92009-11-10 23:06:00 +00001518
1519 InlinedSubprogramDIEs.insert(OriginDIE);
1520
1521 // Track the start label for this inlined function.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001522 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +00001523 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001524
1525 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001526 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001527 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +00001528 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001529 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +00001530 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001531
Devang Patel53bb5c92009-11-10 23:06:00 +00001532 DILocation DL(Scope->getInlinedAt());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001533 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1534 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001535
1536 return ScopeDIE;
1537}
1538
Devang Patel2c4ceb12009-11-21 02:48:08 +00001539
1540/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001541DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001542 // Get the descriptor.
1543 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001544 StringRef Name = VD.getName();
1545 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001546 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001547
1548 // Translate tag to proper Dwarf tag. The result variable is dropped for
1549 // now.
1550 unsigned Tag;
1551 switch (VD.getTag()) {
1552 case dwarf::DW_TAG_return_variable:
1553 return NULL;
1554 case dwarf::DW_TAG_arg_variable:
1555 Tag = dwarf::DW_TAG_formal_parameter;
1556 break;
1557 case dwarf::DW_TAG_auto_variable: // fall thru
1558 default:
1559 Tag = dwarf::DW_TAG_variable;
1560 break;
1561 }
1562
1563 // Define variable debug information entry.
1564 DIE *VariableDie = new DIE(Tag);
1565
1566
1567 DIE *AbsDIE = NULL;
1568 if (DbgVariable *AV = DV->getAbstractVariable())
1569 AbsDIE = AV->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001570
Devang Patel53bb5c92009-11-10 23:06:00 +00001571 if (AbsDIE) {
1572 DIScope DS(Scope->getScopeNode());
Devang Patel2db49d72010-05-07 18:11:54 +00001573 DISubprogram InlinedSP = getDISubprogram(DS);
1574 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP);
Daniel Dunbarc0326792009-11-11 03:09:50 +00001575 (void) OriginSPDIE;
Chris Lattnered7a77b2010-03-31 05:36:29 +00001576 assert(OriginSPDIE && "Unable to find Origin DIE for the SP!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001577 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
Chris Lattnered7a77b2010-03-31 05:36:29 +00001578 assert(AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001579 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001580 dwarf::DW_FORM_ref4, AbsDIE);
1581 }
1582 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001583 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1584 addSourceLine(VariableDie, &VD);
Devang Patel53bb5c92009-11-10 23:06:00 +00001585
1586 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001587 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001588 // addresses instead.
1589 if (VD.isBlockByrefVariable())
Devang Patel8a241142009-12-09 18:24:21 +00001590 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel53bb5c92009-11-10 23:06:00 +00001591 else
Devang Patel8a241142009-12-09 18:24:21 +00001592 addType(VariableDie, VD.getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001593 }
1594
1595 // Add variable address.
1596 if (!Scope->isAbstractScope()) {
Devang Patel90a48ad2010-03-15 18:33:46 +00001597 // Check if variable is described by DBG_VALUE instruction.
Devang Patela43098d2010-04-28 01:03:09 +00001598 if (const MachineInstr *DVInsn = DV->getDbgValue()) {
1599 bool updated = false;
1600 // FIXME : Handle getNumOperands != 3
1601 if (DVInsn->getNumOperands() == 3) {
1602 if (DVInsn->getOperand(0).isReg())
1603 updated = addRegisterAddress(VariableDie, DV, DVInsn->getOperand(0));
1604 else if (DVInsn->getOperand(0).isImm())
1605 updated = addConstantValue(VariableDie, DV, DVInsn->getOperand(0));
1606 else if (DVInsn->getOperand(0).isFPImm())
1607 updated = addConstantFPValue(VariableDie, DV, DVInsn->getOperand(0));
Devang Patel28ff35d2010-04-28 01:39:28 +00001608 } else {
1609 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1610 if (Location.getReg()) {
1611 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1612 if (MCSymbol *VS = DV->getDbgValueLabel())
1613 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1614 VS);
1615 updated = true;
1616 }
Devang Patela43098d2010-04-28 01:03:09 +00001617 }
1618 if (!updated) {
1619 // If variableDie is not updated then DBG_VALUE instruction does not
1620 // have valid variable info.
1621 delete VariableDie;
1622 return NULL;
1623 }
1624 }
1625 else {
Devang Patel90a48ad2010-03-15 18:33:46 +00001626 MachineLocation Location;
1627 unsigned FrameReg;
Chris Lattnerd38fee82010-04-05 00:13:49 +00001628 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1629 int Offset = RI->getFrameIndexReference(*Asm->MF, DV->getFrameIndex(),
Chris Lattner1d65ba72010-03-31 06:06:37 +00001630 FrameReg);
Devang Patel90a48ad2010-03-15 18:33:46 +00001631 Location.set(FrameReg, Offset);
1632
1633 if (VD.hasComplexAddress())
1634 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1635 else if (VD.isBlockByrefVariable())
1636 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1637 else
1638 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1639 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001640 }
Devang Patelb4645642010-02-06 01:02:37 +00001641
1642 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1643 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel53bb5c92009-11-10 23:06:00 +00001644 DV->setDIE(VariableDie);
1645 return VariableDie;
1646
1647}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001648
Devang Patel193f7202009-11-24 01:14:22 +00001649void DwarfDebug::addPubTypes(DISubprogram SP) {
1650 DICompositeType SPTy = SP.getType();
1651 unsigned SPTag = SPTy.getTag();
1652 if (SPTag != dwarf::DW_TAG_subroutine_type)
1653 return;
1654
1655 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +00001656 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel2db49d72010-05-07 18:11:54 +00001657 DIType ATy(Args.getElement(i));
Devang Patel9c004872010-05-07 21:45:47 +00001658 if (!ATy.Verify())
Devang Patel193f7202009-11-24 01:14:22 +00001659 continue;
1660 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel2db49d72010-05-07 18:11:54 +00001661 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel50d80e32010-04-13 20:35:04 +00001662 && !CATy.isForwardDecl()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001663 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy))
Devang Patel193f7202009-11-24 01:14:22 +00001664 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1665 }
1666 }
1667}
1668
Devang Patel2c4ceb12009-11-21 02:48:08 +00001669/// constructScopeDIE - Construct a DIE for this scope.
1670DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +00001671 if (!Scope || !Scope->getScopeNode())
1672 return NULL;
1673
1674 DIScope DS(Scope->getScopeNode());
1675 DIE *ScopeDIE = NULL;
1676 if (Scope->getInlinedAt())
1677 ScopeDIE = constructInlinedScopeDIE(Scope);
1678 else if (DS.isSubprogram()) {
1679 if (Scope->isAbstractScope())
Devang Patel2db49d72010-05-07 18:11:54 +00001680 ScopeDIE = ModuleCU->getDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +00001681 else
Devang Patel2db49d72010-05-07 18:11:54 +00001682 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +00001683 }
Devang Patelaead63c2010-03-29 22:59:58 +00001684 else
Devang Patel3c91b052010-03-08 20:52:55 +00001685 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patelaead63c2010-03-29 22:59:58 +00001686 if (!ScopeDIE) return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001687
Devang Patel53bb5c92009-11-10 23:06:00 +00001688 // Add variables to scope.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001689 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel53bb5c92009-11-10 23:06:00 +00001690 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001691 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001692 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001693 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001694 }
1695
1696 // Add nested scopes.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001697 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel53bb5c92009-11-10 23:06:00 +00001698 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1699 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001700 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001701 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001702 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001703 }
Devang Patel193f7202009-11-24 01:14:22 +00001704
1705 if (DS.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001706 addPubTypes(DISubprogram(DS));
Devang Patel193f7202009-11-24 01:14:22 +00001707
1708 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001709}
1710
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001711/// GetOrCreateSourceID - Look up the source id with the given directory and
1712/// source file names. If none currently exists, create a new id and insert it
1713/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1714/// maps as well.
Chris Lattner1d65ba72010-03-31 06:06:37 +00001715unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001716 unsigned DId;
1717 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1718 if (DI != DirectoryIdMap.end()) {
1719 DId = DI->getValue();
1720 } else {
1721 DId = DirectoryNames.size() + 1;
1722 DirectoryIdMap[DirName] = DId;
1723 DirectoryNames.push_back(DirName);
1724 }
1725
1726 unsigned FId;
1727 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1728 if (FI != SourceFileIdMap.end()) {
1729 FId = FI->getValue();
1730 } else {
1731 FId = SourceFileNames.size() + 1;
1732 SourceFileIdMap[FileName] = FId;
1733 SourceFileNames.push_back(FileName);
1734 }
1735
1736 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1737 SourceIdMap.find(std::make_pair(DId, FId));
1738 if (SI != SourceIdMap.end())
1739 return SI->second;
1740
1741 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1742 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1743 SourceIds.push_back(std::make_pair(DId, FId));
1744
1745 return SrcId;
1746}
1747
Devang Patel6404e4e2009-12-15 19:16:48 +00001748/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1749DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel2db49d72010-05-07 18:11:54 +00001750 DIE *NDie = ModuleCU->getDIE(NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001751 if (NDie)
1752 return NDie;
1753 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel2db49d72010-05-07 18:11:54 +00001754 ModuleCU->insertDIE(NS, NDie);
Devang Patel6404e4e2009-12-15 19:16:48 +00001755 if (!NS.getName().empty())
1756 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1757 addSourceLine(NDie, &NS);
1758 addToContextOwner(NDie, NS.getContext());
1759 return NDie;
1760}
1761
Devang Patele9f8f5e2010-05-07 20:54:48 +00001762void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001763 DICompileUnit DIUnit(N);
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +00001764 // Use first compile unit marked as isMain as the compile unit for this
1765 // module.
1766 if (ModuleCU || !DIUnit.isMain())
1767 return;
Devang Patel65dbc902009-11-25 17:36:49 +00001768 StringRef FN = DIUnit.getFilename();
1769 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001770 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001771
1772 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001773 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001774 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001775 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001776 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001777 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +00001778 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1779 // simplifies debug range entries.
1780 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_data4, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +00001781 // DW_AT_stmt_list is a offset of line number information for this
1782 // compile unit in debug_line section. It is always zero when only one
1783 // compile unit is emitted in one object file.
1784 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001785
Devang Patel65dbc902009-11-25 17:36:49 +00001786 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001787 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001788 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001789 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001790
Devang Patel65dbc902009-11-25 17:36:49 +00001791 StringRef Flags = DIUnit.getFlags();
1792 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001793 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001794
1795 unsigned RVer = DIUnit.getRunTimeVersion();
1796 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001797 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001798 dwarf::DW_FORM_data1, RVer);
1799
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +00001800 assert(!ModuleCU &&
1801 "ModuleCU assigned since the top of constructCompileUnit");
1802 ModuleCU = new CompileUnit(ID, Die);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001803}
1804
Devang Patele9f8f5e2010-05-07 20:54:48 +00001805void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001806 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001807
Devang Patel905cf5e2009-09-04 23:59:07 +00001808 // If debug information is malformed then ignore it.
1809 if (DI_GV.Verify() == false)
1810 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001811
1812 // Check for pre-existence.
Devang Patel2db49d72010-05-07 18:11:54 +00001813 if (ModuleCU->getDIE(DI_GV))
Devang Patel13e16b62009-06-26 01:49:18 +00001814 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001815
Devang Patel8a241142009-12-09 18:24:21 +00001816 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Pateledb45632009-12-10 23:25:41 +00001817 if (!VariableDie)
1818 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001819
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001820 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001821 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001822
1823 // Add to context owner.
Devang Patelc9b16cc2010-01-15 01:12:22 +00001824 DIDescriptor GVContext = DI_GV.getContext();
1825 // Do not create specification DIE if context is either compile unit
1826 // or a subprogram.
Chris Lattner1d65ba72010-03-31 06:06:37 +00001827 if (DI_GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel5f094002010-04-06 23:53:48 +00001828 !GVContext.isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +00001829 !isSubprogramContext(GVContext)) {
Devang Patel6404e4e2009-12-15 19:16:48 +00001830 // Create specification DIE.
1831 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1832 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1833 dwarf::DW_FORM_ref4, VariableDie);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001834 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel6404e4e2009-12-15 19:16:48 +00001835 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner4faf59a2010-03-08 22:31:46 +00001836 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattnerdeb0cba2010-03-12 21:09:07 +00001837 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel6404e4e2009-12-15 19:16:48 +00001838 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel8581e012010-02-09 01:58:33 +00001839 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel6404e4e2009-12-15 19:16:48 +00001840 ModuleCU->addDie(VariableSpecDIE);
1841 } else {
Benjamin Kramer345ef342010-03-31 19:34:01 +00001842 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel6404e4e2009-12-15 19:16:48 +00001843 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner4faf59a2010-03-08 22:31:46 +00001844 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattnerdeb0cba2010-03-12 21:09:07 +00001845 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel6404e4e2009-12-15 19:16:48 +00001846 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1847 }
Devang Patelc9b16cc2010-01-15 01:12:22 +00001848 addToContextOwner(VariableDie, GVContext);
Devang Patelc366f832009-12-10 19:14:49 +00001849
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001850 // Expose as global. FIXME - need to check external flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001851 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel193f7202009-11-24 01:14:22 +00001852
1853 DIType GTy = DI_GV.getType();
Devang Patel50d80e32010-04-13 20:35:04 +00001854 if (GTy.isCompositeType() && !GTy.getName().empty()
1855 && !GTy.isForwardDecl()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001856 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001857 assert(Entry && "Missing global type!");
Devang Patel193f7202009-11-24 01:14:22 +00001858 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1859 }
Devang Patel13e16b62009-06-26 01:49:18 +00001860 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001861}
1862
Devang Patele9f8f5e2010-05-07 20:54:48 +00001863void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001864 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001865
Stuart Hastings639336e2010-04-06 21:38:29 +00001866 // Check for pre-existence.
1867 if (ModuleCU->getDIE(N))
1868 return;
1869
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001870 if (!SP.isDefinition())
1871 // This is a method declaration which will be handled while constructing
1872 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001873 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001874
Stuart Hastings639336e2010-04-06 21:38:29 +00001875 DIE *SubprogramDie = createSubprogramDIE(SP);
1876
1877 // Add to map.
1878 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001879
1880 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00001881 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00001882
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001883 // Expose as global.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001884 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001885
Devang Patel13e16b62009-06-26 01:49:18 +00001886 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001887}
1888
Devang Patel2c4ceb12009-11-21 02:48:08 +00001889/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001890/// content. Create global DIEs and emit initial debug info sections.
1891/// This is inovked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +00001892void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +00001893 if (DisableDebugInfoPrinting)
1894 return;
1895
Devang Patel78ab9e22009-07-30 18:56:46 +00001896 DebugInfoFinder DbgFinder;
1897 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001898
Chris Lattnerd850ac72010-04-05 02:19:28 +00001899 bool HasDebugInfo = false;
1900
1901 // Scan all the compile-units to see if there are any marked as the main unit.
1902 // if not, we do not generate debug info.
1903 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1904 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1905 if (DICompileUnit(*I).isMain()) {
1906 HasDebugInfo = true;
1907 break;
1908 }
1909 }
1910
1911 if (!HasDebugInfo) return;
1912
1913 // Tell MMI that we have debug info.
1914 MMI->setDebugInfoAvailability(true);
1915
Chris Lattnerbe15beb2010-04-04 23:17:54 +00001916 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +00001917 EmitSectionLabels();
Chris Lattnerbe15beb2010-04-04 23:17:54 +00001918
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001919 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001920 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1921 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001922 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001923
Devang Patel53bb5c92009-11-10 23:06:00 +00001924 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001925 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1926 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001927 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00001928
Devang Patelc366f832009-12-10 19:14:49 +00001929 // Create DIEs for each global variable.
1930 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1931 E = DbgFinder.global_variable_end(); I != E; ++I)
1932 constructGlobalVariableDIE(*I);
1933
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001934 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001935 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001936
1937 // Print out .file directives to specify files for .loc directives. These are
1938 // printed out early so that they precede any .loc directives.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001939 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001940 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1941 // Remember source id starts at 1.
1942 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001943 // FIXME: don't use sys::path for this! This should not depend on the
1944 // host.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001945 sys::Path FullPath(getSourceDirectoryName(Id.first));
1946 bool AppendOk =
1947 FullPath.appendComponent(getSourceFileName(Id.second));
1948 assert(AppendOk && "Could not append filename to directory!");
1949 AppendOk = false;
Chris Lattnera6594fc2010-01-25 18:58:59 +00001950 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001951 }
1952 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001953}
1954
Devang Patel2c4ceb12009-11-21 02:48:08 +00001955/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001956///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001957void DwarfDebug::endModule() {
Bill Wendling5f017e82010-04-07 09:28:04 +00001958 if (!ModuleCU) return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001959
Devang Patel53bb5c92009-11-10 23:06:00 +00001960 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1961 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1962 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1963 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001964 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00001965 }
1966
Devang Patele9f8f5e2010-05-07 20:54:48 +00001967 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00001968 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1969 DIE *SPDie = CI->first;
Devang Patele9f8f5e2010-05-07 20:54:48 +00001970 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel5d11eb02009-12-03 19:11:07 +00001971 if (!N) continue;
1972 DIE *NDie = ModuleCU->getDIE(N);
1973 if (!NDie) continue;
1974 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00001975 }
1976
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001977 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001978 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00001979 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001980 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00001981 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001982
1983 // End text sections.
1984 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001985 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +00001986 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001987 }
1988
1989 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001990 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001991
1992 // Emit function debug frame information
1993 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1994 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001995 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001996
1997 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001998 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001999
2000 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00002001 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002002
2003 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002004 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002005
2006 // Emit source line correspondence into a debug line section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002007 emitDebugLines();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002008
2009 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002010 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002011
Devang Patel193f7202009-11-24 01:14:22 +00002012 // Emit info into a debug pubtypes section.
2013 emitDebugPubTypes();
2014
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002015 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002016 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002017
2018 // Emit info into a debug aranges section.
2019 EmitDebugARanges();
2020
2021 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002022 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002023
2024 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002025 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002026
2027 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002028 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002029
Chris Lattnerbc733f52010-03-13 02:17:42 +00002030 // Emit info into a debug str section.
2031 emitDebugStr();
2032
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +00002033 delete ModuleCU;
2034 ModuleCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002035}
2036
Devang Patel53bb5c92009-11-10 23:06:00 +00002037/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002038DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2039 unsigned FrameIdx,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002040 DebugLoc ScopeLoc) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002041
Devang Patel2db49d72010-05-07 18:11:54 +00002042 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00002043 if (AbsDbgVariable)
2044 return AbsDbgVariable;
2045
Devang Patel2db49d72010-05-07 18:11:54 +00002046 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002047 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00002048 if (!Scope)
2049 return NULL;
2050
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002051 AbsDbgVariable = new DbgVariable(Var, FrameIdx,
2052 NULL /* No more-abstract variable*/);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002053 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00002054 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00002055 return AbsDbgVariable;
2056}
2057
Devang Patel90a48ad2010-03-15 18:33:46 +00002058/// findAbstractVariable - Find abstract variable, if any, associated with Var.
2059/// FIXME : Refactor findAbstractVariable.
2060DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2061 const MachineInstr *MI,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002062 DebugLoc ScopeLoc) {
Devang Patel90a48ad2010-03-15 18:33:46 +00002063
Devang Patel2db49d72010-05-07 18:11:54 +00002064 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel90a48ad2010-03-15 18:33:46 +00002065 if (AbsDbgVariable)
2066 return AbsDbgVariable;
2067
Devang Patel2db49d72010-05-07 18:11:54 +00002068 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002069 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel90a48ad2010-03-15 18:33:46 +00002070 if (!Scope)
2071 return NULL;
2072
Devang Patelaead63c2010-03-29 22:59:58 +00002073 AbsDbgVariable = new DbgVariable(Var, MI,
Devang Patel90a48ad2010-03-15 18:33:46 +00002074 NULL /* No more-abstract variable*/);
2075 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00002076 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelaead63c2010-03-29 22:59:58 +00002077 DbgValueStartMap[MI] = AbsDbgVariable;
Devang Patel90a48ad2010-03-15 18:33:46 +00002078 return AbsDbgVariable;
2079}
2080
Devang Patel2c4ceb12009-11-21 02:48:08 +00002081/// collectVariableInfo - Populate DbgScope entries with variables' info.
2082void DwarfDebug::collectVariableInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002083 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002084
Devang Patele717faa2009-10-06 01:26:37 +00002085 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2086 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2087 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00002088 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00002089 if (!Var) continue;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002090 DIVariable DV(Var);
2091 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002092
Chris Lattnerde4845c2010-04-02 19:42:39 +00002093 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002094 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00002095 Scope = ConcreteScopes.lookup(IA);
2096 if (Scope == 0)
2097 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
2098
Devang Patelfb0ee432009-11-10 23:20:04 +00002099 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002100 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00002101 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00002102
Chris Lattnerde4845c2010-04-02 19:42:39 +00002103 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, VP.second);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002104 DbgVariable *RegVar = new DbgVariable(DV, VP.first, AbsDbgVariable);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002105 Scope->addVariable(RegVar);
Devang Patele717faa2009-10-06 01:26:37 +00002106 }
Devang Patel90a48ad2010-03-15 18:33:46 +00002107
2108 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002109 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel90a48ad2010-03-15 18:33:46 +00002110 I != E; ++I) {
2111 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2112 II != IE; ++II) {
2113 const MachineInstr *MInsn = II;
Chris Lattner14d750d2010-03-31 05:39:57 +00002114 if (!MInsn->isDebugValue())
Devang Patel90a48ad2010-03-15 18:33:46 +00002115 continue;
Devang Patelaead63c2010-03-29 22:59:58 +00002116
Devang Patel0f9d9522010-04-27 20:54:45 +00002117 // Ignore Undef values.
Devang Patel97303ee2010-04-27 22:04:41 +00002118 if (MInsn->getOperand(0).isReg() && !MInsn->getOperand(0).getReg())
Devang Patel0f9d9522010-04-27 20:54:45 +00002119 continue;
2120
Dan Gohman82d5eaf2010-04-17 16:43:55 +00002121 DIVariable DV(
Devang Patele9f8f5e2010-05-07 20:54:48 +00002122 const_cast<const MDNode *>(MInsn->getOperand(MInsn->getNumOperands() - 1)
Dan Gohman82d5eaf2010-04-17 16:43:55 +00002123 .getMetadata()));
Devang Patel90a48ad2010-03-15 18:33:46 +00002124 if (DV.getTag() == dwarf::DW_TAG_arg_variable) {
2125 // FIXME Handle inlined subroutine arguments.
2126 DbgVariable *ArgVar = new DbgVariable(DV, MInsn, NULL);
2127 CurrentFnDbgScope->addVariable(ArgVar);
Devang Patelaead63c2010-03-29 22:59:58 +00002128 DbgValueStartMap[MInsn] = ArgVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00002129 continue;
2130 }
2131
2132 DebugLoc DL = MInsn->getDebugLoc();
2133 if (DL.isUnknown()) continue;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002134 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002135 if (const MDNode *IA = DL.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00002136 Scope = ConcreteScopes.lookup(IA);
2137 if (Scope == 0)
2138 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2139
Devang Patel90a48ad2010-03-15 18:33:46 +00002140 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002141 if (Scope == 0)
Devang Patel90a48ad2010-03-15 18:33:46 +00002142 continue;
2143
Chris Lattnerde4845c2010-04-02 19:42:39 +00002144 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, MInsn, DL);
Devang Patel90a48ad2010-03-15 18:33:46 +00002145 DbgVariable *RegVar = new DbgVariable(DV, MInsn, AbsDbgVariable);
Devang Patelaead63c2010-03-29 22:59:58 +00002146 DbgValueStartMap[MInsn] = RegVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00002147 Scope->addVariable(RegVar);
2148 }
2149 }
Devang Patele717faa2009-10-06 01:26:37 +00002150}
2151
Devang Patel553881b2010-03-29 17:20:31 +00002152/// beginScope - Process beginning of a scope.
2153void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patel553881b2010-03-29 17:20:31 +00002154 // Check location.
2155 DebugLoc DL = MI->getDebugLoc();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002156 if (DL.isUnknown()) {
Dan Gohman281d65d2010-05-07 01:08:53 +00002157 if (UnknownLocations) {
2158 // This instruction has no debug location. If the preceding instruction
2159 // did, emit debug location information to indicate that the debug
2160 // location is now unknown.
2161 MCSymbol *Label = NULL;
2162 if (DL == PrevInstLoc)
2163 Label = PrevLabel;
2164 else {
2165 Label = recordSourceLine(DL.getLine(), DL.getCol(), 0);
2166 PrevInstLoc = DL;
2167 PrevLabel = Label;
2168 }
Dan Gohman75395842010-05-06 00:29:41 +00002169
Dan Gohman281d65d2010-05-07 01:08:53 +00002170 // If this instruction begins a scope then note down corresponding label.
2171 if (InsnsBeginScopeSet.count(MI) != 0)
2172 LabelsBeforeInsn[MI] = Label;
2173 }
Dan Gohman75395842010-05-06 00:29:41 +00002174
Devang Patel553881b2010-03-29 17:20:31 +00002175 return;
Dan Gohman1cc0d622010-05-05 23:41:32 +00002176 }
Devang Patel553881b2010-03-29 17:20:31 +00002177
Devang Patele9f8f5e2010-05-07 20:54:48 +00002178 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Chris Lattnerde4845c2010-04-02 19:42:39 +00002179
2180 // FIXME: Should only verify each scope once!
2181 if (!DIScope(Scope).Verify())
Devang Patel553881b2010-03-29 17:20:31 +00002182 return;
Devang Patel553881b2010-03-29 17:20:31 +00002183
Devang Patelaead63c2010-03-29 22:59:58 +00002184 // DBG_VALUE instruction establishes new value.
Chris Lattner14d750d2010-03-31 05:39:57 +00002185 if (MI->isDebugValue()) {
Devang Patelaead63c2010-03-29 22:59:58 +00002186 DenseMap<const MachineInstr *, DbgVariable *>::iterator DI
2187 = DbgValueStartMap.find(MI);
2188 if (DI != DbgValueStartMap.end()) {
Devang Pateleac9c072010-04-27 19:46:33 +00002189 MCSymbol *Label = NULL;
2190 if (DL == PrevInstLoc)
2191 Label = PrevLabel;
2192 else {
2193 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2194 PrevInstLoc = DL;
2195 PrevLabel = Label;
2196 }
Devang Patelaead63c2010-03-29 22:59:58 +00002197 DI->second->setDbgValueLabel(Label);
2198 }
Devang Patel7ed63112010-03-30 18:07:00 +00002199 return;
Devang Patelaead63c2010-03-29 22:59:58 +00002200 }
2201
Devang Patel553881b2010-03-29 17:20:31 +00002202 // Emit a label to indicate location change. This is used for line
Devang Pateleac9c072010-04-27 19:46:33 +00002203 // table even if this instruction does not start a new scope.
2204 MCSymbol *Label = NULL;
2205 if (DL == PrevInstLoc)
2206 Label = PrevLabel;
2207 else {
2208 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2209 PrevInstLoc = DL;
2210 PrevLabel = Label;
2211 }
Devang Patel553881b2010-03-29 17:20:31 +00002212
Devang Patel1c246352010-04-08 16:50:29 +00002213 // If this instruction begins a scope then note down corresponding label.
2214 if (InsnsBeginScopeSet.count(MI) != 0)
Devang Pateleac9c072010-04-27 19:46:33 +00002215 LabelsBeforeInsn[MI] = Label;
Devang Patel0d20ac82009-10-06 01:50:42 +00002216}
2217
Devang Patel2c4ceb12009-11-21 02:48:08 +00002218/// endScope - Process end of a scope.
2219void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel1c246352010-04-08 16:50:29 +00002220 if (InsnsEndScopeSet.count(MI) != 0) {
2221 // Emit a label if this instruction ends a scope.
2222 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2223 Asm->OutStreamer.EmitLabel(Label);
Devang Pateleac9c072010-04-27 19:46:33 +00002224 LabelsAfterInsn[MI] = Label;
Devang Patel1c246352010-04-08 16:50:29 +00002225 }
Devang Patel53bb5c92009-11-10 23:06:00 +00002226}
2227
Devang Pateleac9c072010-04-27 19:46:33 +00002228/// getOrCreateDbgScope - Create DbgScope for the scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00002229DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002230 if (!InlinedAt) {
2231 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2232 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002233 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002234 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2235 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Pateleac9c072010-04-27 19:46:33 +00002236 if (DIDescriptor(Scope).isLexicalBlock()) {
2237 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002238 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Pateleac9c072010-04-27 19:46:33 +00002239 WScope->setParent(Parent);
2240 Parent->addScope(WScope);
2241 }
2242
2243 if (!WScope->getParent()) {
2244 StringRef SPName = DISubprogram(Scope).getLinkageName();
2245 if (SPName == Asm->MF->getFunction()->getName())
2246 CurrentFnDbgScope = WScope;
2247 }
2248
2249 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002250 }
2251
2252 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2253 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002254 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002255
2256 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2257 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2258 DILocation DL(InlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002259 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002260 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Pateleac9c072010-04-27 19:46:33 +00002261 WScope->setParent(Parent);
2262 Parent->addScope(WScope);
2263
2264 ConcreteScopes[InlinedAt] = WScope;
2265 getOrCreateAbstractScope(Scope);
2266
2267 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00002268}
2269
Devang Pateleac9c072010-04-27 19:46:33 +00002270/// hasValidLocation - Return true if debug location entry attached with
2271/// machine instruction encodes valid location info.
2272static bool hasValidLocation(LLVMContext &Ctx,
2273 const MachineInstr *MInsn,
Devang Patele9f8f5e2010-05-07 20:54:48 +00002274 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Pateleac9c072010-04-27 19:46:33 +00002275 if (MInsn->isDebugValue())
2276 return false;
2277 DebugLoc DL = MInsn->getDebugLoc();
2278 if (DL.isUnknown()) return false;
2279
Devang Patele9f8f5e2010-05-07 20:54:48 +00002280 const MDNode *S = DL.getScope(Ctx);
Devang Pateleac9c072010-04-27 19:46:33 +00002281
2282 // There is no need to create another DIE for compile unit. For all
2283 // other scopes, create one DbgScope now. This will be translated
2284 // into a scope DIE at the end.
2285 if (DIScope(S).isCompileUnit()) return false;
2286
2287 Scope = S;
2288 InlinedAt = DL.getInlinedAt(Ctx);
2289 return true;
2290}
2291
2292/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2293/// hierarchy.
2294static void calculateDominanceGraph(DbgScope *Scope) {
2295 assert (Scope && "Unable to calculate scop edominance graph!");
2296 SmallVector<DbgScope *, 4> WorkStack;
2297 WorkStack.push_back(Scope);
2298 unsigned Counter = 0;
2299 while (!WorkStack.empty()) {
2300 DbgScope *WS = WorkStack.back();
2301 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2302 bool visitedChildren = false;
2303 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2304 SE = Children.end(); SI != SE; ++SI) {
2305 DbgScope *ChildScope = *SI;
2306 if (!ChildScope->getDFSOut()) {
2307 WorkStack.push_back(ChildScope);
2308 visitedChildren = true;
2309 ChildScope->setDFSIn(++Counter);
2310 break;
2311 }
2312 }
2313 if (!visitedChildren) {
2314 WorkStack.pop_back();
2315 WS->setDFSOut(++Counter);
2316 }
2317 }
2318}
2319
2320/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
2321static
2322void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2323 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2324{
2325#ifndef NDEBUG
2326 unsigned PrevDFSIn = 0;
2327 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2328 I != E; ++I) {
2329 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2330 II != IE; ++II) {
2331 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002332 const MDNode *Scope = NULL;
2333 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002334
2335 // Check if instruction has valid location information.
2336 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2337 dbgs() << " [ ";
2338 if (InlinedAt)
2339 dbgs() << "*";
2340 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
2341 MI2ScopeMap.find(MInsn);
2342 if (DI != MI2ScopeMap.end()) {
2343 DbgScope *S = DI->second;
2344 dbgs() << S->getDFSIn();
2345 PrevDFSIn = S->getDFSIn();
2346 } else
2347 dbgs() << PrevDFSIn;
2348 } else
2349 dbgs() << " [ x" << PrevDFSIn;
2350 dbgs() << " ]";
2351 MInsn->dump();
2352 }
2353 dbgs() << "\n";
2354 }
2355#endif
2356}
Devang Patel2c4ceb12009-11-21 02:48:08 +00002357/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00002358/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00002359bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00002360 // If scope information was extracted using .dbg intrinsics then there is not
2361 // any need to extract these information by scanning each instruction.
2362 if (!DbgScopeMap.empty())
2363 return false;
2364
Dan Gohman314bf7c2010-04-23 01:18:53 +00002365 // Scan each instruction and create scopes. First build working set of scopes.
Devang Pateleac9c072010-04-27 19:46:33 +00002366 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2367 SmallVector<DbgRange, 4> MIRanges;
2368 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002369 const MDNode *PrevScope = NULL;
2370 const MDNode *PrevInlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002371 const MachineInstr *RangeBeginMI = NULL;
2372 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002373 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00002374 I != E; ++I) {
2375 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2376 II != IE; ++II) {
2377 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002378 const MDNode *Scope = NULL;
2379 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002380
2381 // Check if instruction has valid location information.
2382 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2383 PrevMI = MInsn;
2384 continue;
2385 }
Chris Lattnerde4845c2010-04-02 19:42:39 +00002386
Devang Pateleac9c072010-04-27 19:46:33 +00002387 // If scope has not changed then skip this instruction.
2388 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2389 PrevMI = MInsn;
2390 continue;
2391 }
2392
2393 if (RangeBeginMI) {
2394 // If we have alread seen a beginning of a instruction range and
2395 // current instruction scope does not match scope of first instruction
2396 // in this range then create a new instruction range.
2397 DbgRange R(RangeBeginMI, PrevMI);
2398 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
2399 MIRanges.push_back(R);
2400 }
2401
2402 // This is a beginning of a new instruction range.
2403 RangeBeginMI = MInsn;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002404
Devang Pateleac9c072010-04-27 19:46:33 +00002405 // Reset previous markers.
2406 PrevMI = MInsn;
2407 PrevScope = Scope;
2408 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00002409 }
2410 }
2411
Devang Pateleac9c072010-04-27 19:46:33 +00002412 // Create last instruction range.
2413 if (RangeBeginMI && PrevMI && PrevScope) {
2414 DbgRange R(RangeBeginMI, PrevMI);
2415 MIRanges.push_back(R);
2416 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00002417 }
Devang Pateleac9c072010-04-27 19:46:33 +00002418
Devang Patel344130e2010-01-04 20:44:00 +00002419 if (!CurrentFnDbgScope)
2420 return false;
2421
Devang Pateleac9c072010-04-27 19:46:33 +00002422 calculateDominanceGraph(CurrentFnDbgScope);
2423 if (PrintDbgScope)
2424 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2425
2426 // Find ranges of instructions covered by each DbgScope;
2427 DbgScope *PrevDbgScope = NULL;
2428 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2429 RE = MIRanges.end(); RI != RE; ++RI) {
2430 const DbgRange &R = *RI;
2431 DbgScope *S = MI2ScopeMap.lookup(R.first);
2432 assert (S && "Lost DbgScope for a machine instruction!");
2433 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2434 PrevDbgScope->closeInsnRange(S);
2435 S->openInsnRange(R.first);
2436 S->extendInsnRange(R.second);
2437 PrevDbgScope = S;
2438 }
2439
2440 if (PrevDbgScope)
2441 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00002442
Devang Patele37b0c62010-04-08 18:43:56 +00002443 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00002444
2445 return !DbgScopeMap.empty();
2446}
2447
Devang Pateleac9c072010-04-27 19:46:33 +00002448/// identifyScopeMarkers() -
2449/// Each DbgScope has first instruction and last instruction to mark beginning
2450/// and end of a scope respectively. Create an inverse map that list scopes
2451/// starts (and ends) with an instruction. One instruction may start (or end)
2452/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00002453void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00002454 SmallVector<DbgScope *, 4> WorkList;
2455 WorkList.push_back(CurrentFnDbgScope);
2456 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00002457 DbgScope *S = WorkList.pop_back_val();
Devang Pateleac9c072010-04-27 19:46:33 +00002458
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002459 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patel42aafd72010-01-20 02:05:23 +00002460 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002461 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00002462 SE = Children.end(); SI != SE; ++SI)
2463 WorkList.push_back(*SI);
2464
Devang Patel53bb5c92009-11-10 23:06:00 +00002465 if (S->isAbstractScope())
2466 continue;
Devang Pateleac9c072010-04-27 19:46:33 +00002467
2468 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2469 if (Ranges.empty())
2470 continue;
2471 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2472 RE = Ranges.end(); RI != RE; ++RI) {
2473 assert(RI->first && "DbgRange does not have first instruction!");
2474 assert(RI->second && "DbgRange does not have second instruction!");
2475 InsnsBeginScopeSet.insert(RI->first);
2476 InsnsEndScopeSet.insert(RI->second);
2477 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002478 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002479}
2480
Dan Gohman084751c2010-04-20 00:37:27 +00002481/// FindFirstDebugLoc - Find the first debug location in the function. This
2482/// is intended to be an approximation for the source position of the
2483/// beginning of the function.
2484static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2485 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2486 I != E; ++I)
2487 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2488 MBBI != MBBE; ++MBBI) {
2489 DebugLoc DL = MBBI->getDebugLoc();
2490 if (!DL.isUnknown())
2491 return DL;
2492 }
2493 return DebugLoc();
2494}
2495
Devang Patel2c4ceb12009-11-21 02:48:08 +00002496/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002497/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00002498void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00002499 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00002500 if (!extractScopeInformation()) return;
Chris Lattnera909d662010-03-29 20:38:20 +00002501
Devang Patel2c4ceb12009-11-21 02:48:08 +00002502 collectVariableInfo();
Devang Patel60b35bd2009-10-06 18:37:31 +00002503
Devang Pateleac9c072010-04-27 19:46:33 +00002504 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2505 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002506 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00002507 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002508
2509 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2510 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00002511 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002512 if (FDL.isUnknown()) return;
2513
Devang Patele9f8f5e2010-05-07 20:54:48 +00002514 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Chris Lattnerde4845c2010-04-02 19:42:39 +00002515
2516 DISubprogram SP = getDISubprogram(Scope);
2517 unsigned Line, Col;
2518 if (SP.Verify()) {
2519 Line = SP.getLineNumber();
2520 Col = 0;
2521 } else {
2522 Line = FDL.getLine();
2523 Col = FDL.getCol();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002524 }
Chris Lattnerde4845c2010-04-02 19:42:39 +00002525
2526 recordSourceLine(Line, Col, Scope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002527}
2528
Devang Patel2c4ceb12009-11-21 02:48:08 +00002529/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002530///
Chris Lattnereec791a2010-01-26 23:18:02 +00002531void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002532 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002533
Devang Patel344130e2010-01-04 20:44:00 +00002534 if (CurrentFnDbgScope) {
2535 // Define end label for subprogram.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002536 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_end",
2537 Asm->getFunctionNumber()));
Devang Patel344130e2010-01-04 20:44:00 +00002538
2539 // Get function line info.
2540 if (!Lines.empty()) {
2541 // Get section line info.
2542 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2543 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2544 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2545 // Append the function info to section info.
2546 SectionLineInfos.insert(SectionLineInfos.end(),
2547 Lines.begin(), Lines.end());
2548 }
2549
2550 // Construct abstract scopes.
2551 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2552 AE = AbstractScopesList.end(); AI != AE; ++AI)
2553 constructScopeDIE(*AI);
2554
Devang Patel9c488372010-05-04 06:15:30 +00002555 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Devang Patel344130e2010-01-04 20:44:00 +00002556
Devang Patel9c488372010-05-04 06:15:30 +00002557 if (!DisableFramePointerElim(*MF))
2558 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
2559 dwarf::DW_FORM_flag, 1);
2560
2561
Chris Lattnerd38fee82010-04-05 00:13:49 +00002562 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002563 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002564 }
2565
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002566 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002567 CurrentFnDbgScope = NULL;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002568 DeleteContainerSeconds(DbgScopeMap);
Devang Patel51424712010-04-09 16:04:20 +00002569 InsnsBeginScopeSet.clear();
2570 InsnsEndScopeSet.clear();
Devang Patelaead63c2010-03-29 22:59:58 +00002571 DbgValueStartMap.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002572 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002573 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002574 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002575 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00002576 LabelsBeforeInsn.clear();
2577 LabelsAfterInsn.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002578 Lines.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002579 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002580}
2581
Chris Lattnerc6087842010-03-09 04:54:43 +00002582/// recordSourceLine - Register a source line with debug info. Returns the
2583/// unique label that was emitted and which provides correspondence to
2584/// the source line list.
Devang Patele9f8f5e2010-05-07 20:54:48 +00002585MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S) {
Devang Patel65dbc902009-11-25 17:36:49 +00002586 StringRef Dir;
2587 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002588
Dan Gohman1cc0d622010-05-05 23:41:32 +00002589 unsigned Src = 1;
2590 if (S) {
2591 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00002592
Dan Gohman1cc0d622010-05-05 23:41:32 +00002593 if (Scope.isCompileUnit()) {
2594 DICompileUnit CU(S);
2595 Dir = CU.getDirectory();
2596 Fn = CU.getFilename();
2597 } else if (Scope.isSubprogram()) {
2598 DISubprogram SP(S);
2599 Dir = SP.getDirectory();
2600 Fn = SP.getFilename();
2601 } else if (Scope.isLexicalBlock()) {
2602 DILexicalBlock DB(S);
2603 Dir = DB.getDirectory();
2604 Fn = DB.getFilename();
2605 } else
2606 assert(0 && "Unexpected scope info");
2607
2608 Src = GetOrCreateSourceID(Dir, Fn);
2609 }
2610
Chris Lattner63d78362010-03-14 08:36:50 +00002611 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner25b68c62010-03-14 08:15:55 +00002612 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002613
Chris Lattnerc6087842010-03-09 04:54:43 +00002614 Asm->OutStreamer.EmitLabel(Label);
2615 return Label;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002616}
2617
Bill Wendling829e67b2009-05-20 23:22:40 +00002618//===----------------------------------------------------------------------===//
2619// Emit Methods
2620//===----------------------------------------------------------------------===//
2621
Devang Patel2c4ceb12009-11-21 02:48:08 +00002622/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002623///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002624unsigned
2625DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002626 // Get the children.
2627 const std::vector<DIE *> &Children = Die->getChildren();
2628
2629 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002630 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00002631 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00002632
2633 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002634 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002635
2636 // Get the abbreviation for this DIE.
2637 unsigned AbbrevNumber = Die->getAbbrevNumber();
2638 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2639
2640 // Set DIE offset
2641 Die->setOffset(Offset);
2642
2643 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002644 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002645
2646 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2647 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2648
2649 // Size the DIE attribute values.
2650 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2651 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00002652 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00002653
2654 // Size the DIE children if any.
2655 if (!Children.empty()) {
2656 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2657 "Children flag not set");
2658
2659 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002660 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002661
2662 // End of children marker.
2663 Offset += sizeof(int8_t);
2664 }
2665
2666 Die->setSize(Offset - Die->getOffset());
2667 return Offset;
2668}
2669
Devang Patel2c4ceb12009-11-21 02:48:08 +00002670/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002671///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002672void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002673 // Compute size of compile unit header.
2674 static unsigned Offset =
2675 sizeof(int32_t) + // Length of Compilation Unit Info
2676 sizeof(int16_t) + // DWARF version number
2677 sizeof(int32_t) + // Offset Into Abbrev. Section
2678 sizeof(int8_t); // Pointer Size (in bytes)
2679
Devang Patel2c4ceb12009-11-21 02:48:08 +00002680 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Bill Wendling94d04b82009-05-20 23:21:38 +00002681}
2682
Chris Lattner11b8f302010-04-04 23:02:02 +00002683/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2684/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00002685static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00002686 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002687 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00002688 if (!SymbolStem) return 0;
2689
Chris Lattner9c69e285532010-04-04 22:59:04 +00002690 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2691 Asm->OutStreamer.EmitLabel(TmpSym);
2692 return TmpSym;
2693}
2694
2695/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2696/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00002697void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002698 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002699
Bill Wendling94d04b82009-05-20 23:21:38 +00002700 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002701 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002702 DwarfFrameSectionSym =
2703 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2704 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002705
Chris Lattner9c69e285532010-04-04 22:59:04 +00002706 DwarfInfoSectionSym =
2707 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2708 DwarfAbbrevSectionSym =
2709 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00002710 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Chris Lattner9c69e285532010-04-04 22:59:04 +00002711
2712 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00002713 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002714
Chris Lattner11b8f302010-04-04 23:02:02 +00002715 EmitSectionSym(Asm, TLOF.getDwarfLineSection());
2716 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2717 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2718 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Chris Lattner9c69e285532010-04-04 22:59:04 +00002719 DwarfStrSectionSym =
2720 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00002721 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2722 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00002723
Chris Lattner9c69e285532010-04-04 22:59:04 +00002724 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00002725 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002726}
2727
Devang Patel2c4ceb12009-11-21 02:48:08 +00002728/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002729///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002730void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002731 // Get the abbreviation for this DIE.
2732 unsigned AbbrevNumber = Die->getAbbrevNumber();
2733 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2734
Bill Wendling94d04b82009-05-20 23:21:38 +00002735 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00002736 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00002737 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2738 Twine::utohexstr(Die->getOffset()) + ":0x" +
2739 Twine::utohexstr(Die->getSize()) + " " +
2740 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002741 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002742
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002743 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00002744 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2745
2746 // Emit the DIE attribute values.
2747 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2748 unsigned Attr = AbbrevData[i].getAttribute();
2749 unsigned Form = AbbrevData[i].getForm();
2750 assert(Form && "Too many attributes for DIE (check abbreviation)");
2751
Chris Lattner3f53c832010-04-04 18:52:31 +00002752 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00002753 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2754
Bill Wendling94d04b82009-05-20 23:21:38 +00002755 switch (Attr) {
2756 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002757 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002758 break;
2759 case dwarf::DW_AT_abstract_origin: {
2760 DIEEntry *E = cast<DIEEntry>(Values[i]);
2761 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002762 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002763 Asm->EmitInt32(Addr);
2764 break;
2765 }
Devang Patelf2548ca2010-04-16 23:33:45 +00002766 case dwarf::DW_AT_ranges: {
2767 // DW_AT_range Value encodes offset in debug_range section.
2768 DIEInteger *V = cast<DIEInteger>(Values[i]);
2769 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2770 V->getValue(),
2771 DwarfDebugRangeSectionSym,
2772 4);
2773 break;
2774 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002775 default:
2776 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002777 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00002778 break;
2779 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002780 }
2781
2782 // Emit the DIE children if any.
2783 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2784 const std::vector<DIE *> &Children = Die->getChildren();
2785
2786 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002787 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002788
Chris Lattner3f53c832010-04-04 18:52:31 +00002789 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00002790 Asm->OutStreamer.AddComment("End Of Children Mark");
2791 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002792 }
2793}
2794
Devang Patel8a241142009-12-09 18:24:21 +00002795/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002796///
Devang Patel8a241142009-12-09 18:24:21 +00002797void DwarfDebug::emitDebugInfo() {
2798 // Start debug info section.
2799 Asm->OutStreamer.SwitchSection(
2800 Asm->getObjFileLowering().getDwarfInfoSection());
2801 DIE *Die = ModuleCU->getCUDie();
Bill Wendling94d04b82009-05-20 23:21:38 +00002802
2803 // Emit the compile units header.
Chris Lattnerc0215722010-04-04 19:25:43 +00002804 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2805 ModuleCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00002806
2807 // Emit size of content not including length itself
2808 unsigned ContentSize = Die->getSize() +
2809 sizeof(int16_t) + // DWARF version number
2810 sizeof(int32_t) + // Offset Into Abbrev. Section
2811 sizeof(int8_t) + // Pointer Size (in bytes)
2812 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2813
Chris Lattner233f52b2010-03-09 23:52:58 +00002814 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2815 Asm->EmitInt32(ContentSize);
2816 Asm->OutStreamer.AddComment("DWARF version number");
2817 Asm->EmitInt16(dwarf::DWARF_VERSION);
2818 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Chris Lattner6189ed12010-04-04 23:25:33 +00002819 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2820 DwarfAbbrevSectionSym);
Chris Lattner233f52b2010-03-09 23:52:58 +00002821 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002822 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002823
Devang Patel2c4ceb12009-11-21 02:48:08 +00002824 emitDIE(Die);
Bill Wendling94d04b82009-05-20 23:21:38 +00002825 // FIXME - extra padding for gdb bug.
Chris Lattner233f52b2010-03-09 23:52:58 +00002826 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2827 Asm->EmitInt8(0);
2828 Asm->EmitInt8(0);
2829 Asm->EmitInt8(0);
2830 Asm->EmitInt8(0);
Chris Lattnerc0215722010-04-04 19:25:43 +00002831 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", ModuleCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00002832}
2833
Devang Patel2c4ceb12009-11-21 02:48:08 +00002834/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002835///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002836void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002837 // Check to see if it is worth the effort.
2838 if (!Abbreviations.empty()) {
2839 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002840 Asm->OutStreamer.SwitchSection(
2841 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002842
Chris Lattnerc0215722010-04-04 19:25:43 +00002843 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002844
2845 // For each abbrevation.
2846 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2847 // Get abbreviation data
2848 const DIEAbbrev *Abbrev = Abbreviations[i];
2849
2850 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002851 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00002852
2853 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002854 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00002855 }
2856
2857 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002858 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002859
Chris Lattnerc0215722010-04-04 19:25:43 +00002860 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002861 }
2862}
2863
Devang Patel2c4ceb12009-11-21 02:48:08 +00002864/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002865/// the line matrix.
2866///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002867void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002868 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00002869 Asm->OutStreamer.AddComment("Extended Op");
2870 Asm->EmitInt8(0);
2871
2872 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002873 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00002874 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2875 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2876
2877 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00002878
Chris Lattnerc0215722010-04-04 19:25:43 +00002879 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00002880 Asm->getTargetData().getPointerSize(),
2881 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002882
2883 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00002884 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2885 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002886 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00002887 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002888}
2889
Devang Patel2c4ceb12009-11-21 02:48:08 +00002890/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00002891///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002892void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002893 // If the target is using .loc/.file, the assembler will be emitting the
2894 // .debug_line table automatically.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002895 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002896 return;
2897
2898 // Minimum line delta, thus ranging from -10..(255-10).
2899 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2900 // Maximum line delta, thus ranging from -10..(255-10).
2901 const int MaxLineDelta = 255 + MinLineDelta;
2902
2903 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002904 Asm->OutStreamer.SwitchSection(
2905 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002906
2907 // Construct the section header.
Chris Lattner233f52b2010-03-09 23:52:58 +00002908 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnera6437182010-04-04 19:58:12 +00002909 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
2910 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00002911 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002912
Chris Lattner233f52b2010-03-09 23:52:58 +00002913 Asm->OutStreamer.AddComment("DWARF version number");
2914 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling94d04b82009-05-20 23:21:38 +00002915
Chris Lattner233f52b2010-03-09 23:52:58 +00002916 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnera6437182010-04-04 19:58:12 +00002917 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
2918 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00002919 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002920
Chris Lattner233f52b2010-03-09 23:52:58 +00002921 Asm->OutStreamer.AddComment("Minimum Instruction Length");
2922 Asm->EmitInt8(1);
2923 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
2924 Asm->EmitInt8(1);
2925 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
2926 Asm->EmitInt8(MinLineDelta);
2927 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
2928 Asm->EmitInt8(MaxLineDelta);
2929 Asm->OutStreamer.AddComment("Special Opcode Base");
2930 Asm->EmitInt8(-MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00002931
2932 // Line number standard opcode encodings argument count
Chris Lattner233f52b2010-03-09 23:52:58 +00002933 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
2934 Asm->EmitInt8(0);
2935 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
2936 Asm->EmitInt8(1);
2937 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
2938 Asm->EmitInt8(1);
2939 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
2940 Asm->EmitInt8(1);
2941 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
2942 Asm->EmitInt8(1);
2943 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
2944 Asm->EmitInt8(0);
2945 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
2946 Asm->EmitInt8(0);
2947 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
2948 Asm->EmitInt8(0);
2949 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
2950 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002951
2952 // Emit directories.
2953 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattner4cf202b2010-01-23 03:11:46 +00002954 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner3f53c832010-04-04 18:52:31 +00002955 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002956 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002957 }
2958
Chris Lattner233f52b2010-03-09 23:52:58 +00002959 Asm->OutStreamer.AddComment("End of directories");
2960 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002961
2962 // Emit files.
2963 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2964 // Remember source id starts at 1.
2965 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattner4cf202b2010-01-23 03:11:46 +00002966 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner3f53c832010-04-04 18:52:31 +00002967 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattner4cf202b2010-01-23 03:11:46 +00002968 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
2969
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002970 Asm->EmitULEB128(Id.first, "Directory #");
2971 Asm->EmitULEB128(0, "Mod date");
2972 Asm->EmitULEB128(0, "File size");
Bill Wendling94d04b82009-05-20 23:21:38 +00002973 }
2974
Chris Lattner233f52b2010-03-09 23:52:58 +00002975 Asm->OutStreamer.AddComment("End of files");
2976 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002977
Chris Lattnerc0215722010-04-04 19:25:43 +00002978 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002979
2980 // A sequence for each text section.
2981 unsigned SecSrcLinesSize = SectionSourceLines.size();
2982
2983 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2984 // Isolate current sections line info.
2985 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2986
Bill Wendling94d04b82009-05-20 23:21:38 +00002987 // Dwarf assumes we start with first line of first source file.
2988 unsigned Source = 1;
2989 unsigned Line = 1;
2990
2991 // Construct rows of the address, source, line, column matrix.
2992 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2993 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner25b68c62010-03-14 08:15:55 +00002994 MCSymbol *Label = LineInfo.getLabel();
Chris Lattnerb9130602010-03-14 02:20:58 +00002995 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling94d04b82009-05-20 23:21:38 +00002996
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002997 if (Asm->isVerbose()) {
Chris Lattner188a87d2010-03-10 01:04:13 +00002998 std::pair<unsigned, unsigned> SrcID =
Bill Wendling94d04b82009-05-20 23:21:38 +00002999 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner188a87d2010-03-10 01:04:13 +00003000 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner49f618a2010-03-10 02:29:31 +00003001 "/" +
3002 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner188a87d2010-03-10 01:04:13 +00003003 ":" + Twine(LineInfo.getLine()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003004 }
3005
3006 // Define the line address.
Chris Lattner233f52b2010-03-09 23:52:58 +00003007 Asm->OutStreamer.AddComment("Extended Op");
3008 Asm->EmitInt8(0);
3009 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003010 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003011
3012 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3013 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3014
3015 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003016 Asm->OutStreamer.EmitSymbolValue(Label,
3017 Asm->getTargetData().getPointerSize(),
Chris Lattnerb9130602010-03-14 02:20:58 +00003018 0/*AddrSpace*/);
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003019
Bill Wendling94d04b82009-05-20 23:21:38 +00003020 // If change of source, then switch to the new source.
3021 if (Source != LineInfo.getSourceID()) {
3022 Source = LineInfo.getSourceID();
Chris Lattner233f52b2010-03-09 23:52:58 +00003023 Asm->OutStreamer.AddComment("DW_LNS_set_file");
3024 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003025 Asm->EmitULEB128(Source, "New Source");
Bill Wendling94d04b82009-05-20 23:21:38 +00003026 }
3027
3028 // If change of line.
3029 if (Line != LineInfo.getLine()) {
3030 // Determine offset.
3031 int Offset = LineInfo.getLine() - Line;
3032 int Delta = Offset - MinLineDelta;
3033
3034 // Update line.
3035 Line = LineInfo.getLine();
3036
3037 // If delta is small enough and in range...
3038 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3039 // ... then use fast opcode.
Chris Lattner233f52b2010-03-09 23:52:58 +00003040 Asm->OutStreamer.AddComment("Line Delta");
3041 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003042 } else {
3043 // ... otherwise use long hand.
Chris Lattner233f52b2010-03-09 23:52:58 +00003044 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling94d04b82009-05-20 23:21:38 +00003045 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003046 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner233f52b2010-03-09 23:52:58 +00003047 Asm->OutStreamer.AddComment("DW_LNS_copy");
3048 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003049 }
3050 } else {
3051 // Copy the previous row (different address or source)
Chris Lattner233f52b2010-03-09 23:52:58 +00003052 Asm->OutStreamer.AddComment("DW_LNS_copy");
3053 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003054 }
3055 }
3056
Devang Patel2c4ceb12009-11-21 02:48:08 +00003057 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003058 }
3059
3060 if (SecSrcLinesSize == 0)
3061 // Because we're emitting a debug_line section, we still need a line
3062 // table. The linker and friends expect it to exist. If there's nothing to
3063 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003064 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003065
Chris Lattnerc0215722010-04-04 19:25:43 +00003066 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003067}
3068
Devang Patel2c4ceb12009-11-21 02:48:08 +00003069/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003070///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003071void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003072 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003073 return;
3074
Chris Lattnerd38fee82010-04-05 00:13:49 +00003075 int stackGrowth = Asm->getTargetData().getPointerSize();
3076 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3077 TargetFrameInfo::StackGrowsDown)
3078 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00003079
3080 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003081 Asm->OutStreamer.SwitchSection(
3082 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003083
Chris Lattnerc0215722010-04-04 19:25:43 +00003084 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003085 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003086 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3087 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003088
Chris Lattnerc0215722010-04-04 19:25:43 +00003089 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003090 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00003091 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00003092 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00003093 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00003094 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003095 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003096 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3097 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00003098 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003099 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling94d04b82009-05-20 23:21:38 +00003100 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00003101
3102 std::vector<MachineMove> Moves;
3103 RI->getInitialFrameState(Moves);
3104
Chris Lattner02b86b92010-04-04 23:41:46 +00003105 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003106
Chris Lattner059ea132010-04-28 01:05:45 +00003107 Asm->EmitAlignment(2);
Chris Lattnerc0215722010-04-04 19:25:43 +00003108 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003109}
3110
Devang Patel2c4ceb12009-11-21 02:48:08 +00003111/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00003112/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00003113void DwarfDebug::
3114emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003115 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003116 return;
3117
3118 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003119 Asm->OutStreamer.SwitchSection(
3120 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003121
Chris Lattner233f52b2010-03-09 23:52:58 +00003122 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00003123 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00003124 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00003125 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00003126 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00003127 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003128
Chris Lattner206d61e2010-03-13 07:26:18 +00003129 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00003130
Chris Lattner233f52b2010-03-09 23:52:58 +00003131 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattner6189ed12010-04-04 23:25:33 +00003132 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
3133 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003134
Chris Lattner233f52b2010-03-09 23:52:58 +00003135 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00003136 MCSymbol *FuncBeginSym =
3137 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00003138 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00003139 Asm->getTargetData().getPointerSize(),
3140 0/*AddrSpace*/);
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003141
3142
Chris Lattner233f52b2010-03-09 23:52:58 +00003143 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00003144 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003145 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003146
Chris Lattner02b86b92010-04-04 23:41:46 +00003147 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003148
Chris Lattner059ea132010-04-28 01:05:45 +00003149 Asm->EmitAlignment(2);
Chris Lattner206d61e2010-03-13 07:26:18 +00003150 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00003151}
3152
Devang Patel8a241142009-12-09 18:24:21 +00003153/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3154///
3155void DwarfDebug::emitDebugPubNames() {
3156 // Start the dwarf pubnames section.
3157 Asm->OutStreamer.SwitchSection(
3158 Asm->getObjFileLowering().getDwarfPubNamesSection());
3159
Chris Lattner233f52b2010-03-09 23:52:58 +00003160 Asm->OutStreamer.AddComment("Length of Public Names Info");
Chris Lattnera6437182010-04-04 19:58:12 +00003161 Asm->EmitLabelDifference(
3162 Asm->GetTempSymbol("pubnames_end", ModuleCU->getID()),
3163 Asm->GetTempSymbol("pubnames_begin", ModuleCU->getID()), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003164
Chris Lattnerc0215722010-04-04 19:25:43 +00003165 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3166 ModuleCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003167
Chris Lattner233f52b2010-03-09 23:52:58 +00003168 Asm->OutStreamer.AddComment("DWARF Version");
3169 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling94d04b82009-05-20 23:21:38 +00003170
Chris Lattner233f52b2010-03-09 23:52:58 +00003171 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Chris Lattner6189ed12010-04-04 23:25:33 +00003172 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3173 DwarfInfoSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003174
Chris Lattner233f52b2010-03-09 23:52:58 +00003175 Asm->OutStreamer.AddComment("Compilation Unit Length");
Chris Lattnera6437182010-04-04 19:58:12 +00003176 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()),
3177 Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3178 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003179
Devang Patel8a241142009-12-09 18:24:21 +00003180 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling94d04b82009-05-20 23:21:38 +00003181 for (StringMap<DIE*>::const_iterator
3182 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3183 const char *Name = GI->getKeyData();
Chris Lattner233f52b2010-03-09 23:52:58 +00003184 DIE *Entity = GI->second;
Bill Wendling94d04b82009-05-20 23:21:38 +00003185
Chris Lattner233f52b2010-03-09 23:52:58 +00003186 Asm->OutStreamer.AddComment("DIE offset");
3187 Asm->EmitInt32(Entity->getOffset());
Chris Lattner4cf202b2010-01-23 03:11:46 +00003188
Chris Lattner3f53c832010-04-04 18:52:31 +00003189 if (Asm->isVerbose())
Chris Lattner4cf202b2010-01-23 03:11:46 +00003190 Asm->OutStreamer.AddComment("External Name");
3191 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003192 }
3193
Chris Lattner233f52b2010-03-09 23:52:58 +00003194 Asm->OutStreamer.AddComment("End Mark");
3195 Asm->EmitInt32(0);
Chris Lattnerc0215722010-04-04 19:25:43 +00003196 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3197 ModuleCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003198}
3199
Devang Patel193f7202009-11-24 01:14:22 +00003200void DwarfDebug::emitDebugPubTypes() {
Devang Patelf3a03762009-11-24 19:18:41 +00003201 // Start the dwarf pubnames section.
3202 Asm->OutStreamer.SwitchSection(
3203 Asm->getObjFileLowering().getDwarfPubTypesSection());
Chris Lattner233f52b2010-03-09 23:52:58 +00003204 Asm->OutStreamer.AddComment("Length of Public Types Info");
Chris Lattnera6437182010-04-04 19:58:12 +00003205 Asm->EmitLabelDifference(
3206 Asm->GetTempSymbol("pubtypes_end", ModuleCU->getID()),
3207 Asm->GetTempSymbol("pubtypes_begin", ModuleCU->getID()), 4);
Devang Patel193f7202009-11-24 01:14:22 +00003208
Chris Lattnerc0215722010-04-04 19:25:43 +00003209 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3210 ModuleCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003211
Chris Lattner3f53c832010-04-04 18:52:31 +00003212 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
Devang Patele3d6d222010-02-02 03:47:27 +00003213 Asm->EmitInt16(dwarf::DWARF_VERSION);
Devang Patel193f7202009-11-24 01:14:22 +00003214
Chris Lattner233f52b2010-03-09 23:52:58 +00003215 Asm->OutStreamer.AddComment("Offset of Compilation ModuleCU Info");
Chris Lattner6189ed12010-04-04 23:25:33 +00003216 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3217 DwarfInfoSectionSym);
Devang Patel193f7202009-11-24 01:14:22 +00003218
Chris Lattner233f52b2010-03-09 23:52:58 +00003219 Asm->OutStreamer.AddComment("Compilation ModuleCU Length");
Chris Lattnera6437182010-04-04 19:58:12 +00003220 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()),
3221 Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3222 4);
Devang Patel193f7202009-11-24 01:14:22 +00003223
3224 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
3225 for (StringMap<DIE*>::const_iterator
3226 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3227 const char *Name = GI->getKeyData();
3228 DIE * Entity = GI->second;
3229
Chris Lattner3f53c832010-04-04 18:52:31 +00003230 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Devang Patele3d6d222010-02-02 03:47:27 +00003231 Asm->EmitInt32(Entity->getOffset());
Chris Lattner4cf202b2010-01-23 03:11:46 +00003232
Chris Lattner3f53c832010-04-04 18:52:31 +00003233 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Devang Patel31acb892010-02-02 03:37:03 +00003234 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
Devang Patel193f7202009-11-24 01:14:22 +00003235 }
3236
Chris Lattner233f52b2010-03-09 23:52:58 +00003237 Asm->OutStreamer.AddComment("End Mark");
3238 Asm->EmitInt32(0);
Chris Lattnerc0215722010-04-04 19:25:43 +00003239 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3240 ModuleCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003241}
3242
Devang Patel2c4ceb12009-11-21 02:48:08 +00003243/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003244///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003245void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003246 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003247 if (StringPool.empty()) return;
3248
3249 // Start the dwarf str section.
3250 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003251 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003252
Chris Lattnerbc733f52010-03-13 02:17:42 +00003253 // Get all of the string pool entries and put them in an array by their ID so
3254 // we can sort them.
3255 SmallVector<std::pair<unsigned,
3256 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3257
3258 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3259 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3260 Entries.push_back(std::make_pair(I->second.second, &*I));
3261
3262 array_pod_sort(Entries.begin(), Entries.end());
3263
3264 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003265 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003266 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003267
3268 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003269 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003270 }
3271}
3272
Devang Patel2c4ceb12009-11-21 02:48:08 +00003273/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003274///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003275void DwarfDebug::emitDebugLoc() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003276 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003277 Asm->OutStreamer.SwitchSection(
3278 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003279}
3280
3281/// EmitDebugARanges - Emit visible names into a debug aranges section.
3282///
3283void DwarfDebug::EmitDebugARanges() {
3284 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003285 Asm->OutStreamer.SwitchSection(
3286 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003287}
3288
Devang Patel2c4ceb12009-11-21 02:48:08 +00003289/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003290///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003291void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003292 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003293 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00003294 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00003295 unsigned char Size = Asm->getTargetData().getPointerSize();
3296 for (SmallVector<const MCSymbol *, 8>::iterator
3297 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
3298 I != E; ++I) {
3299 if (*I)
3300 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003301 else
Devang Pateleac9c072010-04-27 19:46:33 +00003302 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003303 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003304}
3305
Devang Patel2c4ceb12009-11-21 02:48:08 +00003306/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003307///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003308void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003309 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00003310 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003311 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003312 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003313 }
3314}
3315
Devang Patel2c4ceb12009-11-21 02:48:08 +00003316/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00003317/// Section Header:
3318/// 1. length of section
3319/// 2. Dwarf version number
3320/// 3. address size.
3321///
3322/// Entries (one "entry" for each function that was inlined):
3323///
3324/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3325/// otherwise offset into __debug_str for regular function name.
3326/// 2. offset into __debug_str section for regular function name.
3327/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3328/// instances for the function.
3329///
3330/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3331/// inlined instance; the die_offset points to the inlined_subroutine die in the
3332/// __debug_info section, and the low_pc is the starting address for the
3333/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003334void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003335 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003336 return;
3337
Devang Patel1dbc7712009-06-29 20:45:18 +00003338 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00003339 return;
3340
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003341 Asm->OutStreamer.SwitchSection(
3342 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00003343
Chris Lattner233f52b2010-03-09 23:52:58 +00003344 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003345 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3346 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003347
Chris Lattnerc0215722010-04-04 19:25:43 +00003348 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003349
Chris Lattner233f52b2010-03-09 23:52:58 +00003350 Asm->OutStreamer.AddComment("Dwarf Version");
3351 Asm->EmitInt16(dwarf::DWARF_VERSION);
3352 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003353 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003354
Devang Patele9f8f5e2010-05-07 20:54:48 +00003355 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00003356 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00003357
Devang Patele9f8f5e2010-05-07 20:54:48 +00003358 const MDNode *Node = *I;
3359 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003360 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00003361 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00003362 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00003363 StringRef LName = SP.getLinkageName();
3364 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003365
Chris Lattner233f52b2010-03-09 23:52:58 +00003366 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003367 if (LName.empty()) {
3368 Asm->OutStreamer.EmitBytes(Name, 0);
3369 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3370 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00003371 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3372 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00003373
Chris Lattner233f52b2010-03-09 23:52:58 +00003374 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00003375 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003376 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00003377
Devang Patel53bb5c92009-11-10 23:06:00 +00003378 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00003379 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00003380 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00003381 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003382
Chris Lattner3f53c832010-04-04 18:52:31 +00003383 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003384 Asm->OutStreamer.EmitSymbolValue(LI->first,
3385 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003386 }
3387 }
3388
Chris Lattnerc0215722010-04-04 19:25:43 +00003389 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003390}