blob: 6baba7255b65db41dd3358e4aa777b5726a675f2 [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 Patel90a48ad2010-03-15 18:33:46 +00002178 continue;
2179 }
2180
2181 DebugLoc DL = MInsn->getDebugLoc();
2182 if (DL.isUnknown()) continue;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002183 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002184 if (const MDNode *IA = DL.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00002185 Scope = ConcreteScopes.lookup(IA);
2186 if (Scope == 0)
2187 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2188
Devang Patel90a48ad2010-03-15 18:33:46 +00002189 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002190 if (Scope == 0)
Devang Patel90a48ad2010-03-15 18:33:46 +00002191 continue;
2192
Devang Patel98e1cac2010-05-14 21:01:35 +00002193 Processed.insert(DV);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002194 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, MInsn, DL);
Devang Patel90a48ad2010-03-15 18:33:46 +00002195 DbgVariable *RegVar = new DbgVariable(DV, MInsn, AbsDbgVariable);
Devang Patelaead63c2010-03-29 22:59:58 +00002196 DbgValueStartMap[MInsn] = RegVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00002197 Scope->addVariable(RegVar);
2198 }
2199 }
Devang Patel98e1cac2010-05-14 21:01:35 +00002200
2201 // Collect info for variables that were optimized out.
2202 if (NamedMDNode *NMD =
2203 MF->getFunction()->getParent()->getNamedMetadata("llvm.dbg.lv")) {
2204 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2205 DIVariable DV(cast_or_null<MDNode>(NMD->getOperand(i)));
2206 if (!Processed.insert(DV))
2207 continue;
2208 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2209 if (Scope)
2210 Scope->addVariable(new DbgVariable(DV, ~0U, NULL));
2211 }
2212 }
2213
Devang Patele717faa2009-10-06 01:26:37 +00002214}
2215
Devang Patel553881b2010-03-29 17:20:31 +00002216/// beginScope - Process beginning of a scope.
2217void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patel553881b2010-03-29 17:20:31 +00002218 // Check location.
2219 DebugLoc DL = MI->getDebugLoc();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002220 if (DL.isUnknown()) {
Dan Gohman281d65d2010-05-07 01:08:53 +00002221 if (UnknownLocations) {
2222 // This instruction has no debug location. If the preceding instruction
2223 // did, emit debug location information to indicate that the debug
2224 // location is now unknown.
2225 MCSymbol *Label = NULL;
2226 if (DL == PrevInstLoc)
2227 Label = PrevLabel;
2228 else {
2229 Label = recordSourceLine(DL.getLine(), DL.getCol(), 0);
2230 PrevInstLoc = DL;
2231 PrevLabel = Label;
2232 }
Dan Gohman75395842010-05-06 00:29:41 +00002233
Dan Gohman281d65d2010-05-07 01:08:53 +00002234 // If this instruction begins a scope then note down corresponding label.
2235 if (InsnsBeginScopeSet.count(MI) != 0)
2236 LabelsBeforeInsn[MI] = Label;
2237 }
Dan Gohman75395842010-05-06 00:29:41 +00002238
Devang Patel553881b2010-03-29 17:20:31 +00002239 return;
Dan Gohman1cc0d622010-05-05 23:41:32 +00002240 }
Devang Patel553881b2010-03-29 17:20:31 +00002241
Devang Patele9f8f5e2010-05-07 20:54:48 +00002242 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Chris Lattnerde4845c2010-04-02 19:42:39 +00002243
2244 // FIXME: Should only verify each scope once!
2245 if (!DIScope(Scope).Verify())
Devang Patel553881b2010-03-29 17:20:31 +00002246 return;
Devang Patel553881b2010-03-29 17:20:31 +00002247
Devang Patelaead63c2010-03-29 22:59:58 +00002248 // DBG_VALUE instruction establishes new value.
Chris Lattner14d750d2010-03-31 05:39:57 +00002249 if (MI->isDebugValue()) {
Devang Patelaead63c2010-03-29 22:59:58 +00002250 DenseMap<const MachineInstr *, DbgVariable *>::iterator DI
2251 = DbgValueStartMap.find(MI);
2252 if (DI != DbgValueStartMap.end()) {
Devang Pateleac9c072010-04-27 19:46:33 +00002253 MCSymbol *Label = NULL;
2254 if (DL == PrevInstLoc)
2255 Label = PrevLabel;
2256 else {
2257 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2258 PrevInstLoc = DL;
2259 PrevLabel = Label;
2260 }
Devang Patelaead63c2010-03-29 22:59:58 +00002261 DI->second->setDbgValueLabel(Label);
2262 }
Devang Patel7ed63112010-03-30 18:07:00 +00002263 return;
Devang Patelaead63c2010-03-29 22:59:58 +00002264 }
2265
Devang Patel553881b2010-03-29 17:20:31 +00002266 // Emit a label to indicate location change. This is used for line
Devang Pateleac9c072010-04-27 19:46:33 +00002267 // table even if this instruction does not start a new scope.
2268 MCSymbol *Label = NULL;
2269 if (DL == PrevInstLoc)
2270 Label = PrevLabel;
2271 else {
2272 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2273 PrevInstLoc = DL;
2274 PrevLabel = Label;
2275 }
Devang Patel553881b2010-03-29 17:20:31 +00002276
Devang Patel1c246352010-04-08 16:50:29 +00002277 // If this instruction begins a scope then note down corresponding label.
2278 if (InsnsBeginScopeSet.count(MI) != 0)
Devang Pateleac9c072010-04-27 19:46:33 +00002279 LabelsBeforeInsn[MI] = Label;
Devang Patel0d20ac82009-10-06 01:50:42 +00002280}
2281
Devang Patel2c4ceb12009-11-21 02:48:08 +00002282/// endScope - Process end of a scope.
2283void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel1c246352010-04-08 16:50:29 +00002284 if (InsnsEndScopeSet.count(MI) != 0) {
2285 // Emit a label if this instruction ends a scope.
2286 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2287 Asm->OutStreamer.EmitLabel(Label);
Devang Pateleac9c072010-04-27 19:46:33 +00002288 LabelsAfterInsn[MI] = Label;
Devang Patel1c246352010-04-08 16:50:29 +00002289 }
Devang Patel53bb5c92009-11-10 23:06:00 +00002290}
2291
Devang Pateleac9c072010-04-27 19:46:33 +00002292/// getOrCreateDbgScope - Create DbgScope for the scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00002293DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002294 if (!InlinedAt) {
2295 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2296 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002297 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002298 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2299 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Pateleac9c072010-04-27 19:46:33 +00002300 if (DIDescriptor(Scope).isLexicalBlock()) {
2301 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002302 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Pateleac9c072010-04-27 19:46:33 +00002303 WScope->setParent(Parent);
2304 Parent->addScope(WScope);
2305 }
2306
2307 if (!WScope->getParent()) {
2308 StringRef SPName = DISubprogram(Scope).getLinkageName();
2309 if (SPName == Asm->MF->getFunction()->getName())
2310 CurrentFnDbgScope = WScope;
2311 }
2312
2313 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002314 }
2315
2316 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2317 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002318 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002319
2320 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2321 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2322 DILocation DL(InlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002323 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002324 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Pateleac9c072010-04-27 19:46:33 +00002325 WScope->setParent(Parent);
2326 Parent->addScope(WScope);
2327
2328 ConcreteScopes[InlinedAt] = WScope;
2329 getOrCreateAbstractScope(Scope);
2330
2331 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00002332}
2333
Devang Pateleac9c072010-04-27 19:46:33 +00002334/// hasValidLocation - Return true if debug location entry attached with
2335/// machine instruction encodes valid location info.
2336static bool hasValidLocation(LLVMContext &Ctx,
2337 const MachineInstr *MInsn,
Devang Patele9f8f5e2010-05-07 20:54:48 +00002338 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Pateleac9c072010-04-27 19:46:33 +00002339 if (MInsn->isDebugValue())
2340 return false;
2341 DebugLoc DL = MInsn->getDebugLoc();
2342 if (DL.isUnknown()) return false;
2343
Devang Patele9f8f5e2010-05-07 20:54:48 +00002344 const MDNode *S = DL.getScope(Ctx);
Devang Pateleac9c072010-04-27 19:46:33 +00002345
2346 // There is no need to create another DIE for compile unit. For all
2347 // other scopes, create one DbgScope now. This will be translated
2348 // into a scope DIE at the end.
2349 if (DIScope(S).isCompileUnit()) return false;
2350
2351 Scope = S;
2352 InlinedAt = DL.getInlinedAt(Ctx);
2353 return true;
2354}
2355
2356/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2357/// hierarchy.
2358static void calculateDominanceGraph(DbgScope *Scope) {
2359 assert (Scope && "Unable to calculate scop edominance graph!");
2360 SmallVector<DbgScope *, 4> WorkStack;
2361 WorkStack.push_back(Scope);
2362 unsigned Counter = 0;
2363 while (!WorkStack.empty()) {
2364 DbgScope *WS = WorkStack.back();
2365 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2366 bool visitedChildren = false;
2367 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2368 SE = Children.end(); SI != SE; ++SI) {
2369 DbgScope *ChildScope = *SI;
2370 if (!ChildScope->getDFSOut()) {
2371 WorkStack.push_back(ChildScope);
2372 visitedChildren = true;
2373 ChildScope->setDFSIn(++Counter);
2374 break;
2375 }
2376 }
2377 if (!visitedChildren) {
2378 WorkStack.pop_back();
2379 WS->setDFSOut(++Counter);
2380 }
2381 }
2382}
2383
2384/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
2385static
2386void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2387 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2388{
2389#ifndef NDEBUG
2390 unsigned PrevDFSIn = 0;
2391 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2392 I != E; ++I) {
2393 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2394 II != IE; ++II) {
2395 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002396 const MDNode *Scope = NULL;
2397 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002398
2399 // Check if instruction has valid location information.
2400 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2401 dbgs() << " [ ";
2402 if (InlinedAt)
2403 dbgs() << "*";
2404 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
2405 MI2ScopeMap.find(MInsn);
2406 if (DI != MI2ScopeMap.end()) {
2407 DbgScope *S = DI->second;
2408 dbgs() << S->getDFSIn();
2409 PrevDFSIn = S->getDFSIn();
2410 } else
2411 dbgs() << PrevDFSIn;
2412 } else
2413 dbgs() << " [ x" << PrevDFSIn;
2414 dbgs() << " ]";
2415 MInsn->dump();
2416 }
2417 dbgs() << "\n";
2418 }
2419#endif
2420}
Devang Patel2c4ceb12009-11-21 02:48:08 +00002421/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00002422/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00002423bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00002424 // If scope information was extracted using .dbg intrinsics then there is not
2425 // any need to extract these information by scanning each instruction.
2426 if (!DbgScopeMap.empty())
2427 return false;
2428
Dan Gohman314bf7c2010-04-23 01:18:53 +00002429 // Scan each instruction and create scopes. First build working set of scopes.
Devang Pateleac9c072010-04-27 19:46:33 +00002430 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2431 SmallVector<DbgRange, 4> MIRanges;
2432 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002433 const MDNode *PrevScope = NULL;
2434 const MDNode *PrevInlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002435 const MachineInstr *RangeBeginMI = NULL;
2436 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002437 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00002438 I != E; ++I) {
2439 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2440 II != IE; ++II) {
2441 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002442 const MDNode *Scope = NULL;
2443 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002444
2445 // Check if instruction has valid location information.
2446 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2447 PrevMI = MInsn;
2448 continue;
2449 }
Chris Lattnerde4845c2010-04-02 19:42:39 +00002450
Devang Pateleac9c072010-04-27 19:46:33 +00002451 // If scope has not changed then skip this instruction.
2452 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2453 PrevMI = MInsn;
2454 continue;
2455 }
2456
2457 if (RangeBeginMI) {
2458 // If we have alread seen a beginning of a instruction range and
2459 // current instruction scope does not match scope of first instruction
2460 // in this range then create a new instruction range.
2461 DbgRange R(RangeBeginMI, PrevMI);
2462 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
2463 MIRanges.push_back(R);
2464 }
2465
2466 // This is a beginning of a new instruction range.
2467 RangeBeginMI = MInsn;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002468
Devang Pateleac9c072010-04-27 19:46:33 +00002469 // Reset previous markers.
2470 PrevMI = MInsn;
2471 PrevScope = Scope;
2472 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00002473 }
2474 }
2475
Devang Pateleac9c072010-04-27 19:46:33 +00002476 // Create last instruction range.
2477 if (RangeBeginMI && PrevMI && PrevScope) {
2478 DbgRange R(RangeBeginMI, PrevMI);
2479 MIRanges.push_back(R);
2480 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00002481 }
Devang Pateleac9c072010-04-27 19:46:33 +00002482
Devang Patel344130e2010-01-04 20:44:00 +00002483 if (!CurrentFnDbgScope)
2484 return false;
2485
Devang Pateleac9c072010-04-27 19:46:33 +00002486 calculateDominanceGraph(CurrentFnDbgScope);
2487 if (PrintDbgScope)
2488 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2489
2490 // Find ranges of instructions covered by each DbgScope;
2491 DbgScope *PrevDbgScope = NULL;
2492 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2493 RE = MIRanges.end(); RI != RE; ++RI) {
2494 const DbgRange &R = *RI;
2495 DbgScope *S = MI2ScopeMap.lookup(R.first);
2496 assert (S && "Lost DbgScope for a machine instruction!");
2497 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2498 PrevDbgScope->closeInsnRange(S);
2499 S->openInsnRange(R.first);
2500 S->extendInsnRange(R.second);
2501 PrevDbgScope = S;
2502 }
2503
2504 if (PrevDbgScope)
2505 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00002506
Devang Patele37b0c62010-04-08 18:43:56 +00002507 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00002508
2509 return !DbgScopeMap.empty();
2510}
2511
Devang Pateleac9c072010-04-27 19:46:33 +00002512/// identifyScopeMarkers() -
2513/// Each DbgScope has first instruction and last instruction to mark beginning
2514/// and end of a scope respectively. Create an inverse map that list scopes
2515/// starts (and ends) with an instruction. One instruction may start (or end)
2516/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00002517void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00002518 SmallVector<DbgScope *, 4> WorkList;
2519 WorkList.push_back(CurrentFnDbgScope);
2520 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00002521 DbgScope *S = WorkList.pop_back_val();
Devang Pateleac9c072010-04-27 19:46:33 +00002522
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002523 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patel42aafd72010-01-20 02:05:23 +00002524 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002525 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00002526 SE = Children.end(); SI != SE; ++SI)
2527 WorkList.push_back(*SI);
2528
Devang Patel53bb5c92009-11-10 23:06:00 +00002529 if (S->isAbstractScope())
2530 continue;
Devang Pateleac9c072010-04-27 19:46:33 +00002531
2532 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2533 if (Ranges.empty())
2534 continue;
2535 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2536 RE = Ranges.end(); RI != RE; ++RI) {
2537 assert(RI->first && "DbgRange does not have first instruction!");
2538 assert(RI->second && "DbgRange does not have second instruction!");
2539 InsnsBeginScopeSet.insert(RI->first);
2540 InsnsEndScopeSet.insert(RI->second);
2541 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002542 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002543}
2544
Dan Gohman084751c2010-04-20 00:37:27 +00002545/// FindFirstDebugLoc - Find the first debug location in the function. This
2546/// is intended to be an approximation for the source position of the
2547/// beginning of the function.
2548static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2549 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2550 I != E; ++I)
2551 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2552 MBBI != MBBE; ++MBBI) {
2553 DebugLoc DL = MBBI->getDebugLoc();
2554 if (!DL.isUnknown())
2555 return DL;
2556 }
2557 return DebugLoc();
2558}
2559
Devang Patel2c4ceb12009-11-21 02:48:08 +00002560/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002561/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00002562void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00002563 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00002564 if (!extractScopeInformation()) return;
Chris Lattnera909d662010-03-29 20:38:20 +00002565
Devang Patel98e1cac2010-05-14 21:01:35 +00002566 collectVariableInfo(MF);
Devang Patel60b35bd2009-10-06 18:37:31 +00002567
Devang Pateleac9c072010-04-27 19:46:33 +00002568 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2569 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002570 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00002571 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002572
2573 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2574 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00002575 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002576 if (FDL.isUnknown()) return;
2577
Devang Patele9f8f5e2010-05-07 20:54:48 +00002578 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Chris Lattnerde4845c2010-04-02 19:42:39 +00002579
2580 DISubprogram SP = getDISubprogram(Scope);
2581 unsigned Line, Col;
2582 if (SP.Verify()) {
2583 Line = SP.getLineNumber();
2584 Col = 0;
2585 } else {
2586 Line = FDL.getLine();
2587 Col = FDL.getCol();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002588 }
Chris Lattnerde4845c2010-04-02 19:42:39 +00002589
2590 recordSourceLine(Line, Col, Scope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002591}
2592
Devang Patel2c4ceb12009-11-21 02:48:08 +00002593/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002594///
Chris Lattnereec791a2010-01-26 23:18:02 +00002595void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002596 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002597
Devang Patel344130e2010-01-04 20:44:00 +00002598 if (CurrentFnDbgScope) {
2599 // Define end label for subprogram.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002600 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_end",
2601 Asm->getFunctionNumber()));
Devang Patel344130e2010-01-04 20:44:00 +00002602
2603 // Get function line info.
2604 if (!Lines.empty()) {
2605 // Get section line info.
2606 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2607 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2608 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2609 // Append the function info to section info.
2610 SectionLineInfos.insert(SectionLineInfos.end(),
2611 Lines.begin(), Lines.end());
2612 }
2613
2614 // Construct abstract scopes.
2615 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2616 AE = AbstractScopesList.end(); AI != AE; ++AI)
2617 constructScopeDIE(*AI);
2618
Devang Patel9c488372010-05-04 06:15:30 +00002619 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Devang Patel344130e2010-01-04 20:44:00 +00002620
Devang Patel9c488372010-05-04 06:15:30 +00002621 if (!DisableFramePointerElim(*MF))
2622 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
2623 dwarf::DW_FORM_flag, 1);
2624
2625
Chris Lattnerd38fee82010-04-05 00:13:49 +00002626 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002627 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002628 }
2629
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002630 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002631 CurrentFnDbgScope = NULL;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002632 DeleteContainerSeconds(DbgScopeMap);
Devang Patel51424712010-04-09 16:04:20 +00002633 InsnsBeginScopeSet.clear();
2634 InsnsEndScopeSet.clear();
Devang Patelaead63c2010-03-29 22:59:58 +00002635 DbgValueStartMap.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002636 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002637 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002638 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002639 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00002640 LabelsBeforeInsn.clear();
2641 LabelsAfterInsn.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002642 Lines.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002643 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002644}
2645
Chris Lattnerc6087842010-03-09 04:54:43 +00002646/// recordSourceLine - Register a source line with debug info. Returns the
2647/// unique label that was emitted and which provides correspondence to
2648/// the source line list.
Devang Patele9f8f5e2010-05-07 20:54:48 +00002649MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S) {
Devang Patel65dbc902009-11-25 17:36:49 +00002650 StringRef Dir;
2651 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002652
Dan Gohman1cc0d622010-05-05 23:41:32 +00002653 unsigned Src = 1;
2654 if (S) {
2655 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00002656
Dan Gohman1cc0d622010-05-05 23:41:32 +00002657 if (Scope.isCompileUnit()) {
2658 DICompileUnit CU(S);
2659 Dir = CU.getDirectory();
2660 Fn = CU.getFilename();
2661 } else if (Scope.isSubprogram()) {
2662 DISubprogram SP(S);
2663 Dir = SP.getDirectory();
2664 Fn = SP.getFilename();
2665 } else if (Scope.isLexicalBlock()) {
2666 DILexicalBlock DB(S);
2667 Dir = DB.getDirectory();
2668 Fn = DB.getFilename();
2669 } else
2670 assert(0 && "Unexpected scope info");
2671
2672 Src = GetOrCreateSourceID(Dir, Fn);
2673 }
2674
Chris Lattner63d78362010-03-14 08:36:50 +00002675 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner25b68c62010-03-14 08:15:55 +00002676 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002677
Chris Lattnerc6087842010-03-09 04:54:43 +00002678 Asm->OutStreamer.EmitLabel(Label);
2679 return Label;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002680}
2681
Bill Wendling829e67b2009-05-20 23:22:40 +00002682//===----------------------------------------------------------------------===//
2683// Emit Methods
2684//===----------------------------------------------------------------------===//
2685
Devang Patel2c4ceb12009-11-21 02:48:08 +00002686/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002687///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002688unsigned
2689DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002690 // Get the children.
2691 const std::vector<DIE *> &Children = Die->getChildren();
2692
2693 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002694 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00002695 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00002696
2697 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002698 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002699
2700 // Get the abbreviation for this DIE.
2701 unsigned AbbrevNumber = Die->getAbbrevNumber();
2702 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2703
2704 // Set DIE offset
2705 Die->setOffset(Offset);
2706
2707 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002708 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002709
2710 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2711 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2712
2713 // Size the DIE attribute values.
2714 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2715 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00002716 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00002717
2718 // Size the DIE children if any.
2719 if (!Children.empty()) {
2720 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2721 "Children flag not set");
2722
2723 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002724 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002725
2726 // End of children marker.
2727 Offset += sizeof(int8_t);
2728 }
2729
2730 Die->setSize(Offset - Die->getOffset());
2731 return Offset;
2732}
2733
Devang Patel2c4ceb12009-11-21 02:48:08 +00002734/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002735///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002736void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00002737 unsigned PrevOffset = 0;
2738 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2739 E = CUMap.end(); I != E; ++I) {
2740 // Compute size of compile unit header.
2741 static unsigned Offset = PrevOffset +
2742 sizeof(int32_t) + // Length of Compilation Unit Info
2743 sizeof(int16_t) + // DWARF version number
2744 sizeof(int32_t) + // Offset Into Abbrev. Section
2745 sizeof(int8_t); // Pointer Size (in bytes)
2746 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
2747 PrevOffset = Offset;
2748 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002749}
2750
Chris Lattner11b8f302010-04-04 23:02:02 +00002751/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2752/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00002753static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00002754 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002755 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00002756 if (!SymbolStem) return 0;
2757
Chris Lattner9c69e285532010-04-04 22:59:04 +00002758 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2759 Asm->OutStreamer.EmitLabel(TmpSym);
2760 return TmpSym;
2761}
2762
2763/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2764/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00002765void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002766 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002767
Bill Wendling94d04b82009-05-20 23:21:38 +00002768 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002769 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002770 DwarfFrameSectionSym =
2771 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2772 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002773
Chris Lattner9c69e285532010-04-04 22:59:04 +00002774 DwarfInfoSectionSym =
2775 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2776 DwarfAbbrevSectionSym =
2777 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00002778 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Chris Lattner9c69e285532010-04-04 22:59:04 +00002779
2780 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00002781 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002782
Chris Lattner11b8f302010-04-04 23:02:02 +00002783 EmitSectionSym(Asm, TLOF.getDwarfLineSection());
2784 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2785 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2786 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Chris Lattner9c69e285532010-04-04 22:59:04 +00002787 DwarfStrSectionSym =
2788 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00002789 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2790 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00002791
Chris Lattner9c69e285532010-04-04 22:59:04 +00002792 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00002793 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002794}
2795
Devang Patel2c4ceb12009-11-21 02:48:08 +00002796/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002797///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002798void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002799 // Get the abbreviation for this DIE.
2800 unsigned AbbrevNumber = Die->getAbbrevNumber();
2801 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2802
Bill Wendling94d04b82009-05-20 23:21:38 +00002803 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00002804 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00002805 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2806 Twine::utohexstr(Die->getOffset()) + ":0x" +
2807 Twine::utohexstr(Die->getSize()) + " " +
2808 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002809 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002810
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002811 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00002812 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2813
2814 // Emit the DIE attribute values.
2815 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2816 unsigned Attr = AbbrevData[i].getAttribute();
2817 unsigned Form = AbbrevData[i].getForm();
2818 assert(Form && "Too many attributes for DIE (check abbreviation)");
2819
Chris Lattner3f53c832010-04-04 18:52:31 +00002820 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00002821 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2822
Bill Wendling94d04b82009-05-20 23:21:38 +00002823 switch (Attr) {
2824 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002825 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002826 break;
2827 case dwarf::DW_AT_abstract_origin: {
2828 DIEEntry *E = cast<DIEEntry>(Values[i]);
2829 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002830 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002831 Asm->EmitInt32(Addr);
2832 break;
2833 }
Devang Patelf2548ca2010-04-16 23:33:45 +00002834 case dwarf::DW_AT_ranges: {
2835 // DW_AT_range Value encodes offset in debug_range section.
2836 DIEInteger *V = cast<DIEInteger>(Values[i]);
2837 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2838 V->getValue(),
2839 DwarfDebugRangeSectionSym,
2840 4);
2841 break;
2842 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002843 default:
2844 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002845 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00002846 break;
2847 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002848 }
2849
2850 // Emit the DIE children if any.
2851 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2852 const std::vector<DIE *> &Children = Die->getChildren();
2853
2854 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002855 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002856
Chris Lattner3f53c832010-04-04 18:52:31 +00002857 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00002858 Asm->OutStreamer.AddComment("End Of Children Mark");
2859 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002860 }
2861}
2862
Devang Patel8a241142009-12-09 18:24:21 +00002863/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002864///
Devang Patel8a241142009-12-09 18:24:21 +00002865void DwarfDebug::emitDebugInfo() {
2866 // Start debug info section.
2867 Asm->OutStreamer.SwitchSection(
2868 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00002869 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2870 E = CUMap.end(); I != E; ++I) {
2871 CompileUnit *TheCU = I->second;
2872 DIE *Die = TheCU->getCUDie();
2873
2874 // Emit the compile units header.
2875 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2876 TheCU->getID()));
2877
2878 // Emit size of content not including length itself
2879 unsigned ContentSize = Die->getSize() +
2880 sizeof(int16_t) + // DWARF version number
2881 sizeof(int32_t) + // Offset Into Abbrev. Section
2882 sizeof(int8_t) + // Pointer Size (in bytes)
2883 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2884
2885 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2886 Asm->EmitInt32(ContentSize);
2887 Asm->OutStreamer.AddComment("DWARF version number");
2888 Asm->EmitInt16(dwarf::DWARF_VERSION);
2889 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2890 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2891 DwarfAbbrevSectionSym);
2892 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2893 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
2894
2895 emitDIE(Die);
2896 // FIXME - extra padding for gdb bug.
2897 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2898 Asm->EmitInt8(0);
2899 Asm->EmitInt8(0);
2900 Asm->EmitInt8(0);
2901 Asm->EmitInt8(0);
2902 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
2903 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002904}
2905
Devang Patel2c4ceb12009-11-21 02:48:08 +00002906/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002907///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002908void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002909 // Check to see if it is worth the effort.
2910 if (!Abbreviations.empty()) {
2911 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002912 Asm->OutStreamer.SwitchSection(
2913 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002914
Chris Lattnerc0215722010-04-04 19:25:43 +00002915 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002916
2917 // For each abbrevation.
2918 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2919 // Get abbreviation data
2920 const DIEAbbrev *Abbrev = Abbreviations[i];
2921
2922 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002923 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00002924
2925 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002926 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00002927 }
2928
2929 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002930 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002931
Chris Lattnerc0215722010-04-04 19:25:43 +00002932 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002933 }
2934}
2935
Devang Patel2c4ceb12009-11-21 02:48:08 +00002936/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002937/// the line matrix.
2938///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002939void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002940 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00002941 Asm->OutStreamer.AddComment("Extended Op");
2942 Asm->EmitInt8(0);
2943
2944 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002945 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00002946 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2947 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2948
2949 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00002950
Chris Lattnerc0215722010-04-04 19:25:43 +00002951 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00002952 Asm->getTargetData().getPointerSize(),
2953 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002954
2955 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00002956 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2957 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002958 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00002959 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002960}
2961
Devang Patel2c4ceb12009-11-21 02:48:08 +00002962/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00002963///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002964void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002965 // If the target is using .loc/.file, the assembler will be emitting the
2966 // .debug_line table automatically.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002967 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002968 return;
2969
2970 // Minimum line delta, thus ranging from -10..(255-10).
2971 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2972 // Maximum line delta, thus ranging from -10..(255-10).
2973 const int MaxLineDelta = 255 + MinLineDelta;
2974
2975 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002976 Asm->OutStreamer.SwitchSection(
2977 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002978
2979 // Construct the section header.
Chris Lattner233f52b2010-03-09 23:52:58 +00002980 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnera6437182010-04-04 19:58:12 +00002981 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
2982 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00002983 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002984
Chris Lattner233f52b2010-03-09 23:52:58 +00002985 Asm->OutStreamer.AddComment("DWARF version number");
2986 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling94d04b82009-05-20 23:21:38 +00002987
Chris Lattner233f52b2010-03-09 23:52:58 +00002988 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnera6437182010-04-04 19:58:12 +00002989 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
2990 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00002991 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002992
Chris Lattner233f52b2010-03-09 23:52:58 +00002993 Asm->OutStreamer.AddComment("Minimum Instruction Length");
2994 Asm->EmitInt8(1);
2995 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
2996 Asm->EmitInt8(1);
2997 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
2998 Asm->EmitInt8(MinLineDelta);
2999 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
3000 Asm->EmitInt8(MaxLineDelta);
3001 Asm->OutStreamer.AddComment("Special Opcode Base");
3002 Asm->EmitInt8(-MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003003
3004 // Line number standard opcode encodings argument count
Chris Lattner233f52b2010-03-09 23:52:58 +00003005 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3006 Asm->EmitInt8(0);
3007 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3008 Asm->EmitInt8(1);
3009 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3010 Asm->EmitInt8(1);
3011 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3012 Asm->EmitInt8(1);
3013 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3014 Asm->EmitInt8(1);
3015 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3016 Asm->EmitInt8(0);
3017 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3018 Asm->EmitInt8(0);
3019 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3020 Asm->EmitInt8(0);
3021 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3022 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003023
3024 // Emit directories.
3025 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattner4cf202b2010-01-23 03:11:46 +00003026 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner3f53c832010-04-04 18:52:31 +00003027 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003028 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003029 }
3030
Chris Lattner233f52b2010-03-09 23:52:58 +00003031 Asm->OutStreamer.AddComment("End of directories");
3032 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003033
3034 // Emit files.
3035 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3036 // Remember source id starts at 1.
3037 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattner4cf202b2010-01-23 03:11:46 +00003038 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner3f53c832010-04-04 18:52:31 +00003039 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003040 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
3041
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003042 Asm->EmitULEB128(Id.first, "Directory #");
3043 Asm->EmitULEB128(0, "Mod date");
3044 Asm->EmitULEB128(0, "File size");
Bill Wendling94d04b82009-05-20 23:21:38 +00003045 }
3046
Chris Lattner233f52b2010-03-09 23:52:58 +00003047 Asm->OutStreamer.AddComment("End of files");
3048 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003049
Chris Lattnerc0215722010-04-04 19:25:43 +00003050 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003051
3052 // A sequence for each text section.
3053 unsigned SecSrcLinesSize = SectionSourceLines.size();
3054
3055 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3056 // Isolate current sections line info.
3057 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3058
Bill Wendling94d04b82009-05-20 23:21:38 +00003059 // Dwarf assumes we start with first line of first source file.
3060 unsigned Source = 1;
3061 unsigned Line = 1;
3062
3063 // Construct rows of the address, source, line, column matrix.
3064 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3065 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner25b68c62010-03-14 08:15:55 +00003066 MCSymbol *Label = LineInfo.getLabel();
Chris Lattnerb9130602010-03-14 02:20:58 +00003067 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling94d04b82009-05-20 23:21:38 +00003068
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003069 if (Asm->isVerbose()) {
Chris Lattner188a87d2010-03-10 01:04:13 +00003070 std::pair<unsigned, unsigned> SrcID =
Bill Wendling94d04b82009-05-20 23:21:38 +00003071 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner188a87d2010-03-10 01:04:13 +00003072 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner49f618a2010-03-10 02:29:31 +00003073 "/" +
3074 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner188a87d2010-03-10 01:04:13 +00003075 ":" + Twine(LineInfo.getLine()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003076 }
3077
3078 // Define the line address.
Chris Lattner233f52b2010-03-09 23:52:58 +00003079 Asm->OutStreamer.AddComment("Extended Op");
3080 Asm->EmitInt8(0);
3081 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003082 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003083
3084 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3085 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3086
3087 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003088 Asm->OutStreamer.EmitSymbolValue(Label,
3089 Asm->getTargetData().getPointerSize(),
Chris Lattnerb9130602010-03-14 02:20:58 +00003090 0/*AddrSpace*/);
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003091
Bill Wendling94d04b82009-05-20 23:21:38 +00003092 // If change of source, then switch to the new source.
3093 if (Source != LineInfo.getSourceID()) {
3094 Source = LineInfo.getSourceID();
Chris Lattner233f52b2010-03-09 23:52:58 +00003095 Asm->OutStreamer.AddComment("DW_LNS_set_file");
3096 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003097 Asm->EmitULEB128(Source, "New Source");
Bill Wendling94d04b82009-05-20 23:21:38 +00003098 }
3099
3100 // If change of line.
3101 if (Line != LineInfo.getLine()) {
3102 // Determine offset.
3103 int Offset = LineInfo.getLine() - Line;
3104 int Delta = Offset - MinLineDelta;
3105
3106 // Update line.
3107 Line = LineInfo.getLine();
3108
3109 // If delta is small enough and in range...
3110 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3111 // ... then use fast opcode.
Chris Lattner233f52b2010-03-09 23:52:58 +00003112 Asm->OutStreamer.AddComment("Line Delta");
3113 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003114 } else {
3115 // ... otherwise use long hand.
Chris Lattner233f52b2010-03-09 23:52:58 +00003116 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling94d04b82009-05-20 23:21:38 +00003117 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003118 Asm->EmitSLEB128(Offset, "Line Offset");
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 } else {
3123 // Copy the previous row (different address or source)
Chris Lattner233f52b2010-03-09 23:52:58 +00003124 Asm->OutStreamer.AddComment("DW_LNS_copy");
3125 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003126 }
3127 }
3128
Devang Patel2c4ceb12009-11-21 02:48:08 +00003129 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003130 }
3131
3132 if (SecSrcLinesSize == 0)
3133 // Because we're emitting a debug_line section, we still need a line
3134 // table. The linker and friends expect it to exist. If there's nothing to
3135 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003136 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003137
Chris Lattnerc0215722010-04-04 19:25:43 +00003138 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003139}
3140
Devang Patel2c4ceb12009-11-21 02:48:08 +00003141/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003142///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003143void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003144 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003145 return;
3146
Chris Lattnerd38fee82010-04-05 00:13:49 +00003147 int stackGrowth = Asm->getTargetData().getPointerSize();
3148 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3149 TargetFrameInfo::StackGrowsDown)
3150 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00003151
3152 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003153 Asm->OutStreamer.SwitchSection(
3154 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003155
Chris Lattnerc0215722010-04-04 19:25:43 +00003156 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003157 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003158 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3159 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003160
Chris Lattnerc0215722010-04-04 19:25:43 +00003161 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003162 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00003163 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00003164 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00003165 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00003166 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003167 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003168 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3169 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00003170 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003171 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling94d04b82009-05-20 23:21:38 +00003172 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00003173
3174 std::vector<MachineMove> Moves;
3175 RI->getInitialFrameState(Moves);
3176
Chris Lattner02b86b92010-04-04 23:41:46 +00003177 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003178
Chris Lattner059ea132010-04-28 01:05:45 +00003179 Asm->EmitAlignment(2);
Chris Lattnerc0215722010-04-04 19:25:43 +00003180 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003181}
3182
Devang Patel2c4ceb12009-11-21 02:48:08 +00003183/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00003184/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00003185void DwarfDebug::
3186emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003187 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003188 return;
3189
3190 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003191 Asm->OutStreamer.SwitchSection(
3192 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003193
Chris Lattner233f52b2010-03-09 23:52:58 +00003194 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00003195 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00003196 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00003197 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00003198 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00003199 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003200
Chris Lattner206d61e2010-03-13 07:26:18 +00003201 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00003202
Chris Lattner233f52b2010-03-09 23:52:58 +00003203 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattner6189ed12010-04-04 23:25:33 +00003204 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
3205 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003206
Chris Lattner233f52b2010-03-09 23:52:58 +00003207 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00003208 MCSymbol *FuncBeginSym =
3209 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00003210 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00003211 Asm->getTargetData().getPointerSize(),
3212 0/*AddrSpace*/);
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003213
3214
Chris Lattner233f52b2010-03-09 23:52:58 +00003215 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00003216 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003217 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003218
Chris Lattner02b86b92010-04-04 23:41:46 +00003219 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003220
Chris Lattner059ea132010-04-28 01:05:45 +00003221 Asm->EmitAlignment(2);
Chris Lattner206d61e2010-03-13 07:26:18 +00003222 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00003223}
3224
Devang Patel8a241142009-12-09 18:24:21 +00003225/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3226///
3227void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00003228 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3229 E = CUMap.end(); I != E; ++I) {
3230 CompileUnit *TheCU = I->second;
3231 // Start the dwarf pubnames section.
3232 Asm->OutStreamer.SwitchSection(
3233 Asm->getObjFileLowering().getDwarfPubNamesSection());
Chris Lattner4cf202b2010-01-23 03:11:46 +00003234
Devang Patel163a9f72010-05-10 22:49:55 +00003235 Asm->OutStreamer.AddComment("Length of Public Names Info");
3236 Asm->EmitLabelDifference(
3237 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3238 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
3239
3240 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3241 TheCU->getID()));
3242
3243 Asm->OutStreamer.AddComment("DWARF Version");
3244 Asm->EmitInt16(dwarf::DWARF_VERSION);
3245
3246 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3247 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3248 DwarfInfoSectionSym);
3249
3250 Asm->OutStreamer.AddComment("Compilation Unit Length");
3251 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3252 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3253 4);
3254
3255 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3256 for (StringMap<DIE*>::const_iterator
3257 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3258 const char *Name = GI->getKeyData();
3259 DIE *Entity = GI->second;
3260
3261 Asm->OutStreamer.AddComment("DIE offset");
3262 Asm->EmitInt32(Entity->getOffset());
3263
3264 if (Asm->isVerbose())
3265 Asm->OutStreamer.AddComment("External Name");
3266 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3267 }
3268
3269 Asm->OutStreamer.AddComment("End Mark");
3270 Asm->EmitInt32(0);
3271 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3272 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003273 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003274}
3275
Devang Patel193f7202009-11-24 01:14:22 +00003276void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00003277 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3278 E = CUMap.end(); I != E; ++I) {
3279 CompileUnit *TheCU = I->second;
3280 // Start the dwarf pubnames section.
3281 Asm->OutStreamer.SwitchSection(
3282 Asm->getObjFileLowering().getDwarfPubTypesSection());
3283 Asm->OutStreamer.AddComment("Length of Public Types Info");
3284 Asm->EmitLabelDifference(
3285 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3286 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Chris Lattner4cf202b2010-01-23 03:11:46 +00003287
Devang Patel163a9f72010-05-10 22:49:55 +00003288 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3289 TheCU->getID()));
3290
3291 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3292 Asm->EmitInt16(dwarf::DWARF_VERSION);
3293
3294 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3295 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3296 DwarfInfoSectionSym);
3297
3298 Asm->OutStreamer.AddComment("Compilation Unit Length");
3299 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3300 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3301 4);
3302
3303 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3304 for (StringMap<DIE*>::const_iterator
3305 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3306 const char *Name = GI->getKeyData();
3307 DIE * Entity = GI->second;
3308
3309 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3310 Asm->EmitInt32(Entity->getOffset());
3311
3312 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3313 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3314 }
3315
3316 Asm->OutStreamer.AddComment("End Mark");
3317 Asm->EmitInt32(0);
3318 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3319 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003320 }
Devang Patel193f7202009-11-24 01:14:22 +00003321}
3322
Devang Patel2c4ceb12009-11-21 02:48:08 +00003323/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003324///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003325void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003326 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003327 if (StringPool.empty()) return;
3328
3329 // Start the dwarf str section.
3330 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003331 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003332
Chris Lattnerbc733f52010-03-13 02:17:42 +00003333 // Get all of the string pool entries and put them in an array by their ID so
3334 // we can sort them.
3335 SmallVector<std::pair<unsigned,
3336 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3337
3338 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3339 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3340 Entries.push_back(std::make_pair(I->second.second, &*I));
3341
3342 array_pod_sort(Entries.begin(), Entries.end());
3343
3344 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003345 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003346 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003347
3348 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003349 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003350 }
3351}
3352
Devang Patel2c4ceb12009-11-21 02:48:08 +00003353/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003354///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003355void DwarfDebug::emitDebugLoc() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003356 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003357 Asm->OutStreamer.SwitchSection(
3358 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003359}
3360
3361/// EmitDebugARanges - Emit visible names into a debug aranges section.
3362///
3363void DwarfDebug::EmitDebugARanges() {
3364 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003365 Asm->OutStreamer.SwitchSection(
3366 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003367}
3368
Devang Patel2c4ceb12009-11-21 02:48:08 +00003369/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003370///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003371void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003372 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003373 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00003374 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00003375 unsigned char Size = Asm->getTargetData().getPointerSize();
3376 for (SmallVector<const MCSymbol *, 8>::iterator
3377 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
3378 I != E; ++I) {
3379 if (*I)
3380 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003381 else
Devang Pateleac9c072010-04-27 19:46:33 +00003382 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003383 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003384}
3385
Devang Patel2c4ceb12009-11-21 02:48:08 +00003386/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003387///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003388void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003389 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00003390 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003391 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003392 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003393 }
3394}
3395
Devang Patel2c4ceb12009-11-21 02:48:08 +00003396/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00003397/// Section Header:
3398/// 1. length of section
3399/// 2. Dwarf version number
3400/// 3. address size.
3401///
3402/// Entries (one "entry" for each function that was inlined):
3403///
3404/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3405/// otherwise offset into __debug_str for regular function name.
3406/// 2. offset into __debug_str section for regular function name.
3407/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3408/// instances for the function.
3409///
3410/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3411/// inlined instance; the die_offset points to the inlined_subroutine die in the
3412/// __debug_info section, and the low_pc is the starting address for the
3413/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003414void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003415 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003416 return;
3417
Devang Patel163a9f72010-05-10 22:49:55 +00003418 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00003419 return;
3420
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003421 Asm->OutStreamer.SwitchSection(
3422 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00003423
Chris Lattner233f52b2010-03-09 23:52:58 +00003424 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003425 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3426 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003427
Chris Lattnerc0215722010-04-04 19:25:43 +00003428 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003429
Chris Lattner233f52b2010-03-09 23:52:58 +00003430 Asm->OutStreamer.AddComment("Dwarf Version");
3431 Asm->EmitInt16(dwarf::DWARF_VERSION);
3432 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003433 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003434
Devang Patele9f8f5e2010-05-07 20:54:48 +00003435 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00003436 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00003437
Devang Patele9f8f5e2010-05-07 20:54:48 +00003438 const MDNode *Node = *I;
3439 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003440 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00003441 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00003442 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00003443 StringRef LName = SP.getLinkageName();
3444 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003445
Chris Lattner233f52b2010-03-09 23:52:58 +00003446 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003447 if (LName.empty()) {
3448 Asm->OutStreamer.EmitBytes(Name, 0);
3449 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3450 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00003451 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3452 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00003453
Chris Lattner233f52b2010-03-09 23:52:58 +00003454 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00003455 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003456 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00003457
Devang Patel53bb5c92009-11-10 23:06:00 +00003458 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00003459 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00003460 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00003461 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003462
Chris Lattner3f53c832010-04-04 18:52:31 +00003463 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003464 Asm->OutStreamer.EmitSymbolValue(LI->first,
3465 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003466 }
3467 }
3468
Chris Lattnerc0215722010-04-04 19:25:43 +00003469 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003470}