blob: ce13945583e2c016823989c840d36f0572e7125c [file] [log] [blame]
Bill Wendling0310d762009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattner6cde3e62010-03-09 00:39:24 +000013
Devang Patele4b27562009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000016#include "DIE.h"
Bill Wendling57fbba42010-04-05 22:59:21 +000017#include "llvm/Constants.h"
Bill Wendling0310d762009-05-15 09:23:25 +000018#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000019#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000022#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattner45111d12010-01-16 21:57:06 +000025#include "llvm/Target/Mangler.h"
Bill Wendling0310d762009-05-15 09:23:25 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000030#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel2a4a3b72010-04-19 19:14:02 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000032#include "llvm/Analysis/DebugInfo.h"
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000033#include "llvm/ADT/STLExtras.h"
Chris Lattner23132b12009-08-24 03:52:50 +000034#include "llvm/ADT/StringExtras.h"
Devang Pateleac9c072010-04-27 19:46:33 +000035#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
Devang Patel3139fcf2010-01-26 21:39:14 +000038#include "llvm/Support/ValueHandle.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000039#include "llvm/Support/FormattedStream.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000040#include "llvm/Support/Timer.h"
41#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000042using namespace llvm;
43
Devang Pateleac9c072010-04-27 19:46:33 +000044static cl::opt<bool> PrintDbgScope("print-dbgscope", cl::Hidden,
45 cl::desc("Print DbgScope information for each machine instruction"));
46
47static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
48 cl::desc("Disable debug info printing"));
49
Dan Gohman281d65d2010-05-07 01:08:53 +000050static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
51 cl::desc("Make an absense of debug location information explicit."),
52 cl::init(false));
53
Bill Wendling5f017e82010-04-07 09:28:04 +000054namespace {
55 const char *DWARFGroupName = "DWARF Emission";
56 const char *DbgTimerName = "DWARF Debug Writer";
57} // end anonymous namespace
58
Bill Wendling0310d762009-05-15 09:23:25 +000059//===----------------------------------------------------------------------===//
60
61/// Configuration values for initial hash set sizes (log2).
62///
Bill Wendling0310d762009-05-15 09:23:25 +000063static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000064
65namespace llvm {
66
67//===----------------------------------------------------------------------===//
68/// CompileUnit - This dwarf writer support class manages information associate
69/// with a source file.
Nick Lewycky5f9843f2009-11-17 08:11:44 +000070class CompileUnit {
Bill Wendling0310d762009-05-15 09:23:25 +000071 /// ID - File identifier for source.
72 ///
73 unsigned ID;
74
75 /// Die - Compile unit debug information entry.
76 ///
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +000077 const OwningPtr<DIE> CUDie;
Bill Wendling0310d762009-05-15 09:23:25 +000078
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +000079 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel6f01d9c2009-11-21 00:31:03 +000080 DIE *IndexTyDie;
81
Bill Wendling0310d762009-05-15 09:23:25 +000082 /// GVToDieMap - Tracks the mapping of unit level debug informaton
83 /// variables to debug information entries.
Devang Patele4b27562009-08-28 23:24:31 +000084 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patele9f8f5e2010-05-07 20:54:48 +000085 DenseMap<const MDNode *, DIE *> GVToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000086
87 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
88 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patele4b27562009-08-28 23:24:31 +000089 /// FIXME : Rename
Devang Patele9f8f5e2010-05-07 20:54:48 +000090 DenseMap<const MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000091
92 /// Globals - A map of globally visible named entities for this unit.
93 ///
94 StringMap<DIE*> Globals;
95
Devang Patel193f7202009-11-24 01:14:22 +000096 /// GlobalTypes - A map of globally visible types for this unit.
97 ///
98 StringMap<DIE*> GlobalTypes;
99
Bill Wendling0310d762009-05-15 09:23:25 +0000100public:
101 CompileUnit(unsigned I, DIE *D)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000102 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000103
104 // Accessors.
Devang Patel193f7202009-11-24 01:14:22 +0000105 unsigned getID() const { return ID; }
Jeffrey Yasskind0f393d2010-03-11 18:29:55 +0000106 DIE* getCUDie() const { return CUDie.get(); }
Devang Patel193f7202009-11-24 01:14:22 +0000107 const StringMap<DIE*> &getGlobals() const { return Globals; }
108 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling0310d762009-05-15 09:23:25 +0000109
110 /// hasContent - Return true if this compile unit has something to write out.
111 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000112 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000113
Devang Patel2c4ceb12009-11-21 02:48:08 +0000114 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000115 ///
Benjamin Kramerbbb88db2010-03-31 20:15:45 +0000116 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +0000117
Devang Patel193f7202009-11-24 01:14:22 +0000118 /// addGlobalType - Add a new global type to the compile unit.
119 ///
Benjamin Kramerbbb88db2010-03-31 20:15:45 +0000120 void addGlobalType(StringRef Name, DIE *Die) {
Devang Patel193f7202009-11-24 01:14:22 +0000121 GlobalTypes[Name] = Die;
122 }
123
Devang Patel017d1212009-11-20 21:37:22 +0000124 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling0310d762009-05-15 09:23:25 +0000125 /// specified debug variable.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000126 DIE *getDIE(const MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000127
Devang Patel017d1212009-11-20 21:37:22 +0000128 /// insertDIE - Insert DIE into the map.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000129 void insertDIE(const MDNode *N, DIE *D) {
Devang Patel017d1212009-11-20 21:37:22 +0000130 GVToDieMap.insert(std::make_pair(N, D));
131 }
Bill Wendling0310d762009-05-15 09:23:25 +0000132
Devang Patel017d1212009-11-20 21:37:22 +0000133 /// getDIEEntry - Returns the debug information entry for the speciefied
134 /// debug variable.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000135 DIEEntry *getDIEEntry(const MDNode *N) {
136 DenseMap<const MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
Devang Patel6404e4e2009-12-15 19:16:48 +0000137 if (I == GVToDIEEntryMap.end())
138 return NULL;
139 return I->second;
140 }
Devang Patel017d1212009-11-20 21:37:22 +0000141
142 /// insertDIEEntry - Insert debug information entry into the map.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000143 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
Devang Patel017d1212009-11-20 21:37:22 +0000144 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000145 }
146
Devang Patel2c4ceb12009-11-21 02:48:08 +0000147 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000148 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000149 void addDie(DIE *Buffer) {
150 this->CUDie->addChild(Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000151 }
Devang Patel6f01d9c2009-11-21 00:31:03 +0000152
153 // getIndexTyDie - Get an anonymous type for index type.
154 DIE *getIndexTyDie() {
155 return IndexTyDie;
156 }
157
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000158 // setIndexTyDie - Set D as anonymous type for index which can be reused
159 // later.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000160 void setIndexTyDie(DIE *D) {
161 IndexTyDie = D;
162 }
163
Bill Wendling0310d762009-05-15 09:23:25 +0000164};
165
166//===----------------------------------------------------------------------===//
167/// DbgVariable - This class is used to track local variable information.
168///
Devang Patelf76a3d62009-11-16 21:53:40 +0000169class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000170 DIVariable Var; // Variable Descriptor.
171 unsigned FrameIndex; // Variable frame index.
Devang Patel90a48ad2010-03-15 18:33:46 +0000172 const MachineInstr *DbgValueMInsn; // DBG_VALUE
Devang Patelaead63c2010-03-29 22:59:58 +0000173 // DbgValueLabel - DBG_VALUE is effective from this label.
174 MCSymbol *DbgValueLabel;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000175 DbgVariable *const AbstractVar; // Abstract variable for this variable.
Devang Patel53bb5c92009-11-10 23:06:00 +0000176 DIE *TheDIE;
Bill Wendling0310d762009-05-15 09:23:25 +0000177public:
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000178 // AbsVar may be NULL.
179 DbgVariable(DIVariable V, unsigned I, DbgVariable *AbsVar)
Devang Patelaead63c2010-03-29 22:59:58 +0000180 : Var(V), FrameIndex(I), DbgValueMInsn(0),
181 DbgValueLabel(0), AbstractVar(AbsVar), TheDIE(0) {}
Devang Patel90a48ad2010-03-15 18:33:46 +0000182 DbgVariable(DIVariable V, const MachineInstr *MI, DbgVariable *AbsVar)
Devang Patelaead63c2010-03-29 22:59:58 +0000183 : Var(V), FrameIndex(0), DbgValueMInsn(MI), DbgValueLabel(0),
184 AbstractVar(AbsVar), TheDIE(0)
Devang Patel90a48ad2010-03-15 18:33:46 +0000185 {}
Bill Wendling0310d762009-05-15 09:23:25 +0000186
187 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000188 DIVariable getVariable() const { return Var; }
189 unsigned getFrameIndex() const { return FrameIndex; }
Devang Patel90a48ad2010-03-15 18:33:46 +0000190 const MachineInstr *getDbgValue() const { return DbgValueMInsn; }
Devang Patelaead63c2010-03-29 22:59:58 +0000191 MCSymbol *getDbgValueLabel() const { return DbgValueLabel; }
192 void setDbgValueLabel(MCSymbol *L) { DbgValueLabel = L; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000193 DbgVariable *getAbstractVariable() const { return AbstractVar; }
194 void setDIE(DIE *D) { TheDIE = D; }
195 DIE *getDIE() const { return TheDIE; }
Devang Patel98e1cac2010-05-14 21:01:35 +0000196 bool hasLocation() {
197 return DbgValueMInsn || FrameIndex != ~0U;
198 }
Bill Wendling0310d762009-05-15 09:23:25 +0000199};
200
201//===----------------------------------------------------------------------===//
Devang Pateleac9c072010-04-27 19:46:33 +0000202/// DbgRange - This is used to track range of instructions with identical
203/// debug info scope.
204///
205typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
206
207//===----------------------------------------------------------------------===//
Bill Wendling0310d762009-05-15 09:23:25 +0000208/// DbgScope - This class is used to track scope information.
209///
Devang Patelf76a3d62009-11-16 21:53:40 +0000210class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000211 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000212 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel3139fcf2010-01-26 21:39:14 +0000213 // Location at which this scope is inlined.
Devang Patele9f8f5e2010-05-07 20:54:48 +0000214 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patel53bb5c92009-11-10 23:06:00 +0000215 bool AbstractScope; // Abstract Scope
Devang Pateld38dd112009-10-01 18:25:23 +0000216 const MachineInstr *LastInsn; // Last instruction of this scope.
217 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Pateleac9c072010-04-27 19:46:33 +0000218 unsigned DFSIn, DFSOut;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000219 // Scopes defined in scope. Contents not owned.
220 SmallVector<DbgScope *, 4> Scopes;
221 // Variables declared in scope. Contents owned.
222 SmallVector<DbgVariable *, 8> Variables;
Devang Pateleac9c072010-04-27 19:46:33 +0000223 SmallVector<DbgRange, 4> Ranges;
Owen Anderson04c05f72009-06-24 22:53:20 +0000224 // Private state for dump()
225 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000226public:
Devang Patele9f8f5e2010-05-07 20:54:48 +0000227 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000228 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Pateleac9c072010-04-27 19:46:33 +0000229 LastInsn(0), FirstInsn(0),
230 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000231 virtual ~DbgScope();
232
233 // Accessors.
234 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000235 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000236 DIDescriptor getDesc() const { return Desc; }
Devang Patele9f8f5e2010-05-07 20:54:48 +0000237 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
238 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000239 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
240 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Pateleac9c072010-04-27 19:46:33 +0000241 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
242
243 /// openInsnRange - This scope covers instruction range starting from MI.
244 void openInsnRange(const MachineInstr *MI) {
245 if (!FirstInsn)
246 FirstInsn = MI;
247
248 if (Parent)
249 Parent->openInsnRange(MI);
250 }
251
252 /// extendInsnRange - Extend the current instruction range covered by
253 /// this scope.
254 void extendInsnRange(const MachineInstr *MI) {
255 assert (FirstInsn && "MI Range is not open!");
256 LastInsn = MI;
257 if (Parent)
258 Parent->extendInsnRange(MI);
259 }
260
261 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
262 /// until now. This is used when a new scope is encountered while walking
263 /// machine instructions.
264 void closeInsnRange(DbgScope *NewScope = NULL) {
265 assert (LastInsn && "Last insn missing!");
266 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
267 FirstInsn = NULL;
268 LastInsn = NULL;
269 // If Parent dominates NewScope then do not close Parent's instruction
270 // range.
271 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
272 Parent->closeInsnRange(NewScope);
273 }
274
Devang Patel53bb5c92009-11-10 23:06:00 +0000275 void setAbstractScope() { AbstractScope = true; }
276 bool isAbstractScope() const { return AbstractScope; }
Devang Pateleac9c072010-04-27 19:46:33 +0000277
278 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
279 unsigned getDFSOut() const { return DFSOut; }
280 void setDFSOut(unsigned O) { DFSOut = O; }
281 unsigned getDFSIn() const { return DFSIn; }
282 void setDFSIn(unsigned I) { DFSIn = I; }
283 bool dominates(const DbgScope *S) {
284 if (S == this)
285 return true;
286 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
287 return true;
288 return false;
289 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000290
Devang Patel2c4ceb12009-11-21 02:48:08 +0000291 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000292 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000293 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000294
Devang Patel2c4ceb12009-11-21 02:48:08 +0000295 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000296 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000297 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000298
Bill Wendling0310d762009-05-15 09:23:25 +0000299#ifndef NDEBUG
300 void dump() const;
301#endif
302};
Devang Pateleac9c072010-04-27 19:46:33 +0000303
Chris Lattnerea761862010-04-05 04:09:20 +0000304} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000305
306#ifndef NDEBUG
307void DbgScope::dump() const {
David Greenef83adbc2009-12-24 00:31:35 +0000308 raw_ostream &err = dbgs();
Chris Lattnerc281de12009-08-23 00:51:00 +0000309 err.indent(IndentLevel);
Devang Patele9f8f5e2010-05-07 20:54:48 +0000310 const MDNode *N = Desc;
Devang Patel53bb5c92009-11-10 23:06:00 +0000311 N->dump();
Devang Patel53bb5c92009-11-10 23:06:00 +0000312 if (AbstractScope)
313 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000314
315 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000316 if (!Scopes.empty())
317 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000318 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
319 if (Scopes[i] != this)
320 Scopes[i]->dump();
321
322 IndentLevel -= 2;
323}
324#endif
325
Bill Wendling0310d762009-05-15 09:23:25 +0000326DbgScope::~DbgScope() {
Bill Wendling0310d762009-05-15 09:23:25 +0000327 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
328 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000329}
330
Chris Lattner49cd6642010-04-05 05:11:15 +0000331DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel163a9f72010-05-10 22:49:55 +0000332 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Chris Lattner994cb122010-04-05 03:52:55 +0000333 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patelf2548ca2010-04-16 23:33:45 +0000334 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerbc733f52010-03-13 02:17:42 +0000335 NextStringPoolNumber = 0;
Chris Lattner9c69e285532010-04-04 22:59:04 +0000336
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000337 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
338 DwarfStrSectionSym = TextSectionSym = 0;
Devang Pateleac9c072010-04-27 19:46:33 +0000339 DwarfDebugRangeSectionSym = 0;
340 FunctionBeginSym = 0;
Torok Edwin9c421072010-04-07 10:44:46 +0000341 if (TimePassesIsEnabled) {
342 NamedRegionTimer T(DbgTimerName, DWARFGroupName);
343 beginModule(M);
344 } else {
345 beginModule(M);
346 }
Bill Wendling0310d762009-05-15 09:23:25 +0000347}
348DwarfDebug::~DwarfDebug() {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000349 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
350 DIEBlocks[j]->~DIEBlock();
Bill Wendling0310d762009-05-15 09:23:25 +0000351}
352
Chris Lattnerbc733f52010-03-13 02:17:42 +0000353MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
354 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
355 if (Entry.first) return Entry.first;
356
357 Entry.second = NextStringPoolNumber++;
Chris Lattnerc0215722010-04-04 19:25:43 +0000358 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000359}
360
361
Devang Patel2c4ceb12009-11-21 02:48:08 +0000362/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000363///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000364void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000365 // Profile the node so that we can make it unique.
366 FoldingSetNodeID ID;
367 Abbrev.Profile(ID);
368
369 // Check the set for priors.
370 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
371
372 // If it's newly added.
373 if (InSet == &Abbrev) {
374 // Add to abbreviation list.
375 Abbreviations.push_back(&Abbrev);
376
377 // Assign the vector position + 1 as its number.
378 Abbrev.setNumber(Abbreviations.size());
379 } else {
380 // Assign existing abbreviation number.
381 Abbrev.setNumber(InSet->getNumber());
382 }
383}
384
Devang Patel2c4ceb12009-11-21 02:48:08 +0000385/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000386/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000387DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000388 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000389 return Value;
390}
391
Devang Patel2c4ceb12009-11-21 02:48:08 +0000392/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000393///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000394void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000395 unsigned Form, uint64_t Integer) {
396 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000397 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000398 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000399}
400
Devang Patel2c4ceb12009-11-21 02:48:08 +0000401/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000402///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000403void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000404 unsigned Form, int64_t Integer) {
405 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000406 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000407 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000408}
409
Devang Patel69f57b12009-12-02 15:25:16 +0000410/// addString - Add a string attribute data and value. DIEString only
411/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000412void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattner4cf202b2010-01-23 03:11:46 +0000413 StringRef String) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000414 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000415 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000416}
417
Devang Patel2c4ceb12009-11-21 02:48:08 +0000418/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000419///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000420void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000421 const MCSymbol *Label) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000422 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000423 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000424}
425
Devang Patel2c4ceb12009-11-21 02:48:08 +0000426/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000427///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000428void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000429 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer345ef342010-03-31 19:34:01 +0000430 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000431 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000432}
433
Chris Lattner74e41f92010-04-05 05:24:55 +0000434/// addDIEEntry - Add a DIE attribute data and value.
435///
436void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
437 DIE *Entry) {
438 Die->addValue(Attribute, Form, createDIEEntry(Entry));
439}
440
441
Devang Patel2c4ceb12009-11-21 02:48:08 +0000442/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000443///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000444void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000445 DIEBlock *Block) {
Chris Lattnera37d5382010-04-05 00:18:22 +0000446 Block->ComputeSize(Asm);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000447 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000448 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000449}
450
Devang Patel2c4ceb12009-11-21 02:48:08 +0000451/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000452/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000453void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Devang Patelc665a512010-05-07 23:33:41 +0000454 // Verify variable.
455 if (!V->Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000456 return;
457
458 unsigned Line = V->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000459 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
460 V->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000461 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000462 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
463 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000464}
465
Devang Patel2c4ceb12009-11-21 02:48:08 +0000466/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000467/// entry.
Devang Patela49d8772010-05-07 23:19:07 +0000468void DwarfDebug::addSourceLine(DIE *Die, const DIGlobalVariable *G) {
Devang Patelc665a512010-05-07 23:33:41 +0000469 // Verify global variable.
470 if (!G->Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000471 return;
472
473 unsigned Line = G->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000474 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
475 G->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000476 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000477 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
478 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000479}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000480
Devang Patel2c4ceb12009-11-21 02:48:08 +0000481/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000482/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000483void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patelc665a512010-05-07 23:33:41 +0000484 // Verify subprogram.
485 if (!SP->Verify())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000486 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000487 // If the line number is 0, don't add it.
488 if (SP->getLineNumber() == 0)
489 return;
490
Devang Patel82dfc0c2009-08-31 22:47:13 +0000491 unsigned Line = SP->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000492 if (!SP->getContext().Verify())
493 return;
Devang Patel9bb59a22010-03-24 21:30:35 +0000494 unsigned FileID = GetOrCreateSourceID(SP->getDirectory(),
495 SP->getFilename());
Devang Patel82dfc0c2009-08-31 22:47:13 +0000496 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000497 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
498 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000499}
500
Devang Patel2c4ceb12009-11-21 02:48:08 +0000501/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000502/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000503void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Devang Patelc665a512010-05-07 23:33:41 +0000504 // Verify type.
505 if (!Ty->Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000506 return;
507
508 unsigned Line = Ty->getLineNumber();
Devang Patel77bf2952010-03-08 22:02:50 +0000509 if (!Ty->getContext().Verify())
510 return;
511 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
512 Ty->getContext().getFilename());
Bill Wendling0310d762009-05-15 09:23:25 +0000513 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000514 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
515 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000516}
517
Devang Patel6404e4e2009-12-15 19:16:48 +0000518/// addSourceLine - Add location information to specified debug information
519/// entry.
520void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
Devang Patelc665a512010-05-07 23:33:41 +0000521 // Verify namespace.
522 if (!NS->Verify())
Devang Patel6404e4e2009-12-15 19:16:48 +0000523 return;
524
525 unsigned Line = NS->getLineNumber();
526 StringRef FN = NS->getFilename();
527 StringRef Dir = NS->getDirectory();
528
529 unsigned FileID = GetOrCreateSourceID(Dir, FN);
530 assert(FileID && "Invalid file id");
531 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
532 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
533}
534
Caroline Ticedc8f6042009-08-31 21:19:37 +0000535/* Byref variables, in Blocks, are declared by the programmer as
536 "SomeType VarName;", but the compiler creates a
537 __Block_byref_x_VarName struct, and gives the variable VarName
538 either the struct, or a pointer to the struct, as its type. This
539 is necessary for various behind-the-scenes things the compiler
540 needs to do with by-reference variables in blocks.
541
542 However, as far as the original *programmer* is concerned, the
543 variable should still have type 'SomeType', as originally declared.
544
545 The following function dives into the __Block_byref_x_VarName
546 struct to find the original type of the variable. This will be
547 passed back to the code generating the type for the Debug
548 Information Entry for the variable 'VarName'. 'VarName' will then
549 have the original type 'SomeType' in its debug information.
550
551 The original type 'SomeType' will be the type of the field named
552 'VarName' inside the __Block_byref_x_VarName struct.
553
554 NOTE: In order for this to not completely fail on the debugger
555 side, the Debug Information Entry for the variable VarName needs to
556 have a DW_AT_location that tells the debugger how to unwind through
557 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patel2c4ceb12009-11-21 02:48:08 +0000558 value of the variable. The function addBlockByrefType does this. */
Caroline Ticedc8f6042009-08-31 21:19:37 +0000559
560/// Find the type the programmer originally declared the variable to be
561/// and return that type.
562///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000563DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000564
565 DIType subType = Ty;
566 unsigned tag = Ty.getTag();
567
568 if (tag == dwarf::DW_TAG_pointer_type) {
Devang Patel2db49d72010-05-07 18:11:54 +0000569 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000570 subType = DTy.getTypeDerivedFrom();
571 }
572
Devang Patel2db49d72010-05-07 18:11:54 +0000573 DICompositeType blockStruct = DICompositeType(subType);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000574 DIArray Elements = blockStruct.getTypeArray();
575
Caroline Ticedc8f6042009-08-31 21:19:37 +0000576 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
577 DIDescriptor Element = Elements.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +0000578 DIDerivedType DT = DIDerivedType(Element);
Devang Patel65dbc902009-11-25 17:36:49 +0000579 if (Name == DT.getName())
Caroline Ticedc8f6042009-08-31 21:19:37 +0000580 return (DT.getTypeDerivedFrom());
581 }
582
583 return Ty;
584}
585
Devang Patel2c4ceb12009-11-21 02:48:08 +0000586/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000587/// and generate the DWARF information necessary to find the actual variable
588/// given the extra address information encoded in the DIVariable, starting from
589/// the starting location. Add the DWARF information to the die.
590///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000591void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000592 unsigned Attribute,
593 const MachineLocation &Location) {
594 const DIVariable &VD = DV->getVariable();
595 DIType Ty = VD.getType();
596
597 // Decode the original location, and use that as the start of the byref
598 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000599 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000600 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000601 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000602
603 if (Location.isReg()) {
604 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000605 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000606 } else {
607 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000608 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
609 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000610 }
611 } else {
612 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000613 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000614 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000615 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
616 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000617 }
618
Devang Patel2c4ceb12009-11-21 02:48:08 +0000619 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000620 }
621
622 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
623 uint64_t Element = VD.getAddrElement(i);
624
625 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000626 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
627 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000628 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000629 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000630 } else llvm_unreachable("unknown DIFactory Opcode");
631 }
632
633 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000634 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000635}
636
Caroline Ticedc8f6042009-08-31 21:19:37 +0000637/* Byref variables, in Blocks, are declared by the programmer as "SomeType
638 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
639 gives the variable VarName either the struct, or a pointer to the struct, as
640 its type. This is necessary for various behind-the-scenes things the
641 compiler needs to do with by-reference variables in Blocks.
642
643 However, as far as the original *programmer* is concerned, the variable
644 should still have type 'SomeType', as originally declared.
645
Devang Patel2c4ceb12009-11-21 02:48:08 +0000646 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000647 struct to find the original type of the variable, which is then assigned to
648 the variable's Debug Information Entry as its real type. So far, so good.
649 However now the debugger will expect the variable VarName to have the type
650 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000651 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000652 pointers and struct to find the actual variable of type SomeType.
653
654 The following function does just that. We start by getting
655 the "normal" location for the variable. This will be the location
656 of either the struct __Block_byref_x_VarName or the pointer to the
657 struct __Block_byref_x_VarName.
658
659 The struct will look something like:
660
661 struct __Block_byref_x_VarName {
662 ... <various fields>
663 struct __Block_byref_x_VarName *forwarding;
664 ... <various other fields>
665 SomeType VarName;
666 ... <maybe more fields>
667 };
668
669 If we are given the struct directly (as our starting point) we
670 need to tell the debugger to:
671
672 1). Add the offset of the forwarding field.
673
Dan Gohmanf451cb82010-02-10 16:03:48 +0000674 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000675 struct to use (the real one may have been copied onto the heap).
676
677 3). Add the offset for the field VarName, to find the actual variable.
678
679 If we started with a pointer to the struct, then we need to
680 dereference that pointer first, before the other steps.
681 Translating this into DWARF ops, we will need to append the following
682 to the current location description for the variable:
683
684 DW_OP_deref -- optional, if we start with a pointer
685 DW_OP_plus_uconst <forward_fld_offset>
686 DW_OP_deref
687 DW_OP_plus_uconst <varName_fld_offset>
688
689 That is what this function does. */
690
Devang Patel2c4ceb12009-11-21 02:48:08 +0000691/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000692/// provided, and generate the DWARF information necessary to find the
693/// actual Block variable (navigating the Block struct) based on the
694/// starting location. Add the DWARF information to the die. For
695/// more information, read large comment just above here.
696///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000697void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000698 unsigned Attribute,
699 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000700 const DIVariable &VD = DV->getVariable();
701 DIType Ty = VD.getType();
702 DIType TmpTy = Ty;
703 unsigned Tag = Ty.getTag();
704 bool isPointer = false;
705
Devang Patel65dbc902009-11-25 17:36:49 +0000706 StringRef varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000707
708 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patel2db49d72010-05-07 18:11:54 +0000709 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000710 TmpTy = DTy.getTypeDerivedFrom();
711 isPointer = true;
712 }
713
Devang Patel2db49d72010-05-07 18:11:54 +0000714 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000715
Daniel Dunbar00564992009-09-19 20:40:14 +0000716 // Find the __forwarding field and the variable field in the __Block_byref
717 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000718 DIArray Fields = blockStruct.getTypeArray();
719 DIDescriptor varField = DIDescriptor();
720 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000721
Daniel Dunbar00564992009-09-19 20:40:14 +0000722 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
723 DIDescriptor Element = Fields.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +0000724 DIDerivedType DT = DIDerivedType(Element);
Devang Patel65dbc902009-11-25 17:36:49 +0000725 StringRef fieldName = DT.getName();
726 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000727 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000728 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000729 varField = Element;
730 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000731
Daniel Dunbar00564992009-09-19 20:40:14 +0000732 // Get the offsets for the forwarding field and the variable field.
Chris Lattner1d65ba72010-03-31 06:06:37 +0000733 unsigned forwardingFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000734 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner1d65ba72010-03-31 06:06:37 +0000735 unsigned varFieldOffset =
Devang Patel2db49d72010-05-07 18:11:54 +0000736 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000737
Mike Stump7e3720d2009-09-24 23:21:26 +0000738 // Decode the original location, and use that as the start of the byref
739 // variable's location.
Chris Lattnerd38fee82010-04-05 00:13:49 +0000740 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar00564992009-09-19 20:40:14 +0000741 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000742 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000743
Daniel Dunbar00564992009-09-19 20:40:14 +0000744 if (Location.isReg()) {
745 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000746 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000747 else {
748 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000749 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
750 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000751 }
752 } else {
753 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000754 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000755 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000756 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
757 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000758 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000759
Devang Patel2c4ceb12009-11-21 02:48:08 +0000760 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000761 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000762
Mike Stump7e3720d2009-09-24 23:21:26 +0000763 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000764 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000765 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000766 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000767
Daniel Dunbar00564992009-09-19 20:40:14 +0000768 // Next add the offset for the '__forwarding' field:
769 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
770 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000771 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000772 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
773 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000774 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000775
Daniel Dunbar00564992009-09-19 20:40:14 +0000776 // Now dereference the __forwarding field to get to the real __Block_byref
777 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000778 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000779
Daniel Dunbar00564992009-09-19 20:40:14 +0000780 // Now that we've got the real __Block_byref... struct, add the offset
781 // for the variable's field to get to the location of the actual variable:
782 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000783 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000784 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
785 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000786 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000787
Daniel Dunbar00564992009-09-19 20:40:14 +0000788 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000789 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000790}
791
Devang Patel2c4ceb12009-11-21 02:48:08 +0000792/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000793/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000794void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000795 const MachineLocation &Location) {
Chris Lattnerd38fee82010-04-05 00:13:49 +0000796 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling0310d762009-05-15 09:23:25 +0000797 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer345ef342010-03-31 19:34:01 +0000798 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bill Wendling0310d762009-05-15 09:23:25 +0000799
800 if (Location.isReg()) {
801 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000802 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000803 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000804 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
805 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000806 }
807 } else {
808 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000809 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000810 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000811 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
812 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000813 }
814
Devang Patel2c4ceb12009-11-21 02:48:08 +0000815 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000816 }
817
Devang Patel2c4ceb12009-11-21 02:48:08 +0000818 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000819}
820
Devang Patela43098d2010-04-28 01:03:09 +0000821/// addRegisterAddress - Add register location entry in variable DIE.
822bool DwarfDebug::addRegisterAddress(DIE *Die, DbgVariable *DV,
823 const MachineOperand &MO) {
824 assert (MO.isReg() && "Invalid machine operand!");
825 if (!MO.getReg())
826 return false;
827 MachineLocation Location;
828 Location.set(MO.getReg());
829 addAddress(Die, dwarf::DW_AT_location, Location);
830 if (MCSymbol *VS = DV->getDbgValueLabel())
831 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
832 return true;
833}
834
835/// addConstantValue - Add constant value entry in variable DIE.
836bool DwarfDebug::addConstantValue(DIE *Die, DbgVariable *DV,
837 const MachineOperand &MO) {
838 assert (MO.isImm() && "Invalid machine operand!");
839 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
840 unsigned Imm = MO.getImm();
841 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
842 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
843 if (MCSymbol *VS = DV->getDbgValueLabel())
844 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
845 return true;
846}
847
848/// addConstantFPValue - Add constant value entry in variable DIE.
849bool DwarfDebug::addConstantFPValue(DIE *Die, DbgVariable *DV,
850 const MachineOperand &MO) {
851 assert (MO.isFPImm() && "Invalid machine operand!");
852 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
853 APFloat FPImm = MO.getFPImm()->getValueAPF();
854
855 // Get the raw data form of the floating point.
856 const APInt FltVal = FPImm.bitcastToAPInt();
857 const char *FltPtr = (const char*)FltVal.getRawData();
858
859 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
860 bool LittleEndian = Asm->getTargetData().isLittleEndian();
861 int Incr = (LittleEndian ? 1 : -1);
862 int Start = (LittleEndian ? 0 : NumBytes - 1);
863 int Stop = (LittleEndian ? NumBytes : -1);
864
865 // Output the constant to DWARF one byte at a time.
866 for (; Start != Stop; Start += Incr)
867 addUInt(Block, 0, dwarf::DW_FORM_data1,
868 (unsigned char)0xFF & FltPtr[Start]);
869
870 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
871
872 if (MCSymbol *VS = DV->getDbgValueLabel())
873 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
874 VS);
875 return true;
876}
877
878
Devang Patelc366f832009-12-10 19:14:49 +0000879/// addToContextOwner - Add Die into the list of its context owner's children.
880void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3c91b052010-03-08 20:52:55 +0000881 if (Context.isType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000882 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patelc366f832009-12-10 19:14:49 +0000883 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000884 } else if (Context.isNameSpace()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000885 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel6404e4e2009-12-15 19:16:48 +0000886 ContextDIE->addChild(Die);
Devang Patel163a9f72010-05-10 22:49:55 +0000887 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patelc366f832009-12-10 19:14:49 +0000888 ContextDIE->addChild(Die);
889 else
Devang Patel163a9f72010-05-10 22:49:55 +0000890 getCompileUnit(Context)->addDie(Die);
Devang Patelc366f832009-12-10 19:14:49 +0000891}
892
Devang Patel16ced732009-12-10 18:05:33 +0000893/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
894/// given DIType.
895DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel163a9f72010-05-10 22:49:55 +0000896 CompileUnit *TypeCU = getCompileUnit(Ty);
897 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patel16ced732009-12-10 18:05:33 +0000898 if (TyDIE)
899 return TyDIE;
900
901 // Create new type.
902 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel163a9f72010-05-10 22:49:55 +0000903 TypeCU->insertDIE(Ty, TyDIE);
Devang Patel16ced732009-12-10 18:05:33 +0000904 if (Ty.isBasicType())
Devang Patel2db49d72010-05-07 18:11:54 +0000905 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000906 else if (Ty.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +0000907 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000908 else {
909 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel2db49d72010-05-07 18:11:54 +0000910 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patel16ced732009-12-10 18:05:33 +0000911 }
912
Devang Patelc366f832009-12-10 19:14:49 +0000913 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000914 return TyDIE;
915}
916
Devang Patel2c4ceb12009-11-21 02:48:08 +0000917/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000918void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9c004872010-05-07 21:45:47 +0000919 if (!Ty.Verify())
Bill Wendling0310d762009-05-15 09:23:25 +0000920 return;
921
922 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +0000923 CompileUnit *TypeCU = getCompileUnit(Ty);
924 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000925 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000926 if (Entry) {
927 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000928 return;
929 }
930
Bill Wendling0310d762009-05-15 09:23:25 +0000931 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000932 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000933
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000934 // Set up proxy.
935 Entry = createDIEEntry(Buffer);
Devang Patel163a9f72010-05-10 22:49:55 +0000936 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +0000937
Devang Patel2c4ceb12009-11-21 02:48:08 +0000938 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000939}
940
Devang Patel2c4ceb12009-11-21 02:48:08 +0000941/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000942void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000943 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000944 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000945 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000946 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000947 BTy.getEncoding());
948
949 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000950 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000951 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000952 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000953 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000954}
955
Devang Patel2c4ceb12009-11-21 02:48:08 +0000956/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000957void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000958 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000959 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000960 uint64_t Size = DTy.getSizeInBits() >> 3;
961 unsigned Tag = DTy.getTag();
962
963 // FIXME - Workaround for templates.
964 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
965
966 Buffer.setTag(Tag);
967
968 // Map to main type, void will not have a type.
969 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +0000970 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000971
972 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000973 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000974 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000975
976 // Add size if non-zero (derived types might be zero-sized.)
977 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000978 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000979
980 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +0000981 if (!DTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000982 addSourceLine(&Buffer, &DTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000983}
984
Devang Patel2c4ceb12009-11-21 02:48:08 +0000985/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +0000986void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000987 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000988 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000989
990 uint64_t Size = CTy.getSizeInBits() >> 3;
991 unsigned Tag = CTy.getTag();
992 Buffer.setTag(Tag);
993
994 switch (Tag) {
995 case dwarf::DW_TAG_vector_type:
996 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +0000997 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000998 break;
999 case dwarf::DW_TAG_enumeration_type: {
1000 DIArray Elements = CTy.getTypeArray();
1001
1002 // Add enumerators to enumeration type.
1003 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1004 DIE *ElemDie = NULL;
Devang Patel2db49d72010-05-07 18:11:54 +00001005 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3c91b052010-03-08 20:52:55 +00001006 if (Enum.isEnumerator()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001007 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001008 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +00001009 }
Bill Wendling0310d762009-05-15 09:23:25 +00001010 }
1011 }
1012 break;
1013 case dwarf::DW_TAG_subroutine_type: {
1014 // Add return type.
1015 DIArray Elements = CTy.getTypeArray();
1016 DIDescriptor RTy = Elements.getElement(0);
Devang Patel2db49d72010-05-07 18:11:54 +00001017 addType(&Buffer, DIType(RTy));
Bill Wendling0310d762009-05-15 09:23:25 +00001018
1019 // Add prototype flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001020 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001021
1022 // Add arguments.
1023 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1024 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1025 DIDescriptor Ty = Elements.getElement(i);
Devang Patel2db49d72010-05-07 18:11:54 +00001026 addType(Arg, DIType(Ty));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001027 Buffer.addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001028 }
1029 }
1030 break;
1031 case dwarf::DW_TAG_structure_type:
1032 case dwarf::DW_TAG_union_type:
1033 case dwarf::DW_TAG_class_type: {
1034 // Add elements to structure type.
1035 DIArray Elements = CTy.getTypeArray();
1036
1037 // A forward struct declared type may not have elements available.
Devang Patel3c91b052010-03-08 20:52:55 +00001038 unsigned N = Elements.getNumElements();
1039 if (N == 0)
Bill Wendling0310d762009-05-15 09:23:25 +00001040 break;
1041
1042 // Add elements to structure type.
Devang Patel3c91b052010-03-08 20:52:55 +00001043 for (unsigned i = 0; i < N; ++i) {
Bill Wendling0310d762009-05-15 09:23:25 +00001044 DIDescriptor Element = Elements.getElement(i);
1045 DIE *ElemDie = NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001046 if (Element.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001047 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel3c91b052010-03-08 20:52:55 +00001048 else if (Element.isVariable()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001049 DIVariable DV(Element);
Devang Patel1ee0cb92010-01-30 01:08:30 +00001050 ElemDie = new DIE(dwarf::DW_TAG_variable);
1051 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1052 DV.getName());
1053 addType(ElemDie, DV.getType());
1054 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1055 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1056 addSourceLine(ElemDie, &DV);
Devang Patel3c91b052010-03-08 20:52:55 +00001057 } else if (Element.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001058 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3c91b052010-03-08 20:52:55 +00001059 else
1060 continue;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001061 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001062 }
1063
Devang Patela1ba2692009-08-27 23:51:51 +00001064 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001065 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001066
1067 unsigned RLang = CTy.getRunTimeLang();
1068 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001069 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +00001070 dwarf::DW_FORM_data1, RLang);
Devang Patelb5544992010-01-26 21:16:06 +00001071
1072 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel2db49d72010-05-07 18:11:54 +00001073 if (DIDescriptor(ContainingType).isCompositeType())
Devang Patelb5544992010-01-26 21:16:06 +00001074 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patel2db49d72010-05-07 18:11:54 +00001075 getOrCreateTypeDIE(DIType(ContainingType)));
Bill Wendling0310d762009-05-15 09:23:25 +00001076 break;
1077 }
1078 default:
1079 break;
1080 }
1081
1082 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +00001083 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001084 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001085
Devang Patel8cbb6122010-01-29 18:34:58 +00001086 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendling0310d762009-05-15 09:23:25 +00001087 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1088 // Add size if non-zero (derived types might be zero-sized.)
1089 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001090 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +00001091 else {
1092 // Add zero size if it is not a forward declaration.
1093 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001094 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001095 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001096 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +00001097 }
1098
1099 // Add source line info if available.
1100 if (!CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001101 addSourceLine(&Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001102 }
1103}
1104
Devang Patel2c4ceb12009-11-21 02:48:08 +00001105/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1106void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +00001107 int64_t L = SR.getLo();
1108 int64_t H = SR.getHi();
1109 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1110
Devang Patel2c4ceb12009-11-21 02:48:08 +00001111 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +00001112 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001113 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +00001114 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +00001115
Devang Patel2c4ceb12009-11-21 02:48:08 +00001116 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +00001117}
1118
Devang Patel2c4ceb12009-11-21 02:48:08 +00001119/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +00001120void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +00001121 DICompositeType *CTy) {
1122 Buffer.setTag(dwarf::DW_TAG_array_type);
1123 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001124 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001125
1126 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +00001127 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001128 DIArray Elements = CTy->getTypeArray();
1129
Devang Patel6f01d9c2009-11-21 00:31:03 +00001130 // Get an anonymous type for index type.
Devang Patel163a9f72010-05-10 22:49:55 +00001131 CompileUnit *TheCU = getCompileUnit(*CTy);
1132 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +00001133 if (!IdxTy) {
1134 // Construct an anonymous type for index type.
1135 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001136 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1137 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001138 dwarf::DW_ATE_signed);
Devang Patel163a9f72010-05-10 22:49:55 +00001139 TheCU->addDie(IdxTy);
1140 TheCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001141 }
Bill Wendling0310d762009-05-15 09:23:25 +00001142
1143 // Add subranges to array type.
1144 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1145 DIDescriptor Element = Elements.getElement(i);
1146 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2db49d72010-05-07 18:11:54 +00001147 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001148 }
1149}
1150
Devang Patel2c4ceb12009-11-21 02:48:08 +00001151/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3c91b052010-03-08 20:52:55 +00001152DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001153 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3c91b052010-03-08 20:52:55 +00001154 StringRef Name = ETy.getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001155 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3c91b052010-03-08 20:52:55 +00001156 int64_t Value = ETy.getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001157 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001158 return Enumerator;
1159}
1160
Devang Patel351ca332010-01-05 01:46:14 +00001161/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1162/// printer to not emit usual symbol prefix before the symbol name is used then
1163/// return linkage name after skipping this special LLVM prefix.
1164static StringRef getRealLinkageName(StringRef LinkageName) {
1165 char One = '\1';
1166 if (LinkageName.startswith(StringRef(&One, 1)))
1167 return LinkageName.substr(1);
1168 return LinkageName;
1169}
1170
Devang Patel2c4ceb12009-11-21 02:48:08 +00001171/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patel8a241142009-12-09 18:24:21 +00001172DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001173 // If the global variable was optmized out then no need to create debug info
1174 // entry.
Devang Patel84c73e92009-11-06 17:58:12 +00001175 if (!GV.getGlobal()) return NULL;
Devang Patel65dbc902009-11-25 17:36:49 +00001176 if (GV.getDisplayName().empty()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001177
Bill Wendling0310d762009-05-15 09:23:25 +00001178 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001179 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001180 GV.getDisplayName());
1181
Devang Patel65dbc902009-11-25 17:36:49 +00001182 StringRef LinkageName = GV.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001183 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001184 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001185 getRealLinkageName(LinkageName));
1186
Devang Patel8a241142009-12-09 18:24:21 +00001187 addType(GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001188 if (!GV.isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001189 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1190 addSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001191
Bill Wendling0310d762009-05-15 09:23:25 +00001192 return GVDie;
1193}
1194
Devang Patel2c4ceb12009-11-21 02:48:08 +00001195/// createMemberDIE - Create new member DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001196DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001197 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001198 StringRef Name = DT.getName();
1199 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001200 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel65dbc902009-11-25 17:36:49 +00001201
Devang Patel8a241142009-12-09 18:24:21 +00001202 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001203
Devang Patel2c4ceb12009-11-21 02:48:08 +00001204 addSourceLine(MemberDie, &DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001205
Benjamin Kramer345ef342010-03-31 19:34:01 +00001206 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001207 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001208
Bill Wendling0310d762009-05-15 09:23:25 +00001209 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001210 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001211
1212 if (Size != FieldSize) {
1213 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001214 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1215 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001216
1217 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling0310d762009-05-15 09:23:25 +00001218 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1219 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer3d594fd2010-01-07 17:50:57 +00001220 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling0310d762009-05-15 09:23:25 +00001221 Offset -= FieldOffset;
1222
1223 // Maybe we need to work from the other end.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001224 if (Asm->getTargetData().isLittleEndian())
1225 Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001226 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001227
Devang Patel33db5082009-11-04 22:06:12 +00001228 // Here WD_AT_data_member_location points to the anonymous
1229 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001230 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001231
1232 } else
1233 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001234 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001235
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001236 if (DT.getTag() == dwarf::DW_TAG_inheritance
1237 && DT.isVirtual()) {
1238
1239 // For C++, virtual base classes are not at fixed offset. Use following
1240 // expression to extract appropriate offset from vtable.
1241 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1242
Benjamin Kramer345ef342010-03-31 19:34:01 +00001243 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc1dc8ff2010-02-03 20:08:48 +00001244 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1245 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1246 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1247 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1248 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1249 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1250 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1251
1252 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1253 VBaseLocationDie);
1254 } else
1255 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001256
1257 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001258 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001259 dwarf::DW_ACCESS_protected);
1260 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001261 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001262 dwarf::DW_ACCESS_private);
Devang Patel5d11eb02009-12-03 19:11:07 +00001263 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1264 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1265 dwarf::DW_ACCESS_public);
1266 if (DT.isVirtual())
1267 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1268 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001269 return MemberDie;
1270}
1271
Devang Patelffe966c2009-12-14 16:18:45 +00001272/// createSubprogramDIE - Create new DIE using SP.
1273DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
Devang Patel163a9f72010-05-10 22:49:55 +00001274 CompileUnit *SPCU = getCompileUnit(SP);
1275 DIE *SPDie = SPCU->getDIE(SP);
Devang Patelffe966c2009-12-14 16:18:45 +00001276 if (SPDie)
1277 return SPDie;
1278
1279 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel1eac3e72010-03-02 17:58:15 +00001280 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel6b506cb2010-03-02 01:26:20 +00001281 if (!SP.getName().empty())
1282 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001283
Devang Patel65dbc902009-11-25 17:36:49 +00001284 StringRef LinkageName = SP.getLinkageName();
Devang Patel351ca332010-01-05 01:46:14 +00001285 if (!LinkageName.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001286 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel351ca332010-01-05 01:46:14 +00001287 getRealLinkageName(LinkageName));
1288
Devang Patel2c4ceb12009-11-21 02:48:08 +00001289 addSourceLine(SPDie, &SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001290
Bill Wendling0310d762009-05-15 09:23:25 +00001291 // Add prototyped tag, if C or ObjC.
1292 unsigned Lang = SP.getCompileUnit().getLanguage();
1293 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1294 Lang == dwarf::DW_LANG_ObjC)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001295 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001296
1297 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001298 DICompositeType SPTy = SP.getType();
1299 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001300 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001301
Devang Patel3c91b052010-03-08 20:52:55 +00001302 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001303 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001304 else
Devang Patel2db49d72010-05-07 18:11:54 +00001305 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001306
Devang Patel5d11eb02009-12-03 19:11:07 +00001307 unsigned VK = SP.getVirtuality();
1308 if (VK) {
1309 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001310 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel5d11eb02009-12-03 19:11:07 +00001311 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1312 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1313 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel622b0262010-01-19 06:19:05 +00001314 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel2db49d72010-05-07 18:11:54 +00001315 SP.getContainingType()));
Devang Patel5d11eb02009-12-03 19:11:07 +00001316 }
1317
Devang Patelffe966c2009-12-14 16:18:45 +00001318 if (MakeDecl || !SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001319 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001320
1321 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001322 // be handled while processing variables.
1323 DICompositeType SPTy = SP.getType();
1324 DIArray Args = SPTy.getTypeArray();
1325 unsigned SPTag = SPTy.getTag();
1326
Bill Wendling0310d762009-05-15 09:23:25 +00001327 if (SPTag == dwarf::DW_TAG_subroutine_type)
1328 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1329 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001330 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelb4645642010-02-06 01:02:37 +00001331 addType(Arg, ATy);
1332 if (ATy.isArtificial())
1333 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001334 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001335 }
1336 }
1337
Devang Patel4e0d19d2010-02-03 19:57:19 +00001338 if (SP.isArtificial())
1339 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1340
Devang Patelccff8122010-04-30 19:38:23 +00001341 if (!SP.isLocalToUnit())
1342 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel2a4a3b72010-04-19 19:14:02 +00001343
Devang Patelccff8122010-04-30 19:38:23 +00001344 if (SP.isOptimized())
1345 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1346
Devang Patelccff8122010-04-30 19:38:23 +00001347 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel163a9f72010-05-10 22:49:55 +00001348 SPCU->insertDIE(SP, SPDie);
Devang Patelccff8122010-04-30 19:38:23 +00001349
Bill Wendling0310d762009-05-15 09:23:25 +00001350 return SPDie;
1351}
1352
Devang Patele9f8f5e2010-05-07 20:54:48 +00001353DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattnered7a77b2010-03-31 05:36:29 +00001354 assert(N && "Invalid Scope encoding!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001355
1356 DbgScope *AScope = AbstractScopes.lookup(N);
1357 if (AScope)
1358 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001359
Devang Patel53bb5c92009-11-10 23:06:00 +00001360 DbgScope *Parent = NULL;
1361
1362 DIDescriptor Scope(N);
1363 if (Scope.isLexicalBlock()) {
1364 DILexicalBlock DB(N);
1365 DIDescriptor ParentDesc = DB.getContext();
Devang Patel2db49d72010-05-07 18:11:54 +00001366 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel53bb5c92009-11-10 23:06:00 +00001367 }
1368
1369 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1370
1371 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001372 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001373 AScope->setAbstractScope();
1374 AbstractScopes[N] = AScope;
1375 if (DIDescriptor(N).isSubprogram())
1376 AbstractScopesList.push_back(AScope);
1377 return AScope;
1378}
Devang Patelaf9e8472009-10-01 20:31:14 +00001379
Devang Patel5f094002010-04-06 23:53:48 +00001380/// isSubprogramContext - Return true if Context is either a subprogram
1381/// or another context nested inside a subprogram.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001382static bool isSubprogramContext(const MDNode *Context) {
Devang Patel5f094002010-04-06 23:53:48 +00001383 if (!Context)
1384 return false;
1385 DIDescriptor D(Context);
1386 if (D.isSubprogram())
1387 return true;
1388 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001389 return isSubprogramContext(DIType(Context).getContext());
Devang Patel5f094002010-04-06 23:53:48 +00001390 return false;
1391}
1392
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001393/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001394/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1395/// If there are global variables in this scope then create and insert
1396/// DIEs for these variables.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001397DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel163a9f72010-05-10 22:49:55 +00001398 CompileUnit *SPCU = getCompileUnit(SPNode);
1399 DIE *SPDie = SPCU->getDIE(SPNode);
Chris Lattnerd38fee82010-04-05 00:13:49 +00001400 assert(SPDie && "Unable to find subprogram DIE!");
1401 DISubprogram SP(SPNode);
Chris Lattner206d61e2010-03-13 07:26:18 +00001402
Chris Lattnerd38fee82010-04-05 00:13:49 +00001403 // There is not any need to generate specification DIE for a function
1404 // defined at compile unit level. If a function is defined inside another
1405 // function then gdb prefers the definition at top level and but does not
1406 // expect specification DIE in parent function. So avoid creating
1407 // specification DIE for a function defined inside a function.
1408 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Devang Patel5f094002010-04-06 23:53:48 +00001409 !SP.getContext().isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +00001410 !isSubprogramContext(SP.getContext())) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00001411 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1412
1413 // Add arguments.
1414 DICompositeType SPTy = SP.getType();
1415 DIArray Args = SPTy.getTypeArray();
1416 unsigned SPTag = SPTy.getTag();
1417 if (SPTag == dwarf::DW_TAG_subroutine_type)
1418 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1419 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel2db49d72010-05-07 18:11:54 +00001420 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattnerd38fee82010-04-05 00:13:49 +00001421 addType(Arg, ATy);
1422 if (ATy.isArtificial())
1423 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1424 SPDie->addChild(Arg);
1425 }
1426 DIE *SPDeclDie = SPDie;
1427 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1428 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1429 SPDeclDie);
Devang Patel163a9f72010-05-10 22:49:55 +00001430 SPCU->addDie(SPDie);
Chris Lattnerd38fee82010-04-05 00:13:49 +00001431 }
1432
1433 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1434 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1435 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1436 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1437 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1438 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1439 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +00001440
Chris Lattnerd38fee82010-04-05 00:13:49 +00001441 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +00001442}
1443
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001444/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001445/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1446DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001447
1448 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1449 if (Scope->isAbstractScope())
1450 return ScopeDIE;
1451
1452 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1453 if (Ranges.empty())
1454 return 0;
1455
1456 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1457 if (Ranges.size() > 1) {
1458 // .debug_range section has not been laid out yet. Emit offset in
1459 // .debug_range as a uint, size 4, for now. emitDIE will handle
1460 // DW_AT_ranges appropriately.
1461 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1462 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1463 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1464 RE = Ranges.end(); RI != RE; ++RI) {
1465 DebugRangeSymbols.push_back(LabelsBeforeInsn.lookup(RI->first));
1466 DebugRangeSymbols.push_back(LabelsAfterInsn.lookup(RI->second));
1467 }
1468 DebugRangeSymbols.push_back(NULL);
1469 DebugRangeSymbols.push_back(NULL);
1470 return ScopeDIE;
1471 }
1472
1473 MCSymbol *Start = LabelsBeforeInsn.lookup(RI->first);
1474 MCSymbol *End = LabelsAfterInsn.lookup(RI->second);
1475
Devang Patelaead63c2010-03-29 22:59:58 +00001476 if (Start == 0 || End == 0) return 0;
Devang Patel53bb5c92009-11-10 23:06:00 +00001477
Chris Lattnerb7db7332010-03-09 01:58:53 +00001478 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1479 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattnera34ec2292010-03-09 01:51:43 +00001480
Devang Pateleac9c072010-04-27 19:46:33 +00001481 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1482 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patel53bb5c92009-11-10 23:06:00 +00001483
1484 return ScopeDIE;
1485}
1486
Devang Patel2c4ceb12009-11-21 02:48:08 +00001487/// constructInlinedScopeDIE - This scope represents inlined body of
1488/// a function. Construct DIE to represent this concrete inlined copy
1489/// of the function.
1490DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Pateleac9c072010-04-27 19:46:33 +00001491
1492 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1493 assert (Ranges.empty() == false
1494 && "DbgScope does not have instruction markers!");
1495
1496 // FIXME : .debug_inlined section specification does not clearly state how
1497 // to emit inlined scope that is split into multiple instruction ranges.
1498 // For now, use first instruction range and emit low_pc/high_pc pair and
1499 // corresponding .debug_inlined section entry for this pair.
1500 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1501 MCSymbol *StartLabel = LabelsBeforeInsn.lookup(RI->first);
1502 MCSymbol *EndLabel = LabelsAfterInsn.lookup(RI->second);
1503
1504 if (StartLabel == 0 || EndLabel == 0) {
1505 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1506 return 0;
1507 }
Chris Lattnerb7db7332010-03-09 01:58:53 +00001508 assert(StartLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001509 "Invalid starting label for an inlined scope!");
Chris Lattnerb7db7332010-03-09 01:58:53 +00001510 assert(EndLabel->isDefined() &&
Chris Lattnera34ec2292010-03-09 01:51:43 +00001511 "Invalid end label for an inlined scope!");
Devang Pateleac9c072010-04-27 19:46:33 +00001512
Devang Patel3c91b052010-03-08 20:52:55 +00001513 if (!Scope->getScopeNode())
Devang Patel0ef3fa62010-03-08 19:20:38 +00001514 return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001515 DIScope DS(Scope->getScopeNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001516 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1517
Devang Patel2db49d72010-05-07 18:11:54 +00001518 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel163a9f72010-05-10 22:49:55 +00001519 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1520 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001521 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001522 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001523 dwarf::DW_FORM_ref4, OriginDIE);
1524
Chris Lattner6ed0f902010-03-09 00:31:02 +00001525 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnerb7db7332010-03-09 01:58:53 +00001526 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel53bb5c92009-11-10 23:06:00 +00001527
1528 InlinedSubprogramDIEs.insert(OriginDIE);
1529
1530 // Track the start label for this inlined function.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001531 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel2db49d72010-05-07 18:11:54 +00001532 I = InlineInfo.find(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001533
1534 if (I == InlineInfo.end()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001535 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001536 ScopeDIE));
Devang Patel2db49d72010-05-07 18:11:54 +00001537 InlinedSPNodes.push_back(InlinedSP);
Devang Patel53bb5c92009-11-10 23:06:00 +00001538 } else
Chris Lattner6ed0f902010-03-09 00:31:02 +00001539 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001540
Devang Patel53bb5c92009-11-10 23:06:00 +00001541 DILocation DL(Scope->getInlinedAt());
Devang Patel163a9f72010-05-10 22:49:55 +00001542 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001543 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001544
1545 return ScopeDIE;
1546}
1547
Devang Patel2c4ceb12009-11-21 02:48:08 +00001548
1549/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001550DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001551 // Get the descriptor.
1552 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001553 StringRef Name = VD.getName();
1554 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001555 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001556
1557 // Translate tag to proper Dwarf tag. The result variable is dropped for
1558 // now.
1559 unsigned Tag;
1560 switch (VD.getTag()) {
1561 case dwarf::DW_TAG_return_variable:
1562 return NULL;
1563 case dwarf::DW_TAG_arg_variable:
1564 Tag = dwarf::DW_TAG_formal_parameter;
1565 break;
1566 case dwarf::DW_TAG_auto_variable: // fall thru
1567 default:
1568 Tag = dwarf::DW_TAG_variable;
1569 break;
1570 }
1571
1572 // Define variable debug information entry.
1573 DIE *VariableDie = new DIE(Tag);
1574
1575
1576 DIE *AbsDIE = NULL;
1577 if (DbgVariable *AV = DV->getAbstractVariable())
1578 AbsDIE = AV->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001579
Devang Patel53bb5c92009-11-10 23:06:00 +00001580 if (AbsDIE) {
1581 DIScope DS(Scope->getScopeNode());
Devang Patel2db49d72010-05-07 18:11:54 +00001582 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel163a9f72010-05-10 22:49:55 +00001583 DIE *OriginSPDIE = getCompileUnit(InlinedSP)->getDIE(InlinedSP);
Daniel Dunbarc0326792009-11-11 03:09:50 +00001584 (void) OriginSPDIE;
Chris Lattnered7a77b2010-03-31 05:36:29 +00001585 assert(OriginSPDIE && "Unable to find Origin DIE for the SP!");
Devang Patel53bb5c92009-11-10 23:06:00 +00001586 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
Chris Lattnered7a77b2010-03-31 05:36:29 +00001587 assert(AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001588 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001589 dwarf::DW_FORM_ref4, AbsDIE);
1590 }
1591 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001592 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1593 addSourceLine(VariableDie, &VD);
Devang Patel53bb5c92009-11-10 23:06:00 +00001594
1595 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001596 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001597 // addresses instead.
1598 if (VD.isBlockByrefVariable())
Devang Patel8a241142009-12-09 18:24:21 +00001599 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel53bb5c92009-11-10 23:06:00 +00001600 else
Devang Patel8a241142009-12-09 18:24:21 +00001601 addType(VariableDie, VD.getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001602 }
1603
1604 // Add variable address.
1605 if (!Scope->isAbstractScope()) {
Devang Patel90a48ad2010-03-15 18:33:46 +00001606 // Check if variable is described by DBG_VALUE instruction.
Devang Patela43098d2010-04-28 01:03:09 +00001607 if (const MachineInstr *DVInsn = DV->getDbgValue()) {
1608 bool updated = false;
1609 // FIXME : Handle getNumOperands != 3
1610 if (DVInsn->getNumOperands() == 3) {
1611 if (DVInsn->getOperand(0).isReg())
1612 updated = addRegisterAddress(VariableDie, DV, DVInsn->getOperand(0));
1613 else if (DVInsn->getOperand(0).isImm())
1614 updated = addConstantValue(VariableDie, DV, DVInsn->getOperand(0));
1615 else if (DVInsn->getOperand(0).isFPImm())
1616 updated = addConstantFPValue(VariableDie, DV, DVInsn->getOperand(0));
Devang Patel28ff35d2010-04-28 01:39:28 +00001617 } else {
1618 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1619 if (Location.getReg()) {
1620 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1621 if (MCSymbol *VS = DV->getDbgValueLabel())
1622 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1623 VS);
1624 updated = true;
1625 }
Devang Patela43098d2010-04-28 01:03:09 +00001626 }
1627 if (!updated) {
1628 // If variableDie is not updated then DBG_VALUE instruction does not
1629 // have valid variable info.
1630 delete VariableDie;
1631 return NULL;
1632 }
1633 }
1634 else {
Devang Patel90a48ad2010-03-15 18:33:46 +00001635 MachineLocation Location;
1636 unsigned FrameReg;
Chris Lattnerd38fee82010-04-05 00:13:49 +00001637 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Devang Patel98e1cac2010-05-14 21:01:35 +00001638 if (DV->hasLocation()) {
1639 int Offset = RI->getFrameIndexReference(*Asm->MF, DV->getFrameIndex(),
1640 FrameReg);
1641 Location.set(FrameReg, Offset);
1642
1643 if (VD.hasComplexAddress())
1644 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1645 else if (VD.isBlockByrefVariable())
1646 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1647 else
1648 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1649 }
Devang Patel90a48ad2010-03-15 18:33:46 +00001650 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001651 }
Devang Patelb4645642010-02-06 01:02:37 +00001652
1653 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1654 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel53bb5c92009-11-10 23:06:00 +00001655 DV->setDIE(VariableDie);
1656 return VariableDie;
1657
1658}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001659
Devang Patel193f7202009-11-24 01:14:22 +00001660void DwarfDebug::addPubTypes(DISubprogram SP) {
1661 DICompositeType SPTy = SP.getType();
1662 unsigned SPTag = SPTy.getTag();
1663 if (SPTag != dwarf::DW_TAG_subroutine_type)
1664 return;
1665
1666 DIArray Args = SPTy.getTypeArray();
Devang Patel193f7202009-11-24 01:14:22 +00001667 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel2db49d72010-05-07 18:11:54 +00001668 DIType ATy(Args.getElement(i));
Devang Patel9c004872010-05-07 21:45:47 +00001669 if (!ATy.Verify())
Devang Patel193f7202009-11-24 01:14:22 +00001670 continue;
1671 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel2db49d72010-05-07 18:11:54 +00001672 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel50d80e32010-04-13 20:35:04 +00001673 && !CATy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001674 CompileUnit *TheCU = getCompileUnit(CATy);
1675 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1676 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001677 }
1678 }
1679}
1680
Devang Patel2c4ceb12009-11-21 02:48:08 +00001681/// constructScopeDIE - Construct a DIE for this scope.
1682DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3c91b052010-03-08 20:52:55 +00001683 if (!Scope || !Scope->getScopeNode())
1684 return NULL;
1685
1686 DIScope DS(Scope->getScopeNode());
1687 DIE *ScopeDIE = NULL;
1688 if (Scope->getInlinedAt())
1689 ScopeDIE = constructInlinedScopeDIE(Scope);
1690 else if (DS.isSubprogram()) {
1691 if (Scope->isAbstractScope())
Devang Patel163a9f72010-05-10 22:49:55 +00001692 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +00001693 else
Devang Patel2db49d72010-05-07 18:11:54 +00001694 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3c91b052010-03-08 20:52:55 +00001695 }
Devang Patelaead63c2010-03-29 22:59:58 +00001696 else
Devang Patel3c91b052010-03-08 20:52:55 +00001697 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patelaead63c2010-03-29 22:59:58 +00001698 if (!ScopeDIE) return NULL;
Devang Patel3c91b052010-03-08 20:52:55 +00001699
Devang Patel53bb5c92009-11-10 23:06:00 +00001700 // Add variables to scope.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001701 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel53bb5c92009-11-10 23:06:00 +00001702 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001703 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001704 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001705 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001706 }
1707
1708 // Add nested scopes.
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001709 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel53bb5c92009-11-10 23:06:00 +00001710 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1711 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001712 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001713 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001714 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001715 }
Devang Patel193f7202009-11-24 01:14:22 +00001716
1717 if (DS.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001718 addPubTypes(DISubprogram(DS));
Devang Patel193f7202009-11-24 01:14:22 +00001719
1720 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001721}
1722
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001723/// GetOrCreateSourceID - Look up the source id with the given directory and
1724/// source file names. If none currently exists, create a new id and insert it
1725/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1726/// maps as well.
Chris Lattner1d65ba72010-03-31 06:06:37 +00001727unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001728 unsigned DId;
1729 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1730 if (DI != DirectoryIdMap.end()) {
1731 DId = DI->getValue();
1732 } else {
1733 DId = DirectoryNames.size() + 1;
1734 DirectoryIdMap[DirName] = DId;
1735 DirectoryNames.push_back(DirName);
1736 }
1737
1738 unsigned FId;
1739 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1740 if (FI != SourceFileIdMap.end()) {
1741 FId = FI->getValue();
1742 } else {
1743 FId = SourceFileNames.size() + 1;
1744 SourceFileIdMap[FileName] = FId;
1745 SourceFileNames.push_back(FileName);
1746 }
1747
1748 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1749 SourceIdMap.find(std::make_pair(DId, FId));
1750 if (SI != SourceIdMap.end())
1751 return SI->second;
1752
1753 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1754 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1755 SourceIds.push_back(std::make_pair(DId, FId));
1756
1757 return SrcId;
1758}
1759
Devang Patel6404e4e2009-12-15 19:16:48 +00001760/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1761DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel163a9f72010-05-10 22:49:55 +00001762 CompileUnit *TheCU = getCompileUnit(NS);
1763 DIE *NDie = TheCU->getDIE(NS);
Devang Patel6404e4e2009-12-15 19:16:48 +00001764 if (NDie)
1765 return NDie;
1766 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel163a9f72010-05-10 22:49:55 +00001767 TheCU->insertDIE(NS, NDie);
Devang Patel6404e4e2009-12-15 19:16:48 +00001768 if (!NS.getName().empty())
1769 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1770 addSourceLine(NDie, &NS);
1771 addToContextOwner(NDie, NS.getContext());
1772 return NDie;
1773}
1774
Devang Patel163a9f72010-05-10 22:49:55 +00001775/// constructCompileUnit - Create new CompileUnit for the given
1776/// metadata node with tag DW_TAG_compile_unit.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001777void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001778 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001779 StringRef FN = DIUnit.getFilename();
1780 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001781 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001782
1783 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001784 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001785 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001786 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001787 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001788 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel5098da02010-04-26 22:54:28 +00001789 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1790 // simplifies debug range entries.
1791 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_data4, 0);
Devang Patel4a602ca2010-03-22 23:11:36 +00001792 // DW_AT_stmt_list is a offset of line number information for this
1793 // compile unit in debug_line section. It is always zero when only one
1794 // compile unit is emitted in one object file.
1795 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001796
Devang Patel65dbc902009-11-25 17:36:49 +00001797 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001798 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001799 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001800 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001801
Devang Patel65dbc902009-11-25 17:36:49 +00001802 StringRef Flags = DIUnit.getFlags();
1803 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001804 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001805
1806 unsigned RVer = DIUnit.getRunTimeVersion();
1807 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001808 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001809 dwarf::DW_FORM_data1, RVer);
1810
Devang Patel163a9f72010-05-10 22:49:55 +00001811 CompileUnit *NewCU = new CompileUnit(ID, Die);
1812 if (!FirstCU)
1813 FirstCU = NewCU;
1814 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001815}
1816
Devang Patel163a9f72010-05-10 22:49:55 +00001817/// getCompielUnit - Get CompileUnit DIE.
1818CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1819 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1820 DIDescriptor D(N);
1821 const MDNode *CUNode = NULL;
1822 if (D.isCompileUnit())
1823 CUNode = N;
1824 else if (D.isSubprogram())
1825 CUNode = DISubprogram(N).getCompileUnit();
1826 else if (D.isType())
1827 CUNode = DIType(N).getCompileUnit();
1828 else if (D.isGlobalVariable())
1829 CUNode = DIGlobalVariable(N).getCompileUnit();
1830 else if (D.isVariable())
1831 CUNode = DIVariable(N).getCompileUnit();
1832 else if (D.isNameSpace())
1833 CUNode = DINameSpace(N).getCompileUnit();
1834 else if (D.isFile())
1835 CUNode = DIFile(N).getCompileUnit();
1836 else
1837 return FirstCU;
1838
1839 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1840 = CUMap.find(CUNode);
1841 if (I == CUMap.end())
1842 return FirstCU;
1843 return I->second;
1844}
1845
1846
1847/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001848void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001849 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001850
Devang Patel905cf5e2009-09-04 23:59:07 +00001851 // If debug information is malformed then ignore it.
1852 if (DI_GV.Verify() == false)
1853 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001854
1855 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001856 CompileUnit *TheCU = getCompileUnit(N);
1857 if (TheCU->getDIE(DI_GV))
Devang Patel13e16b62009-06-26 01:49:18 +00001858 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001859
Devang Patel8a241142009-12-09 18:24:21 +00001860 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Pateledb45632009-12-10 23:25:41 +00001861 if (!VariableDie)
1862 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001863
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001864 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +00001865 TheCU->insertDIE(N, VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001866
1867 // Add to context owner.
Devang Patelc9b16cc2010-01-15 01:12:22 +00001868 DIDescriptor GVContext = DI_GV.getContext();
1869 // Do not create specification DIE if context is either compile unit
1870 // or a subprogram.
Chris Lattner1d65ba72010-03-31 06:06:37 +00001871 if (DI_GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel5f094002010-04-06 23:53:48 +00001872 !GVContext.isFile() &&
Devang Patel2db49d72010-05-07 18:11:54 +00001873 !isSubprogramContext(GVContext)) {
Devang Patel6404e4e2009-12-15 19:16:48 +00001874 // Create specification DIE.
1875 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1876 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1877 dwarf::DW_FORM_ref4, VariableDie);
Benjamin Kramer345ef342010-03-31 19:34:01 +00001878 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel6404e4e2009-12-15 19:16:48 +00001879 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner4faf59a2010-03-08 22:31:46 +00001880 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattnerdeb0cba2010-03-12 21:09:07 +00001881 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel6404e4e2009-12-15 19:16:48 +00001882 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patel8581e012010-02-09 01:58:33 +00001883 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel163a9f72010-05-10 22:49:55 +00001884 TheCU->addDie(VariableSpecDIE);
Devang Patel6404e4e2009-12-15 19:16:48 +00001885 } else {
Benjamin Kramer345ef342010-03-31 19:34:01 +00001886 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel6404e4e2009-12-15 19:16:48 +00001887 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner4faf59a2010-03-08 22:31:46 +00001888 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattnerdeb0cba2010-03-12 21:09:07 +00001889 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel6404e4e2009-12-15 19:16:48 +00001890 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1891 }
Devang Patelc9b16cc2010-01-15 01:12:22 +00001892 addToContextOwner(VariableDie, GVContext);
Devang Patelc366f832009-12-10 19:14:49 +00001893
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001894 // Expose as global. FIXME - need to check external flag.
Devang Patel163a9f72010-05-10 22:49:55 +00001895 TheCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel193f7202009-11-24 01:14:22 +00001896
1897 DIType GTy = DI_GV.getType();
Devang Patel50d80e32010-04-13 20:35:04 +00001898 if (GTy.isCompositeType() && !GTy.getName().empty()
1899 && !GTy.isForwardDecl()) {
Devang Patel163a9f72010-05-10 22:49:55 +00001900 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattnered7a77b2010-03-31 05:36:29 +00001901 assert(Entry && "Missing global type!");
Devang Patel163a9f72010-05-10 22:49:55 +00001902 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel193f7202009-11-24 01:14:22 +00001903 }
Devang Patel13e16b62009-06-26 01:49:18 +00001904 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001905}
1906
Devang Patel163a9f72010-05-10 22:49:55 +00001907/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001908void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001909 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001910
Stuart Hastings639336e2010-04-06 21:38:29 +00001911 // Check for pre-existence.
Devang Patel163a9f72010-05-10 22:49:55 +00001912 CompileUnit *TheCU = getCompileUnit(N);
1913 if (TheCU->getDIE(N))
Stuart Hastings639336e2010-04-06 21:38:29 +00001914 return;
1915
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001916 if (!SP.isDefinition())
1917 // This is a method declaration which will be handled while constructing
1918 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001919 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001920
Stuart Hastings639336e2010-04-06 21:38:29 +00001921 DIE *SubprogramDie = createSubprogramDIE(SP);
1922
1923 // Add to map.
Devang Patel163a9f72010-05-10 22:49:55 +00001924 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001925
1926 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00001927 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00001928
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001929 // Expose as global.
Devang Patel163a9f72010-05-10 22:49:55 +00001930 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001931
Devang Patel13e16b62009-06-26 01:49:18 +00001932 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001933}
1934
Devang Patel2c4ceb12009-11-21 02:48:08 +00001935/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001936/// content. Create global DIEs and emit initial debug info sections.
1937/// This is inovked by the target AsmPrinter.
Chris Lattner75f50722010-04-04 07:48:20 +00001938void DwarfDebug::beginModule(Module *M) {
Devang Pateleac9c072010-04-27 19:46:33 +00001939 if (DisableDebugInfoPrinting)
1940 return;
1941
Devang Patel78ab9e22009-07-30 18:56:46 +00001942 DebugInfoFinder DbgFinder;
1943 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001944
Chris Lattnerd850ac72010-04-05 02:19:28 +00001945 bool HasDebugInfo = false;
1946
1947 // Scan all the compile-units to see if there are any marked as the main unit.
1948 // if not, we do not generate debug info.
1949 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1950 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1951 if (DICompileUnit(*I).isMain()) {
1952 HasDebugInfo = true;
1953 break;
1954 }
1955 }
1956
1957 if (!HasDebugInfo) return;
1958
1959 // Tell MMI that we have debug info.
1960 MMI->setDebugInfoAvailability(true);
1961
Chris Lattnerbe15beb2010-04-04 23:17:54 +00001962 // Emit initial sections.
Chris Lattnerd850ac72010-04-05 02:19:28 +00001963 EmitSectionLabels();
Chris Lattnerbe15beb2010-04-04 23:17:54 +00001964
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001965 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001966 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1967 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001968 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001969
Devang Patel53bb5c92009-11-10 23:06:00 +00001970 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001971 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1972 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001973 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00001974
Devang Patelc366f832009-12-10 19:14:49 +00001975 // Create DIEs for each global variable.
1976 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1977 E = DbgFinder.global_variable_end(); I != E; ++I)
1978 constructGlobalVariableDIE(*I);
1979
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001980 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001981 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001982
1983 // Print out .file directives to specify files for .loc directives. These are
1984 // printed out early so that they precede any .loc directives.
Chris Lattnerd38fee82010-04-05 00:13:49 +00001985 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001986 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1987 // Remember source id starts at 1.
1988 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001989 // FIXME: don't use sys::path for this! This should not depend on the
1990 // host.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001991 sys::Path FullPath(getSourceDirectoryName(Id.first));
1992 bool AppendOk =
1993 FullPath.appendComponent(getSourceFileName(Id.second));
1994 assert(AppendOk && "Could not append filename to directory!");
1995 AppendOk = false;
Chris Lattnera6594fc2010-01-25 18:58:59 +00001996 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001997 }
1998 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001999}
2000
Devang Patel2c4ceb12009-11-21 02:48:08 +00002001/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002002///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002003void DwarfDebug::endModule() {
Devang Patel163a9f72010-05-10 22:49:55 +00002004 if (!FirstCU) return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002005
Devang Patel53bb5c92009-11-10 23:06:00 +00002006 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
2007 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
2008 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
2009 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002010 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00002011 }
2012
Devang Patele9f8f5e2010-05-07 20:54:48 +00002013 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel5d11eb02009-12-03 19:11:07 +00002014 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2015 DIE *SPDie = CI->first;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002016 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel5d11eb02009-12-03 19:11:07 +00002017 if (!N) continue;
Devang Patel163a9f72010-05-10 22:49:55 +00002018 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Patel5d11eb02009-12-03 19:11:07 +00002019 if (!NDie) continue;
2020 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel5d11eb02009-12-03 19:11:07 +00002021 }
2022
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002023 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002024 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002025 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002026 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerc0215722010-04-04 19:25:43 +00002027 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002028
2029 // End text sections.
2030 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002031 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerc0215722010-04-04 19:25:43 +00002032 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002033 }
2034
2035 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002036 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002037
2038 // Emit function debug frame information
2039 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2040 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002041 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002042
2043 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002044 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002045
2046 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00002047 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002048
2049 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002050 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002051
2052 // Emit source line correspondence into a debug line section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002053 emitDebugLines();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002054
2055 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002056 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002057
Devang Patel193f7202009-11-24 01:14:22 +00002058 // Emit info into a debug pubtypes section.
2059 emitDebugPubTypes();
2060
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002061 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002062 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002063
2064 // Emit info into a debug aranges section.
2065 EmitDebugARanges();
2066
2067 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002068 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002069
2070 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002071 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002072
2073 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002074 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002075
Chris Lattnerbc733f52010-03-13 02:17:42 +00002076 // Emit info into a debug str section.
2077 emitDebugStr();
2078
Devang Patel163a9f72010-05-10 22:49:55 +00002079 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2080 E = CUMap.end(); I != E; ++I)
2081 delete I->second;
2082 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002083}
2084
Devang Patel53bb5c92009-11-10 23:06:00 +00002085/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002086DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2087 unsigned FrameIdx,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002088 DebugLoc ScopeLoc) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002089
Devang Patel2db49d72010-05-07 18:11:54 +00002090 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00002091 if (AbsDbgVariable)
2092 return AbsDbgVariable;
2093
Devang Patel2db49d72010-05-07 18:11:54 +00002094 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002095 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00002096 if (!Scope)
2097 return NULL;
2098
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002099 AbsDbgVariable = new DbgVariable(Var, FrameIdx,
2100 NULL /* No more-abstract variable*/);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002101 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00002102 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00002103 return AbsDbgVariable;
2104}
2105
Devang Patel90a48ad2010-03-15 18:33:46 +00002106/// findAbstractVariable - Find abstract variable, if any, associated with Var.
2107/// FIXME : Refactor findAbstractVariable.
2108DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2109 const MachineInstr *MI,
Chris Lattnerde4845c2010-04-02 19:42:39 +00002110 DebugLoc ScopeLoc) {
Devang Patel90a48ad2010-03-15 18:33:46 +00002111
Devang Patel2db49d72010-05-07 18:11:54 +00002112 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel90a48ad2010-03-15 18:33:46 +00002113 if (AbsDbgVariable)
2114 return AbsDbgVariable;
2115
Devang Patel2db49d72010-05-07 18:11:54 +00002116 LLVMContext &Ctx = Var->getContext();
Chris Lattnerde4845c2010-04-02 19:42:39 +00002117 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel90a48ad2010-03-15 18:33:46 +00002118 if (!Scope)
2119 return NULL;
2120
Devang Patelaead63c2010-03-29 22:59:58 +00002121 AbsDbgVariable = new DbgVariable(Var, MI,
Devang Patel90a48ad2010-03-15 18:33:46 +00002122 NULL /* No more-abstract variable*/);
2123 Scope->addVariable(AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00002124 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelaead63c2010-03-29 22:59:58 +00002125 DbgValueStartMap[MI] = AbsDbgVariable;
Devang Patel90a48ad2010-03-15 18:33:46 +00002126 return AbsDbgVariable;
2127}
2128
Devang Patel2c4ceb12009-11-21 02:48:08 +00002129/// collectVariableInfo - Populate DbgScope entries with variables' info.
Devang Patel98e1cac2010-05-14 21:01:35 +00002130void DwarfDebug::collectVariableInfo(const MachineFunction *MF) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00002131 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patel98e1cac2010-05-14 21:01:35 +00002132 SmallPtrSet<const MDNode *, 16> Processed;
Devang Patele717faa2009-10-06 01:26:37 +00002133 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2134 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2135 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00002136 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00002137 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00002138 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002139 DIVariable DV(Var);
2140 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002141
Chris Lattnerde4845c2010-04-02 19:42:39 +00002142 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002143 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00002144 Scope = ConcreteScopes.lookup(IA);
2145 if (Scope == 0)
2146 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
2147
Devang Patelfb0ee432009-11-10 23:20:04 +00002148 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002149 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00002150 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00002151
Chris Lattnerde4845c2010-04-02 19:42:39 +00002152 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, VP.second);
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002153 DbgVariable *RegVar = new DbgVariable(DV, VP.first, AbsDbgVariable);
Devang Patel2c4ceb12009-11-21 02:48:08 +00002154 Scope->addVariable(RegVar);
Devang Patele717faa2009-10-06 01:26:37 +00002155 }
Devang Patel90a48ad2010-03-15 18:33:46 +00002156
2157 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002158 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel90a48ad2010-03-15 18:33:46 +00002159 I != E; ++I) {
2160 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2161 II != IE; ++II) {
2162 const MachineInstr *MInsn = II;
Chris Lattner14d750d2010-03-31 05:39:57 +00002163 if (!MInsn->isDebugValue())
Devang Patel90a48ad2010-03-15 18:33:46 +00002164 continue;
Devang Patelaead63c2010-03-29 22:59:58 +00002165
Devang Patel0f9d9522010-04-27 20:54:45 +00002166 // Ignore Undef values.
Devang Patel97303ee2010-04-27 22:04:41 +00002167 if (MInsn->getOperand(0).isReg() && !MInsn->getOperand(0).getReg())
Devang Patel0f9d9522010-04-27 20:54:45 +00002168 continue;
2169
Dan Gohman82d5eaf2010-04-17 16:43:55 +00002170 DIVariable DV(
Devang Patele9f8f5e2010-05-07 20:54:48 +00002171 const_cast<const MDNode *>(MInsn->getOperand(MInsn->getNumOperands() - 1)
Dan Gohman82d5eaf2010-04-17 16:43:55 +00002172 .getMetadata()));
Devang Patel90a48ad2010-03-15 18:33:46 +00002173 if (DV.getTag() == dwarf::DW_TAG_arg_variable) {
2174 // FIXME Handle inlined subroutine arguments.
2175 DbgVariable *ArgVar = new DbgVariable(DV, MInsn, NULL);
2176 CurrentFnDbgScope->addVariable(ArgVar);
Devang Patelaead63c2010-03-29 22:59:58 +00002177 DbgValueStartMap[MInsn] = ArgVar;
Devang Patel26c69952010-05-14 21:55:50 +00002178 Processed.insert(DV);
Devang Patel90a48ad2010-03-15 18:33:46 +00002179 continue;
2180 }
2181
2182 DebugLoc DL = MInsn->getDebugLoc();
2183 if (DL.isUnknown()) continue;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002184 DbgScope *Scope = 0;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002185 if (const MDNode *IA = DL.getInlinedAt(Ctx))
Chris Lattnerde4845c2010-04-02 19:42:39 +00002186 Scope = ConcreteScopes.lookup(IA);
2187 if (Scope == 0)
2188 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2189
Devang Patel90a48ad2010-03-15 18:33:46 +00002190 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00002191 if (Scope == 0)
Devang Patel90a48ad2010-03-15 18:33:46 +00002192 continue;
2193
Devang Patel98e1cac2010-05-14 21:01:35 +00002194 Processed.insert(DV);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002195 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, MInsn, DL);
Devang Patel90a48ad2010-03-15 18:33:46 +00002196 DbgVariable *RegVar = new DbgVariable(DV, MInsn, AbsDbgVariable);
Devang Patelaead63c2010-03-29 22:59:58 +00002197 DbgValueStartMap[MInsn] = RegVar;
Devang Patel90a48ad2010-03-15 18:33:46 +00002198 Scope->addVariable(RegVar);
2199 }
2200 }
Devang Patel98e1cac2010-05-14 21:01:35 +00002201
2202 // Collect info for variables that were optimized out.
2203 if (NamedMDNode *NMD =
2204 MF->getFunction()->getParent()->getNamedMetadata("llvm.dbg.lv")) {
2205 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2206 DIVariable DV(cast_or_null<MDNode>(NMD->getOperand(i)));
2207 if (!Processed.insert(DV))
2208 continue;
2209 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2210 if (Scope)
2211 Scope->addVariable(new DbgVariable(DV, ~0U, NULL));
2212 }
2213 }
2214
Devang Patele717faa2009-10-06 01:26:37 +00002215}
2216
Devang Patel553881b2010-03-29 17:20:31 +00002217/// beginScope - Process beginning of a scope.
2218void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patel553881b2010-03-29 17:20:31 +00002219 // Check location.
2220 DebugLoc DL = MI->getDebugLoc();
Dan Gohman1cc0d622010-05-05 23:41:32 +00002221 if (DL.isUnknown()) {
Dan Gohman281d65d2010-05-07 01:08:53 +00002222 if (UnknownLocations) {
2223 // This instruction has no debug location. If the preceding instruction
2224 // did, emit debug location information to indicate that the debug
2225 // location is now unknown.
2226 MCSymbol *Label = NULL;
2227 if (DL == PrevInstLoc)
2228 Label = PrevLabel;
2229 else {
2230 Label = recordSourceLine(DL.getLine(), DL.getCol(), 0);
2231 PrevInstLoc = DL;
2232 PrevLabel = Label;
Devang Patelf4ccaea2010-05-19 21:26:53 +00002233 }
Devang Patel461a6462010-05-19 21:58:28 +00002234
2235 // If this instruction begins a scope then note down corresponding label.
2236 if (InsnsBeginScopeSet.count(MI) != 0)
2237 LabelsBeforeInsn[MI] = Label;
Dan Gohman281d65d2010-05-07 01:08:53 +00002238 }
Dan Gohman75395842010-05-06 00:29:41 +00002239
Devang Patel553881b2010-03-29 17:20:31 +00002240 return;
Dan Gohman1cc0d622010-05-05 23:41:32 +00002241 }
Devang Patel553881b2010-03-29 17:20:31 +00002242
Devang Patele9f8f5e2010-05-07 20:54:48 +00002243 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Chris Lattnerde4845c2010-04-02 19:42:39 +00002244
2245 // FIXME: Should only verify each scope once!
2246 if (!DIScope(Scope).Verify())
Devang Patel553881b2010-03-29 17:20:31 +00002247 return;
Devang Patel553881b2010-03-29 17:20:31 +00002248
Devang Patelaead63c2010-03-29 22:59:58 +00002249 // DBG_VALUE instruction establishes new value.
Chris Lattner14d750d2010-03-31 05:39:57 +00002250 if (MI->isDebugValue()) {
Devang Patelaead63c2010-03-29 22:59:58 +00002251 DenseMap<const MachineInstr *, DbgVariable *>::iterator DI
2252 = DbgValueStartMap.find(MI);
2253 if (DI != DbgValueStartMap.end()) {
Devang Pateleac9c072010-04-27 19:46:33 +00002254 MCSymbol *Label = NULL;
2255 if (DL == PrevInstLoc)
2256 Label = PrevLabel;
2257 else {
2258 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2259 PrevInstLoc = DL;
2260 PrevLabel = Label;
2261 }
Devang Patelaead63c2010-03-29 22:59:58 +00002262 DI->second->setDbgValueLabel(Label);
2263 }
Devang Patel7ed63112010-03-30 18:07:00 +00002264 return;
Devang Patelaead63c2010-03-29 22:59:58 +00002265 }
2266
Devang Patel553881b2010-03-29 17:20:31 +00002267 // Emit a label to indicate location change. This is used for line
Devang Pateleac9c072010-04-27 19:46:33 +00002268 // table even if this instruction does not start a new scope.
2269 MCSymbol *Label = NULL;
2270 if (DL == PrevInstLoc)
2271 Label = PrevLabel;
2272 else {
2273 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2274 PrevInstLoc = DL;
2275 PrevLabel = Label;
2276 }
Devang Patel553881b2010-03-29 17:20:31 +00002277
Devang Patel461a6462010-05-19 21:58:28 +00002278 // If this instruction begins a scope then note down corresponding label.
2279 if (InsnsBeginScopeSet.count(MI) != 0)
2280 LabelsBeforeInsn[MI] = Label;
Devang Patel0d20ac82009-10-06 01:50:42 +00002281}
2282
Devang Patel2c4ceb12009-11-21 02:48:08 +00002283/// endScope - Process end of a scope.
2284void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel1c246352010-04-08 16:50:29 +00002285 if (InsnsEndScopeSet.count(MI) != 0) {
2286 // Emit a label if this instruction ends a scope.
2287 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2288 Asm->OutStreamer.EmitLabel(Label);
Devang Pateleac9c072010-04-27 19:46:33 +00002289 LabelsAfterInsn[MI] = Label;
Devang Patel1c246352010-04-08 16:50:29 +00002290 }
Devang Patel53bb5c92009-11-10 23:06:00 +00002291}
2292
Devang Pateleac9c072010-04-27 19:46:33 +00002293/// getOrCreateDbgScope - Create DbgScope for the scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00002294DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002295 if (!InlinedAt) {
2296 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2297 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002298 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002299 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2300 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Pateleac9c072010-04-27 19:46:33 +00002301 if (DIDescriptor(Scope).isLexicalBlock()) {
2302 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002303 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Pateleac9c072010-04-27 19:46:33 +00002304 WScope->setParent(Parent);
2305 Parent->addScope(WScope);
2306 }
2307
2308 if (!WScope->getParent()) {
2309 StringRef SPName = DISubprogram(Scope).getLinkageName();
2310 if (SPName == Asm->MF->getFunction()->getName())
2311 CurrentFnDbgScope = WScope;
2312 }
2313
2314 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002315 }
2316
2317 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2318 if (WScope)
Devang Pateleac9c072010-04-27 19:46:33 +00002319 return WScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00002320
2321 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2322 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2323 DILocation DL(InlinedAt);
Devang Pateleac9c072010-04-27 19:46:33 +00002324 DbgScope *Parent =
Devang Patel2db49d72010-05-07 18:11:54 +00002325 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Pateleac9c072010-04-27 19:46:33 +00002326 WScope->setParent(Parent);
2327 Parent->addScope(WScope);
2328
2329 ConcreteScopes[InlinedAt] = WScope;
2330 getOrCreateAbstractScope(Scope);
2331
2332 return WScope;
Devang Patel0d20ac82009-10-06 01:50:42 +00002333}
2334
Devang Pateleac9c072010-04-27 19:46:33 +00002335/// hasValidLocation - Return true if debug location entry attached with
2336/// machine instruction encodes valid location info.
2337static bool hasValidLocation(LLVMContext &Ctx,
2338 const MachineInstr *MInsn,
Devang Patele9f8f5e2010-05-07 20:54:48 +00002339 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Pateleac9c072010-04-27 19:46:33 +00002340 if (MInsn->isDebugValue())
2341 return false;
2342 DebugLoc DL = MInsn->getDebugLoc();
2343 if (DL.isUnknown()) return false;
2344
Devang Patele9f8f5e2010-05-07 20:54:48 +00002345 const MDNode *S = DL.getScope(Ctx);
Devang Pateleac9c072010-04-27 19:46:33 +00002346
2347 // There is no need to create another DIE for compile unit. For all
2348 // other scopes, create one DbgScope now. This will be translated
2349 // into a scope DIE at the end.
2350 if (DIScope(S).isCompileUnit()) return false;
2351
2352 Scope = S;
2353 InlinedAt = DL.getInlinedAt(Ctx);
2354 return true;
2355}
2356
2357/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2358/// hierarchy.
2359static void calculateDominanceGraph(DbgScope *Scope) {
2360 assert (Scope && "Unable to calculate scop edominance graph!");
2361 SmallVector<DbgScope *, 4> WorkStack;
2362 WorkStack.push_back(Scope);
2363 unsigned Counter = 0;
2364 while (!WorkStack.empty()) {
2365 DbgScope *WS = WorkStack.back();
2366 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2367 bool visitedChildren = false;
2368 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2369 SE = Children.end(); SI != SE; ++SI) {
2370 DbgScope *ChildScope = *SI;
2371 if (!ChildScope->getDFSOut()) {
2372 WorkStack.push_back(ChildScope);
2373 visitedChildren = true;
2374 ChildScope->setDFSIn(++Counter);
2375 break;
2376 }
2377 }
2378 if (!visitedChildren) {
2379 WorkStack.pop_back();
2380 WS->setDFSOut(++Counter);
2381 }
2382 }
2383}
2384
2385/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
2386static
2387void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2388 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2389{
2390#ifndef NDEBUG
2391 unsigned PrevDFSIn = 0;
2392 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2393 I != E; ++I) {
2394 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2395 II != IE; ++II) {
2396 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002397 const MDNode *Scope = NULL;
2398 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002399
2400 // Check if instruction has valid location information.
2401 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2402 dbgs() << " [ ";
2403 if (InlinedAt)
2404 dbgs() << "*";
2405 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
2406 MI2ScopeMap.find(MInsn);
2407 if (DI != MI2ScopeMap.end()) {
2408 DbgScope *S = DI->second;
2409 dbgs() << S->getDFSIn();
2410 PrevDFSIn = S->getDFSIn();
2411 } else
2412 dbgs() << PrevDFSIn;
2413 } else
2414 dbgs() << " [ x" << PrevDFSIn;
2415 dbgs() << " ]";
2416 MInsn->dump();
2417 }
2418 dbgs() << "\n";
2419 }
2420#endif
2421}
Devang Patel2c4ceb12009-11-21 02:48:08 +00002422/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner14d750d2010-03-31 05:39:57 +00002423/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattnereec791a2010-01-26 23:18:02 +00002424bool DwarfDebug::extractScopeInformation() {
Devang Patelaf9e8472009-10-01 20:31:14 +00002425 // If scope information was extracted using .dbg intrinsics then there is not
2426 // any need to extract these information by scanning each instruction.
2427 if (!DbgScopeMap.empty())
2428 return false;
2429
Dan Gohman314bf7c2010-04-23 01:18:53 +00002430 // Scan each instruction and create scopes. First build working set of scopes.
Devang Pateleac9c072010-04-27 19:46:33 +00002431 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2432 SmallVector<DbgRange, 4> MIRanges;
2433 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002434 const MDNode *PrevScope = NULL;
2435 const MDNode *PrevInlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002436 const MachineInstr *RangeBeginMI = NULL;
2437 const MachineInstr *PrevMI = NULL;
Chris Lattnerd38fee82010-04-05 00:13:49 +00002438 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patelaf9e8472009-10-01 20:31:14 +00002439 I != E; ++I) {
2440 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2441 II != IE; ++II) {
2442 const MachineInstr *MInsn = II;
Devang Patele9f8f5e2010-05-07 20:54:48 +00002443 const MDNode *Scope = NULL;
2444 const MDNode *InlinedAt = NULL;
Devang Pateleac9c072010-04-27 19:46:33 +00002445
2446 // Check if instruction has valid location information.
2447 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2448 PrevMI = MInsn;
2449 continue;
2450 }
Chris Lattnerde4845c2010-04-02 19:42:39 +00002451
Devang Pateleac9c072010-04-27 19:46:33 +00002452 // If scope has not changed then skip this instruction.
2453 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2454 PrevMI = MInsn;
2455 continue;
2456 }
2457
2458 if (RangeBeginMI) {
2459 // If we have alread seen a beginning of a instruction range and
2460 // current instruction scope does not match scope of first instruction
2461 // in this range then create a new instruction range.
2462 DbgRange R(RangeBeginMI, PrevMI);
2463 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
2464 MIRanges.push_back(R);
2465 }
2466
2467 // This is a beginning of a new instruction range.
2468 RangeBeginMI = MInsn;
Chris Lattnerde4845c2010-04-02 19:42:39 +00002469
Devang Pateleac9c072010-04-27 19:46:33 +00002470 // Reset previous markers.
2471 PrevMI = MInsn;
2472 PrevScope = Scope;
2473 PrevInlinedAt = InlinedAt;
Devang Patel53bb5c92009-11-10 23:06:00 +00002474 }
2475 }
2476
Devang Pateleac9c072010-04-27 19:46:33 +00002477 // Create last instruction range.
2478 if (RangeBeginMI && PrevMI && PrevScope) {
2479 DbgRange R(RangeBeginMI, PrevMI);
2480 MIRanges.push_back(R);
2481 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patelaf9e8472009-10-01 20:31:14 +00002482 }
Devang Pateleac9c072010-04-27 19:46:33 +00002483
Devang Patel344130e2010-01-04 20:44:00 +00002484 if (!CurrentFnDbgScope)
2485 return false;
2486
Devang Pateleac9c072010-04-27 19:46:33 +00002487 calculateDominanceGraph(CurrentFnDbgScope);
2488 if (PrintDbgScope)
2489 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2490
2491 // Find ranges of instructions covered by each DbgScope;
2492 DbgScope *PrevDbgScope = NULL;
2493 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2494 RE = MIRanges.end(); RI != RE; ++RI) {
2495 const DbgRange &R = *RI;
2496 DbgScope *S = MI2ScopeMap.lookup(R.first);
2497 assert (S && "Lost DbgScope for a machine instruction!");
2498 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2499 PrevDbgScope->closeInsnRange(S);
2500 S->openInsnRange(R.first);
2501 S->extendInsnRange(R.second);
2502 PrevDbgScope = S;
2503 }
2504
2505 if (PrevDbgScope)
2506 PrevDbgScope->closeInsnRange();
Devang Patelaf9e8472009-10-01 20:31:14 +00002507
Devang Patele37b0c62010-04-08 18:43:56 +00002508 identifyScopeMarkers();
Devang Patel6122a4d2010-04-08 15:37:09 +00002509
2510 return !DbgScopeMap.empty();
2511}
2512
Devang Pateleac9c072010-04-27 19:46:33 +00002513/// identifyScopeMarkers() -
2514/// Each DbgScope has first instruction and last instruction to mark beginning
2515/// and end of a scope respectively. Create an inverse map that list scopes
2516/// starts (and ends) with an instruction. One instruction may start (or end)
2517/// multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00002518void DwarfDebug::identifyScopeMarkers() {
Devang Patel42aafd72010-01-20 02:05:23 +00002519 SmallVector<DbgScope *, 4> WorkList;
2520 WorkList.push_back(CurrentFnDbgScope);
2521 while (!WorkList.empty()) {
Chris Lattner14d750d2010-03-31 05:39:57 +00002522 DbgScope *S = WorkList.pop_back_val();
Devang Pateleac9c072010-04-27 19:46:33 +00002523
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002524 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patel42aafd72010-01-20 02:05:23 +00002525 if (!Children.empty())
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002526 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00002527 SE = Children.end(); SI != SE; ++SI)
2528 WorkList.push_back(*SI);
2529
Devang Patel53bb5c92009-11-10 23:06:00 +00002530 if (S->isAbstractScope())
2531 continue;
Devang Pateleac9c072010-04-27 19:46:33 +00002532
2533 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2534 if (Ranges.empty())
2535 continue;
2536 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2537 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel461a6462010-05-19 21:58:28 +00002538 assert(RI->first && "DbgRange does not have first instruction!");
2539 assert(RI->second && "DbgRange does not have second instruction!");
2540 InsnsBeginScopeSet.insert(RI->first);
Devang Pateleac9c072010-04-27 19:46:33 +00002541 InsnsEndScopeSet.insert(RI->second);
2542 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002543 }
Devang Patelaf9e8472009-10-01 20:31:14 +00002544}
2545
Dan Gohman084751c2010-04-20 00:37:27 +00002546/// FindFirstDebugLoc - Find the first debug location in the function. This
2547/// is intended to be an approximation for the source position of the
2548/// beginning of the function.
2549static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2550 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2551 I != E; ++I)
2552 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2553 MBBI != MBBE; ++MBBI) {
2554 DebugLoc DL = MBBI->getDebugLoc();
2555 if (!DL.isUnknown())
2556 return DL;
2557 }
2558 return DebugLoc();
2559}
2560
Devang Patel2c4ceb12009-11-21 02:48:08 +00002561/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002562/// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +00002563void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00002564 if (!MMI->hasDebugInfo()) return;
Bill Wendling5f017e82010-04-07 09:28:04 +00002565 if (!extractScopeInformation()) return;
Chris Lattnera909d662010-03-29 20:38:20 +00002566
Devang Patel98e1cac2010-05-14 21:01:35 +00002567 collectVariableInfo(MF);
Devang Patel60b35bd2009-10-06 18:37:31 +00002568
Devang Pateleac9c072010-04-27 19:46:33 +00002569 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2570 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002571 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00002572 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002573
2574 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2575 // function.
Dan Gohman084751c2010-04-20 00:37:27 +00002576 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerde4845c2010-04-02 19:42:39 +00002577 if (FDL.isUnknown()) return;
2578
Devang Patele9f8f5e2010-05-07 20:54:48 +00002579 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Chris Lattnerde4845c2010-04-02 19:42:39 +00002580
2581 DISubprogram SP = getDISubprogram(Scope);
2582 unsigned Line, Col;
2583 if (SP.Verify()) {
2584 Line = SP.getLineNumber();
2585 Col = 0;
2586 } else {
2587 Line = FDL.getLine();
2588 Col = FDL.getCol();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002589 }
Chris Lattnerde4845c2010-04-02 19:42:39 +00002590
2591 recordSourceLine(Line, Col, Scope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002592}
2593
Devang Patel2c4ceb12009-11-21 02:48:08 +00002594/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002595///
Chris Lattnereec791a2010-01-26 23:18:02 +00002596void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling5f017e82010-04-07 09:28:04 +00002597 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002598
Devang Patel344130e2010-01-04 20:44:00 +00002599 if (CurrentFnDbgScope) {
2600 // Define end label for subprogram.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002601 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_end",
2602 Asm->getFunctionNumber()));
Devang Patel344130e2010-01-04 20:44:00 +00002603
2604 // Get function line info.
2605 if (!Lines.empty()) {
2606 // Get section line info.
2607 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2608 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2609 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2610 // Append the function info to section info.
2611 SectionLineInfos.insert(SectionLineInfos.end(),
2612 Lines.begin(), Lines.end());
2613 }
2614
2615 // Construct abstract scopes.
2616 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2617 AE = AbstractScopesList.end(); AI != AE; ++AI)
2618 constructScopeDIE(*AI);
2619
Devang Patel9c488372010-05-04 06:15:30 +00002620 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Devang Patel344130e2010-01-04 20:44:00 +00002621
Devang Patel9c488372010-05-04 06:15:30 +00002622 if (!DisableFramePointerElim(*MF))
2623 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
2624 dwarf::DW_FORM_flag, 1);
2625
2626
Chris Lattnerd38fee82010-04-05 00:13:49 +00002627 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel344130e2010-01-04 20:44:00 +00002628 MMI->getFrameMoves()));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002629 }
2630
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002631 // Clear debug info
Devang Patelf54b8522010-01-19 01:26:02 +00002632 CurrentFnDbgScope = NULL;
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002633 DeleteContainerSeconds(DbgScopeMap);
Devang Patel461a6462010-05-19 21:58:28 +00002634 InsnsBeginScopeSet.clear();
Devang Patel51424712010-04-09 16:04:20 +00002635 InsnsEndScopeSet.clear();
Devang Patelaead63c2010-03-29 22:59:58 +00002636 DbgValueStartMap.clear();
Devang Patelf54b8522010-01-19 01:26:02 +00002637 ConcreteScopes.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002638 DeleteContainerSeconds(AbstractScopes);
Devang Patelf54b8522010-01-19 01:26:02 +00002639 AbstractScopesList.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00002640 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00002641 LabelsBeforeInsn.clear();
2642 LabelsAfterInsn.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002643 Lines.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00002644 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002645}
2646
Chris Lattnerc6087842010-03-09 04:54:43 +00002647/// recordSourceLine - Register a source line with debug info. Returns the
2648/// unique label that was emitted and which provides correspondence to
2649/// the source line list.
Devang Patele9f8f5e2010-05-07 20:54:48 +00002650MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S) {
Devang Patel65dbc902009-11-25 17:36:49 +00002651 StringRef Dir;
2652 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002653
Dan Gohman1cc0d622010-05-05 23:41:32 +00002654 unsigned Src = 1;
2655 if (S) {
2656 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00002657
Dan Gohman1cc0d622010-05-05 23:41:32 +00002658 if (Scope.isCompileUnit()) {
2659 DICompileUnit CU(S);
2660 Dir = CU.getDirectory();
2661 Fn = CU.getFilename();
2662 } else if (Scope.isSubprogram()) {
2663 DISubprogram SP(S);
2664 Dir = SP.getDirectory();
2665 Fn = SP.getFilename();
2666 } else if (Scope.isLexicalBlock()) {
2667 DILexicalBlock DB(S);
2668 Dir = DB.getDirectory();
2669 Fn = DB.getFilename();
2670 } else
2671 assert(0 && "Unexpected scope info");
2672
2673 Src = GetOrCreateSourceID(Dir, Fn);
2674 }
2675
Chris Lattner63d78362010-03-14 08:36:50 +00002676 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner25b68c62010-03-14 08:15:55 +00002677 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002678
Chris Lattnerc6087842010-03-09 04:54:43 +00002679 Asm->OutStreamer.EmitLabel(Label);
2680 return Label;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002681}
2682
Bill Wendling829e67b2009-05-20 23:22:40 +00002683//===----------------------------------------------------------------------===//
2684// Emit Methods
2685//===----------------------------------------------------------------------===//
2686
Devang Patel2c4ceb12009-11-21 02:48:08 +00002687/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002688///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002689unsigned
2690DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002691 // Get the children.
2692 const std::vector<DIE *> &Children = Die->getChildren();
2693
2694 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002695 if (!Last && !Children.empty())
Benjamin Kramer345ef342010-03-31 19:34:01 +00002696 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling94d04b82009-05-20 23:21:38 +00002697
2698 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002699 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002700
2701 // Get the abbreviation for this DIE.
2702 unsigned AbbrevNumber = Die->getAbbrevNumber();
2703 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2704
2705 // Set DIE offset
2706 Die->setOffset(Offset);
2707
2708 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002709 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002710
2711 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2712 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2713
2714 // Size the DIE attribute values.
2715 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2716 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00002717 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00002718
2719 // Size the DIE children if any.
2720 if (!Children.empty()) {
2721 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2722 "Children flag not set");
2723
2724 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002725 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002726
2727 // End of children marker.
2728 Offset += sizeof(int8_t);
2729 }
2730
2731 Die->setSize(Offset - Die->getOffset());
2732 return Offset;
2733}
2734
Devang Patel2c4ceb12009-11-21 02:48:08 +00002735/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002736///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002737void DwarfDebug::computeSizeAndOffsets() {
Devang Patel163a9f72010-05-10 22:49:55 +00002738 unsigned PrevOffset = 0;
2739 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2740 E = CUMap.end(); I != E; ++I) {
2741 // Compute size of compile unit header.
2742 static unsigned Offset = PrevOffset +
2743 sizeof(int32_t) + // Length of Compilation Unit Info
2744 sizeof(int16_t) + // DWARF version number
2745 sizeof(int32_t) + // Offset Into Abbrev. Section
2746 sizeof(int8_t); // Pointer Size (in bytes)
2747 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
2748 PrevOffset = Offset;
2749 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002750}
2751
Chris Lattner11b8f302010-04-04 23:02:02 +00002752/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2753/// temporary label to it if SymbolStem is specified.
Chris Lattner9c69e285532010-04-04 22:59:04 +00002754static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner11b8f302010-04-04 23:02:02 +00002755 const char *SymbolStem = 0) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002756 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner11b8f302010-04-04 23:02:02 +00002757 if (!SymbolStem) return 0;
2758
Chris Lattner9c69e285532010-04-04 22:59:04 +00002759 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2760 Asm->OutStreamer.EmitLabel(TmpSym);
2761 return TmpSym;
2762}
2763
2764/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2765/// the start of each one.
Chris Lattnerfa070b02010-04-04 22:33:59 +00002766void DwarfDebug::EmitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002767 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002768
Bill Wendling94d04b82009-05-20 23:21:38 +00002769 // Dwarf sections base addresses.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002770 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner9c69e285532010-04-04 22:59:04 +00002771 DwarfFrameSectionSym =
2772 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2773 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002774
Chris Lattner9c69e285532010-04-04 22:59:04 +00002775 DwarfInfoSectionSym =
2776 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2777 DwarfAbbrevSectionSym =
2778 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner11b8f302010-04-04 23:02:02 +00002779 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Chris Lattner9c69e285532010-04-04 22:59:04 +00002780
2781 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner11b8f302010-04-04 23:02:02 +00002782 EmitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002783
Chris Lattner11b8f302010-04-04 23:02:02 +00002784 EmitSectionSym(Asm, TLOF.getDwarfLineSection());
2785 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2786 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2787 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Chris Lattner9c69e285532010-04-04 22:59:04 +00002788 DwarfStrSectionSym =
2789 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patelf2548ca2010-04-16 23:33:45 +00002790 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2791 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00002792
Chris Lattner9c69e285532010-04-04 22:59:04 +00002793 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner4ad1efe2010-04-04 23:10:38 +00002794 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002795}
2796
Devang Patel2c4ceb12009-11-21 02:48:08 +00002797/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002798///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002799void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002800 // Get the abbreviation for this DIE.
2801 unsigned AbbrevNumber = Die->getAbbrevNumber();
2802 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2803
Bill Wendling94d04b82009-05-20 23:21:38 +00002804 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00002805 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00002806 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2807 Twine::utohexstr(Die->getOffset()) + ":0x" +
2808 Twine::utohexstr(Die->getSize()) + " " +
2809 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002810 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002811
Jeffrey Yasskin638fe8d2010-03-22 18:47:14 +00002812 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling94d04b82009-05-20 23:21:38 +00002813 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2814
2815 // Emit the DIE attribute values.
2816 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2817 unsigned Attr = AbbrevData[i].getAttribute();
2818 unsigned Form = AbbrevData[i].getForm();
2819 assert(Form && "Too many attributes for DIE (check abbreviation)");
2820
Chris Lattner3f53c832010-04-04 18:52:31 +00002821 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00002822 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2823
Bill Wendling94d04b82009-05-20 23:21:38 +00002824 switch (Attr) {
2825 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002826 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002827 break;
2828 case dwarf::DW_AT_abstract_origin: {
2829 DIEEntry *E = cast<DIEEntry>(Values[i]);
2830 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002831 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002832 Asm->EmitInt32(Addr);
2833 break;
2834 }
Devang Patelf2548ca2010-04-16 23:33:45 +00002835 case dwarf::DW_AT_ranges: {
2836 // DW_AT_range Value encodes offset in debug_range section.
2837 DIEInteger *V = cast<DIEInteger>(Values[i]);
2838 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2839 V->getValue(),
2840 DwarfDebugRangeSectionSym,
2841 4);
2842 break;
2843 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002844 default:
2845 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002846 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00002847 break;
2848 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002849 }
2850
2851 // Emit the DIE children if any.
2852 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2853 const std::vector<DIE *> &Children = Die->getChildren();
2854
2855 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002856 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002857
Chris Lattner3f53c832010-04-04 18:52:31 +00002858 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00002859 Asm->OutStreamer.AddComment("End Of Children Mark");
2860 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002861 }
2862}
2863
Devang Patel8a241142009-12-09 18:24:21 +00002864/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002865///
Devang Patel8a241142009-12-09 18:24:21 +00002866void DwarfDebug::emitDebugInfo() {
2867 // Start debug info section.
2868 Asm->OutStreamer.SwitchSection(
2869 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel163a9f72010-05-10 22:49:55 +00002870 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2871 E = CUMap.end(); I != E; ++I) {
2872 CompileUnit *TheCU = I->second;
2873 DIE *Die = TheCU->getCUDie();
2874
2875 // Emit the compile units header.
2876 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2877 TheCU->getID()));
2878
2879 // Emit size of content not including length itself
2880 unsigned ContentSize = Die->getSize() +
2881 sizeof(int16_t) + // DWARF version number
2882 sizeof(int32_t) + // Offset Into Abbrev. Section
2883 sizeof(int8_t) + // Pointer Size (in bytes)
2884 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2885
2886 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2887 Asm->EmitInt32(ContentSize);
2888 Asm->OutStreamer.AddComment("DWARF version number");
2889 Asm->EmitInt16(dwarf::DWARF_VERSION);
2890 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2891 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2892 DwarfAbbrevSectionSym);
2893 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2894 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
2895
2896 emitDIE(Die);
2897 // FIXME - extra padding for gdb bug.
2898 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2899 Asm->EmitInt8(0);
2900 Asm->EmitInt8(0);
2901 Asm->EmitInt8(0);
2902 Asm->EmitInt8(0);
2903 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
2904 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002905}
2906
Devang Patel2c4ceb12009-11-21 02:48:08 +00002907/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002908///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002909void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002910 // Check to see if it is worth the effort.
2911 if (!Abbreviations.empty()) {
2912 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002913 Asm->OutStreamer.SwitchSection(
2914 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002915
Chris Lattnerc0215722010-04-04 19:25:43 +00002916 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002917
2918 // For each abbrevation.
2919 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2920 // Get abbreviation data
2921 const DIEAbbrev *Abbrev = Abbreviations[i];
2922
2923 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002924 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00002925
2926 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002927 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00002928 }
2929
2930 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002931 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002932
Chris Lattnerc0215722010-04-04 19:25:43 +00002933 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002934 }
2935}
2936
Devang Patel2c4ceb12009-11-21 02:48:08 +00002937/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002938/// the line matrix.
2939///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002940void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002941 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00002942 Asm->OutStreamer.AddComment("Extended Op");
2943 Asm->EmitInt8(0);
2944
2945 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00002946 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00002947 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2948 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2949
2950 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00002951
Chris Lattnerc0215722010-04-04 19:25:43 +00002952 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerd38fee82010-04-05 00:13:49 +00002953 Asm->getTargetData().getPointerSize(),
2954 0/*AddrSpace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00002955
2956 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00002957 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2958 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002959 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00002960 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002961}
2962
Devang Patel2c4ceb12009-11-21 02:48:08 +00002963/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00002964///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002965void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002966 // If the target is using .loc/.file, the assembler will be emitting the
2967 // .debug_line table automatically.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002968 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002969 return;
2970
2971 // Minimum line delta, thus ranging from -10..(255-10).
2972 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2973 // Maximum line delta, thus ranging from -10..(255-10).
2974 const int MaxLineDelta = 255 + MinLineDelta;
2975
2976 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002977 Asm->OutStreamer.SwitchSection(
2978 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002979
2980 // Construct the section header.
Chris Lattner233f52b2010-03-09 23:52:58 +00002981 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnera6437182010-04-04 19:58:12 +00002982 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
2983 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00002984 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002985
Chris Lattner233f52b2010-03-09 23:52:58 +00002986 Asm->OutStreamer.AddComment("DWARF version number");
2987 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling94d04b82009-05-20 23:21:38 +00002988
Chris Lattner233f52b2010-03-09 23:52:58 +00002989 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnera6437182010-04-04 19:58:12 +00002990 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
2991 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerc0215722010-04-04 19:25:43 +00002992 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling94d04b82009-05-20 23:21:38 +00002993
Chris Lattner233f52b2010-03-09 23:52:58 +00002994 Asm->OutStreamer.AddComment("Minimum Instruction Length");
2995 Asm->EmitInt8(1);
2996 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
2997 Asm->EmitInt8(1);
2998 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
2999 Asm->EmitInt8(MinLineDelta);
3000 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
3001 Asm->EmitInt8(MaxLineDelta);
3002 Asm->OutStreamer.AddComment("Special Opcode Base");
3003 Asm->EmitInt8(-MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003004
3005 // Line number standard opcode encodings argument count
Chris Lattner233f52b2010-03-09 23:52:58 +00003006 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3007 Asm->EmitInt8(0);
3008 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3009 Asm->EmitInt8(1);
3010 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3011 Asm->EmitInt8(1);
3012 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3013 Asm->EmitInt8(1);
3014 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3015 Asm->EmitInt8(1);
3016 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3017 Asm->EmitInt8(0);
3018 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3019 Asm->EmitInt8(0);
3020 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3021 Asm->EmitInt8(0);
3022 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3023 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003024
3025 // Emit directories.
3026 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattner4cf202b2010-01-23 03:11:46 +00003027 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner3f53c832010-04-04 18:52:31 +00003028 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003029 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003030 }
3031
Chris Lattner233f52b2010-03-09 23:52:58 +00003032 Asm->OutStreamer.AddComment("End of directories");
3033 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003034
3035 // Emit files.
3036 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3037 // Remember source id starts at 1.
3038 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattner4cf202b2010-01-23 03:11:46 +00003039 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner3f53c832010-04-04 18:52:31 +00003040 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003041 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
3042
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003043 Asm->EmitULEB128(Id.first, "Directory #");
3044 Asm->EmitULEB128(0, "Mod date");
3045 Asm->EmitULEB128(0, "File size");
Bill Wendling94d04b82009-05-20 23:21:38 +00003046 }
3047
Chris Lattner233f52b2010-03-09 23:52:58 +00003048 Asm->OutStreamer.AddComment("End of files");
3049 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003050
Chris Lattnerc0215722010-04-04 19:25:43 +00003051 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003052
3053 // A sequence for each text section.
3054 unsigned SecSrcLinesSize = SectionSourceLines.size();
3055
3056 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3057 // Isolate current sections line info.
3058 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3059
Bill Wendling94d04b82009-05-20 23:21:38 +00003060 // Dwarf assumes we start with first line of first source file.
3061 unsigned Source = 1;
3062 unsigned Line = 1;
3063
3064 // Construct rows of the address, source, line, column matrix.
3065 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3066 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner25b68c62010-03-14 08:15:55 +00003067 MCSymbol *Label = LineInfo.getLabel();
Chris Lattnerb9130602010-03-14 02:20:58 +00003068 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling94d04b82009-05-20 23:21:38 +00003069
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003070 if (Asm->isVerbose()) {
Chris Lattner188a87d2010-03-10 01:04:13 +00003071 std::pair<unsigned, unsigned> SrcID =
Bill Wendling94d04b82009-05-20 23:21:38 +00003072 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner188a87d2010-03-10 01:04:13 +00003073 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner49f618a2010-03-10 02:29:31 +00003074 "/" +
3075 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner188a87d2010-03-10 01:04:13 +00003076 ":" + Twine(LineInfo.getLine()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003077 }
3078
3079 // Define the line address.
Chris Lattner233f52b2010-03-09 23:52:58 +00003080 Asm->OutStreamer.AddComment("Extended Op");
3081 Asm->EmitInt8(0);
3082 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003083 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00003084
3085 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3086 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3087
3088 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003089 Asm->OutStreamer.EmitSymbolValue(Label,
3090 Asm->getTargetData().getPointerSize(),
Chris Lattnerb9130602010-03-14 02:20:58 +00003091 0/*AddrSpace*/);
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003092
Bill Wendling94d04b82009-05-20 23:21:38 +00003093 // If change of source, then switch to the new source.
3094 if (Source != LineInfo.getSourceID()) {
3095 Source = LineInfo.getSourceID();
Chris Lattner233f52b2010-03-09 23:52:58 +00003096 Asm->OutStreamer.AddComment("DW_LNS_set_file");
3097 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003098 Asm->EmitULEB128(Source, "New Source");
Bill Wendling94d04b82009-05-20 23:21:38 +00003099 }
3100
3101 // If change of line.
3102 if (Line != LineInfo.getLine()) {
3103 // Determine offset.
3104 int Offset = LineInfo.getLine() - Line;
3105 int Delta = Offset - MinLineDelta;
3106
3107 // Update line.
3108 Line = LineInfo.getLine();
3109
3110 // If delta is small enough and in range...
3111 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3112 // ... then use fast opcode.
Chris Lattner233f52b2010-03-09 23:52:58 +00003113 Asm->OutStreamer.AddComment("Line Delta");
3114 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling94d04b82009-05-20 23:21:38 +00003115 } else {
3116 // ... otherwise use long hand.
Chris Lattner233f52b2010-03-09 23:52:58 +00003117 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling94d04b82009-05-20 23:21:38 +00003118 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003119 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner233f52b2010-03-09 23:52:58 +00003120 Asm->OutStreamer.AddComment("DW_LNS_copy");
3121 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003122 }
3123 } else {
3124 // Copy the previous row (different address or source)
Chris Lattner233f52b2010-03-09 23:52:58 +00003125 Asm->OutStreamer.AddComment("DW_LNS_copy");
3126 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling94d04b82009-05-20 23:21:38 +00003127 }
3128 }
3129
Devang Patel2c4ceb12009-11-21 02:48:08 +00003130 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003131 }
3132
3133 if (SecSrcLinesSize == 0)
3134 // Because we're emitting a debug_line section, we still need a line
3135 // table. The linker and friends expect it to exist. If there's nothing to
3136 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003137 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00003138
Chris Lattnerc0215722010-04-04 19:25:43 +00003139 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003140}
3141
Devang Patel2c4ceb12009-11-21 02:48:08 +00003142/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003143///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003144void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003145 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003146 return;
3147
Chris Lattnerd38fee82010-04-05 00:13:49 +00003148 int stackGrowth = Asm->getTargetData().getPointerSize();
3149 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3150 TargetFrameInfo::StackGrowsDown)
3151 stackGrowth *= -1;
Bill Wendling94d04b82009-05-20 23:21:38 +00003152
3153 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003154 Asm->OutStreamer.SwitchSection(
3155 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003156
Chris Lattnerc0215722010-04-04 19:25:43 +00003157 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003158 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003159 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3160 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003161
Chris Lattnerc0215722010-04-04 19:25:43 +00003162 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner233f52b2010-03-09 23:52:58 +00003163 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling94d04b82009-05-20 23:21:38 +00003164 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner233f52b2010-03-09 23:52:58 +00003165 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling94d04b82009-05-20 23:21:38 +00003166 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner233f52b2010-03-09 23:52:58 +00003167 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003168 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003169 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3170 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +00003171 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003172 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling94d04b82009-05-20 23:21:38 +00003173 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling94d04b82009-05-20 23:21:38 +00003174
3175 std::vector<MachineMove> Moves;
3176 RI->getInitialFrameState(Moves);
3177
Chris Lattner02b86b92010-04-04 23:41:46 +00003178 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003179
Chris Lattner059ea132010-04-28 01:05:45 +00003180 Asm->EmitAlignment(2);
Chris Lattnerc0215722010-04-04 19:25:43 +00003181 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling94d04b82009-05-20 23:21:38 +00003182}
3183
Devang Patel2c4ceb12009-11-21 02:48:08 +00003184/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00003185/// section.
Chris Lattner206d61e2010-03-13 07:26:18 +00003186void DwarfDebug::
3187emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003188 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003189 return;
3190
3191 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003192 Asm->OutStreamer.SwitchSection(
3193 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003194
Chris Lattner233f52b2010-03-09 23:52:58 +00003195 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner206d61e2010-03-13 07:26:18 +00003196 MCSymbol *DebugFrameBegin =
Chris Lattnerc0215722010-04-04 19:25:43 +00003197 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner206d61e2010-03-13 07:26:18 +00003198 MCSymbol *DebugFrameEnd =
Chris Lattnerc0215722010-04-04 19:25:43 +00003199 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnera6437182010-04-04 19:58:12 +00003200 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003201
Chris Lattner206d61e2010-03-13 07:26:18 +00003202 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling94d04b82009-05-20 23:21:38 +00003203
Chris Lattner233f52b2010-03-09 23:52:58 +00003204 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattner6189ed12010-04-04 23:25:33 +00003205 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
3206 DwarfFrameSectionSym);
Bill Wendling94d04b82009-05-20 23:21:38 +00003207
Chris Lattner233f52b2010-03-09 23:52:58 +00003208 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerc0215722010-04-04 19:25:43 +00003209 MCSymbol *FuncBeginSym =
3210 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattnerfb658072010-03-13 07:40:56 +00003211 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerd38fee82010-04-05 00:13:49 +00003212 Asm->getTargetData().getPointerSize(),
3213 0/*AddrSpace*/);
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00003214
3215
Chris Lattner233f52b2010-03-09 23:52:58 +00003216 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnera6437182010-04-04 19:58:12 +00003217 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerd38fee82010-04-05 00:13:49 +00003218 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003219
Chris Lattner02b86b92010-04-04 23:41:46 +00003220 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00003221
Chris Lattner059ea132010-04-28 01:05:45 +00003222 Asm->EmitAlignment(2);
Chris Lattner206d61e2010-03-13 07:26:18 +00003223 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling94d04b82009-05-20 23:21:38 +00003224}
3225
Devang Patel8a241142009-12-09 18:24:21 +00003226/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3227///
3228void DwarfDebug::emitDebugPubNames() {
Devang Patel163a9f72010-05-10 22:49:55 +00003229 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3230 E = CUMap.end(); I != E; ++I) {
3231 CompileUnit *TheCU = I->second;
3232 // Start the dwarf pubnames section.
3233 Asm->OutStreamer.SwitchSection(
3234 Asm->getObjFileLowering().getDwarfPubNamesSection());
Chris Lattner4cf202b2010-01-23 03:11:46 +00003235
Devang Patel163a9f72010-05-10 22:49:55 +00003236 Asm->OutStreamer.AddComment("Length of Public Names Info");
3237 Asm->EmitLabelDifference(
3238 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3239 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
3240
3241 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3242 TheCU->getID()));
3243
3244 Asm->OutStreamer.AddComment("DWARF Version");
3245 Asm->EmitInt16(dwarf::DWARF_VERSION);
3246
3247 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3248 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3249 DwarfInfoSectionSym);
3250
3251 Asm->OutStreamer.AddComment("Compilation Unit Length");
3252 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3253 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3254 4);
3255
3256 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3257 for (StringMap<DIE*>::const_iterator
3258 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3259 const char *Name = GI->getKeyData();
3260 DIE *Entity = GI->second;
3261
3262 Asm->OutStreamer.AddComment("DIE offset");
3263 Asm->EmitInt32(Entity->getOffset());
3264
3265 if (Asm->isVerbose())
3266 Asm->OutStreamer.AddComment("External Name");
3267 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3268 }
3269
3270 Asm->OutStreamer.AddComment("End Mark");
3271 Asm->EmitInt32(0);
3272 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3273 TheCU->getID()));
Bill Wendling94d04b82009-05-20 23:21:38 +00003274 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003275}
3276
Devang Patel193f7202009-11-24 01:14:22 +00003277void DwarfDebug::emitDebugPubTypes() {
Devang Patel163a9f72010-05-10 22:49:55 +00003278 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3279 E = CUMap.end(); I != E; ++I) {
3280 CompileUnit *TheCU = I->second;
3281 // Start the dwarf pubnames section.
3282 Asm->OutStreamer.SwitchSection(
3283 Asm->getObjFileLowering().getDwarfPubTypesSection());
3284 Asm->OutStreamer.AddComment("Length of Public Types Info");
3285 Asm->EmitLabelDifference(
3286 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3287 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Chris Lattner4cf202b2010-01-23 03:11:46 +00003288
Devang Patel163a9f72010-05-10 22:49:55 +00003289 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3290 TheCU->getID()));
3291
3292 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3293 Asm->EmitInt16(dwarf::DWARF_VERSION);
3294
3295 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3296 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3297 DwarfInfoSectionSym);
3298
3299 Asm->OutStreamer.AddComment("Compilation Unit Length");
3300 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3301 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3302 4);
3303
3304 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3305 for (StringMap<DIE*>::const_iterator
3306 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3307 const char *Name = GI->getKeyData();
3308 DIE * Entity = GI->second;
3309
3310 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3311 Asm->EmitInt32(Entity->getOffset());
3312
3313 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3314 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3315 }
3316
3317 Asm->OutStreamer.AddComment("End Mark");
3318 Asm->EmitInt32(0);
3319 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3320 TheCU->getID()));
Devang Patel193f7202009-11-24 01:14:22 +00003321 }
Devang Patel193f7202009-11-24 01:14:22 +00003322}
3323
Devang Patel2c4ceb12009-11-21 02:48:08 +00003324/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003325///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003326void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003327 // Check to see if it is worth the effort.
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003328 if (StringPool.empty()) return;
3329
3330 // Start the dwarf str section.
3331 Asm->OutStreamer.SwitchSection(
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003332 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003333
Chris Lattnerbc733f52010-03-13 02:17:42 +00003334 // Get all of the string pool entries and put them in an array by their ID so
3335 // we can sort them.
3336 SmallVector<std::pair<unsigned,
3337 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3338
3339 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3340 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3341 Entries.push_back(std::make_pair(I->second.second, &*I));
3342
3343 array_pod_sort(Entries.begin(), Entries.end());
3344
3345 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003346 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003347 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner0d9d70f2010-03-09 23:38:23 +00003348
3349 // Emit the string itself.
Chris Lattnerbc733f52010-03-13 02:17:42 +00003350 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling94d04b82009-05-20 23:21:38 +00003351 }
3352}
3353
Devang Patel2c4ceb12009-11-21 02:48:08 +00003354/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003355///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003356void DwarfDebug::emitDebugLoc() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003357 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003358 Asm->OutStreamer.SwitchSection(
3359 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003360}
3361
3362/// EmitDebugARanges - Emit visible names into a debug aranges section.
3363///
3364void DwarfDebug::EmitDebugARanges() {
3365 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003366 Asm->OutStreamer.SwitchSection(
3367 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00003368}
3369
Devang Patel2c4ceb12009-11-21 02:48:08 +00003370/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003371///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003372void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00003373 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003374 Asm->OutStreamer.SwitchSection(
Devang Patelf2548ca2010-04-16 23:33:45 +00003375 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Pateleac9c072010-04-27 19:46:33 +00003376 unsigned char Size = Asm->getTargetData().getPointerSize();
3377 for (SmallVector<const MCSymbol *, 8>::iterator
3378 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
3379 I != E; ++I) {
3380 if (*I)
3381 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003382 else
Devang Pateleac9c072010-04-27 19:46:33 +00003383 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patelf2548ca2010-04-16 23:33:45 +00003384 }
Bill Wendling94d04b82009-05-20 23:21:38 +00003385}
3386
Devang Patel2c4ceb12009-11-21 02:48:08 +00003387/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00003388///
Devang Patel2c4ceb12009-11-21 02:48:08 +00003389void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00003390 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00003391 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00003392 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003393 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00003394 }
3395}
3396
Devang Patel2c4ceb12009-11-21 02:48:08 +00003397/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00003398/// Section Header:
3399/// 1. length of section
3400/// 2. Dwarf version number
3401/// 3. address size.
3402///
3403/// Entries (one "entry" for each function that was inlined):
3404///
3405/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3406/// otherwise offset into __debug_str for regular function name.
3407/// 2. offset into __debug_str section for regular function name.
3408/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3409/// instances for the function.
3410///
3411/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3412/// inlined instance; the die_offset points to the inlined_subroutine die in the
3413/// __debug_info section, and the low_pc is the starting address for the
3414/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00003415void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerd38fee82010-04-05 00:13:49 +00003416 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00003417 return;
3418
Devang Patel163a9f72010-05-10 22:49:55 +00003419 if (!FirstCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00003420 return;
3421
Chris Lattner6c2f9e12009-08-19 05:49:37 +00003422 Asm->OutStreamer.SwitchSection(
3423 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattner0ad9c912010-01-22 22:09:00 +00003424
Chris Lattner233f52b2010-03-09 23:52:58 +00003425 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnera6437182010-04-04 19:58:12 +00003426 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3427 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling94d04b82009-05-20 23:21:38 +00003428
Chris Lattnerc0215722010-04-04 19:25:43 +00003429 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003430
Chris Lattner233f52b2010-03-09 23:52:58 +00003431 Asm->OutStreamer.AddComment("Dwarf Version");
3432 Asm->EmitInt16(dwarf::DWARF_VERSION);
3433 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003434 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00003435
Devang Patele9f8f5e2010-05-07 20:54:48 +00003436 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel53bb5c92009-11-10 23:06:00 +00003437 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00003438
Devang Patele9f8f5e2010-05-07 20:54:48 +00003439 const MDNode *Node = *I;
3440 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach7ab38df2009-11-22 19:20:36 +00003441 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00003442 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00003443 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00003444 StringRef LName = SP.getLinkageName();
3445 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00003446
Chris Lattner233f52b2010-03-09 23:52:58 +00003447 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattner4cf202b2010-01-23 03:11:46 +00003448 if (LName.empty()) {
3449 Asm->OutStreamer.EmitBytes(Name, 0);
3450 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3451 } else
Chris Lattner6189ed12010-04-04 23:25:33 +00003452 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3453 DwarfStrSectionSym);
Devang Patel53bb5c92009-11-10 23:06:00 +00003454
Chris Lattner233f52b2010-03-09 23:52:58 +00003455 Asm->OutStreamer.AddComment("Function name");
Chris Lattner6189ed12010-04-04 23:25:33 +00003456 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner7e1a8f82010-04-04 19:09:29 +00003457 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling94d04b82009-05-20 23:21:38 +00003458
Devang Patel53bb5c92009-11-10 23:06:00 +00003459 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00003460 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner3f53c832010-04-04 18:52:31 +00003461 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner6ed0f902010-03-09 00:31:02 +00003462 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00003463
Chris Lattner3f53c832010-04-04 18:52:31 +00003464 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerd38fee82010-04-05 00:13:49 +00003465 Asm->OutStreamer.EmitSymbolValue(LI->first,
3466 Asm->getTargetData().getPointerSize(),0);
Bill Wendling94d04b82009-05-20 23:21:38 +00003467 }
3468 }
3469
Chris Lattnerc0215722010-04-04 19:25:43 +00003470 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling94d04b82009-05-20 23:21:38 +00003471}