blob: 4287c115bddfc904c70ba464b848d203fe045afd [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; }
Devang Patel98e1cac2010-05-14 21:01:35 +0000196 bool hasLocation() {
197 return DbgValueMInsn || FrameIndex != ~0U;
198 }
Bill Wendling0310d762009-05-15 09:23:25 +0000199};
200
201//===----------------------------------------------------------------------===//
Devang Pateleac9c072010-04-27 19:46:33 +0000202/// DbgRange - This is used to track range of instructions with identical
203/// debug info scope.
204///
205typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
206
207//===----------------------------------------------------------------------===//
Bill Wendling0310d762009-05-15 09:23:25 +0000208/// DbgScope - This class is used to track scope information.
209///
Devang Patelf76a3d62009-11-16 21:53:40 +0000210class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000211 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000212 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel3139fcf2010-01-26 21:39:14 +0000213 // Location at which this scope is inlined.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000214 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patel53bb5c92009-11-10 23:06:00 +0000215 bool AbstractScope; // Abstract Scope
Devang Pateld38dd112009-10-01 18:25:23 +0000216 const MachineInstr *LastInsn; // Last instruction of this scope.
217 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Pateleac9c072010-04-27 19:46:33 +0000218 unsigned DFSIn, DFSOut;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000219 // Scopes defined in scope. Contents not owned.
220 SmallVector<DbgScope *, 4> Scopes;
221 // Variables declared in scope. Contents owned.
222 SmallVector<DbgVariable *, 8> Variables;
Devang Pateleac9c072010-04-27 19:46:33 +0000223 SmallVector<DbgRange, 4> Ranges;
Owen Anderson04c05f72009-06-24 22:53:20 +0000224 // Private state for dump()
225 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000226public:
Devang Patele9f8f5e2010-05-07 20:54:48 +0000227 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000228 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Pateleac9c072010-04-27 19:46:33 +0000229 LastInsn(0), FirstInsn(0),
230 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000231 virtual ~DbgScope();
232
233 // Accessors.
234 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000235 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000236 DIDescriptor getDesc() const { return Desc; }
Devang Patele9f8f5e2010-05-07 20:54:48 +0000237 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
238 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000239 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
240 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Pateleac9c072010-04-27 19:46:33 +0000241 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
242
243 /// openInsnRange - This scope covers instruction range starting from MI.
244 void openInsnRange(const MachineInstr *MI) {
245 if (!FirstInsn)
246 FirstInsn = MI;
247
248 if (Parent)
249 Parent->openInsnRange(MI);
250 }
251
252 /// extendInsnRange - Extend the current instruction range covered by
253 /// this scope.
254 void extendInsnRange(const MachineInstr *MI) {
255 assert (FirstInsn && "MI Range is not open!");
256 LastInsn = MI;
257 if (Parent)
258 Parent->extendInsnRange(MI);
259 }
260
261 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
262 /// until now. This is used when a new scope is encountered while walking
263 /// machine instructions.
264 void closeInsnRange(DbgScope *NewScope = NULL) {
265 assert (LastInsn && "Last insn missing!");
266 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
267 FirstInsn = NULL;
268 LastInsn = NULL;
269 // If Parent dominates NewScope then do not close Parent's instruction
270 // range.
271 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
272 Parent->closeInsnRange(NewScope);
273 }
274
Devang Patel53bb5c92009-11-10 23:06:00 +0000275 void setAbstractScope() { AbstractScope = true; }
276 bool isAbstractScope() const { return AbstractScope; }
Devang Pateleac9c072010-04-27 19:46:33 +0000277
278 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
279 unsigned getDFSOut() const { return DFSOut; }
280 void setDFSOut(unsigned O) { DFSOut = O; }
281 unsigned getDFSIn() const { return DFSIn; }
282 void setDFSIn(unsigned I) { DFSIn = I; }
283 bool dominates(const DbgScope *S) {
284 if (S == this)
285 return true;
286 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
287 return true;
288 return false;
289 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000290
Devang Patel2c4ceb12009-11-21 02:48:08 +0000291 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000292 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000293 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000294
Devang Patel2c4ceb12009-11-21 02:48:08 +0000295 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000296 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000297 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000298
Bill Wendling0310d762009-05-15 09:23:25 +0000299#ifndef NDEBUG
300 void dump() const;
301#endif
302};
Devang Pateleac9c072010-04-27 19:46:33 +0000303
Chris Lattnerea761862010-04-05 04:09:20 +0000304} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000305
306#ifndef NDEBUG
307void DbgScope::dump() const {
David Greenef83adbc2009-12-24 00:31:35 +0000308 raw_ostream &err = dbgs();
Chris Lattnerc281de12009-08-23 00:51:00 +0000309 err.indent(IndentLevel);
Devang Patele9f8f5e2010-05-07 20:54:48 +0000310 const MDNode *N = Desc;
Devang Patel53bb5c92009-11-10 23:06:00 +0000311 N->dump();
Devang Patel53bb5c92009-11-10 23:06:00 +0000312 if (AbstractScope)
313 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000314
315 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000316 if (!Scopes.empty())
317 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000318 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
319 if (Scopes[i] != this)
320 Scopes[i]->dump();
321
322 IndentLevel -= 2;
323}
324#endif
325
Bill Wendling0310d762009-05-15 09:23:25 +0000326DbgScope::~DbgScope() {
Bill Wendling0310d762009-05-15 09:23:25 +0000327 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
328 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000329}
330
Chris Lattner49cd6642010-04-05 05:11:15 +0000331DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000332 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Chris Lattner994cb122010-04-05 03:52:55 +0000333 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelf2548ca2010-04-16 23:33:45 +0000334 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000335 NextStringPoolNumber = 0;
Chris Lattner9c69e285532010-04-04 22:59:04 +0000336
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000337 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
338 DwarfStrSectionSym = TextSectionSym = 0;
Devang Pateleac9c072010-04-27 19:46:33 +0000339 DwarfDebugRangeSectionSym = 0;
340 FunctionBeginSym = 0;
Torok Edwin9c421072010-04-07 10:44:46 +0000341 if (TimePassesIsEnabled) {
342 NamedRegionTimer T(DbgTimerName, DWARFGroupName);
343 beginModule(M);
344 } else {
345 beginModule(M);
346 }
Bill Wendling0310d762009-05-15 09:23:25 +0000347}
348DwarfDebug::~DwarfDebug() {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000349 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
350 DIEBlocks[j]->~DIEBlock();
Bill Wendling0310d762009-05-15 09:23:25 +0000351}
352
Chris Lattnerbc733f52010-03-13 02:17:42 +0000353MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
354 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
355 if (Entry.first) return Entry.first;
356
357 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000358 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000359}
360
361
Devang Patel2c4ceb12009-11-21 02:48:08 +0000362/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000363///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000364void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000365 // Profile the node so that we can make it unique.
366 FoldingSetNodeID ID;
367 Abbrev.Profile(ID);
368
369 // Check the set for priors.
370 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
371
372 // If it's newly added.
373 if (InSet == &Abbrev) {
374 // Add to abbreviation list.
375 Abbreviations.push_back(&Abbrev);
376
377 // Assign the vector position + 1 as its number.
378 Abbrev.setNumber(Abbreviations.size());
379 } else {
380 // Assign existing abbreviation number.
381 Abbrev.setNumber(InSet->getNumber());
382 }
383}
384
Devang Patel2c4ceb12009-11-21 02:48:08 +0000385/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000386/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000387DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000388 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000389 return Value;
390}
391
Devang Patel2c4ceb12009-11-21 02:48:08 +0000392/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000393///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000394void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000395 unsigned Form, uint64_t Integer) {
396 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000397 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000398 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000399}
400
Devang Patel2c4ceb12009-11-21 02:48:08 +0000401/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000402///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000403void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000404 unsigned Form, int64_t Integer) {
405 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000406 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000407 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000408}
409
Devang Patel69f57b12009-12-02 15:25:16 +0000410/// addString - Add a string attribute data and value. DIEString only
411/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000412void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattner4cf202b2010-01-23 03:11:46 +0000413 StringRef String) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000414 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000415 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000416}
417
Devang Patel2c4ceb12009-11-21 02:48:08 +0000418/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000419///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000420void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000421 const MCSymbol *Label) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000422 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000423 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000424}
425
Devang Patel2c4ceb12009-11-21 02:48:08 +0000426/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000427///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000428void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000429 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000430 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000431 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000432}
433
Chris Lattner74e41f92010-04-05 05:24:55 +0000434/// addDIEEntry - Add a DIE attribute data and value.
435///
436void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
437 DIE *Entry) {
438 Die->addValue(Attribute, Form, createDIEEntry(Entry));
439}
440
441
Devang Patel2c4ceb12009-11-21 02:48:08 +0000442/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000443///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000444void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000445 DIEBlock *Block) {
Chris Lattnera37d5382010-04-05 00:18:22 +0000446 Block->ComputeSize(Asm);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000447 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000448 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000449}
450
Devang Patel2c4ceb12009-11-21 02:48:08 +0000451/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000452/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000453void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Devang Patelc665a512010-05-07 23:33:41 +0000454 // Verify variable.
455 if (!V->Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000456 return;
457
458 unsigned Line = V->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000459 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
460 V->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000461 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000462 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
463 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000464}
465
Devang Patel2c4ceb12009-11-21 02:48:08 +0000466/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000467/// entry.
Devang Patela49d8772010-05-07 23:19:07 +0000468void DwarfDebug::addSourceLine(DIE *Die, const DIGlobalVariable *G) {
Devang Patelc665a512010-05-07 23:33:41 +0000469 // Verify global variable.
470 if (!G->Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000471 return;
472
473 unsigned Line = G->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000474 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
475 G->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000476 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000477 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
478 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000479}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000480
Devang Patel2c4ceb12009-11-21 02:48:08 +0000481/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000482/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000483void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patelc665a512010-05-07 23:33:41 +0000484 // Verify subprogram.
485 if (!SP->Verify())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000486 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000487 // If the line number is 0, don't add it.
488 if (SP->getLineNumber() == 0)
489 return;
490
Devang Patel82dfc0c2009-08-31 22:47:13 +0000491 unsigned Line = SP->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000492 if (!SP->getContext().Verify())
493 return;
Devang Patel9bb59a22010-03-24 21:30:35 +0000494 unsigned FileID = GetOrCreateSourceID(SP->getDirectory(),
495 SP->getFilename());
Devang Patel82dfc0c2009-08-31 22:47:13 +0000496 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000497 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
498 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000499}
500
Devang Patel2c4ceb12009-11-21 02:48:08 +0000501/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000502/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000503void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Devang Patelc665a512010-05-07 23:33:41 +0000504 // Verify type.
505 if (!Ty->Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000506 return;
507
508 unsigned Line = Ty->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000509 if (!Ty->getContext().Verify())
510 return;
511 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
512 Ty->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000513 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000514 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
515 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000516}
517
Devang Patel6404e4e2009-12-15 19:16:48 +0000518/// addSourceLine - Add location information to specified debug information
519/// entry.
520void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
Devang Patelc665a512010-05-07 23:33:41 +0000521 // Verify namespace.
522 if (!NS->Verify())
Devang Patel6404e4e2009-12-15 19:16:48 +0000523 return;
524
525 unsigned Line = NS->getLineNumber();
526 StringRef FN = NS->getFilename();
527 StringRef Dir = NS->getDirectory();
528
529 unsigned FileID = GetOrCreateSourceID(Dir, FN);
530 assert(FileID && "Invalid file id");
531 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
532 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
533}
534
Caroline Ticedc8f6042009-08-31 21:19:37 +0000535/* Byref variables, in Blocks, are declared by the programmer as
536 "SomeType VarName;", but the compiler creates a
537 __Block_byref_x_VarName struct, and gives the variable VarName
538 either the struct, or a pointer to the struct, as its type. This
539 is necessary for various behind-the-scenes things the compiler
540 needs to do with by-reference variables in blocks.
541
542 However, as far as the original *programmer* is concerned, the
543 variable should still have type 'SomeType', as originally declared.
544
545 The following function dives into the __Block_byref_x_VarName
546 struct to find the original type of the variable. This will be
547 passed back to the code generating the type for the Debug
548 Information Entry for the variable 'VarName'. 'VarName' will then
549 have the original type 'SomeType' in its debug information.
550
551 The original type 'SomeType' will be the type of the field named
552 'VarName' inside the __Block_byref_x_VarName struct.
553
554 NOTE: In order for this to not completely fail on the debugger
555 side, the Debug Information Entry for the variable VarName needs to
556 have a DW_AT_location that tells the debugger how to unwind through
557 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patel2c4ceb12009-11-21 02:48:08 +0000558 value of the variable. The function addBlockByrefType does this. */
Caroline Ticedc8f6042009-08-31 21:19:37 +0000559
560/// Find the type the programmer originally declared the variable to be
561/// and return that type.
562///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000563DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000564
565 DIType subType = Ty;
566 unsigned tag = Ty.getTag();
567
568 if (tag == dwarf::DW_TAG_pointer_type) {
Devang Patel2db49d72010-05-07 18:11:54 +0000569 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000570 subType = DTy.getTypeDerivedFrom();
571 }
572
Devang Patel2db49d72010-05-07 18:11:54 +0000573 DICompositeType blockStruct = DICompositeType(subType);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000574 DIArray Elements = blockStruct.getTypeArray();
575
Caroline Ticedc8f6042009-08-31 21:19:37 +0000576 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
577 DIDescriptor Element = Elements.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +0000578 DIDerivedType DT = DIDerivedType(Element);
Devang Patel65dbc902009-11-25 17:36:49 +0000579 if (Name == DT.getName())
Caroline Ticedc8f6042009-08-31 21:19:37 +0000580 return (DT.getTypeDerivedFrom());
581 }
582
583 return Ty;
584}
585
Devang Patel2c4ceb12009-11-21 02:48:08 +0000586/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000587/// and generate the DWARF information necessary to find the actual variable
588/// given the extra address information encoded in the DIVariable, starting from
589/// the starting location. Add the DWARF information to the die.
590///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000591void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000592 unsigned Attribute,
593 const MachineLocation &Location) {
594 const DIVariable &VD = DV->getVariable();
595 DIType Ty = VD.getType();
596
597 // Decode the original location, and use that as the start of the byref
598 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000599 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000600 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000601 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000602
603 if (Location.isReg()) {
604 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000605 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000606 } else {
607 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000608 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
609 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000610 }
611 } else {
612 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000613 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000614 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000615 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
616 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000617 }
618
Devang Patel2c4ceb12009-11-21 02:48:08 +0000619 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000620 }
621
622 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
623 uint64_t Element = VD.getAddrElement(i);
624
625 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000626 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
627 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000628 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000629 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000630 } else llvm_unreachable("unknown DIFactory Opcode");
631 }
632
633 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000634 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000635}
636
Caroline Ticedc8f6042009-08-31 21:19:37 +0000637/* Byref variables, in Blocks, are declared by the programmer as "SomeType
638 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
639 gives the variable VarName either the struct, or a pointer to the struct, as
640 its type. This is necessary for various behind-the-scenes things the
641 compiler needs to do with by-reference variables in Blocks.
642
643 However, as far as the original *programmer* is concerned, the variable
644 should still have type 'SomeType', as originally declared.
645
Devang Patel2c4ceb12009-11-21 02:48:08 +0000646 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000647 struct to find the original type of the variable, which is then assigned to
648 the variable's Debug Information Entry as its real type. So far, so good.
649 However now the debugger will expect the variable VarName to have the type
650 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000651 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000652 pointers and struct to find the actual variable of type SomeType.
653
654 The following function does just that. We start by getting
655 the "normal" location for the variable. This will be the location
656 of either the struct __Block_byref_x_VarName or the pointer to the
657 struct __Block_byref_x_VarName.
658
659 The struct will look something like:
660
661 struct __Block_byref_x_VarName {
662 ... <various fields>
663 struct __Block_byref_x_VarName *forwarding;
664 ... <various other fields>
665 SomeType VarName;
666 ... <maybe more fields>
667 };
668
669 If we are given the struct directly (as our starting point) we
670 need to tell the debugger to:
671
672 1). Add the offset of the forwarding field.
673
Dan Gohmanf451cb82010-02-10 16:03:48 +0000674 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000675 struct to use (the real one may have been copied onto the heap).
676
677 3). Add the offset for the field VarName, to find the actual variable.
678
679 If we started with a pointer to the struct, then we need to
680 dereference that pointer first, before the other steps.
681 Translating this into DWARF ops, we will need to append the following
682 to the current location description for the variable:
683
684 DW_OP_deref -- optional, if we start with a pointer
685 DW_OP_plus_uconst <forward_fld_offset>
686 DW_OP_deref
687 DW_OP_plus_uconst <varName_fld_offset>
688
689 That is what this function does. */
690
Devang Patel2c4ceb12009-11-21 02:48:08 +0000691/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000692/// provided, and generate the DWARF information necessary to find the
693/// actual Block variable (navigating the Block struct) based on the
694/// starting location. Add the DWARF information to the die. For
695/// more information, read large comment just above here.
696///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000697void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000698 unsigned Attribute,
699 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000700 const DIVariable &VD = DV->getVariable();
701 DIType Ty = VD.getType();
702 DIType TmpTy = Ty;
703 unsigned Tag = Ty.getTag();
704 bool isPointer = false;
705
Devang Patel65dbc902009-11-25 17:36:49 +0000706 StringRef varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000707
708 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patel2db49d72010-05-07 18:11:54 +0000709 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000710 TmpTy = DTy.getTypeDerivedFrom();
711 isPointer = true;
712 }
713
Devang Patel2db49d72010-05-07 18:11:54 +0000714 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000715
Daniel Dunbar00564992009-09-19 20:40:14 +0000716 // Find the __forwarding field and the variable field in the __Block_byref
717 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000718 DIArray Fields = blockStruct.getTypeArray();
719 DIDescriptor varField = DIDescriptor();
720 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000721
Daniel Dunbar00564992009-09-19 20:40:14 +0000722 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
723 DIDescriptor Element = Fields.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +0000724 DIDerivedType DT = DIDerivedType(Element);
Devang Patel65dbc902009-11-25 17:36:49 +0000725 StringRef fieldName = DT.getName();
726 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000727 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000728 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000729 varField = Element;
730 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000731
Daniel Dunbar00564992009-09-19 20:40:14 +0000732 // Get the offsets for the forwarding field and the variable field.
Chris Lattner1d65ba72010-03-31 06:06:37 +0000733 unsigned forwardingFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000734 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner1d65ba72010-03-31 06:06:37 +0000735 unsigned varFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000736 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000737
Mike Stump7e3720d2009-09-24 23:21:26 +0000738 // Decode the original location, and use that as the start of the byref
739 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000740 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar00564992009-09-19 20:40:14 +0000741 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000742 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000743
Daniel Dunbar00564992009-09-19 20:40:14 +0000744 if (Location.isReg()) {
745 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000746 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000747 else {
748 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000749 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
750 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000751 }
752 } else {
753 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000754 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000755 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000756 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
757 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000758 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000759
Devang Patel2c4ceb12009-11-21 02:48:08 +0000760 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000761 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000762
Mike Stump7e3720d2009-09-24 23:21:26 +0000763 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000764 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000765 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000766 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000767
Daniel Dunbar00564992009-09-19 20:40:14 +0000768 // Next add the offset for the '__forwarding' field:
769 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
770 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000771 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000772 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
773 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000774 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000775
Daniel Dunbar00564992009-09-19 20:40:14 +0000776 // Now dereference the __forwarding field to get to the real __Block_byref
777 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000778 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000779
Daniel Dunbar00564992009-09-19 20:40:14 +0000780 // Now that we've got the real __Block_byref... struct, add the offset
781 // for the variable's field to get to the location of the actual variable:
782 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000783 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000784 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
785 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000786 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000787
Daniel Dunbar00564992009-09-19 20:40:14 +0000788 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000789 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000790}
791
Devang Patel2c4ceb12009-11-21 02:48:08 +0000792/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000793/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000794void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000795 const MachineLocation &Location) {
Chris Lattnerd38fee82010-04-05 00:13:49 +0000796 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling0310d762009-05-15 09:23:25 +0000797 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000798 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bill Wendling0310d762009-05-15 09:23:25 +0000799
800 if (Location.isReg()) {
801 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000802 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000803 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000804 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
805 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000806 }
807 } else {
808 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000809 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000810 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000811 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
812 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000813 }
814
Devang Patel2c4ceb12009-11-21 02:48:08 +0000815 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000816 }
817
Devang Patel2c4ceb12009-11-21 02:48:08 +0000818 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000819}
820
Devang Patela43098d2010-04-28 01:03:09 +0000821/// addRegisterAddress - Add register location entry in variable DIE.
822bool DwarfDebug::addRegisterAddress(DIE *Die, DbgVariable *DV,
823 const MachineOperand &MO) {
824 assert (MO.isReg() && "Invalid machine operand!");
825 if (!MO.getReg())
826 return false;
827 MachineLocation Location;
828 Location.set(MO.getReg());
829 addAddress(Die, dwarf::DW_AT_location, Location);
830 if (MCSymbol *VS = DV->getDbgValueLabel())
831 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
832 return true;
833}
834
835/// addConstantValue - Add constant value entry in variable DIE.
836bool DwarfDebug::addConstantValue(DIE *Die, DbgVariable *DV,
837 const MachineOperand &MO) {
838 assert (MO.isImm() && "Invalid machine operand!");
839 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
840 unsigned Imm = MO.getImm();
841 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
842 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
843 if (MCSymbol *VS = DV->getDbgValueLabel())
844 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
845 return true;
846}
847
848/// addConstantFPValue - Add constant value entry in variable DIE.
849bool DwarfDebug::addConstantFPValue(DIE *Die, DbgVariable *DV,
850 const MachineOperand &MO) {
851 assert (MO.isFPImm() && "Invalid machine operand!");
852 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
853 APFloat FPImm = MO.getFPImm()->getValueAPF();
854
855 // Get the raw data form of the floating point.
856 const APInt FltVal = FPImm.bitcastToAPInt();
857 const char *FltPtr = (const char*)FltVal.getRawData();
858
859 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
860 bool LittleEndian = Asm->getTargetData().isLittleEndian();
861 int Incr = (LittleEndian ? 1 : -1);
862 int Start = (LittleEndian ? 0 : NumBytes - 1);
863 int Stop = (LittleEndian ? NumBytes : -1);
864
865 // Output the constant to DWARF one byte at a time.
866 for (; Start != Stop; Start += Incr)
867 addUInt(Block, 0, dwarf::DW_FORM_data1,
868 (unsigned char)0xFF & FltPtr[Start]);
869
870 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
871
872 if (MCSymbol *VS = DV->getDbgValueLabel())
873 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
874 VS);
875 return true;
876}
877
878
Devang Patelc366f832009-12-10 19:14:49 +0000879/// addToContextOwner - Add Die into the list of its context owner's children.
880void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3c91b052010-03-08 20:52:55 +0000881 if (Context.isType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000882 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patelc366f832009-12-10 19:14:49 +0000883 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000884 } else if (Context.isNameSpace()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000885 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel6404e4e2009-12-15 19:16:48 +0000886 ContextDIE->addChild(Die);
Devang Patel163a9f72010-05-10 22:49:55 +0000887 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patelc366f832009-12-10 19:14:49 +0000888 ContextDIE->addChild(Die);
889 else
Devang Patel163a9f72010-05-10 22:49:55 +0000890 getCompileUnit(Context)->addDie(Die);
Devang Patelc366f832009-12-10 19:14:49 +0000891}
892
Devang Patel16ced732009-12-10 18:05:33 +0000893/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
894/// given DIType.
895DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel163a9f72010-05-10 22:49:55 +0000896 CompileUnit *TypeCU = getCompileUnit(Ty);
897 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patel16ced732009-12-10 18:05:33 +0000898 if (TyDIE)
899 return TyDIE;
900
901 // Create new type.
902 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel163a9f72010-05-10 22:49:55 +0000903 TypeCU->insertDIE(Ty, TyDIE);
Devang Patel16ced732009-12-10 18:05:33 +0000904 if (Ty.isBasicType())
Devang Patel2db49d72010-05-07 18:11:54 +0000905 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000906 else if (Ty.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +0000907 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000908 else {
909 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel2db49d72010-05-07 18:11:54 +0000910 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000911 }
912
Devang Patelc366f832009-12-10 19:14:49 +0000913 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000914 return TyDIE;
915}
916
Devang Patel2c4ceb12009-11-21 02:48:08 +0000917/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000918void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9c004872010-05-07 21:45:47 +0000919 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000920 return;
921
922 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000923 CompileUnit *TypeCU = getCompileUnit(Ty);
924 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000925 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000926 if (Entry) {
927 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000928 return;
929 }
930
Bill Wendling0310d762009-05-15 09:23:25 +0000931 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000932 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000933
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000934 // Set up proxy.
935 Entry = createDIEEntry(Buffer);
Devang Patel163a9f72010-05-10 22:49:55 +0000936 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000937
Devang Patel2c4ceb12009-11-21 02:48:08 +0000938 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000939}
940
Devang Patel2c4ceb12009-11-21 02:48:08 +0000941/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000942void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000943 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000944 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000945 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000946 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000947 BTy.getEncoding());
948
949 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000950 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000951 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000952 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000953 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000954}
955
Devang Patel2c4ceb12009-11-21 02:48:08 +0000956/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000957void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000958 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000959 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000960 uint64_t Size = DTy.getSizeInBits() >> 3;
961 unsigned Tag = DTy.getTag();
962
963 // FIXME - Workaround for templates.
964 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
965
966 Buffer.setTag(Tag);
967
968 // Map to main type, void will not have a type.
969 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +0000970 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000971
972 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000973 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000974 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000975
976 // Add size if non-zero (derived types might be zero-sized.)
977 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000978 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000979
980 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +0000981 if (!DTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000982 addSourceLine(&Buffer, &DTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000983}
984
Devang Patel2c4ceb12009-11-21 02:48:08 +0000985/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +0000986void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000987 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000988 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000989
990 uint64_t Size = CTy.getSizeInBits() >> 3;
991 unsigned Tag = CTy.getTag();
992 Buffer.setTag(Tag);
993
994 switch (Tag) {
995 case dwarf::DW_TAG_vector_type:
996 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +0000997 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000998 break;
999 case dwarf::DW_TAG_enumeration_type: {
1000 DIArray Elements = CTy.getTypeArray();
1001
1002 // Add enumerators to enumeration type.
1003 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1004 DIE *ElemDie = NULL;
Devang Patel2db49d72010-05-07 18:11:54 +00001005 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3c91b052010-03-08 20:52:55 +00001006 if (Enum.isEnumerator()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001007 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001008 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +00001009 }
Bill Wendling0310d762009-05-15 09:23:25 +00001010 }
1011 }
1012 break;
1013 case dwarf::DW_TAG_subroutine_type: {
1014 // Add return type.
1015 DIArray Elements = CTy.getTypeArray();
1016 DIDescriptor RTy = Elements.getElement(0);
Devang Patel2db49d72010-05-07 18:11:54 +00001017 addType(&Buffer, DIType(RTy));
Bill Wendling0310d762009-05-15 09:23:25 +00001018
1019 // Add prototype flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001020 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001021
1022 // Add arguments.
1023 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1024 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1025 DIDescriptor Ty = Elements.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +00001026 addType(Arg, DIType(Ty));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001027 Buffer.addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001028 }
1029 }
1030 break;
1031 case dwarf::DW_TAG_structure_type:
1032 case dwarf::DW_TAG_union_type:
1033 case dwarf::DW_TAG_class_type: {
1034 // Add elements to structure type.
1035 DIArray Elements = CTy.getTypeArray();
1036
1037 // A forward struct declared type may not have elements available.
Devang Patel3c91b052010-03-08 20:52:55 +00001038 unsigned N = Elements.getNumElements();
1039 if (N == 0)
Bill Wendling0310d762009-05-15 09:23:25 +00001040 break;
1041
1042 // Add elements to structure type.
Devang Patel3c91b052010-03-08 20:52:55 +00001043 for (unsigned i = 0; i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001044 DIDescriptor Element = Elements.getElement(i);
1045 DIE *ElemDie = NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001046 if (Element.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001047 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel3c91b052010-03-08 20:52:55 +00001048 else if (Element.isVariable()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001049 DIVariable DV(Element);
Devang Patel1ee0cb92010-01-30 01:08:30 +00001050 ElemDie = new DIE(dwarf::DW_TAG_variable);
1051 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1052 DV.getName());
1053 addType(ElemDie, DV.getType());
1054 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1055 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1056 addSourceLine(ElemDie, &DV);
Devang Patel3c91b052010-03-08 20:52:55 +00001057 } else if (Element.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001058 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3c91b052010-03-08 20:52:55 +00001059 else
1060 continue;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001061 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001062 }
1063
Devang Patela1ba2692009-08-27 23:51:51 +00001064 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001065 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001066
1067 unsigned RLang = CTy.getRunTimeLang();
1068 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001069 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +00001070 dwarf::DW_FORM_data1, RLang);
Devang Patelb5544992010-01-26 21:16:06 +00001071
1072 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel2db49d72010-05-07 18:11:54 +00001073 if (DIDescriptor(ContainingType).isCompositeType())
Devang Patelb5544992010-01-26 21:16:06 +00001074 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patel2db49d72010-05-07 18:11:54 +00001075 getOrCreateTypeDIE(DIType(ContainingType)));
Bill Wendling0310d762009-05-15 09:23:25 +00001076 break;
1077 }
1078 default:
1079 break;
1080 }
1081
1082 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +00001083 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001084 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001085
Devang Patel8cbb6122010-01-29 18:34:58 +00001086 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendling0310d762009-05-15 09:23:25 +00001087 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1088 // Add size if non-zero (derived types might be zero-sized.)
1089 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001090 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001091 else {
1092 // Add zero size if it is not a forward declaration.
1093 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001094 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001095 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001096 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +00001097 }
1098
1099 // Add source line info if available.
1100 if (!CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001101 addSourceLine(&Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001102 }
1103}
1104
Devang Patel2c4ceb12009-11-21 02:48:08 +00001105/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1106void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +00001107 int64_t L = SR.getLo();
1108 int64_t H = SR.getHi();
1109 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1110
Devang Patel2c4ceb12009-11-21 02:48:08 +00001111 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +00001112 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001113 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +00001114 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001115
Devang Patel2c4ceb12009-11-21 02:48:08 +00001116 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +00001117}
1118
Devang Patel2c4ceb12009-11-21 02:48:08 +00001119/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001120void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +00001121 DICompositeType *CTy) {
1122 Buffer.setTag(dwarf::DW_TAG_array_type);
1123 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001124 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001125
1126 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +00001127 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001128 DIArray Elements = CTy->getTypeArray();
1129
Devang Patel6f01d9c2009-11-21 00:31:03 +00001130 // Get an anonymous type for index type.
Devang Patel163a9f72010-05-10 22:49:55 +00001131 CompileUnit *TheCU = getCompileUnit(*CTy);
1132 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +00001133 if (!IdxTy) {
1134 // Construct an anonymous type for index type.
1135 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001136 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1137 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001138 dwarf::DW_ATE_signed);
Devang Patel163a9f72010-05-10 22:49:55 +00001139 TheCU->addDie(IdxTy);
1140 TheCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001141 }
Bill Wendling0310d762009-05-15 09:23:25 +00001142
1143 // Add subranges to array type.
1144 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1145 DIDescriptor Element = Elements.getElement(i);
1146 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2db49d72010-05-07 18:11:54 +00001147 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001148 }
1149}
1150
Devang Patel2c4ceb12009-11-21 02:48:08 +00001151/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3c91b052010-03-08 20:52:55 +00001152DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001153 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3c91b052010-03-08 20:52:55 +00001154 StringRef Name = ETy.getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001155 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3c91b052010-03-08 20:52:55 +00001156 int64_t Value = ETy.getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001157 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001158 return Enumerator;
1159}
1160
Devang Patel351ca332010-01-05 01:46:14 +00001161/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1162/// printer to not emit usual symbol prefix before the symbol name is used then
1163/// return linkage name after skipping this special LLVM prefix.
1164static StringRef getRealLinkageName(StringRef LinkageName) {
1165 char One = '\1';
1166 if (LinkageName.startswith(StringRef(&One, 1)))
1167 return LinkageName.substr(1);
1168 return LinkageName;
1169}
1170
Devang Patel2c4ceb12009-11-21 02:48:08 +00001171/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patel8a241142009-12-09 18:24:21 +00001172DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001173 // If the global variable was optmized out then no need to create debug info
1174 // entry.
Devang Patel84c73e92009-11-06 17:58:12 +00001175 if (!GV.getGlobal()) return NULL;
Devang Patel65dbc902009-11-25 17:36:49 +00001176 if (GV.getDisplayName().empty()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001177
Bill Wendling0310d762009-05-15 09:23:25 +00001178 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001179 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001180 GV.getDisplayName());
1181
Devang Patel65dbc902009-11-25 17:36:49 +00001182 StringRef LinkageName = GV.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001183 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001184 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001185 getRealLinkageName(LinkageName));
1186
Devang Patel8a241142009-12-09 18:24:21 +00001187 addType(GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001188 if (!GV.isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001189 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1190 addSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001191
Bill Wendling0310d762009-05-15 09:23:25 +00001192 return GVDie;
1193}
1194
Devang Patel2c4ceb12009-11-21 02:48:08 +00001195/// createMemberDIE - Create new member DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001196DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001197 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001198 StringRef Name = DT.getName();
1199 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001200 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel65dbc902009-11-25 17:36:49 +00001201
Devang Patel8a241142009-12-09 18:24:21 +00001202 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001203
Devang Patel2c4ceb12009-11-21 02:48:08 +00001204 addSourceLine(MemberDie, &DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001205
Benjamin Kramer345ef342010-03-31 19:34:01 +00001206 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001207 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001208
Bill Wendling0310d762009-05-15 09:23:25 +00001209 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001210 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001211
1212 if (Size != FieldSize) {
1213 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001214 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1215 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001216
1217 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling0310d762009-05-15 09:23:25 +00001218 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1219 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer3d594fd2010-01-07 17:50:57 +00001220 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling0310d762009-05-15 09:23:25 +00001221 Offset -= FieldOffset;
1222
1223 // Maybe we need to work from the other end.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001224 if (Asm->getTargetData().isLittleEndian())
1225 Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001226 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001227
Devang Patel33db5082009-11-04 22:06:12 +00001228 // Here WD_AT_data_member_location points to the anonymous
1229 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001230 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001231
1232 } else
1233 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001234 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001235
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001236 if (DT.getTag() == dwarf::DW_TAG_inheritance
1237 && DT.isVirtual()) {
1238
1239 // For C++, virtual base classes are not at fixed offset. Use following
1240 // expression to extract appropriate offset from vtable.
1241 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1242
Benjamin Kramer345ef342010-03-31 19:34:01 +00001243 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001244 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1245 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1246 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1247 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1248 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1249 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1250 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1251
1252 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1253 VBaseLocationDie);
1254 } else
1255 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001256
1257 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001258 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001259 dwarf::DW_ACCESS_protected);
1260 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001261 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001262 dwarf::DW_ACCESS_private);
Devang Patel5d11eb02009-12-03 19:11:07 +00001263 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1264 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1265 dwarf::DW_ACCESS_public);
1266 if (DT.isVirtual())
1267 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1268 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001269 return MemberDie;
1270}
1271
Devang Patelffe966c2009-12-14 16:18:45 +00001272/// createSubprogramDIE - Create new DIE using SP.
1273DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
Devang Patel163a9f72010-05-10 22:49:55 +00001274 CompileUnit *SPCU = getCompileUnit(SP);
1275 DIE *SPDie = SPCU->getDIE(SP);
Devang Patelffe966c2009-12-14 16:18:45 +00001276 if (SPDie)
1277 return SPDie;
1278
1279 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1eac3e72010-03-02 17:58:15 +00001280 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +00001281 if (!SP.getName().empty())
1282 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001283
Devang Patel65dbc902009-11-25 17:36:49 +00001284 StringRef LinkageName = SP.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001285 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001286 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001287 getRealLinkageName(LinkageName));
1288
Devang Patel2c4ceb12009-11-21 02:48:08 +00001289 addSourceLine(SPDie, &SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001290
Bill Wendling0310d762009-05-15 09:23:25 +00001291 // Add prototyped tag, if C or ObjC.
1292 unsigned Lang = SP.getCompileUnit().getLanguage();
1293 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1294 Lang == dwarf::DW_LANG_ObjC)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001295 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001296
1297 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001298 DICompositeType SPTy = SP.getType();
1299 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001300 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001301
Devang Patel3c91b052010-03-08 20:52:55 +00001302 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001303 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001304 else
Devang Patel2db49d72010-05-07 18:11:54 +00001305 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001306
Devang Patel5d11eb02009-12-03 19:11:07 +00001307 unsigned VK = SP.getVirtuality();
1308 if (VK) {
1309 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001310 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel5d11eb02009-12-03 19:11:07 +00001311 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1312 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1313 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel622b0262010-01-19 06:19:05 +00001314 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel2db49d72010-05-07 18:11:54 +00001315 SP.getContainingType()));
Devang Patel5d11eb02009-12-03 19:11:07 +00001316 }
1317
Devang Patelffe966c2009-12-14 16:18:45 +00001318 if (MakeDecl || !SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001319 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001320
1321 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001322 // be handled while processing variables.
1323 DICompositeType SPTy = SP.getType();
1324 DIArray Args = SPTy.getTypeArray();
1325 unsigned SPTag = SPTy.getTag();
1326
Bill Wendling0310d762009-05-15 09:23:25 +00001327 if (SPTag == dwarf::DW_TAG_subroutine_type)
1328 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1329 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001330 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelb4645642010-02-06 01:02:37 +00001331 addType(Arg, ATy);
1332 if (ATy.isArtificial())
1333 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001334 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001335 }
1336 }
1337
Devang Patel4e0d19d2010-02-03 19:57:19 +00001338 if (SP.isArtificial())
1339 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1340
Devang Patelccff8122010-04-30 19:38:23 +00001341 if (!SP.isLocalToUnit())
1342 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel2a4a3b72010-04-19 19:14:02 +00001343
Devang Patelccff8122010-04-30 19:38:23 +00001344 if (SP.isOptimized())
1345 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1346
Devang Patelccff8122010-04-30 19:38:23 +00001347 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel163a9f72010-05-10 22:49:55 +00001348 SPCU->insertDIE(SP, SPDie);
Devang Patelccff8122010-04-30 19:38:23 +00001349
Bill Wendling0310d762009-05-15 09:23:25 +00001350 return SPDie;
1351}
1352
Devang Patele9f8f5e2010-05-07 20:54:48 +00001353DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattnered7a77b2010-03-31 05:36:29 +00001354 assert(N && "Invalid Scope encoding!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001355
1356 DbgScope *AScope = AbstractScopes.lookup(N);
1357 if (AScope)
1358 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001359
Devang Patel53bb5c92009-11-10 23:06:00 +00001360 DbgScope *Parent = NULL;
1361
1362 DIDescriptor Scope(N);
1363 if (Scope.isLexicalBlock()) {
1364 DILexicalBlock DB(N);
1365 DIDescriptor ParentDesc = DB.getContext();
Devang Patel2db49d72010-05-07 18:11:54 +00001366 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel53bb5c92009-11-10 23:06:00 +00001367 }
1368
1369 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1370
1371 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001372 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001373 AScope->setAbstractScope();
1374 AbstractScopes[N] = AScope;
1375 if (DIDescriptor(N).isSubprogram())
1376 AbstractScopesList.push_back(AScope);
1377 return AScope;
1378}
Devang Patelaf9e8472009-10-01 20:31:14 +00001379
Devang Patel5f094002010-04-06 23:53:48 +00001380/// isSubprogramContext - Return true if Context is either a subprogram
1381/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001382static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +00001383 if (!Context)
1384 return false;
1385 DIDescriptor D(Context);
1386 if (D.isSubprogram())
1387 return true;
1388 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001389 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +00001390 return false;
1391}
1392
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001393/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001394/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1395/// If there are global variables in this scope then create and insert
1396/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001397DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +00001398 CompileUnit *SPCU = getCompileUnit(SPNode);
1399 DIE *SPDie = SPCU->getDIE(SPNode);
Chris Lattnerd38fee82010-04-05 00:13:49 +00001400 assert(SPDie && "Unable to find subprogram DIE!");
1401 DISubprogram SP(SPNode);
Chris Lattner206d61e2010-03-13 07:26:18 +00001402
Chris Lattnerd38fee82010-04-05 00:13:49 +00001403 // There is not any need to generate specification DIE for a function
1404 // defined at compile unit level. If a function is defined inside another
1405 // function then gdb prefers the definition at top level and but does not
1406 // expect specification DIE in parent function. So avoid creating
1407 // specification DIE for a function defined inside a function.
1408 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Devang Patel5f094002010-04-06 23:53:48 +00001409 !SP.getContext().isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +00001410 !isSubprogramContext(SP.getContext())) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001411 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1412
1413 // Add arguments.
1414 DICompositeType SPTy = SP.getType();
1415 DIArray Args = SPTy.getTypeArray();
1416 unsigned SPTag = SPTy.getTag();
1417 if (SPTag == dwarf::DW_TAG_subroutine_type)
1418 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1419 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001420 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattnerd38fee82010-04-05 00:13:49 +00001421 addType(Arg, ATy);
1422 if (ATy.isArtificial())
1423 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1424 SPDie->addChild(Arg);
1425 }
1426 DIE *SPDeclDie = SPDie;
1427 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1428 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1429 SPDeclDie);
Devang Patel163a9f72010-05-10 22:49:55 +00001430 SPCU->addDie(SPDie);
Chris Lattnerd38fee82010-04-05 00:13:49 +00001431 }
1432
1433 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1434 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1435 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1436 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1437 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1438 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1439 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +00001440
Chris Lattnerd38fee82010-04-05 00:13:49 +00001441 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +00001442}
1443
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001444/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001445/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1446DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001447
1448 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1449 if (Scope->isAbstractScope())
1450 return ScopeDIE;
1451
1452 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1453 if (Ranges.empty())
1454 return 0;
1455
1456 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1457 if (Ranges.size() > 1) {
1458 // .debug_range section has not been laid out yet. Emit offset in
1459 // .debug_range as a uint, size 4, for now. emitDIE will handle
1460 // DW_AT_ranges appropriately.
1461 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1462 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1463 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1464 RE = Ranges.end(); RI != RE; ++RI) {
1465 DebugRangeSymbols.push_back(LabelsBeforeInsn.lookup(RI->first));
1466 DebugRangeSymbols.push_back(LabelsAfterInsn.lookup(RI->second));
1467 }
1468 DebugRangeSymbols.push_back(NULL);
1469 DebugRangeSymbols.push_back(NULL);
1470 return ScopeDIE;
1471 }
1472
1473 MCSymbol *Start = LabelsBeforeInsn.lookup(RI->first);
1474 MCSymbol *End = LabelsAfterInsn.lookup(RI->second);
1475
Devang Patelaead63c2010-03-29 22:59:58 +00001476 if (Start == 0 || End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +00001477
Chris Lattnerb7db7332010-03-09 01:58:53 +00001478 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1479 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattnera34ec2292010-03-09 01:51:43 +00001480
Devang Pateleac9c072010-04-27 19:46:33 +00001481 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1482 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +00001483
1484 return ScopeDIE;
1485}
1486
Devang Patel2c4ceb12009-11-21 02:48:08 +00001487/// constructInlinedScopeDIE - This scope represents inlined body of
1488/// a function. Construct DIE to represent this concrete inlined copy
1489/// of the function.
1490DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001491
1492 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1493 assert (Ranges.empty() == false
1494 && "DbgScope does not have instruction markers!");
1495
1496 // FIXME : .debug_inlined section specification does not clearly state how
1497 // to emit inlined scope that is split into multiple instruction ranges.
1498 // For now, use first instruction range and emit low_pc/high_pc pair and
1499 // corresponding .debug_inlined section entry for this pair.
1500 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1501 MCSymbol *StartLabel = LabelsBeforeInsn.lookup(RI->first);
1502 MCSymbol *EndLabel = LabelsAfterInsn.lookup(RI->second);
1503
1504 if (StartLabel == 0 || EndLabel == 0) {
1505 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1506 return 0;
1507 }
Chris Lattnerb7db7332010-03-09 01:58:53 +00001508 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001509 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +00001510 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001511 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +00001512
Devang Patel3c91b052010-03-08 20:52:55 +00001513 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +00001514 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001515 DIScope DS(Scope->getScopeNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001516 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1517
Devang Patel2db49d72010-05-07 18:11:54 +00001518 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel163a9f72010-05-10 22:49:55 +00001519 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1520 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001521 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001522 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001523 dwarf::DW_FORM_ref4, OriginDIE);
1524
Chris Lattner6ed0f902010-03-09 00:31:02 +00001525 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnerb7db7332010-03-09 01:58:53 +00001526 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel53bb5c92009-11-10 23:06:00 +00001527
1528 InlinedSubprogramDIEs.insert(OriginDIE);
1529
1530 // Track the start label for this inlined function.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001531 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +00001532 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001533
1534 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001535 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001536 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +00001537 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001538 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +00001539 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001540
Devang Patel53bb5c92009-11-10 23:06:00 +00001541 DILocation DL(Scope->getInlinedAt());
Devang Patel163a9f72010-05-10 22:49:55 +00001542 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001543 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001544
1545 return ScopeDIE;
1546}
1547
Devang Patel2c4ceb12009-11-21 02:48:08 +00001548
1549/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001550DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001551 // Get the descriptor.
1552 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001553 StringRef Name = VD.getName();
1554 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001555 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001556
1557 // Translate tag to proper Dwarf tag. The result variable is dropped for
1558 // now.
1559 unsigned Tag;
1560 switch (VD.getTag()) {
1561 case dwarf::DW_TAG_return_variable:
1562 return NULL;
1563 case dwarf::DW_TAG_arg_variable:
1564 Tag = dwarf::DW_TAG_formal_parameter;
1565 break;
1566 case dwarf::DW_TAG_auto_variable: // fall thru
1567 default:
1568 Tag = dwarf::DW_TAG_variable;
1569 break;
1570 }
1571
1572 // Define variable debug information entry.
1573 DIE *VariableDie = new DIE(Tag);
1574
1575
1576 DIE *AbsDIE = NULL;
1577 if (DbgVariable *AV = DV->getAbstractVariable())
1578 AbsDIE = AV->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001579
Devang Patel53bb5c92009-11-10 23:06:00 +00001580 if (AbsDIE) {
1581 DIScope DS(Scope->getScopeNode());
Devang Patel2db49d72010-05-07 18:11:54 +00001582 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel163a9f72010-05-10 22:49:55 +00001583 DIE *OriginSPDIE = getCompileUnit(InlinedSP)->getDIE(InlinedSP);
Daniel Dunbarc0326792009-11-11 03:09:50 +00001584 (void) OriginSPDIE;
Chris Lattnered7a77b2010-03-31 05:36:29 +00001585 assert(OriginSPDIE && "Unable to find Origin DIE for the SP!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001586 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
Chris Lattnered7a77b2010-03-31 05:36:29 +00001587 assert(AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001588 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001589 dwarf::DW_FORM_ref4, AbsDIE);
1590 }
1591 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001592 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1593 addSourceLine(VariableDie, &VD);
Devang Patel53bb5c92009-11-10 23:06:00 +00001594
1595 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001596 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001597 // addresses instead.
1598 if (VD.isBlockByrefVariable())
Devang Patel8a241142009-12-09 18:24:21 +00001599 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel53bb5c92009-11-10 23:06:00 +00001600 else
Devang Patel8a241142009-12-09 18:24:21 +00001601 addType(VariableDie, VD.getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001602 }
1603
1604 // Add variable address.
1605 if (!Scope->isAbstractScope()) {
Devang Patel90a48ad2010-03-15 18:33:46 +00001606 // Check if variable is described by DBG_VALUE instruction.
Devang Patela43098d2010-04-28 01:03:09 +00001607 if (const MachineInstr *DVInsn = DV->getDbgValue()) {
1608 bool updated = false;
1609 // FIXME : Handle getNumOperands != 3
1610 if (DVInsn->getNumOperands() == 3) {
1611 if (DVInsn->getOperand(0).isReg())
1612 updated = addRegisterAddress(VariableDie, DV, DVInsn->getOperand(0));
1613 else if (DVInsn->getOperand(0).isImm())
1614 updated = addConstantValue(VariableDie, DV, DVInsn->getOperand(0));
1615 else if (DVInsn->getOperand(0).isFPImm())
1616 updated = addConstantFPValue(VariableDie, DV, DVInsn->getOperand(0));
Devang Patel28ff35d2010-04-28 01:39:28 +00001617 } else {
1618 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1619 if (Location.getReg()) {
1620 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1621 if (MCSymbol *VS = DV->getDbgValueLabel())
1622 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1623 VS);
1624 updated = true;
1625 }
Devang Patela43098d2010-04-28 01:03:09 +00001626 }
1627 if (!updated) {
1628 // If variableDie is not updated then DBG_VALUE instruction does not
1629 // have valid variable info.
1630 delete VariableDie;
1631 return NULL;
1632 }
1633 }
1634 else {
Devang Patel90a48ad2010-03-15 18:33:46 +00001635 MachineLocation Location;
1636 unsigned FrameReg;
Chris Lattnerd38fee82010-04-05 00:13:49 +00001637 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Devang Patel98e1cac2010-05-14 21:01:35 +00001638 if (DV->hasLocation()) {
1639 int Offset = RI->getFrameIndexReference(*Asm->MF, DV->getFrameIndex(),
1640 FrameReg);
1641 Location.set(FrameReg, Offset);
1642
1643 if (VD.hasComplexAddress())
1644 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1645 else if (VD.isBlockByrefVariable())
1646 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1647 else
1648 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1649 }
Devang Patel90a48ad2010-03-15 18:33:46 +00001650 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001651 }
Devang Patelb4645642010-02-06 01:02:37 +00001652
1653 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1654 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel53bb5c92009-11-10 23:06:00 +00001655 DV->setDIE(VariableDie);
1656 return VariableDie;
1657
1658}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001659
Devang Patel193f7202009-11-24 01:14:22 +00001660void DwarfDebug::addPubTypes(DISubprogram SP) {
1661 DICompositeType SPTy = SP.getType();
1662 unsigned SPTag = SPTy.getTag();
1663 if (SPTag != dwarf::DW_TAG_subroutine_type)
1664 return;
1665
1666 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +00001667 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel2db49d72010-05-07 18:11:54 +00001668 DIType ATy(Args.getElement(i));
Devang Patel9c004872010-05-07 21:45:47 +00001669 if (!ATy.Verify())
Devang Patel193f7202009-11-24 01:14:22 +00001670 continue;
1671 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel2db49d72010-05-07 18:11:54 +00001672 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel50d80e32010-04-13 20:35:04 +00001673 && !CATy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001674 CompileUnit *TheCU = getCompileUnit(CATy);
1675 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1676 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001677 }
1678 }
1679}
1680
Devang Patel2c4ceb12009-11-21 02:48:08 +00001681/// constructScopeDIE - Construct a DIE for this scope.
1682DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +00001683 if (!Scope || !Scope->getScopeNode())
1684 return NULL;
1685
1686 DIScope DS(Scope->getScopeNode());
1687 DIE *ScopeDIE = NULL;
1688 if (Scope->getInlinedAt())
1689 ScopeDIE = constructInlinedScopeDIE(Scope);
1690 else if (DS.isSubprogram()) {
1691 if (Scope->isAbstractScope())
Devang Patel163a9f72010-05-10 22:49:55 +00001692 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +00001693 else
Devang Patel2db49d72010-05-07 18:11:54 +00001694 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +00001695 }
Devang Patelaead63c2010-03-29 22:59:58 +00001696 else
Devang Patel3c91b052010-03-08 20:52:55 +00001697 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patelaead63c2010-03-29 22:59:58 +00001698 if (!ScopeDIE) return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001699
Devang Patel53bb5c92009-11-10 23:06:00 +00001700 // Add variables to scope.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001701 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel53bb5c92009-11-10 23:06:00 +00001702 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001703 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001704 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001705 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001706 }
1707
1708 // Add nested scopes.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001709 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel53bb5c92009-11-10 23:06:00 +00001710 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1711 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001712 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001713 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001714 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001715 }
Devang Patel193f7202009-11-24 01:14:22 +00001716
1717 if (DS.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001718 addPubTypes(DISubprogram(DS));
Devang Patel193f7202009-11-24 01:14:22 +00001719
1720 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001721}
1722
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001723/// GetOrCreateSourceID - Look up the source id with the given directory and
1724/// source file names. If none currently exists, create a new id and insert it
1725/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1726/// maps as well.
Chris Lattner1d65ba72010-03-31 06:06:37 +00001727unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001728 unsigned DId;
1729 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1730 if (DI != DirectoryIdMap.end()) {
1731 DId = DI->getValue();
1732 } else {
1733 DId = DirectoryNames.size() + 1;
1734 DirectoryIdMap[DirName] = DId;
1735 DirectoryNames.push_back(DirName);
1736 }
1737
1738 unsigned FId;
1739 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1740 if (FI != SourceFileIdMap.end()) {
1741 FId = FI->getValue();
1742 } else {
1743 FId = SourceFileNames.size() + 1;
1744 SourceFileIdMap[FileName] = FId;
1745 SourceFileNames.push_back(FileName);
1746 }
1747
1748 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1749 SourceIdMap.find(std::make_pair(DId, FId));
1750 if (SI != SourceIdMap.end())
1751 return SI->second;
1752
1753 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1754 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1755 SourceIds.push_back(std::make_pair(DId, FId));
1756
1757 return SrcId;
1758}
1759
Devang Patel6404e4e2009-12-15 19:16:48 +00001760/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1761DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel163a9f72010-05-10 22:49:55 +00001762 CompileUnit *TheCU = getCompileUnit(NS);
1763 DIE *NDie = TheCU->getDIE(NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001764 if (NDie)
1765 return NDie;
1766 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel163a9f72010-05-10 22:49:55 +00001767 TheCU->insertDIE(NS, NDie);
Devang Patel6404e4e2009-12-15 19:16:48 +00001768 if (!NS.getName().empty())
1769 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1770 addSourceLine(NDie, &NS);
1771 addToContextOwner(NDie, NS.getContext());
1772 return NDie;
1773}
1774
Devang Patel163a9f72010-05-10 22:49:55 +00001775/// constructCompileUnit - Create new CompileUnit for the given
1776/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001777void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001778 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001779 StringRef FN = DIUnit.getFilename();
1780 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001781 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001782
1783 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001784 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001785 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001786 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001787 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001788 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +00001789 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1790 // simplifies debug range entries.
1791 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_data4, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +00001792 // DW_AT_stmt_list is a offset of line number information for this
1793 // compile unit in debug_line section. It is always zero when only one
1794 // compile unit is emitted in one object file.
1795 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001796
Devang Patel65dbc902009-11-25 17:36:49 +00001797 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001798 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001799 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001800 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001801
Devang Patel65dbc902009-11-25 17:36:49 +00001802 StringRef Flags = DIUnit.getFlags();
1803 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001804 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001805
1806 unsigned RVer = DIUnit.getRunTimeVersion();
1807 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001808 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001809 dwarf::DW_FORM_data1, RVer);
1810
Devang Patel163a9f72010-05-10 22:49:55 +00001811 CompileUnit *NewCU = new CompileUnit(ID, Die);
1812 if (!FirstCU)
1813 FirstCU = NewCU;
1814 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001815}
1816
Devang Patel163a9f72010-05-10 22:49:55 +00001817/// getCompielUnit - Get CompileUnit DIE.
1818CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1819 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1820 DIDescriptor D(N);
1821 const MDNode *CUNode = NULL;
1822 if (D.isCompileUnit())
1823 CUNode = N;
1824 else if (D.isSubprogram())
1825 CUNode = DISubprogram(N).getCompileUnit();
1826 else if (D.isType())
1827 CUNode = DIType(N).getCompileUnit();
1828 else if (D.isGlobalVariable())
1829 CUNode = DIGlobalVariable(N).getCompileUnit();
1830 else if (D.isVariable())
1831 CUNode = DIVariable(N).getCompileUnit();
1832 else if (D.isNameSpace())
1833 CUNode = DINameSpace(N).getCompileUnit();
1834 else if (D.isFile())
1835 CUNode = DIFile(N).getCompileUnit();
1836 else
1837 return FirstCU;
1838
1839 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1840 = CUMap.find(CUNode);
1841 if (I == CUMap.end())
1842 return FirstCU;
1843 return I->second;
1844}
1845
1846
1847/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001848void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001849 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001850
Devang Patel905cf5e2009-09-04 23:59:07 +00001851 // If debug information is malformed then ignore it.
1852 if (DI_GV.Verify() == false)
1853 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001854
1855 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001856 CompileUnit *TheCU = getCompileUnit(N);
1857 if (TheCU->getDIE(DI_GV))
Devang Patel13e16b62009-06-26 01:49:18 +00001858 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001859
Devang Patel8a241142009-12-09 18:24:21 +00001860 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Pateledb45632009-12-10 23:25:41 +00001861 if (!VariableDie)
1862 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001863
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001864 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +00001865 TheCU->insertDIE(N, VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001866
1867 // Add to context owner.
Devang Patelc9b16cc2010-01-15 01:12:22 +00001868 DIDescriptor GVContext = DI_GV.getContext();
1869 // Do not create specification DIE if context is either compile unit
1870 // or a subprogram.
Chris Lattner1d65ba72010-03-31 06:06:37 +00001871 if (DI_GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel5f094002010-04-06 23:53:48 +00001872 !GVContext.isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +00001873 !isSubprogramContext(GVContext)) {
Devang Patel6404e4e2009-12-15 19:16:48 +00001874 // Create specification DIE.
1875 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1876 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1877 dwarf::DW_FORM_ref4, VariableDie);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001878 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel6404e4e2009-12-15 19:16:48 +00001879 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner4faf59a2010-03-08 22:31:46 +00001880 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattnerdeb0cba2010-03-12 21:09:07 +00001881 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel6404e4e2009-12-15 19:16:48 +00001882 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel8581e012010-02-09 01:58:33 +00001883 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel163a9f72010-05-10 22:49:55 +00001884 TheCU->addDie(VariableSpecDIE);
Devang Patel6404e4e2009-12-15 19:16:48 +00001885 } else {
Benjamin Kramer345ef342010-03-31 19:34:01 +00001886 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel6404e4e2009-12-15 19:16:48 +00001887 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner4faf59a2010-03-08 22:31:46 +00001888 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattnerdeb0cba2010-03-12 21:09:07 +00001889 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel6404e4e2009-12-15 19:16:48 +00001890 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1891 }
Devang Patelc9b16cc2010-01-15 01:12:22 +00001892 addToContextOwner(VariableDie, GVContext);
Devang Patelc366f832009-12-10 19:14:49 +00001893
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001894 // Expose as global. FIXME - need to check external flag.
Devang Patel163a9f72010-05-10 22:49:55 +00001895 TheCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel193f7202009-11-24 01:14:22 +00001896
1897 DIType GTy = DI_GV.getType();
Devang Patel50d80e32010-04-13 20:35:04 +00001898 if (GTy.isCompositeType() && !GTy.getName().empty()
1899 && !GTy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001900 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001901 assert(Entry && "Missing global type!");
Devang Patel163a9f72010-05-10 22:49:55 +00001902 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001903 }
Devang Patel13e16b62009-06-26 01:49:18 +00001904 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001905}
1906
Devang Patel163a9f72010-05-10 22:49:55 +00001907/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001908void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001909 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001910
Stuart Hastings639336e2010-04-06 21:38:29 +00001911 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001912 CompileUnit *TheCU = getCompileUnit(N);
1913 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +00001914 return;
1915
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001916 if (!SP.isDefinition())
1917 // This is a method declaration which will be handled while constructing
1918 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001919 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001920
Stuart Hastings639336e2010-04-06 21:38:29 +00001921 DIE *SubprogramDie = createSubprogramDIE(SP);
1922
1923 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +00001924 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001925
1926 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00001927 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00001928
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001929 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +00001930 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001931
Devang Patel13e16b62009-06-26 01:49:18 +00001932 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001933}
1934
Devang Patel2c4ceb12009-11-21 02:48:08 +00001935/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001936/// content. Create global DIEs and emit initial debug info sections.
1937/// This is inovked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +00001938void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +00001939 if (DisableDebugInfoPrinting)
1940 return;
1941
Devang Patel78ab9e22009-07-30 18:56:46 +00001942 DebugInfoFinder DbgFinder;
1943 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001944
Chris Lattnerd850ac72010-04-05 02:19:28 +00001945 bool HasDebugInfo = false;
1946
1947 // Scan all the compile-units to see if there are any marked as the main unit.
1948 // if not, we do not generate debug info.
1949 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1950 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1951 if (DICompileUnit(*I).isMain()) {
1952 HasDebugInfo = true;
1953 break;
1954 }
1955 }
1956
1957 if (!HasDebugInfo) return;
1958
1959 // Tell MMI that we have debug info.
1960 MMI->setDebugInfoAvailability(true);
1961
Chris Lattnerbe15beb2010-04-04 23:17:54 +00001962 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +00001963 EmitSectionLabels();
Chris Lattnerbe15beb2010-04-04 23:17:54 +00001964
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001965 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001966 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1967 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001968 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001969
Devang Patel53bb5c92009-11-10 23:06:00 +00001970 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001971 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1972 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001973 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00001974
Devang Patelc366f832009-12-10 19:14:49 +00001975 // Create DIEs for each global variable.
1976 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1977 E = DbgFinder.global_variable_end(); I != E; ++I)
1978 constructGlobalVariableDIE(*I);
1979
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001980 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001981 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001982
1983 // Print out .file directives to specify files for .loc directives. These are
1984 // printed out early so that they precede any .loc directives.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001985 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001986 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1987 // Remember source id starts at 1.
1988 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001989 // FIXME: don't use sys::path for this! This should not depend on the
1990 // host.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001991 sys::Path FullPath(getSourceDirectoryName(Id.first));
1992 bool AppendOk =
1993 FullPath.appendComponent(getSourceFileName(Id.second));
1994 assert(AppendOk && "Could not append filename to directory!");
1995 AppendOk = false;
Chris Lattnera6594fc2010-01-25 18:58:59 +00001996 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001997 }
1998 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001999}
2000
Devang Patel2c4ceb12009-11-21 02:48:08 +00002001/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002002///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002003void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +00002004 if (!FirstCU) return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002005
Devang Patel53bb5c92009-11-10 23:06:00 +00002006 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
2007 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
2008 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
2009 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002010 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00002011 }
2012
Devang Patele9f8f5e2010-05-07 20:54:48 +00002013 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00002014 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2015 DIE *SPDie = CI->first;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002016 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel5d11eb02009-12-03 19:11:07 +00002017 if (!N) continue;
Devang Patel163a9f72010-05-10 22:49:55 +00002018 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Patel5d11eb02009-12-03 19:11:07 +00002019 if (!NDie) continue;
2020 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00002021 }
2022
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002023 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002024 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002025 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002026 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002027 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002028
2029 // End text sections.
2030 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002031 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +00002032 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002033 }
2034
2035 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002036 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002037
2038 // Emit function debug frame information
2039 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2040 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002041 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002042
2043 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002044 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002045
2046 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00002047 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002048
2049 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002050 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002051
2052 // Emit source line correspondence into a debug line section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002053 emitDebugLines();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002054
2055 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002056 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002057
Devang Patel193f7202009-11-24 01:14:22 +00002058 // Emit info into a debug pubtypes section.
2059 emitDebugPubTypes();
2060
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002061 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002062 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002063
2064 // Emit info into a debug aranges section.
2065 EmitDebugARanges();
2066
2067 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002068 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002069
2070 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002071 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002072
2073 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002074 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002075
Chris Lattnerbc733f52010-03-13 02:17:42 +00002076 // Emit info into a debug str section.
2077 emitDebugStr();
2078
Devang Patel163a9f72010-05-10 22:49:55 +00002079 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2080 E = CUMap.end(); I != E; ++I)
2081 delete I->second;
2082 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002083}
2084
Devang Patel53bb5c92009-11-10 23:06:00 +00002085/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002086DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2087 unsigned FrameIdx,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002088 DebugLoc ScopeLoc) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002089
Devang Patel2db49d72010-05-07 18:11:54 +00002090 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00002091 if (AbsDbgVariable)
2092 return AbsDbgVariable;
2093
Devang Patel2db49d72010-05-07 18:11:54 +00002094 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002095 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00002096 if (!Scope)
2097 return NULL;
2098
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002099 AbsDbgVariable = new DbgVariable(Var, FrameIdx,
2100 NULL /* No more-abstract variable*/);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002101 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00002102 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00002103 return AbsDbgVariable;
2104}
2105
Devang Patel90a48ad2010-03-15 18:33:46 +00002106/// findAbstractVariable - Find abstract variable, if any, associated with Var.
2107/// FIXME : Refactor findAbstractVariable.
2108DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2109 const MachineInstr *MI,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002110 DebugLoc ScopeLoc) {
Devang Patel90a48ad2010-03-15 18:33:46 +00002111
Devang Patel2db49d72010-05-07 18:11:54 +00002112 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel90a48ad2010-03-15 18:33:46 +00002113 if (AbsDbgVariable)
2114 return AbsDbgVariable;
2115
Devang Patel2db49d72010-05-07 18:11:54 +00002116 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002117 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel90a48ad2010-03-15 18:33:46 +00002118 if (!Scope)
2119 return NULL;
2120
Devang Patelaead63c2010-03-29 22:59:58 +00002121 AbsDbgVariable = new DbgVariable(Var, MI,
Devang Patel90a48ad2010-03-15 18:33:46 +00002122 NULL /* No more-abstract variable*/);
2123 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00002124 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelaead63c2010-03-29 22:59:58 +00002125 DbgValueStartMap[MI] = AbsDbgVariable;
Devang Patel90a48ad2010-03-15 18:33:46 +00002126 return AbsDbgVariable;
2127}
2128
Devang Patel2c4ceb12009-11-21 02:48:08 +00002129/// collectVariableInfo - Populate DbgScope entries with variables' info.
Devang Patel98e1cac2010-05-14 21:01:35 +00002130void DwarfDebug::collectVariableInfo(const MachineFunction *MF) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002131 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patel98e1cac2010-05-14 21:01:35 +00002132 SmallPtrSet<const MDNode *, 16> Processed;
Devang Patele717faa2009-10-06 01:26:37 +00002133 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2134 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2135 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00002136 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00002137 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00002138 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002139 DIVariable DV(Var);
2140 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002141
Chris Lattnerde4845c2010-04-02 19:42:39 +00002142 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002143 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00002144 Scope = ConcreteScopes.lookup(IA);
2145 if (Scope == 0)
2146 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
2147
Devang Patelfb0ee432009-11-10 23:20:04 +00002148 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002149 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00002150 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00002151
Chris Lattnerde4845c2010-04-02 19:42:39 +00002152 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, VP.second);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002153 DbgVariable *RegVar = new DbgVariable(DV, VP.first, AbsDbgVariable);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002154 Scope->addVariable(RegVar);
Devang Patele717faa2009-10-06 01:26:37 +00002155 }
Devang Patel90a48ad2010-03-15 18:33:46 +00002156
2157 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002158 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel90a48ad2010-03-15 18:33:46 +00002159 I != E; ++I) {
2160 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2161 II != IE; ++II) {
2162 const MachineInstr *MInsn = II;
Chris Lattner14d750d2010-03-31 05:39:57 +00002163 if (!MInsn->isDebugValue())
Devang Patel90a48ad2010-03-15 18:33:46 +00002164 continue;
Devang Patelaead63c2010-03-29 22:59:58 +00002165
Devang Patel0f9d9522010-04-27 20:54:45 +00002166 // Ignore Undef values.
Devang Patel97303ee2010-04-27 22:04:41 +00002167 if (MInsn->getOperand(0).isReg() && !MInsn->getOperand(0).getReg())
Devang Patel0f9d9522010-04-27 20:54:45 +00002168 continue;
2169
Dan Gohman82d5eaf2010-04-17 16:43:55 +00002170 DIVariable DV(
Devang Patele9f8f5e2010-05-07 20:54:48 +00002171 const_cast<const MDNode *>(MInsn->getOperand(MInsn->getNumOperands() - 1)
Dan Gohman82d5eaf2010-04-17 16:43:55 +00002172 .getMetadata()));
Devang Patel90a48ad2010-03-15 18:33:46 +00002173 if (DV.getTag() == dwarf::DW_TAG_arg_variable) {
2174 // FIXME Handle inlined subroutine arguments.
2175 DbgVariable *ArgVar = new DbgVariable(DV, MInsn, NULL);
2176 CurrentFnDbgScope->addVariable(ArgVar);
Devang Patelaead63c2010-03-29 22:59:58 +00002177 DbgValueStartMap[MInsn] = ArgVar;
Devang Patel26c69952010-05-14 21:55:50 +00002178 Processed.insert(DV);
Devang Patel90a48ad2010-03-15 18:33:46 +00002179 continue;
2180 }
2181
2182 DebugLoc DL = MInsn->getDebugLoc();
2183 if (DL.isUnknown()) continue;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002184 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002185 if (const MDNode *IA = DL.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00002186 Scope = ConcreteScopes.lookup(IA);
2187 if (Scope == 0)
2188 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2189
Devang Patel90a48ad2010-03-15 18:33:46 +00002190 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002191 if (Scope == 0)
Devang Patel90a48ad2010-03-15 18:33:46 +00002192 continue;
2193
Devang Patel98e1cac2010-05-14 21:01:35 +00002194 Processed.insert(DV);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002195 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, MInsn, DL);
Devang Patel90a48ad2010-03-15 18:33:46 +00002196 DbgVariable *RegVar = new DbgVariable(DV, MInsn, AbsDbgVariable);
Devang Patelaead63c2010-03-29 22:59:58 +00002197 DbgValueStartMap[MInsn] = RegVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00002198 Scope->addVariable(RegVar);
2199 }
2200 }
Devang Patel98e1cac2010-05-14 21:01:35 +00002201
2202 // Collect info for variables that were optimized out.
2203 if (NamedMDNode *NMD =
2204 MF->getFunction()->getParent()->getNamedMetadata("llvm.dbg.lv")) {
2205 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2206 DIVariable DV(cast_or_null<MDNode>(NMD->getOperand(i)));
2207 if (!Processed.insert(DV))
2208 continue;
2209 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2210 if (Scope)
2211 Scope->addVariable(new DbgVariable(DV, ~0U, NULL));
2212 }
2213 }
2214
Devang Patele717faa2009-10-06 01:26:37 +00002215}
2216
Devang Patel553881b2010-03-29 17:20:31 +00002217/// beginScope - Process beginning of a scope.
2218void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patel553881b2010-03-29 17:20:31 +00002219 // Check location.
2220 DebugLoc DL = MI->getDebugLoc();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002221 if (DL.isUnknown()) {
Dan Gohman281d65d2010-05-07 01:08:53 +00002222 if (UnknownLocations) {
2223 // This instruction has no debug location. If the preceding instruction
2224 // did, emit debug location information to indicate that the debug
2225 // location is now unknown.
2226 MCSymbol *Label = NULL;
2227 if (DL == PrevInstLoc)
2228 Label = PrevLabel;
2229 else {
2230 Label = recordSourceLine(DL.getLine(), DL.getCol(), 0);
2231 PrevInstLoc = DL;
2232 PrevLabel = Label;
Dan Gohman281d65d2010-05-07 01:08:53 +00002233 LabelsBeforeInsn[MI] = Label;
Devang Patelf4ccaea2010-05-19 21:26:53 +00002234 }
Dan Gohman281d65d2010-05-07 01:08:53 +00002235 }
Dan Gohman75395842010-05-06 00:29:41 +00002236
Devang Patel553881b2010-03-29 17:20:31 +00002237 return;
Dan Gohman1cc0d622010-05-05 23:41:32 +00002238 }
Devang Patel553881b2010-03-29 17:20:31 +00002239
Devang Patele9f8f5e2010-05-07 20:54:48 +00002240 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Chris Lattnerde4845c2010-04-02 19:42:39 +00002241
2242 // FIXME: Should only verify each scope once!
2243 if (!DIScope(Scope).Verify())
Devang Patel553881b2010-03-29 17:20:31 +00002244 return;
Devang Patel553881b2010-03-29 17:20:31 +00002245
Devang Patelaead63c2010-03-29 22:59:58 +00002246 // DBG_VALUE instruction establishes new value.
Chris Lattner14d750d2010-03-31 05:39:57 +00002247 if (MI->isDebugValue()) {
Devang Patelaead63c2010-03-29 22:59:58 +00002248 DenseMap<const MachineInstr *, DbgVariable *>::iterator DI
2249 = DbgValueStartMap.find(MI);
2250 if (DI != DbgValueStartMap.end()) {
Devang Pateleac9c072010-04-27 19:46:33 +00002251 MCSymbol *Label = NULL;
2252 if (DL == PrevInstLoc)
2253 Label = PrevLabel;
2254 else {
2255 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2256 PrevInstLoc = DL;
2257 PrevLabel = Label;
Devang Patelf4ccaea2010-05-19 21:26:53 +00002258 LabelsBeforeInsn[MI] = Label;
Devang Pateleac9c072010-04-27 19:46:33 +00002259 }
Devang Patelaead63c2010-03-29 22:59:58 +00002260 DI->second->setDbgValueLabel(Label);
2261 }
Devang Patel7ed63112010-03-30 18:07:00 +00002262 return;
Devang Patelaead63c2010-03-29 22:59:58 +00002263 }
2264
Devang Patel553881b2010-03-29 17:20:31 +00002265 // Emit a label to indicate location change. This is used for line
Devang Pateleac9c072010-04-27 19:46:33 +00002266 // table even if this instruction does not start a new scope.
2267 MCSymbol *Label = NULL;
2268 if (DL == PrevInstLoc)
2269 Label = PrevLabel;
2270 else {
2271 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2272 PrevInstLoc = DL;
2273 PrevLabel = Label;
Devang Patelf4ccaea2010-05-19 21:26:53 +00002274 LabelsBeforeInsn[MI] = Label;
Devang Pateleac9c072010-04-27 19:46:33 +00002275 }
Devang Patel553881b2010-03-29 17:20:31 +00002276
Devang Patel0d20ac82009-10-06 01:50:42 +00002277}
2278
Devang Patel2c4ceb12009-11-21 02:48:08 +00002279/// endScope - Process end of a scope.
2280void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel1c246352010-04-08 16:50:29 +00002281 if (InsnsEndScopeSet.count(MI) != 0) {
2282 // Emit a label if this instruction ends a scope.
2283 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2284 Asm->OutStreamer.EmitLabel(Label);
Devang Pateleac9c072010-04-27 19:46:33 +00002285 LabelsAfterInsn[MI] = Label;
Devang Patel1c246352010-04-08 16:50:29 +00002286 }
Devang Patel53bb5c92009-11-10 23:06:00 +00002287}
2288
Devang Pateleac9c072010-04-27 19:46:33 +00002289/// getOrCreateDbgScope - Create DbgScope for the scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00002290DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002291 if (!InlinedAt) {
2292 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2293 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002294 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002295 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2296 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Pateleac9c072010-04-27 19:46:33 +00002297 if (DIDescriptor(Scope).isLexicalBlock()) {
2298 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002299 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Pateleac9c072010-04-27 19:46:33 +00002300 WScope->setParent(Parent);
2301 Parent->addScope(WScope);
2302 }
2303
2304 if (!WScope->getParent()) {
2305 StringRef SPName = DISubprogram(Scope).getLinkageName();
2306 if (SPName == Asm->MF->getFunction()->getName())
2307 CurrentFnDbgScope = WScope;
2308 }
2309
2310 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002311 }
2312
2313 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2314 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002315 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002316
2317 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2318 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2319 DILocation DL(InlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002320 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002321 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Pateleac9c072010-04-27 19:46:33 +00002322 WScope->setParent(Parent);
2323 Parent->addScope(WScope);
2324
2325 ConcreteScopes[InlinedAt] = WScope;
2326 getOrCreateAbstractScope(Scope);
2327
2328 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00002329}
2330
Devang Pateleac9c072010-04-27 19:46:33 +00002331/// hasValidLocation - Return true if debug location entry attached with
2332/// machine instruction encodes valid location info.
2333static bool hasValidLocation(LLVMContext &Ctx,
2334 const MachineInstr *MInsn,
Devang Patele9f8f5e2010-05-07 20:54:48 +00002335 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Pateleac9c072010-04-27 19:46:33 +00002336 if (MInsn->isDebugValue())
2337 return false;
2338 DebugLoc DL = MInsn->getDebugLoc();
2339 if (DL.isUnknown()) return false;
2340
Devang Patele9f8f5e2010-05-07 20:54:48 +00002341 const MDNode *S = DL.getScope(Ctx);
Devang Pateleac9c072010-04-27 19:46:33 +00002342
2343 // There is no need to create another DIE for compile unit. For all
2344 // other scopes, create one DbgScope now. This will be translated
2345 // into a scope DIE at the end.
2346 if (DIScope(S).isCompileUnit()) return false;
2347
2348 Scope = S;
2349 InlinedAt = DL.getInlinedAt(Ctx);
2350 return true;
2351}
2352
2353/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2354/// hierarchy.
2355static void calculateDominanceGraph(DbgScope *Scope) {
2356 assert (Scope && "Unable to calculate scop edominance graph!");
2357 SmallVector<DbgScope *, 4> WorkStack;
2358 WorkStack.push_back(Scope);
2359 unsigned Counter = 0;
2360 while (!WorkStack.empty()) {
2361 DbgScope *WS = WorkStack.back();
2362 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2363 bool visitedChildren = false;
2364 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2365 SE = Children.end(); SI != SE; ++SI) {
2366 DbgScope *ChildScope = *SI;
2367 if (!ChildScope->getDFSOut()) {
2368 WorkStack.push_back(ChildScope);
2369 visitedChildren = true;
2370 ChildScope->setDFSIn(++Counter);
2371 break;
2372 }
2373 }
2374 if (!visitedChildren) {
2375 WorkStack.pop_back();
2376 WS->setDFSOut(++Counter);
2377 }
2378 }
2379}
2380
2381/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
2382static
2383void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2384 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2385{
2386#ifndef NDEBUG
2387 unsigned PrevDFSIn = 0;
2388 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2389 I != E; ++I) {
2390 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2391 II != IE; ++II) {
2392 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002393 const MDNode *Scope = NULL;
2394 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002395
2396 // Check if instruction has valid location information.
2397 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2398 dbgs() << " [ ";
2399 if (InlinedAt)
2400 dbgs() << "*";
2401 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
2402 MI2ScopeMap.find(MInsn);
2403 if (DI != MI2ScopeMap.end()) {
2404 DbgScope *S = DI->second;
2405 dbgs() << S->getDFSIn();
2406 PrevDFSIn = S->getDFSIn();
2407 } else
2408 dbgs() << PrevDFSIn;
2409 } else
2410 dbgs() << " [ x" << PrevDFSIn;
2411 dbgs() << " ]";
2412 MInsn->dump();
2413 }
2414 dbgs() << "\n";
2415 }
2416#endif
2417}
Devang Patel2c4ceb12009-11-21 02:48:08 +00002418/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00002419/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00002420bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00002421 // If scope information was extracted using .dbg intrinsics then there is not
2422 // any need to extract these information by scanning each instruction.
2423 if (!DbgScopeMap.empty())
2424 return false;
2425
Dan Gohman314bf7c2010-04-23 01:18:53 +00002426 // Scan each instruction and create scopes. First build working set of scopes.
Devang Pateleac9c072010-04-27 19:46:33 +00002427 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2428 SmallVector<DbgRange, 4> MIRanges;
2429 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002430 const MDNode *PrevScope = NULL;
2431 const MDNode *PrevInlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002432 const MachineInstr *RangeBeginMI = NULL;
2433 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002434 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00002435 I != E; ++I) {
2436 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2437 II != IE; ++II) {
2438 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002439 const MDNode *Scope = NULL;
2440 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002441
2442 // Check if instruction has valid location information.
2443 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2444 PrevMI = MInsn;
2445 continue;
2446 }
Chris Lattnerde4845c2010-04-02 19:42:39 +00002447
Devang Pateleac9c072010-04-27 19:46:33 +00002448 // If scope has not changed then skip this instruction.
2449 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2450 PrevMI = MInsn;
2451 continue;
2452 }
2453
2454 if (RangeBeginMI) {
2455 // If we have alread seen a beginning of a instruction range and
2456 // current instruction scope does not match scope of first instruction
2457 // in this range then create a new instruction range.
2458 DbgRange R(RangeBeginMI, PrevMI);
2459 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
2460 MIRanges.push_back(R);
2461 }
2462
2463 // This is a beginning of a new instruction range.
2464 RangeBeginMI = MInsn;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002465
Devang Pateleac9c072010-04-27 19:46:33 +00002466 // Reset previous markers.
2467 PrevMI = MInsn;
2468 PrevScope = Scope;
2469 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00002470 }
2471 }
2472
Devang Pateleac9c072010-04-27 19:46:33 +00002473 // Create last instruction range.
2474 if (RangeBeginMI && PrevMI && PrevScope) {
2475 DbgRange R(RangeBeginMI, PrevMI);
2476 MIRanges.push_back(R);
2477 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00002478 }
Devang Pateleac9c072010-04-27 19:46:33 +00002479
Devang Patel344130e2010-01-04 20:44:00 +00002480 if (!CurrentFnDbgScope)
2481 return false;
2482
Devang Pateleac9c072010-04-27 19:46:33 +00002483 calculateDominanceGraph(CurrentFnDbgScope);
2484 if (PrintDbgScope)
2485 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2486
2487 // Find ranges of instructions covered by each DbgScope;
2488 DbgScope *PrevDbgScope = NULL;
2489 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2490 RE = MIRanges.end(); RI != RE; ++RI) {
2491 const DbgRange &R = *RI;
2492 DbgScope *S = MI2ScopeMap.lookup(R.first);
2493 assert (S && "Lost DbgScope for a machine instruction!");
2494 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2495 PrevDbgScope->closeInsnRange(S);
2496 S->openInsnRange(R.first);
2497 S->extendInsnRange(R.second);
2498 PrevDbgScope = S;
2499 }
2500
2501 if (PrevDbgScope)
2502 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00002503
Devang Patele37b0c62010-04-08 18:43:56 +00002504 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00002505
2506 return !DbgScopeMap.empty();
2507}
2508
Devang Pateleac9c072010-04-27 19:46:33 +00002509/// identifyScopeMarkers() -
2510/// Each DbgScope has first instruction and last instruction to mark beginning
2511/// and end of a scope respectively. Create an inverse map that list scopes
2512/// starts (and ends) with an instruction. One instruction may start (or end)
2513/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00002514void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00002515 SmallVector<DbgScope *, 4> WorkList;
2516 WorkList.push_back(CurrentFnDbgScope);
2517 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00002518 DbgScope *S = WorkList.pop_back_val();
Devang Pateleac9c072010-04-27 19:46:33 +00002519
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002520 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patel42aafd72010-01-20 02:05:23 +00002521 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002522 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00002523 SE = Children.end(); SI != SE; ++SI)
2524 WorkList.push_back(*SI);
2525
Devang Patel53bb5c92009-11-10 23:06:00 +00002526 if (S->isAbstractScope())
2527 continue;
Devang Pateleac9c072010-04-27 19:46:33 +00002528
2529 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2530 if (Ranges.empty())
2531 continue;
2532 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2533 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelf4ccaea2010-05-19 21:26:53 +00002534 assert(RI->first && "DbgRange does not have first instruction!");
2535 assert(RI->second && "DbgRange does not have second instruction!");
Devang Pateleac9c072010-04-27 19:46:33 +00002536 InsnsEndScopeSet.insert(RI->second);
2537 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002538 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002539}
2540
Dan Gohman084751c2010-04-20 00:37:27 +00002541/// FindFirstDebugLoc - Find the first debug location in the function. This
2542/// is intended to be an approximation for the source position of the
2543/// beginning of the function.
2544static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2545 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2546 I != E; ++I)
2547 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2548 MBBI != MBBE; ++MBBI) {
2549 DebugLoc DL = MBBI->getDebugLoc();
2550 if (!DL.isUnknown())
2551 return DL;
2552 }
2553 return DebugLoc();
2554}
2555
Devang Patel2c4ceb12009-11-21 02:48:08 +00002556/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002557/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00002558void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00002559 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00002560 if (!extractScopeInformation()) return;
Chris Lattnera909d662010-03-29 20:38:20 +00002561
Devang Patel98e1cac2010-05-14 21:01:35 +00002562 collectVariableInfo(MF);
Devang Patel60b35bd2009-10-06 18:37:31 +00002563
Devang Pateleac9c072010-04-27 19:46:33 +00002564 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2565 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002566 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00002567 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002568
2569 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2570 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00002571 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002572 if (FDL.isUnknown()) return;
2573
Devang Patele9f8f5e2010-05-07 20:54:48 +00002574 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Chris Lattnerde4845c2010-04-02 19:42:39 +00002575
2576 DISubprogram SP = getDISubprogram(Scope);
2577 unsigned Line, Col;
2578 if (SP.Verify()) {
2579 Line = SP.getLineNumber();
2580 Col = 0;
2581 } else {
2582 Line = FDL.getLine();
2583 Col = FDL.getCol();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002584 }
Chris Lattnerde4845c2010-04-02 19:42:39 +00002585
2586 recordSourceLine(Line, Col, Scope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002587}
2588
Devang Patel2c4ceb12009-11-21 02:48:08 +00002589/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002590///
Chris Lattnereec791a2010-01-26 23:18:02 +00002591void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002592 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002593
Devang Patel344130e2010-01-04 20:44:00 +00002594 if (CurrentFnDbgScope) {
2595 // Define end label for subprogram.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002596 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_end",
2597 Asm->getFunctionNumber()));
Devang Patel344130e2010-01-04 20:44:00 +00002598
2599 // Get function line info.
2600 if (!Lines.empty()) {
2601 // Get section line info.
2602 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2603 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2604 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2605 // Append the function info to section info.
2606 SectionLineInfos.insert(SectionLineInfos.end(),
2607 Lines.begin(), Lines.end());
2608 }
2609
2610 // Construct abstract scopes.
2611 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2612 AE = AbstractScopesList.end(); AI != AE; ++AI)
2613 constructScopeDIE(*AI);
2614
Devang Patel9c488372010-05-04 06:15:30 +00002615 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Devang Patel344130e2010-01-04 20:44:00 +00002616
Devang Patel9c488372010-05-04 06:15:30 +00002617 if (!DisableFramePointerElim(*MF))
2618 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
2619 dwarf::DW_FORM_flag, 1);
2620
2621
Chris Lattnerd38fee82010-04-05 00:13:49 +00002622 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002623 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002624 }
2625
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002626 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002627 CurrentFnDbgScope = NULL;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002628 DeleteContainerSeconds(DbgScopeMap);
Devang Patel51424712010-04-09 16:04:20 +00002629 InsnsEndScopeSet.clear();
Devang Patelaead63c2010-03-29 22:59:58 +00002630 DbgValueStartMap.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002631 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002632 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002633 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002634 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00002635 LabelsBeforeInsn.clear();
2636 LabelsAfterInsn.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002637 Lines.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002638 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002639}
2640
Chris Lattnerc6087842010-03-09 04:54:43 +00002641/// recordSourceLine - Register a source line with debug info. Returns the
2642/// unique label that was emitted and which provides correspondence to
2643/// the source line list.
Devang Patele9f8f5e2010-05-07 20:54:48 +00002644MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S) {
Devang Patel65dbc902009-11-25 17:36:49 +00002645 StringRef Dir;
2646 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002647
Dan Gohman1cc0d622010-05-05 23:41:32 +00002648 unsigned Src = 1;
2649 if (S) {
2650 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00002651
Dan Gohman1cc0d622010-05-05 23:41:32 +00002652 if (Scope.isCompileUnit()) {
2653 DICompileUnit CU(S);
2654 Dir = CU.getDirectory();
2655 Fn = CU.getFilename();
2656 } else if (Scope.isSubprogram()) {
2657 DISubprogram SP(S);
2658 Dir = SP.getDirectory();
2659 Fn = SP.getFilename();
2660 } else if (Scope.isLexicalBlock()) {
2661 DILexicalBlock DB(S);
2662 Dir = DB.getDirectory();
2663 Fn = DB.getFilename();
2664 } else
2665 assert(0 && "Unexpected scope info");
2666
2667 Src = GetOrCreateSourceID(Dir, Fn);
2668 }
2669
Chris Lattner63d78362010-03-14 08:36:50 +00002670 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner25b68c62010-03-14 08:15:55 +00002671 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002672
Chris Lattnerc6087842010-03-09 04:54:43 +00002673 Asm->OutStreamer.EmitLabel(Label);
2674 return Label;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002675}
2676
Bill Wendling829e67b2009-05-20 23:22:40 +00002677//===----------------------------------------------------------------------===//
2678// Emit Methods
2679//===----------------------------------------------------------------------===//
2680
Devang Patel2c4ceb12009-11-21 02:48:08 +00002681/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002682///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002683unsigned
2684DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002685 // Get the children.
2686 const std::vector<DIE *> &Children = Die->getChildren();
2687
2688 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002689 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00002690 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00002691
2692 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002693 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002694
2695 // Get the abbreviation for this DIE.
2696 unsigned AbbrevNumber = Die->getAbbrevNumber();
2697 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2698
2699 // Set DIE offset
2700 Die->setOffset(Offset);
2701
2702 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002703 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002704
2705 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2706 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2707
2708 // Size the DIE attribute values.
2709 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2710 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00002711 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00002712
2713 // Size the DIE children if any.
2714 if (!Children.empty()) {
2715 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2716 "Children flag not set");
2717
2718 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002719 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002720
2721 // End of children marker.
2722 Offset += sizeof(int8_t);
2723 }
2724
2725 Die->setSize(Offset - Die->getOffset());
2726 return Offset;
2727}
2728
Devang Patel2c4ceb12009-11-21 02:48:08 +00002729/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002730///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002731void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00002732 unsigned PrevOffset = 0;
2733 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2734 E = CUMap.end(); I != E; ++I) {
2735 // Compute size of compile unit header.
2736 static unsigned Offset = PrevOffset +
2737 sizeof(int32_t) + // Length of Compilation Unit Info
2738 sizeof(int16_t) + // DWARF version number
2739 sizeof(int32_t) + // Offset Into Abbrev. Section
2740 sizeof(int8_t); // Pointer Size (in bytes)
2741 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
2742 PrevOffset = Offset;
2743 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002744}
2745
Chris Lattner11b8f302010-04-04 23:02:02 +00002746/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2747/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00002748static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00002749 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002750 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00002751 if (!SymbolStem) return 0;
2752
Chris Lattner9c69e285532010-04-04 22:59:04 +00002753 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2754 Asm->OutStreamer.EmitLabel(TmpSym);
2755 return TmpSym;
2756}
2757
2758/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2759/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00002760void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002761 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002762
Bill Wendling94d04b82009-05-20 23:21:38 +00002763 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002764 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002765 DwarfFrameSectionSym =
2766 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2767 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002768
Chris Lattner9c69e285532010-04-04 22:59:04 +00002769 DwarfInfoSectionSym =
2770 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2771 DwarfAbbrevSectionSym =
2772 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00002773 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Chris Lattner9c69e285532010-04-04 22:59:04 +00002774
2775 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00002776 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002777
Chris Lattner11b8f302010-04-04 23:02:02 +00002778 EmitSectionSym(Asm, TLOF.getDwarfLineSection());
2779 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2780 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2781 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Chris Lattner9c69e285532010-04-04 22:59:04 +00002782 DwarfStrSectionSym =
2783 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00002784 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2785 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00002786
Chris Lattner9c69e285532010-04-04 22:59:04 +00002787 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00002788 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002789}
2790
Devang Patel2c4ceb12009-11-21 02:48:08 +00002791/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002792///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002793void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002794 // Get the abbreviation for this DIE.
2795 unsigned AbbrevNumber = Die->getAbbrevNumber();
2796 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2797
Bill Wendling94d04b82009-05-20 23:21:38 +00002798 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00002799 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00002800 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2801 Twine::utohexstr(Die->getOffset()) + ":0x" +
2802 Twine::utohexstr(Die->getSize()) + " " +
2803 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002804 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002805
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002806 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00002807 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2808
2809 // Emit the DIE attribute values.
2810 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2811 unsigned Attr = AbbrevData[i].getAttribute();
2812 unsigned Form = AbbrevData[i].getForm();
2813 assert(Form && "Too many attributes for DIE (check abbreviation)");
2814
Chris Lattner3f53c832010-04-04 18:52:31 +00002815 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00002816 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2817
Bill Wendling94d04b82009-05-20 23:21:38 +00002818 switch (Attr) {
2819 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002820 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002821 break;
2822 case dwarf::DW_AT_abstract_origin: {
2823 DIEEntry *E = cast<DIEEntry>(Values[i]);
2824 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002825 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002826 Asm->EmitInt32(Addr);
2827 break;
2828 }
Devang Patelf2548ca2010-04-16 23:33:45 +00002829 case dwarf::DW_AT_ranges: {
2830 // DW_AT_range Value encodes offset in debug_range section.
2831 DIEInteger *V = cast<DIEInteger>(Values[i]);
2832 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2833 V->getValue(),
2834 DwarfDebugRangeSectionSym,
2835 4);
2836 break;
2837 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002838 default:
2839 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002840 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00002841 break;
2842 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002843 }
2844
2845 // Emit the DIE children if any.
2846 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2847 const std::vector<DIE *> &Children = Die->getChildren();
2848
2849 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002850 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002851
Chris Lattner3f53c832010-04-04 18:52:31 +00002852 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00002853 Asm->OutStreamer.AddComment("End Of Children Mark");
2854 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002855 }
2856}
2857
Devang Patel8a241142009-12-09 18:24:21 +00002858/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002859///
Devang Patel8a241142009-12-09 18:24:21 +00002860void DwarfDebug::emitDebugInfo() {
2861 // Start debug info section.
2862 Asm->OutStreamer.SwitchSection(
2863 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00002864 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2865 E = CUMap.end(); I != E; ++I) {
2866 CompileUnit *TheCU = I->second;
2867 DIE *Die = TheCU->getCUDie();
2868
2869 // Emit the compile units header.
2870 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2871 TheCU->getID()));
2872
2873 // Emit size of content not including length itself
2874 unsigned ContentSize = Die->getSize() +
2875 sizeof(int16_t) + // DWARF version number
2876 sizeof(int32_t) + // Offset Into Abbrev. Section
2877 sizeof(int8_t) + // Pointer Size (in bytes)
2878 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2879
2880 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2881 Asm->EmitInt32(ContentSize);
2882 Asm->OutStreamer.AddComment("DWARF version number");
2883 Asm->EmitInt16(dwarf::DWARF_VERSION);
2884 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2885 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2886 DwarfAbbrevSectionSym);
2887 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2888 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
2889
2890 emitDIE(Die);
2891 // FIXME - extra padding for gdb bug.
2892 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2893 Asm->EmitInt8(0);
2894 Asm->EmitInt8(0);
2895 Asm->EmitInt8(0);
2896 Asm->EmitInt8(0);
2897 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
2898 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002899}
2900
Devang Patel2c4ceb12009-11-21 02:48:08 +00002901/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002902///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002903void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002904 // Check to see if it is worth the effort.
2905 if (!Abbreviations.empty()) {
2906 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002907 Asm->OutStreamer.SwitchSection(
2908 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002909
Chris Lattnerc0215722010-04-04 19:25:43 +00002910 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002911
2912 // For each abbrevation.
2913 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2914 // Get abbreviation data
2915 const DIEAbbrev *Abbrev = Abbreviations[i];
2916
2917 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002918 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00002919
2920 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002921 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00002922 }
2923
2924 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002925 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002926
Chris Lattnerc0215722010-04-04 19:25:43 +00002927 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002928 }
2929}
2930
Devang Patel2c4ceb12009-11-21 02:48:08 +00002931/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002932/// the line matrix.
2933///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002934void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002935 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00002936 Asm->OutStreamer.AddComment("Extended Op");
2937 Asm->EmitInt8(0);
2938
2939 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002940 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00002941 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2942 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2943
2944 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00002945
Chris Lattnerc0215722010-04-04 19:25:43 +00002946 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00002947 Asm->getTargetData().getPointerSize(),
2948 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002949
2950 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00002951 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2952 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002953 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00002954 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002955}
2956
Devang Patel2c4ceb12009-11-21 02:48:08 +00002957/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00002958///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002959void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002960 // If the target is using .loc/.file, the assembler will be emitting the
2961 // .debug_line table automatically.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002962 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002963 return;
2964
2965 // Minimum line delta, thus ranging from -10..(255-10).
2966 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2967 // Maximum line delta, thus ranging from -10..(255-10).
2968 const int MaxLineDelta = 255 + MinLineDelta;
2969
2970 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002971 Asm->OutStreamer.SwitchSection(
2972 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002973
2974 // Construct the section header.
Chris Lattner233f52b2010-03-09 23:52:58 +00002975 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnera6437182010-04-04 19:58:12 +00002976 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
2977 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00002978 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002979
Chris Lattner233f52b2010-03-09 23:52:58 +00002980 Asm->OutStreamer.AddComment("DWARF version number");
2981 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling94d04b82009-05-20 23:21:38 +00002982
Chris Lattner233f52b2010-03-09 23:52:58 +00002983 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnera6437182010-04-04 19:58:12 +00002984 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
2985 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00002986 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002987
Chris Lattner233f52b2010-03-09 23:52:58 +00002988 Asm->OutStreamer.AddComment("Minimum Instruction Length");
2989 Asm->EmitInt8(1);
2990 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
2991 Asm->EmitInt8(1);
2992 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
2993 Asm->EmitInt8(MinLineDelta);
2994 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
2995 Asm->EmitInt8(MaxLineDelta);
2996 Asm->OutStreamer.AddComment("Special Opcode Base");
2997 Asm->EmitInt8(-MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00002998
2999 // Line number standard opcode encodings argument count
Chris Lattner233f52b2010-03-09 23:52:58 +00003000 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3001 Asm->EmitInt8(0);
3002 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3003 Asm->EmitInt8(1);
3004 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3005 Asm->EmitInt8(1);
3006 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3007 Asm->EmitInt8(1);
3008 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3009 Asm->EmitInt8(1);
3010 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3011 Asm->EmitInt8(0);
3012 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3013 Asm->EmitInt8(0);
3014 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3015 Asm->EmitInt8(0);
3016 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3017 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003018
3019 // Emit directories.
3020 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattner4cf202b2010-01-23 03:11:46 +00003021 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner3f53c832010-04-04 18:52:31 +00003022 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003023 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003024 }
3025
Chris Lattner233f52b2010-03-09 23:52:58 +00003026 Asm->OutStreamer.AddComment("End of directories");
3027 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003028
3029 // Emit files.
3030 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3031 // Remember source id starts at 1.
3032 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattner4cf202b2010-01-23 03:11:46 +00003033 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner3f53c832010-04-04 18:52:31 +00003034 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003035 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
3036
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003037 Asm->EmitULEB128(Id.first, "Directory #");
3038 Asm->EmitULEB128(0, "Mod date");
3039 Asm->EmitULEB128(0, "File size");
Bill Wendling94d04b82009-05-20 23:21:38 +00003040 }
3041
Chris Lattner233f52b2010-03-09 23:52:58 +00003042 Asm->OutStreamer.AddComment("End of files");
3043 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003044
Chris Lattnerc0215722010-04-04 19:25:43 +00003045 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003046
3047 // A sequence for each text section.
3048 unsigned SecSrcLinesSize = SectionSourceLines.size();
3049
3050 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3051 // Isolate current sections line info.
3052 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3053
Bill Wendling94d04b82009-05-20 23:21:38 +00003054 // Dwarf assumes we start with first line of first source file.
3055 unsigned Source = 1;
3056 unsigned Line = 1;
3057
3058 // Construct rows of the address, source, line, column matrix.
3059 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3060 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner25b68c62010-03-14 08:15:55 +00003061 MCSymbol *Label = LineInfo.getLabel();
Chris Lattnerb9130602010-03-14 02:20:58 +00003062 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling94d04b82009-05-20 23:21:38 +00003063
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003064 if (Asm->isVerbose()) {
Chris Lattner188a87d2010-03-10 01:04:13 +00003065 std::pair<unsigned, unsigned> SrcID =
Bill Wendling94d04b82009-05-20 23:21:38 +00003066 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner188a87d2010-03-10 01:04:13 +00003067 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner49f618a2010-03-10 02:29:31 +00003068 "/" +
3069 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner188a87d2010-03-10 01:04:13 +00003070 ":" + Twine(LineInfo.getLine()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003071 }
3072
3073 // Define the line address.
Chris Lattner233f52b2010-03-09 23:52:58 +00003074 Asm->OutStreamer.AddComment("Extended Op");
3075 Asm->EmitInt8(0);
3076 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003077 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003078
3079 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3080 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3081
3082 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003083 Asm->OutStreamer.EmitSymbolValue(Label,
3084 Asm->getTargetData().getPointerSize(),
Chris Lattnerb9130602010-03-14 02:20:58 +00003085 0/*AddrSpace*/);
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003086
Bill Wendling94d04b82009-05-20 23:21:38 +00003087 // If change of source, then switch to the new source.
3088 if (Source != LineInfo.getSourceID()) {
3089 Source = LineInfo.getSourceID();
Chris Lattner233f52b2010-03-09 23:52:58 +00003090 Asm->OutStreamer.AddComment("DW_LNS_set_file");
3091 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003092 Asm->EmitULEB128(Source, "New Source");
Bill Wendling94d04b82009-05-20 23:21:38 +00003093 }
3094
3095 // If change of line.
3096 if (Line != LineInfo.getLine()) {
3097 // Determine offset.
3098 int Offset = LineInfo.getLine() - Line;
3099 int Delta = Offset - MinLineDelta;
3100
3101 // Update line.
3102 Line = LineInfo.getLine();
3103
3104 // If delta is small enough and in range...
3105 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3106 // ... then use fast opcode.
Chris Lattner233f52b2010-03-09 23:52:58 +00003107 Asm->OutStreamer.AddComment("Line Delta");
3108 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003109 } else {
3110 // ... otherwise use long hand.
Chris Lattner233f52b2010-03-09 23:52:58 +00003111 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling94d04b82009-05-20 23:21:38 +00003112 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003113 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner233f52b2010-03-09 23:52:58 +00003114 Asm->OutStreamer.AddComment("DW_LNS_copy");
3115 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003116 }
3117 } else {
3118 // Copy the previous row (different address or source)
Chris Lattner233f52b2010-03-09 23:52:58 +00003119 Asm->OutStreamer.AddComment("DW_LNS_copy");
3120 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003121 }
3122 }
3123
Devang Patel2c4ceb12009-11-21 02:48:08 +00003124 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003125 }
3126
3127 if (SecSrcLinesSize == 0)
3128 // Because we're emitting a debug_line section, we still need a line
3129 // table. The linker and friends expect it to exist. If there's nothing to
3130 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003131 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003132
Chris Lattnerc0215722010-04-04 19:25:43 +00003133 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003134}
3135
Devang Patel2c4ceb12009-11-21 02:48:08 +00003136/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003137///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003138void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003139 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003140 return;
3141
Chris Lattnerd38fee82010-04-05 00:13:49 +00003142 int stackGrowth = Asm->getTargetData().getPointerSize();
3143 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3144 TargetFrameInfo::StackGrowsDown)
3145 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00003146
3147 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003148 Asm->OutStreamer.SwitchSection(
3149 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003150
Chris Lattnerc0215722010-04-04 19:25:43 +00003151 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003152 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003153 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3154 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003155
Chris Lattnerc0215722010-04-04 19:25:43 +00003156 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003157 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00003158 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00003159 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00003160 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00003161 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003162 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003163 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3164 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00003165 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003166 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling94d04b82009-05-20 23:21:38 +00003167 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00003168
3169 std::vector<MachineMove> Moves;
3170 RI->getInitialFrameState(Moves);
3171
Chris Lattner02b86b92010-04-04 23:41:46 +00003172 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003173
Chris Lattner059ea132010-04-28 01:05:45 +00003174 Asm->EmitAlignment(2);
Chris Lattnerc0215722010-04-04 19:25:43 +00003175 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003176}
3177
Devang Patel2c4ceb12009-11-21 02:48:08 +00003178/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00003179/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00003180void DwarfDebug::
3181emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003182 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003183 return;
3184
3185 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003186 Asm->OutStreamer.SwitchSection(
3187 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003188
Chris Lattner233f52b2010-03-09 23:52:58 +00003189 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00003190 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00003191 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00003192 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00003193 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00003194 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003195
Chris Lattner206d61e2010-03-13 07:26:18 +00003196 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00003197
Chris Lattner233f52b2010-03-09 23:52:58 +00003198 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattner6189ed12010-04-04 23:25:33 +00003199 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
3200 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003201
Chris Lattner233f52b2010-03-09 23:52:58 +00003202 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00003203 MCSymbol *FuncBeginSym =
3204 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00003205 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00003206 Asm->getTargetData().getPointerSize(),
3207 0/*AddrSpace*/);
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003208
3209
Chris Lattner233f52b2010-03-09 23:52:58 +00003210 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00003211 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003212 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003213
Chris Lattner02b86b92010-04-04 23:41:46 +00003214 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003215
Chris Lattner059ea132010-04-28 01:05:45 +00003216 Asm->EmitAlignment(2);
Chris Lattner206d61e2010-03-13 07:26:18 +00003217 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00003218}
3219
Devang Patel8a241142009-12-09 18:24:21 +00003220/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3221///
3222void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00003223 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3224 E = CUMap.end(); I != E; ++I) {
3225 CompileUnit *TheCU = I->second;
3226 // Start the dwarf pubnames section.
3227 Asm->OutStreamer.SwitchSection(
3228 Asm->getObjFileLowering().getDwarfPubNamesSection());
Chris Lattner4cf202b2010-01-23 03:11:46 +00003229
Devang Patel163a9f72010-05-10 22:49:55 +00003230 Asm->OutStreamer.AddComment("Length of Public Names Info");
3231 Asm->EmitLabelDifference(
3232 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3233 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
3234
3235 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3236 TheCU->getID()));
3237
3238 Asm->OutStreamer.AddComment("DWARF Version");
3239 Asm->EmitInt16(dwarf::DWARF_VERSION);
3240
3241 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3242 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3243 DwarfInfoSectionSym);
3244
3245 Asm->OutStreamer.AddComment("Compilation Unit Length");
3246 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3247 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3248 4);
3249
3250 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3251 for (StringMap<DIE*>::const_iterator
3252 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3253 const char *Name = GI->getKeyData();
3254 DIE *Entity = GI->second;
3255
3256 Asm->OutStreamer.AddComment("DIE offset");
3257 Asm->EmitInt32(Entity->getOffset());
3258
3259 if (Asm->isVerbose())
3260 Asm->OutStreamer.AddComment("External Name");
3261 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3262 }
3263
3264 Asm->OutStreamer.AddComment("End Mark");
3265 Asm->EmitInt32(0);
3266 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3267 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003268 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003269}
3270
Devang Patel193f7202009-11-24 01:14:22 +00003271void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00003272 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3273 E = CUMap.end(); I != E; ++I) {
3274 CompileUnit *TheCU = I->second;
3275 // Start the dwarf pubnames section.
3276 Asm->OutStreamer.SwitchSection(
3277 Asm->getObjFileLowering().getDwarfPubTypesSection());
3278 Asm->OutStreamer.AddComment("Length of Public Types Info");
3279 Asm->EmitLabelDifference(
3280 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3281 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Chris Lattner4cf202b2010-01-23 03:11:46 +00003282
Devang Patel163a9f72010-05-10 22:49:55 +00003283 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3284 TheCU->getID()));
3285
3286 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3287 Asm->EmitInt16(dwarf::DWARF_VERSION);
3288
3289 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3290 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3291 DwarfInfoSectionSym);
3292
3293 Asm->OutStreamer.AddComment("Compilation Unit Length");
3294 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3295 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3296 4);
3297
3298 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3299 for (StringMap<DIE*>::const_iterator
3300 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3301 const char *Name = GI->getKeyData();
3302 DIE * Entity = GI->second;
3303
3304 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3305 Asm->EmitInt32(Entity->getOffset());
3306
3307 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3308 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3309 }
3310
3311 Asm->OutStreamer.AddComment("End Mark");
3312 Asm->EmitInt32(0);
3313 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3314 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003315 }
Devang Patel193f7202009-11-24 01:14:22 +00003316}
3317
Devang Patel2c4ceb12009-11-21 02:48:08 +00003318/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003319///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003320void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003321 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003322 if (StringPool.empty()) return;
3323
3324 // Start the dwarf str section.
3325 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003326 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003327
Chris Lattnerbc733f52010-03-13 02:17:42 +00003328 // Get all of the string pool entries and put them in an array by their ID so
3329 // we can sort them.
3330 SmallVector<std::pair<unsigned,
3331 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3332
3333 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3334 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3335 Entries.push_back(std::make_pair(I->second.second, &*I));
3336
3337 array_pod_sort(Entries.begin(), Entries.end());
3338
3339 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003340 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003341 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003342
3343 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003344 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003345 }
3346}
3347
Devang Patel2c4ceb12009-11-21 02:48:08 +00003348/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003349///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003350void DwarfDebug::emitDebugLoc() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003351 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003352 Asm->OutStreamer.SwitchSection(
3353 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003354}
3355
3356/// EmitDebugARanges - Emit visible names into a debug aranges section.
3357///
3358void DwarfDebug::EmitDebugARanges() {
3359 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003360 Asm->OutStreamer.SwitchSection(
3361 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003362}
3363
Devang Patel2c4ceb12009-11-21 02:48:08 +00003364/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003365///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003366void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003367 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003368 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00003369 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00003370 unsigned char Size = Asm->getTargetData().getPointerSize();
3371 for (SmallVector<const MCSymbol *, 8>::iterator
3372 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
3373 I != E; ++I) {
3374 if (*I)
3375 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003376 else
Devang Pateleac9c072010-04-27 19:46:33 +00003377 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003378 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003379}
3380
Devang Patel2c4ceb12009-11-21 02:48:08 +00003381/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003382///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003383void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003384 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00003385 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003386 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003387 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003388 }
3389}
3390
Devang Patel2c4ceb12009-11-21 02:48:08 +00003391/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00003392/// Section Header:
3393/// 1. length of section
3394/// 2. Dwarf version number
3395/// 3. address size.
3396///
3397/// Entries (one "entry" for each function that was inlined):
3398///
3399/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3400/// otherwise offset into __debug_str for regular function name.
3401/// 2. offset into __debug_str section for regular function name.
3402/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3403/// instances for the function.
3404///
3405/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3406/// inlined instance; the die_offset points to the inlined_subroutine die in the
3407/// __debug_info section, and the low_pc is the starting address for the
3408/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003409void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003410 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003411 return;
3412
Devang Patel163a9f72010-05-10 22:49:55 +00003413 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00003414 return;
3415
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003416 Asm->OutStreamer.SwitchSection(
3417 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00003418
Chris Lattner233f52b2010-03-09 23:52:58 +00003419 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003420 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3421 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003422
Chris Lattnerc0215722010-04-04 19:25:43 +00003423 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003424
Chris Lattner233f52b2010-03-09 23:52:58 +00003425 Asm->OutStreamer.AddComment("Dwarf Version");
3426 Asm->EmitInt16(dwarf::DWARF_VERSION);
3427 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003428 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003429
Devang Patele9f8f5e2010-05-07 20:54:48 +00003430 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00003431 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00003432
Devang Patele9f8f5e2010-05-07 20:54:48 +00003433 const MDNode *Node = *I;
3434 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003435 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00003436 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00003437 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00003438 StringRef LName = SP.getLinkageName();
3439 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003440
Chris Lattner233f52b2010-03-09 23:52:58 +00003441 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003442 if (LName.empty()) {
3443 Asm->OutStreamer.EmitBytes(Name, 0);
3444 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3445 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00003446 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3447 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00003448
Chris Lattner233f52b2010-03-09 23:52:58 +00003449 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00003450 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003451 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00003452
Devang Patel53bb5c92009-11-10 23:06:00 +00003453 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00003454 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00003455 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00003456 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003457
Chris Lattner3f53c832010-04-04 18:52:31 +00003458 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003459 Asm->OutStreamer.EmitSymbolValue(LI->first,
3460 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003461 }
3462 }
3463
Chris Lattnerc0215722010-04-04 19:25:43 +00003464 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003465}