blob: 6baba7255b65db41dd3358e4aa777b5726a675f2 [file] [log] [blame]
Bill Wendlingb12b3d72009-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 Lattner2ab0c442010-03-09 00:39:24 +000013
Devang Patel15e723d2009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner855b6bb2010-04-05 05:24:55 +000016#include "DIE.h"
Bill Wendling989a27e2010-04-05 22:59:21 +000017#include "llvm/Constants.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000018#include "llvm/Module.h"
David Greened87baff2009-08-19 21:52:55 +000019#include "llvm/CodeGen/MachineFunction.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner855e8f52010-03-09 01:58:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000022#include "llvm/MC/MCSection.h"
Chris Lattner73266f92009-08-19 05:49:37 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattner855e8f52010-03-09 01:58:53 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattner31a54742010-01-16 21:57:06 +000025#include "llvm/Target/Mangler.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner017c7f92010-04-04 18:06:11 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000030#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel91474172010-04-19 19:14:02 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattner855b6bb2010-04-05 05:24:55 +000032#include "llvm/Analysis/DebugInfo.h"
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +000033#include "llvm/ADT/STLExtras.h"
Chris Lattnerf5377682009-08-24 03:52:50 +000034#include "llvm/ADT/StringExtras.h"
Devang Patelce7bf012010-04-27 19:46:33 +000035#include "llvm/Support/CommandLine.h"
Daniel Dunbarc74255d2009-10-13 06:47:08 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
Devang Patelae89d3b2010-01-26 21:39:14 +000038#include "llvm/Support/ValueHandle.h"
Chris Lattnerad653482010-01-22 22:09:00 +000039#include "llvm/Support/FormattedStream.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000040#include "llvm/Support/Timer.h"
41#include "llvm/System/Path.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000042using namespace llvm;
43
Devang Patelce7bf012010-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 Gohman77c565e2010-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 Wendlingce3c6252010-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 Wendlingb12b3d72009-05-15 09:23:25 +000059//===----------------------------------------------------------------------===//
60
61/// Configuration values for initial hash set sizes (log2).
62///
Bill Wendlingb12b3d72009-05-15 09:23:25 +000063static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendlingb12b3d72009-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 Lewyckyee68f452009-11-17 08:11:44 +000070class CompileUnit {
Bill Wendlingb12b3d72009-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 Yasskinecca2e62010-03-12 17:45:06 +000077 const OwningPtr<DIE> CUDie;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000078
Jeffrey Yasskinc3545712010-03-11 18:29:55 +000079 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel1233f912009-11-21 00:31:03 +000080 DIE *IndexTyDie;
81
Bill Wendlingb12b3d72009-05-15 09:23:25 +000082 /// GVToDieMap - Tracks the mapping of unit level debug informaton
83 /// variables to debug information entries.
Devang Patel15e723d2009-08-28 23:24:31 +000084 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel9c371c62010-05-07 20:54:48 +000085 DenseMap<const MDNode *, DIE *> GVToDieMap;
Bill Wendlingb12b3d72009-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 Patel15e723d2009-08-28 23:24:31 +000089 /// FIXME : Rename
Devang Patel9c371c62010-05-07 20:54:48 +000090 DenseMap<const MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendlingb12b3d72009-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 Patelec13b4f2009-11-24 01:14:22 +000096 /// GlobalTypes - A map of globally visible types for this unit.
97 ///
98 StringMap<DIE*> GlobalTypes;
99
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000100public:
101 CompileUnit(unsigned I, DIE *D)
Devang Patelc50078e2009-11-21 02:48:08 +0000102 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000103
104 // Accessors.
Devang Patelec13b4f2009-11-24 01:14:22 +0000105 unsigned getID() const { return ID; }
Jeffrey Yasskinc3545712010-03-11 18:29:55 +0000106 DIE* getCUDie() const { return CUDie.get(); }
Devang Patelec13b4f2009-11-24 01:14:22 +0000107 const StringMap<DIE*> &getGlobals() const { return Globals; }
108 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000109
110 /// hasContent - Return true if this compile unit has something to write out.
111 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000112 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000113
Devang Patelc50078e2009-11-21 02:48:08 +0000114 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000115 ///
Benjamin Kramer648940b2010-03-31 20:15:45 +0000116 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000117
Devang Patelec13b4f2009-11-24 01:14:22 +0000118 /// addGlobalType - Add a new global type to the compile unit.
119 ///
Benjamin Kramer648940b2010-03-31 20:15:45 +0000120 void addGlobalType(StringRef Name, DIE *Die) {
Devang Patelec13b4f2009-11-24 01:14:22 +0000121 GlobalTypes[Name] = Die;
122 }
123
Devang Pateld90672c2009-11-20 21:37:22 +0000124 /// getDIE - Returns the debug information entry map slot for the
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000125 /// specified debug variable.
Devang Patel9c371c62010-05-07 20:54:48 +0000126 DIE *getDIE(const MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach652b7432009-11-21 23:12:12 +0000127
Devang Pateld90672c2009-11-20 21:37:22 +0000128 /// insertDIE - Insert DIE into the map.
Devang Patel9c371c62010-05-07 20:54:48 +0000129 void insertDIE(const MDNode *N, DIE *D) {
Devang Pateld90672c2009-11-20 21:37:22 +0000130 GVToDieMap.insert(std::make_pair(N, D));
131 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000132
Devang Pateld90672c2009-11-20 21:37:22 +0000133 /// getDIEEntry - Returns the debug information entry for the speciefied
134 /// debug variable.
Devang Patel9c371c62010-05-07 20:54:48 +0000135 DIEEntry *getDIEEntry(const MDNode *N) {
136 DenseMap<const MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
Devang Patel8287d662009-12-15 19:16:48 +0000137 if (I == GVToDIEEntryMap.end())
138 return NULL;
139 return I->second;
140 }
Devang Pateld90672c2009-11-20 21:37:22 +0000141
142 /// insertDIEEntry - Insert debug information entry into the map.
Devang Patel9c371c62010-05-07 20:54:48 +0000143 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
Devang Pateld90672c2009-11-20 21:37:22 +0000144 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000145 }
146
Devang Patelc50078e2009-11-21 02:48:08 +0000147 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000148 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000149 void addDie(DIE *Buffer) {
150 this->CUDie->addChild(Buffer);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000151 }
Devang Patel1233f912009-11-21 00:31:03 +0000152
153 // getIndexTyDie - Get an anonymous type for index type.
154 DIE *getIndexTyDie() {
155 return IndexTyDie;
156 }
157
Jim Grosbachb23f2422009-11-22 19:20:36 +0000158 // setIndexTyDie - Set D as anonymous type for index which can be reused
159 // later.
Devang Patel1233f912009-11-21 00:31:03 +0000160 void setIndexTyDie(DIE *D) {
161 IndexTyDie = D;
162 }
163
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000164};
165
166//===----------------------------------------------------------------------===//
167/// DbgVariable - This class is used to track local variable information.
168///
Devang Patel4cb32c32009-11-16 21:53:40 +0000169class DbgVariable {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000170 DIVariable Var; // Variable Descriptor.
171 unsigned FrameIndex; // Variable frame index.
Devang Patel14da02f2010-03-15 18:33:46 +0000172 const MachineInstr *DbgValueMInsn; // DBG_VALUE
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000173 // DbgValueLabel - DBG_VALUE is effective from this label.
174 MCSymbol *DbgValueLabel;
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000175 DbgVariable *const AbstractVar; // Abstract variable for this variable.
Devang Patel90a0fe32009-11-10 23:06:00 +0000176 DIE *TheDIE;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000177public:
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000178 // AbsVar may be NULL.
179 DbgVariable(DIVariable V, unsigned I, DbgVariable *AbsVar)
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000180 : Var(V), FrameIndex(I), DbgValueMInsn(0),
181 DbgValueLabel(0), AbstractVar(AbsVar), TheDIE(0) {}
Devang Patel14da02f2010-03-15 18:33:46 +0000182 DbgVariable(DIVariable V, const MachineInstr *MI, DbgVariable *AbsVar)
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000183 : Var(V), FrameIndex(0), DbgValueMInsn(MI), DbgValueLabel(0),
184 AbstractVar(AbsVar), TheDIE(0)
Devang Patel14da02f2010-03-15 18:33:46 +0000185 {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000186
187 // Accessors.
Devang Patel90a0fe32009-11-10 23:06:00 +0000188 DIVariable getVariable() const { return Var; }
189 unsigned getFrameIndex() const { return FrameIndex; }
Devang Patel14da02f2010-03-15 18:33:46 +0000190 const MachineInstr *getDbgValue() const { return DbgValueMInsn; }
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000191 MCSymbol *getDbgValueLabel() const { return DbgValueLabel; }
192 void setDbgValueLabel(MCSymbol *L) { DbgValueLabel = L; }
Devang Patel90a0fe32009-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 Patelfad01682010-05-14 21:01:35 +0000196 bool hasLocation() {
197 return DbgValueMInsn || FrameIndex != ~0U;
198 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000199};
200
201//===----------------------------------------------------------------------===//
Devang Patelce7bf012010-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 Wendlingb12b3d72009-05-15 09:23:25 +0000208/// DbgScope - This class is used to track scope information.
209///
Devang Patel4cb32c32009-11-16 21:53:40 +0000210class DbgScope {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000211 DbgScope *Parent; // Parent to this scope.
Jim Grosbach652b7432009-11-21 23:12:12 +0000212 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patelae89d3b2010-01-26 21:39:14 +0000213 // Location at which this scope is inlined.
Devang Patel9c371c62010-05-07 20:54:48 +0000214 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patel90a0fe32009-11-10 23:06:00 +0000215 bool AbstractScope; // Abstract Scope
Devang Patel90ecd192009-10-01 18:25:23 +0000216 const MachineInstr *LastInsn; // Last instruction of this scope.
217 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Patelce7bf012010-04-27 19:46:33 +0000218 unsigned DFSIn, DFSOut;
Jeffrey Yasskinecca2e62010-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 Patelce7bf012010-04-27 19:46:33 +0000223 SmallVector<DbgRange, 4> Ranges;
Owen Anderson696d4862009-06-24 22:53:20 +0000224 // Private state for dump()
225 mutable unsigned IndentLevel;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000226public:
Devang Patel9c371c62010-05-07 20:54:48 +0000227 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patel90a0fe32009-11-10 23:06:00 +0000228 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Patelce7bf012010-04-27 19:46:33 +0000229 LastInsn(0), FirstInsn(0),
230 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000231 virtual ~DbgScope();
232
233 // Accessors.
234 DbgScope *getParent() const { return Parent; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000235 void setParent(DbgScope *P) { Parent = P; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000236 DIDescriptor getDesc() const { return Desc; }
Devang Patel9c371c62010-05-07 20:54:48 +0000237 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
238 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000239 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
240 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patelce7bf012010-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 Patel90a0fe32009-11-10 23:06:00 +0000275 void setAbstractScope() { AbstractScope = true; }
276 bool isAbstractScope() const { return AbstractScope; }
Devang Patelce7bf012010-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 Patel90a0fe32009-11-10 23:06:00 +0000290
Devang Patelc50078e2009-11-21 02:48:08 +0000291 /// addScope - Add a scope to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000292 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000293 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000294
Devang Patelc50078e2009-11-21 02:48:08 +0000295 /// addVariable - Add a variable to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000296 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000297 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000298
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000299#ifndef NDEBUG
300 void dump() const;
301#endif
302};
Devang Patelce7bf012010-04-27 19:46:33 +0000303
Chris Lattner8b9430c2010-04-05 04:09:20 +0000304} // end llvm namespace
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000305
306#ifndef NDEBUG
307void DbgScope::dump() const {
David Greenedbc06132009-12-24 00:31:35 +0000308 raw_ostream &err = dbgs();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000309 err.indent(IndentLevel);
Devang Patel9c371c62010-05-07 20:54:48 +0000310 const MDNode *N = Desc;
Devang Patel90a0fe32009-11-10 23:06:00 +0000311 N->dump();
Devang Patel90a0fe32009-11-10 23:06:00 +0000312 if (AbstractScope)
313 err << "Abstract Scope\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000314
315 IndentLevel += 2;
Devang Patel90a0fe32009-11-10 23:06:00 +0000316 if (!Scopes.empty())
317 err << "Children ...\n";
Bill Wendlingb12b3d72009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000326DbgScope::~DbgScope() {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000327 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
328 delete Variables[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000329}
330
Chris Lattnerec9a1f42010-04-05 05:11:15 +0000331DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel7e5a86e2010-05-10 22:49:55 +0000332 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Chris Lattner2cb53302010-04-05 03:52:55 +0000333 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel146c6f72010-04-16 23:33:45 +0000334 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattner0f093f82010-03-13 02:17:42 +0000335 NextStringPoolNumber = 0;
Chris Lattner66143d22010-04-04 22:59:04 +0000336
Chris Lattner094932e2010-04-04 23:10:38 +0000337 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
338 DwarfStrSectionSym = TextSectionSym = 0;
Devang Patelce7bf012010-04-27 19:46:33 +0000339 DwarfDebugRangeSectionSym = 0;
340 FunctionBeginSym = 0;
Edwin Törökc669cb62010-04-07 10:44:46 +0000341 if (TimePassesIsEnabled) {
342 NamedRegionTimer T(DbgTimerName, DWARFGroupName);
343 beginModule(M);
344 } else {
345 beginModule(M);
346 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000347}
348DwarfDebug::~DwarfDebug() {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000349 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
350 DIEBlocks[j]->~DIEBlock();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000351}
352
Chris Lattner0f093f82010-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 Lattnerb93558d2010-04-04 19:25:43 +0000358 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattner0f093f82010-03-13 02:17:42 +0000359}
360
361
Devang Patelc50078e2009-11-21 02:48:08 +0000362/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000363///
Devang Patelc50078e2009-11-21 02:48:08 +0000364void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendlingb12b3d72009-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 Patelc50078e2009-11-21 02:48:08 +0000385/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling0d3db8b2009-05-20 23:24:48 +0000386/// information entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000387DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000388 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000389 return Value;
390}
391
Devang Patelc50078e2009-11-21 02:48:08 +0000392/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000393///
Devang Patelc50078e2009-11-21 02:48:08 +0000394void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000395 unsigned Form, uint64_t Integer) {
396 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000397 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000398 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000399}
400
Devang Patelc50078e2009-11-21 02:48:08 +0000401/// addSInt - Add an signed integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000402///
Devang Patelc50078e2009-11-21 02:48:08 +0000403void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000404 unsigned Form, int64_t Integer) {
405 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000406 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000407 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000408}
409
Devang Pateldf0f2152009-12-02 15:25:16 +0000410/// addString - Add a string attribute data and value. DIEString only
411/// keeps string reference.
Devang Patelc50078e2009-11-21 02:48:08 +0000412void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnercc564642010-01-23 03:11:46 +0000413 StringRef String) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000414 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patelc50078e2009-11-21 02:48:08 +0000415 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000416}
417
Devang Patelc50078e2009-11-21 02:48:08 +0000418/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000419///
Devang Patelc50078e2009-11-21 02:48:08 +0000420void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000421 const MCSymbol *Label) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000422 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000423 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000424}
425
Devang Patelc50078e2009-11-21 02:48:08 +0000426/// addDelta - Add a label delta attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000427///
Devang Patelc50078e2009-11-21 02:48:08 +0000428void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000429 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000430 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patelc50078e2009-11-21 02:48:08 +0000431 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000432}
433
Chris Lattner855b6bb2010-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 Patelc50078e2009-11-21 02:48:08 +0000442/// addBlock - Add block data.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000443///
Devang Patelc50078e2009-11-21 02:48:08 +0000444void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000445 DIEBlock *Block) {
Chris Lattner352c8e22010-04-05 00:18:22 +0000446 Block->ComputeSize(Asm);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000447 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patelc50078e2009-11-21 02:48:08 +0000448 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000449}
450
Devang Patelc50078e2009-11-21 02:48:08 +0000451/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000452/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000453void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel532a9a82010-05-07 23:33:41 +0000454 // Verify variable.
455 if (!V->Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000456 return;
457
458 unsigned Line = V->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000459 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
460 V->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000461 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000464}
465
Devang Patelc50078e2009-11-21 02:48:08 +0000466/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000467/// entry.
Devang Patel4942c4b2010-05-07 23:19:07 +0000468void DwarfDebug::addSourceLine(DIE *Die, const DIGlobalVariable *G) {
Devang Patel532a9a82010-05-07 23:33:41 +0000469 // Verify global variable.
470 if (!G->Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000471 return;
472
473 unsigned Line = G->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000474 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
475 G->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000476 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000479}
Devang Patel318d70d2009-08-31 22:47:13 +0000480
Devang Patelc50078e2009-11-21 02:48:08 +0000481/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000482/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000483void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel532a9a82010-05-07 23:33:41 +0000484 // Verify subprogram.
485 if (!SP->Verify())
Devang Patel318d70d2009-08-31 22:47:13 +0000486 return;
Caroline Tice9da96d82009-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 Patel318d70d2009-08-31 22:47:13 +0000491 unsigned Line = SP->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000492 if (!SP->getContext().Verify())
493 return;
Devang Patele485a5d2010-03-24 21:30:35 +0000494 unsigned FileID = GetOrCreateSourceID(SP->getDirectory(),
495 SP->getFilename());
Devang Patel318d70d2009-08-31 22:47:13 +0000496 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-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 Patel318d70d2009-08-31 22:47:13 +0000499}
500
Devang Patelc50078e2009-11-21 02:48:08 +0000501/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000502/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000503void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel532a9a82010-05-07 23:33:41 +0000504 // Verify type.
505 if (!Ty->Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000506 return;
507
508 unsigned Line = Ty->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000509 if (!Ty->getContext().Verify())
510 return;
511 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
512 Ty->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000513 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000516}
517
Devang Patel8287d662009-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 Patel532a9a82010-05-07 23:33:41 +0000521 // Verify namespace.
522 if (!NS->Verify())
Devang Patel8287d662009-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 Tice248d5572009-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 Patelc50078e2009-11-21 02:48:08 +0000558 value of the variable. The function addBlockByrefType does this. */
Caroline Tice248d5572009-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 Patelc50078e2009-11-21 02:48:08 +0000563DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Tice248d5572009-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 Patel19302aa2010-05-07 18:11:54 +0000569 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Tice248d5572009-08-31 21:19:37 +0000570 subType = DTy.getTypeDerivedFrom();
571 }
572
Devang Patel19302aa2010-05-07 18:11:54 +0000573 DICompositeType blockStruct = DICompositeType(subType);
Caroline Tice248d5572009-08-31 21:19:37 +0000574 DIArray Elements = blockStruct.getTypeArray();
575
Caroline Tice248d5572009-08-31 21:19:37 +0000576 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
577 DIDescriptor Element = Elements.getElement(i);
Devang Patel19302aa2010-05-07 18:11:54 +0000578 DIDerivedType DT = DIDerivedType(Element);
Devang Patel7f75bbe2009-11-25 17:36:49 +0000579 if (Name == DT.getName())
Caroline Tice248d5572009-08-31 21:19:37 +0000580 return (DT.getTypeDerivedFrom());
581 }
582
583 return Ty;
584}
585
Devang Patelc50078e2009-11-21 02:48:08 +0000586/// addComplexAddress - Start with the address based on the location provided,
Mike Stumpb22cd0f2009-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 Patelc50078e2009-11-21 02:48:08 +0000591void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stumpb22cd0f2009-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 Lattnerbc0027b2010-04-05 00:13:49 +0000599 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000600 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000601 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000602
603 if (Location.isReg()) {
604 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000605 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000606 } else {
607 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-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 Stumpb22cd0f2009-09-30 00:08:22 +0000610 }
611 } else {
612 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000613 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000614 else {
Devang Patelc50078e2009-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 Stumpb22cd0f2009-09-30 00:08:22 +0000617 }
618
Devang Patelc50078e2009-11-21 02:48:08 +0000619 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stumpb22cd0f2009-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 Patelc50078e2009-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 Stumpb22cd0f2009-09-30 00:08:22 +0000628 } else if (Element == DIFactory::OpDeref) {
Devang Patelc50078e2009-11-21 02:48:08 +0000629 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000630 } else llvm_unreachable("unknown DIFactory Opcode");
631 }
632
633 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000634 addBlock(Die, Attribute, 0, Block);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000635}
636
Caroline Tice248d5572009-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 Patelc50078e2009-11-21 02:48:08 +0000646 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Tice248d5572009-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 Dunbar41716322009-09-19 20:40:05 +0000651 expression that explains to the debugger how to navigate through the
Caroline Tice248d5572009-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 Gohmandf1a7ff2010-02-10 16:03:48 +0000674 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Tice248d5572009-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 Patelc50078e2009-11-21 02:48:08 +0000691/// addBlockByrefAddress - Start with the address based on the location
Caroline Tice248d5572009-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 Patelc50078e2009-11-21 02:48:08 +0000697void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000698 unsigned Attribute,
699 const MachineLocation &Location) {
Caroline Tice248d5572009-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 Patel7f75bbe2009-11-25 17:36:49 +0000706 StringRef varName = VD.getName();
Caroline Tice248d5572009-08-31 21:19:37 +0000707
708 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patel19302aa2010-05-07 18:11:54 +0000709 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Tice248d5572009-08-31 21:19:37 +0000710 TmpTy = DTy.getTypeDerivedFrom();
711 isPointer = true;
712 }
713
Devang Patel19302aa2010-05-07 18:11:54 +0000714 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Tice248d5572009-08-31 21:19:37 +0000715
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000716 // Find the __forwarding field and the variable field in the __Block_byref
717 // struct.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000718 DIArray Fields = blockStruct.getTypeArray();
719 DIDescriptor varField = DIDescriptor();
720 DIDescriptor forwardingField = DIDescriptor();
Caroline Tice248d5572009-08-31 21:19:37 +0000721
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000722 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
723 DIDescriptor Element = Fields.getElement(i);
Devang Patel19302aa2010-05-07 18:11:54 +0000724 DIDerivedType DT = DIDerivedType(Element);
Devang Patel7f75bbe2009-11-25 17:36:49 +0000725 StringRef fieldName = DT.getName();
726 if (fieldName == "__forwarding")
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000727 forwardingField = Element;
Devang Patel7f75bbe2009-11-25 17:36:49 +0000728 else if (fieldName == varName)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000729 varField = Element;
730 }
Daniel Dunbar41716322009-09-19 20:40:05 +0000731
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000732 // Get the offsets for the forwarding field and the variable field.
Chris Lattner5df82552010-03-31 06:06:37 +0000733 unsigned forwardingFieldOffset =
Devang Patel19302aa2010-05-07 18:11:54 +0000734 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner5df82552010-03-31 06:06:37 +0000735 unsigned varFieldOffset =
Devang Patel19302aa2010-05-07 18:11:54 +0000736 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Tice248d5572009-08-31 21:19:37 +0000737
Mike Stump2fd84e22009-09-24 23:21:26 +0000738 // Decode the original location, and use that as the start of the byref
739 // variable's location.
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000740 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000741 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000742 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Tice248d5572009-08-31 21:19:37 +0000743
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000744 if (Location.isReg()) {
745 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000746 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000747 else {
748 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-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 Dunbar19f1d442009-09-19 20:40:14 +0000751 }
752 } else {
753 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000754 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000755 else {
Devang Patelc50078e2009-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 Dunbar19f1d442009-09-19 20:40:14 +0000758 }
Caroline Tice248d5572009-08-31 21:19:37 +0000759
Devang Patelc50078e2009-11-21 02:48:08 +0000760 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000761 }
Caroline Tice248d5572009-08-31 21:19:37 +0000762
Mike Stump2fd84e22009-09-24 23:21:26 +0000763 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000764 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000765 if (isPointer)
Devang Patelc50078e2009-11-21 02:48:08 +0000766 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000767
Daniel Dunbar19f1d442009-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 Dunbar19f1d442009-09-19 20:40:14 +0000771 if (forwardingFieldOffset > 0) {
Devang Patelc50078e2009-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 Dunbar19f1d442009-09-19 20:40:14 +0000774 }
Caroline Tice248d5572009-08-31 21:19:37 +0000775
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000776 // Now dereference the __forwarding field to get to the real __Block_byref
777 // struct: DW_OP_deref.
Devang Patelc50078e2009-11-21 02:48:08 +0000778 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000779
Daniel Dunbar19f1d442009-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 Dunbar19f1d442009-09-19 20:40:14 +0000783 if (varFieldOffset > 0) {
Devang Patelc50078e2009-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 Dunbar19f1d442009-09-19 20:40:14 +0000786 }
Caroline Tice248d5572009-08-31 21:19:37 +0000787
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000788 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000789 addBlock(Die, Attribute, 0, Block);
Caroline Tice248d5572009-08-31 21:19:37 +0000790}
791
Devang Patelc50078e2009-11-21 02:48:08 +0000792/// addAddress - Add an address attribute to a die based on the location
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000793/// provided.
Devang Patelc50078e2009-11-21 02:48:08 +0000794void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000795 const MachineLocation &Location) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000796 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000797 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000798 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000799
800 if (Location.isReg()) {
801 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000802 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000803 } else {
Devang Patelc50078e2009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000806 }
807 } else {
808 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000809 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000810 } else {
Devang Patelc50078e2009-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 Wendlingb12b3d72009-05-15 09:23:25 +0000813 }
814
Devang Patelc50078e2009-11-21 02:48:08 +0000815 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000816 }
817
Devang Patelc50078e2009-11-21 02:48:08 +0000818 addBlock(Die, Attribute, 0, Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000819}
820
Devang Patel7e625392010-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 Patel1a8f9a82009-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 Patel9e592492010-03-08 20:52:55 +0000881 if (Context.isType()) {
Devang Patel19302aa2010-05-07 18:11:54 +0000882 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patel1a8f9a82009-12-10 19:14:49 +0000883 ContextDIE->addChild(Die);
Devang Patel8287d662009-12-15 19:16:48 +0000884 } else if (Context.isNameSpace()) {
Devang Patel19302aa2010-05-07 18:11:54 +0000885 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel8287d662009-12-15 19:16:48 +0000886 ContextDIE->addChild(Die);
Devang Patel7e5a86e2010-05-10 22:49:55 +0000887 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patel1a8f9a82009-12-10 19:14:49 +0000888 ContextDIE->addChild(Die);
889 else
Devang Patel7e5a86e2010-05-10 22:49:55 +0000890 getCompileUnit(Context)->addDie(Die);
Devang Patel1a8f9a82009-12-10 19:14:49 +0000891}
892
Devang Patel7f139c12009-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 Patel7e5a86e2010-05-10 22:49:55 +0000896 CompileUnit *TypeCU = getCompileUnit(Ty);
897 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patel7f139c12009-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 Patel7e5a86e2010-05-10 22:49:55 +0000903 TypeCU->insertDIE(Ty, TyDIE);
Devang Patel7f139c12009-12-10 18:05:33 +0000904 if (Ty.isBasicType())
Devang Patel19302aa2010-05-07 18:11:54 +0000905 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patel7f139c12009-12-10 18:05:33 +0000906 else if (Ty.isCompositeType())
Devang Patel19302aa2010-05-07 18:11:54 +0000907 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patel7f139c12009-12-10 18:05:33 +0000908 else {
909 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel19302aa2010-05-07 18:11:54 +0000910 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patel7f139c12009-12-10 18:05:33 +0000911 }
912
Devang Patel1a8f9a82009-12-10 19:14:49 +0000913 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel7f139c12009-12-10 18:05:33 +0000914 return TyDIE;
915}
916
Devang Patelc50078e2009-11-21 02:48:08 +0000917/// addType - Add a new type attribute to the specified entity.
Devang Patelfe0be132009-12-09 18:24:21 +0000918void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9738af92010-05-07 21:45:47 +0000919 if (!Ty.Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000920 return;
921
922 // Check for pre-existence.
Devang Patel7e5a86e2010-05-10 22:49:55 +0000923 CompileUnit *TypeCU = getCompileUnit(Ty);
924 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000925 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000926 if (Entry) {
927 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000928 return;
929 }
930
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000931 // Construct type.
Devang Patel7f139c12009-12-10 18:05:33 +0000932 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000933
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000934 // Set up proxy.
935 Entry = createDIEEntry(Buffer);
Devang Patel7e5a86e2010-05-10 22:49:55 +0000936 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000937
Devang Patelc50078e2009-11-21 02:48:08 +0000938 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000939}
940
Devang Patelc50078e2009-11-21 02:48:08 +0000941/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patelfe0be132009-12-09 18:24:21 +0000942void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000943 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000944 StringRef Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000945 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000946 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000947 BTy.getEncoding());
948
949 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000950 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000951 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000952 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000953 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000954}
955
Devang Patelc50078e2009-11-21 02:48:08 +0000956/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patelfe0be132009-12-09 18:24:21 +0000957void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000958 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000959 StringRef Name = DTy.getName();
Bill Wendlingb12b3d72009-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 Patelfe0be132009-12-09 18:24:21 +0000970 addType(&Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000971
972 // Add name if not anonymous or intermediate type.
Devang Patel76b80672009-11-30 23:56:56 +0000973 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000974 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000975
976 // Add size if non-zero (derived types might be zero-sized.)
977 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000978 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000979
980 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb125c6e2009-11-23 18:43:37 +0000981 if (!DTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000982 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000983}
984
Devang Patelc50078e2009-11-21 02:48:08 +0000985/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +0000986void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000987 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000988 StringRef Name = CTy.getName();
Bill Wendlingb12b3d72009-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 Patelfe0be132009-12-09 18:24:21 +0000997 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendlingb12b3d72009-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 Patel19302aa2010-05-07 18:11:54 +00001005 DIDescriptor Enum(Elements.getElement(i));
Devang Patel9e592492010-03-08 20:52:55 +00001006 if (Enum.isEnumerator()) {
Devang Patel19302aa2010-05-07 18:11:54 +00001007 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patelc50078e2009-11-21 02:48:08 +00001008 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +00001009 }
Bill Wendlingb12b3d72009-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 Patel19302aa2010-05-07 18:11:54 +00001017 addType(&Buffer, DIType(RTy));
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001018
1019 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001020 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-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 Patel19302aa2010-05-07 18:11:54 +00001026 addType(Arg, DIType(Ty));
Devang Patelc50078e2009-11-21 02:48:08 +00001027 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-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 Patel9e592492010-03-08 20:52:55 +00001038 unsigned N = Elements.getNumElements();
1039 if (N == 0)
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001040 break;
1041
1042 // Add elements to structure type.
Devang Patel9e592492010-03-08 20:52:55 +00001043 for (unsigned i = 0; i < N; ++i) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001044 DIDescriptor Element = Elements.getElement(i);
1045 DIE *ElemDie = NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001046 if (Element.isSubprogram())
Devang Patel19302aa2010-05-07 18:11:54 +00001047 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patel9e592492010-03-08 20:52:55 +00001048 else if (Element.isVariable()) {
Devang Patel19302aa2010-05-07 18:11:54 +00001049 DIVariable DV(Element);
Devang Patel423dfa02010-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 Patel9e592492010-03-08 20:52:55 +00001057 } else if (Element.isDerivedType())
Devang Patel19302aa2010-05-07 18:11:54 +00001058 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel9e592492010-03-08 20:52:55 +00001059 else
1060 continue;
Devang Patelc50078e2009-11-21 02:48:08 +00001061 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001062 }
1063
Devang Patel20b32102009-08-27 23:51:51 +00001064 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +00001065 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001066
1067 unsigned RLang = CTy.getRunTimeLang();
1068 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +00001069 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001070 dwarf::DW_FORM_data1, RLang);
Devang Patel5ae52402010-01-26 21:16:06 +00001071
1072 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel19302aa2010-05-07 18:11:54 +00001073 if (DIDescriptor(ContainingType).isCompositeType())
Devang Patel5ae52402010-01-26 21:16:06 +00001074 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patel19302aa2010-05-07 18:11:54 +00001075 getOrCreateTypeDIE(DIType(ContainingType)));
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001076 break;
1077 }
1078 default:
1079 break;
1080 }
1081
1082 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +00001083 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001084 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001085
Devang Pateldc73c372010-01-29 18:34:58 +00001086 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendlingb12b3d72009-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 Patelc50078e2009-11-21 02:48:08 +00001090 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001091 else {
1092 // Add zero size if it is not a forward declaration.
1093 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +00001094 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001095 else
Devang Patelc50078e2009-11-21 02:48:08 +00001096 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001097 }
1098
1099 // Add source line info if available.
1100 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +00001101 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001102 }
1103}
1104
Devang Patelc50078e2009-11-21 02:48:08 +00001105/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1106void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-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 Patelc50078e2009-11-21 02:48:08 +00001111 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +00001112 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +00001113 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld3df6972009-12-04 23:10:24 +00001114 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001115
Devang Patelc50078e2009-11-21 02:48:08 +00001116 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001117}
1118
Devang Patelc50078e2009-11-21 02:48:08 +00001119/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +00001120void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendlingb12b3d72009-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 Patelc50078e2009-11-21 02:48:08 +00001124 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001125
1126 // Emit derived type.
Devang Patelfe0be132009-12-09 18:24:21 +00001127 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001128 DIArray Elements = CTy->getTypeArray();
1129
Devang Patel1233f912009-11-21 00:31:03 +00001130 // Get an anonymous type for index type.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001131 CompileUnit *TheCU = getCompileUnit(*CTy);
1132 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel1233f912009-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 Patelc50078e2009-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 Patel1233f912009-11-21 00:31:03 +00001138 dwarf::DW_ATE_signed);
Devang Patel7e5a86e2010-05-10 22:49:55 +00001139 TheCU->addDie(IdxTy);
1140 TheCU->setIndexTyDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +00001141 }
Bill Wendlingb12b3d72009-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 Patel19302aa2010-05-07 18:11:54 +00001147 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001148 }
1149}
1150
Devang Patelc50078e2009-11-21 02:48:08 +00001151/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel9e592492010-03-08 20:52:55 +00001152DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001153 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel9e592492010-03-08 20:52:55 +00001154 StringRef Name = ETy.getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001155 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel9e592492010-03-08 20:52:55 +00001156 int64_t Value = ETy.getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +00001157 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001158 return Enumerator;
1159}
1160
Devang Patel141e1c42010-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 Patelc50078e2009-11-21 02:48:08 +00001171/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patelfe0be132009-12-09 18:24:21 +00001172DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001173 // If the global variable was optmized out then no need to create debug info
1174 // entry.
Devang Patel83e42c72009-11-06 17:58:12 +00001175 if (!GV.getGlobal()) return NULL;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001176 if (GV.getDisplayName().empty()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +00001177
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001178 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach652b7432009-11-21 23:12:12 +00001179 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001180 GV.getDisplayName());
1181
Devang Patel7f75bbe2009-11-25 17:36:49 +00001182 StringRef LinkageName = GV.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001183 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001184 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001185 getRealLinkageName(LinkageName));
1186
Devang Patelfe0be132009-12-09 18:24:21 +00001187 addType(GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001188 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001189 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1190 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001191
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001192 return GVDie;
1193}
1194
Devang Patelc50078e2009-11-21 02:48:08 +00001195/// createMemberDIE - Create new member DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001196DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001197 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel7f75bbe2009-11-25 17:36:49 +00001198 StringRef Name = DT.getName();
1199 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001200 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001201
Devang Patelfe0be132009-12-09 18:24:21 +00001202 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001203
Devang Patelc50078e2009-11-21 02:48:08 +00001204 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001205
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001206 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001207 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001208
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001209 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001210 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001211
1212 if (Size != FieldSize) {
1213 // Handle bitfield.
Devang Patelc50078e2009-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 Wendlingb12b3d72009-05-15 09:23:25 +00001216
1217 uint64_t Offset = DT.getOffsetInBits();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001218 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1219 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer631e3e32010-01-07 17:50:57 +00001220 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001221 Offset -= FieldOffset;
1222
1223 // Maybe we need to work from the other end.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001224 if (Asm->getTargetData().isLittleEndian())
1225 Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001226 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001227
Devang Patel7d9fe582009-11-04 22:06:12 +00001228 // Here WD_AT_data_member_location points to the anonymous
1229 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001230 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001231
1232 } else
1233 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001234 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001235
Devang Patel84aba942010-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 Kramer138c7ff2010-03-31 19:34:01 +00001243 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel84aba942010-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 Wendlingb12b3d72009-05-15 09:23:25 +00001256
1257 if (DT.isProtected())
Devang Patel188c85d2009-12-03 19:11:07 +00001258 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001259 dwarf::DW_ACCESS_protected);
1260 else if (DT.isPrivate())
Devang Patel188c85d2009-12-03 19:11:07 +00001261 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001262 dwarf::DW_ACCESS_private);
Devang Patel188c85d2009-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 Wendlingb12b3d72009-05-15 09:23:25 +00001269 return MemberDie;
1270}
1271
Devang Patel814a12c2009-12-14 16:18:45 +00001272/// createSubprogramDIE - Create new DIE using SP.
1273DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
Devang Patel7e5a86e2010-05-10 22:49:55 +00001274 CompileUnit *SPCU = getCompileUnit(SP);
1275 DIE *SPDie = SPCU->getDIE(SP);
Devang Patel814a12c2009-12-14 16:18:45 +00001276 if (SPDie)
1277 return SPDie;
1278
1279 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel36cd9f82010-03-02 17:58:15 +00001280 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel0ab194f2010-03-02 01:26:20 +00001281 if (!SP.getName().empty())
1282 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001283
Devang Patel7f75bbe2009-11-25 17:36:49 +00001284 StringRef LinkageName = SP.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001285 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001286 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001287 getRealLinkageName(LinkageName));
1288
Devang Patelc50078e2009-11-21 02:48:08 +00001289 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001290
Bill Wendlingb12b3d72009-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 Patelc50078e2009-11-21 02:48:08 +00001295 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001296
1297 // Add Return Type.
Devang Patelc1df8792009-12-03 01:25:38 +00001298 DICompositeType SPTy = SP.getType();
1299 DIArray Args = SPTy.getTypeArray();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001300 unsigned SPTag = SPTy.getTag();
Devang Patel188c85d2009-12-03 19:11:07 +00001301
Devang Patel9e592492010-03-08 20:52:55 +00001302 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelfe0be132009-12-09 18:24:21 +00001303 addType(SPDie, SPTy);
Devang Patelc1df8792009-12-03 01:25:38 +00001304 else
Devang Patel19302aa2010-05-07 18:11:54 +00001305 addType(SPDie, DIType(Args.getElement(0)));
Devang Patelc1df8792009-12-03 01:25:38 +00001306
Devang Patel188c85d2009-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 Kramer138c7ff2010-03-31 19:34:01 +00001310 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel188c85d2009-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 Patel7d707f92010-01-19 06:19:05 +00001314 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patel19302aa2010-05-07 18:11:54 +00001315 SP.getContainingType()));
Devang Patel188c85d2009-12-03 19:11:07 +00001316 }
1317
Devang Patel814a12c2009-12-14 16:18:45 +00001318 if (MakeDecl || !SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001319 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001320
1321 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patelc1df8792009-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 Wendlingb12b3d72009-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 Patel19302aa2010-05-07 18:11:54 +00001330 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patelc5dc7252010-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 Patelc50078e2009-11-21 02:48:08 +00001334 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001335 }
1336 }
1337
Devang Patelbf7d00a2010-02-03 19:57:19 +00001338 if (SP.isArtificial())
1339 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1340
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001341 if (!SP.isLocalToUnit())
1342 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel91474172010-04-19 19:14:02 +00001343
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001344 if (SP.isOptimized())
1345 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1346
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001347 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001348 SPCU->insertDIE(SP, SPDie);
Devang Patel9e3b7ae2010-04-30 19:38:23 +00001349
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001350 return SPDie;
1351}
1352
Devang Patel9c371c62010-05-07 20:54:48 +00001353DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattner8143ce82010-03-31 05:36:29 +00001354 assert(N && "Invalid Scope encoding!");
Devang Patel90a0fe32009-11-10 23:06:00 +00001355
1356 DbgScope *AScope = AbstractScopes.lookup(N);
1357 if (AScope)
1358 return AScope;
Jim Grosbach652b7432009-11-21 23:12:12 +00001359
Devang Patel90a0fe32009-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 Patel19302aa2010-05-07 18:11:54 +00001366 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patel90a0fe32009-11-10 23:06:00 +00001367 }
1368
1369 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1370
1371 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001372 Parent->addScope(AScope);
Devang Patel90a0fe32009-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 Patel6a260102009-10-01 20:31:14 +00001379
Devang Patel75c10622010-04-06 23:53:48 +00001380/// isSubprogramContext - Return true if Context is either a subprogram
1381/// or another context nested inside a subprogram.
Devang Patel9c371c62010-05-07 20:54:48 +00001382static bool isSubprogramContext(const MDNode *Context) {
Devang Patel75c10622010-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 Patel19302aa2010-05-07 18:11:54 +00001389 return isSubprogramContext(DIType(Context).getContext());
Devang Patel75c10622010-04-06 23:53:48 +00001390 return false;
1391}
1392
Jim Grosbach652b7432009-11-21 23:12:12 +00001393/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patelc50078e2009-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 Patel9c371c62010-05-07 20:54:48 +00001397DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel7e5a86e2010-05-10 22:49:55 +00001398 CompileUnit *SPCU = getCompileUnit(SPNode);
1399 DIE *SPDie = SPCU->getDIE(SPNode);
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001400 assert(SPDie && "Unable to find subprogram DIE!");
1401 DISubprogram SP(SPNode);
Chris Lattnerdd21da82010-03-13 07:26:18 +00001402
Chris Lattnerbc0027b2010-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 Patel75c10622010-04-06 23:53:48 +00001409 !SP.getContext().isFile() &&
Devang Patel19302aa2010-05-07 18:11:54 +00001410 !isSubprogramContext(SP.getContext())) {
Chris Lattnerbc0027b2010-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 Patel19302aa2010-05-07 18:11:54 +00001420 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattnerbc0027b2010-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 Patel7e5a86e2010-05-10 22:49:55 +00001430 SPCU->addDie(SPDie);
Chris Lattnerbc0027b2010-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 Patelc5dc7252010-02-06 01:02:37 +00001440
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001441 return SPDie;
Devang Patel90a0fe32009-11-10 23:06:00 +00001442}
1443
Jim Grosbach652b7432009-11-21 23:12:12 +00001444/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-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 Patelce7bf012010-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 Patel9a9a4ac2010-03-29 22:59:58 +00001476 if (Start == 0 || End == 0) return 0;
Devang Patel90a0fe32009-11-10 23:06:00 +00001477
Chris Lattner855e8f52010-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 Lattner5e423432010-03-09 01:51:43 +00001480
Devang Patelce7bf012010-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 Patel90a0fe32009-11-10 23:06:00 +00001483
1484 return ScopeDIE;
1485}
1486
Devang Patelc50078e2009-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 Patelce7bf012010-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 Lattner855e8f52010-03-09 01:58:53 +00001508 assert(StartLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001509 "Invalid starting label for an inlined scope!");
Chris Lattner855e8f52010-03-09 01:58:53 +00001510 assert(EndLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001511 "Invalid end label for an inlined scope!");
Devang Patelce7bf012010-04-27 19:46:33 +00001512
Devang Patel9e592492010-03-08 20:52:55 +00001513 if (!Scope->getScopeNode())
Devang Patelbe149872010-03-08 19:20:38 +00001514 return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001515 DIScope DS(Scope->getScopeNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001516 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1517
Devang Patel19302aa2010-05-07 18:11:54 +00001518 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel7e5a86e2010-05-10 22:49:55 +00001519 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1520 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattner8143ce82010-03-31 05:36:29 +00001521 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001522 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001523 dwarf::DW_FORM_ref4, OriginDIE);
1524
Chris Lattneread58652010-03-09 00:31:02 +00001525 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattner855e8f52010-03-09 01:58:53 +00001526 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel90a0fe32009-11-10 23:06:00 +00001527
1528 InlinedSubprogramDIEs.insert(OriginDIE);
1529
1530 // Track the start label for this inlined function.
Devang Patel9c371c62010-05-07 20:54:48 +00001531 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel19302aa2010-05-07 18:11:54 +00001532 I = InlineInfo.find(InlinedSP);
Devang Patel90a0fe32009-11-10 23:06:00 +00001533
1534 if (I == InlineInfo.end()) {
Devang Patel19302aa2010-05-07 18:11:54 +00001535 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbachb23f2422009-11-22 19:20:36 +00001536 ScopeDIE));
Devang Patel19302aa2010-05-07 18:11:54 +00001537 InlinedSPNodes.push_back(InlinedSP);
Devang Patel90a0fe32009-11-10 23:06:00 +00001538 } else
Chris Lattneread58652010-03-09 00:31:02 +00001539 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001540
Devang Patel90a0fe32009-11-10 23:06:00 +00001541 DILocation DL(Scope->getInlinedAt());
Devang Patel7e5a86e2010-05-10 22:49:55 +00001542 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patelc50078e2009-11-21 02:48:08 +00001543 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001544
1545 return ScopeDIE;
1546}
1547
Devang Patelc50078e2009-11-21 02:48:08 +00001548
1549/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelfe0be132009-12-09 18:24:21 +00001550DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001551 // Get the descriptor.
1552 const DIVariable &VD = DV->getVariable();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001553 StringRef Name = VD.getName();
1554 if (Name.empty())
Devang Patelab9a0682009-11-13 02:25:26 +00001555 return NULL;
Devang Patel90a0fe32009-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 Grosbach652b7432009-11-21 23:12:12 +00001579
Devang Patel90a0fe32009-11-10 23:06:00 +00001580 if (AbsDIE) {
1581 DIScope DS(Scope->getScopeNode());
Devang Patel19302aa2010-05-07 18:11:54 +00001582 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel7e5a86e2010-05-10 22:49:55 +00001583 DIE *OriginSPDIE = getCompileUnit(InlinedSP)->getDIE(InlinedSP);
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001584 (void) OriginSPDIE;
Chris Lattner8143ce82010-03-31 05:36:29 +00001585 assert(OriginSPDIE && "Unable to find Origin DIE for the SP!");
Devang Patel90a0fe32009-11-10 23:06:00 +00001586 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
Chris Lattner8143ce82010-03-31 05:36:29 +00001587 assert(AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001588 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001589 dwarf::DW_FORM_ref4, AbsDIE);
1590 }
1591 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001592 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1593 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001594
1595 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001596 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001597 // addresses instead.
1598 if (VD.isBlockByrefVariable())
Devang Patelfe0be132009-12-09 18:24:21 +00001599 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001600 else
Devang Patelfe0be132009-12-09 18:24:21 +00001601 addType(VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001602 }
1603
1604 // Add variable address.
1605 if (!Scope->isAbstractScope()) {
Devang Patel14da02f2010-03-15 18:33:46 +00001606 // Check if variable is described by DBG_VALUE instruction.
Devang Patel7e625392010-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 Patel7c3efb12010-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 Patel7e625392010-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 Patel14da02f2010-03-15 18:33:46 +00001635 MachineLocation Location;
1636 unsigned FrameReg;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001637 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Devang Patelfad01682010-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 Patel14da02f2010-03-15 18:33:46 +00001650 }
Devang Patel90a0fe32009-11-10 23:06:00 +00001651 }
Devang Patelc5dc7252010-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 Patel90a0fe32009-11-10 23:06:00 +00001655 DV->setDIE(VariableDie);
1656 return VariableDie;
1657
1658}
Devang Patelc50078e2009-11-21 02:48:08 +00001659
Devang Patelec13b4f2009-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 Patelec13b4f2009-11-24 01:14:22 +00001667 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patel19302aa2010-05-07 18:11:54 +00001668 DIType ATy(Args.getElement(i));
Devang Patel9738af92010-05-07 21:45:47 +00001669 if (!ATy.Verify())
Devang Patelec13b4f2009-11-24 01:14:22 +00001670 continue;
1671 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel19302aa2010-05-07 18:11:54 +00001672 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel21171c22010-04-13 20:35:04 +00001673 && !CATy.isForwardDecl()) {
Devang Patel7e5a86e2010-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 Patelec13b4f2009-11-24 01:14:22 +00001677 }
1678 }
1679}
1680
Devang Patelc50078e2009-11-21 02:48:08 +00001681/// constructScopeDIE - Construct a DIE for this scope.
1682DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel9e592492010-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 Patel7e5a86e2010-05-10 22:49:55 +00001692 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patel9e592492010-03-08 20:52:55 +00001693 else
Devang Patel19302aa2010-05-07 18:11:54 +00001694 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel9e592492010-03-08 20:52:55 +00001695 }
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001696 else
Devang Patel9e592492010-03-08 20:52:55 +00001697 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001698 if (!ScopeDIE) return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001699
Devang Patel90a0fe32009-11-10 23:06:00 +00001700 // Add variables to scope.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001701 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel90a0fe32009-11-10 23:06:00 +00001702 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelfe0be132009-12-09 18:24:21 +00001703 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach652b7432009-11-21 23:12:12 +00001704 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001705 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001706 }
1707
1708 // Add nested scopes.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001709 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel90a0fe32009-11-10 23:06:00 +00001710 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1711 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001712 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001713 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001714 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001715 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001716
1717 if (DS.isSubprogram())
Devang Patel19302aa2010-05-07 18:11:54 +00001718 addPubTypes(DISubprogram(DS));
Devang Patelec13b4f2009-11-24 01:14:22 +00001719
1720 return ScopeDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001721}
1722
Bill Wendlingf5839192009-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 Lattner5df82552010-03-31 06:06:37 +00001727unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf5839192009-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 Patel8287d662009-12-15 19:16:48 +00001760/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1761DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel7e5a86e2010-05-10 22:49:55 +00001762 CompileUnit *TheCU = getCompileUnit(NS);
1763 DIE *NDie = TheCU->getDIE(NS);
Devang Patel8287d662009-12-15 19:16:48 +00001764 if (NDie)
1765 return NDie;
1766 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel7e5a86e2010-05-10 22:49:55 +00001767 TheCU->insertDIE(NS, NDie);
Devang Patel8287d662009-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 Patel7e5a86e2010-05-10 22:49:55 +00001775/// constructCompileUnit - Create new CompileUnit for the given
1776/// metadata node with tag DW_TAG_compile_unit.
Devang Patel9c371c62010-05-07 20:54:48 +00001777void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001778 DICompileUnit DIUnit(N);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001779 StringRef FN = DIUnit.getFilename();
1780 StringRef Dir = DIUnit.getDirectory();
Devang Patelaaf012e2009-09-29 18:40:58 +00001781 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001782
1783 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelc50078e2009-11-21 02:48:08 +00001784 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001785 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001786 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001787 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001788 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patel325691a2010-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 Patel7ac39832010-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 Wendlingf5839192009-05-20 23:19:06 +00001796
Devang Patel7f75bbe2009-11-25 17:36:49 +00001797 if (!Dir.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001798 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001799 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001800 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001801
Devang Patel7f75bbe2009-11-25 17:36:49 +00001802 StringRef Flags = DIUnit.getFlags();
1803 if (!Flags.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001804 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001805
1806 unsigned RVer = DIUnit.getRunTimeVersion();
1807 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001808 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001809 dwarf::DW_FORM_data1, RVer);
1810
Devang Patel7e5a86e2010-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 Wendlingf5839192009-05-20 23:19:06 +00001815}
1816
Devang Patel7e5a86e2010-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 Patel9c371c62010-05-07 20:54:48 +00001848void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001849 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001850
Devang Patel0c03f062009-09-04 23:59:07 +00001851 // If debug information is malformed then ignore it.
1852 if (DI_GV.Verify() == false)
1853 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001854
1855 // Check for pre-existence.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001856 CompileUnit *TheCU = getCompileUnit(N);
1857 if (TheCU->getDIE(DI_GV))
Devang Patel166f8432009-06-26 01:49:18 +00001858 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001859
Devang Patelfe0be132009-12-09 18:24:21 +00001860 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel6d479632009-12-10 23:25:41 +00001861 if (!VariableDie)
1862 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001863
Bill Wendlingf5839192009-05-20 23:19:06 +00001864 // Add to map.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001865 TheCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001866
1867 // Add to context owner.
Devang Pateldcc0dce2010-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 Lattner5df82552010-03-31 06:06:37 +00001871 if (DI_GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel75c10622010-04-06 23:53:48 +00001872 !GVContext.isFile() &&
Devang Patel19302aa2010-05-07 18:11:54 +00001873 !isSubprogramContext(GVContext)) {
Devang Patel8287d662009-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 Kramer138c7ff2010-03-31 19:34:01 +00001878 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel8287d662009-12-15 19:16:48 +00001879 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001880 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner90825792010-03-12 21:09:07 +00001881 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001882 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patela0a98582010-02-09 01:58:33 +00001883 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel7e5a86e2010-05-10 22:49:55 +00001884 TheCU->addDie(VariableSpecDIE);
Devang Patel8287d662009-12-15 19:16:48 +00001885 } else {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001886 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel8287d662009-12-15 19:16:48 +00001887 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001888 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner90825792010-03-12 21:09:07 +00001889 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001890 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1891 }
Devang Pateldcc0dce2010-01-15 01:12:22 +00001892 addToContextOwner(VariableDie, GVContext);
Devang Patel1a8f9a82009-12-10 19:14:49 +00001893
Bill Wendlingf5839192009-05-20 23:19:06 +00001894 // Expose as global. FIXME - need to check external flag.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001895 TheCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001896
1897 DIType GTy = DI_GV.getType();
Devang Patel21171c22010-04-13 20:35:04 +00001898 if (GTy.isCompositeType() && !GTy.getName().empty()
1899 && !GTy.isForwardDecl()) {
Devang Patel7e5a86e2010-05-10 22:49:55 +00001900 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattner8143ce82010-03-31 05:36:29 +00001901 assert(Entry && "Missing global type!");
Devang Patel7e5a86e2010-05-10 22:49:55 +00001902 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patelec13b4f2009-11-24 01:14:22 +00001903 }
Devang Patel166f8432009-06-26 01:49:18 +00001904 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001905}
1906
Devang Patel7e5a86e2010-05-10 22:49:55 +00001907/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel9c371c62010-05-07 20:54:48 +00001908void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001909 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001910
Stuart Hastings0f5779e2010-04-06 21:38:29 +00001911 // Check for pre-existence.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001912 CompileUnit *TheCU = getCompileUnit(N);
1913 if (TheCU->getDIE(N))
Stuart Hastings0f5779e2010-04-06 21:38:29 +00001914 return;
1915
Bill Wendlingf5839192009-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 Patel166f8432009-06-26 01:49:18 +00001919 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001920
Stuart Hastings0f5779e2010-04-06 21:38:29 +00001921 DIE *SubprogramDie = createSubprogramDIE(SP);
1922
1923 // Add to map.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001924 TheCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001925
1926 // Add to context owner.
Devang Patel8287d662009-12-15 19:16:48 +00001927 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patelde2d3682009-12-08 23:21:45 +00001928
Bill Wendlingf5839192009-05-20 23:19:06 +00001929 // Expose as global.
Devang Patel7e5a86e2010-05-10 22:49:55 +00001930 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001931
Devang Patel166f8432009-06-26 01:49:18 +00001932 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001933}
1934
Devang Patelc50078e2009-11-21 02:48:08 +00001935/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-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 Lattner75e113c2010-04-04 07:48:20 +00001938void DwarfDebug::beginModule(Module *M) {
Devang Patelce7bf012010-04-27 19:46:33 +00001939 if (DisableDebugInfoPrinting)
1940 return;
1941
Devang Patelfda766d2009-07-30 18:56:46 +00001942 DebugInfoFinder DbgFinder;
1943 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001944
Chris Lattnera52b6172010-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 Lattner706afc02010-04-04 23:17:54 +00001962 // Emit initial sections.
Chris Lattnera52b6172010-04-05 02:19:28 +00001963 EmitSectionLabels();
Chris Lattner706afc02010-04-04 23:17:54 +00001964
Bill Wendlingf5839192009-05-20 23:19:06 +00001965 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001966 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1967 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001968 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001969
Devang Patel90a0fe32009-11-10 23:06:00 +00001970 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001971 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1972 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001973 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001974
Devang Patel1a8f9a82009-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 Wendlingf5839192009-05-20 23:19:06 +00001980 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001981 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-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 Lattnerbc0027b2010-04-05 00:13:49 +00001985 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-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 Lattnerad653482010-01-22 22:09:00 +00001989 // FIXME: don't use sys::path for this! This should not depend on the
1990 // host.
Bill Wendlingf5839192009-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 Lattner59be4ea2010-01-25 18:58:59 +00001996 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001997 }
1998 }
Bill Wendlingf5839192009-05-20 23:19:06 +00001999}
2000
Devang Patelc50078e2009-11-21 02:48:08 +00002001/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00002002///
Devang Patelc50078e2009-11-21 02:48:08 +00002003void DwarfDebug::endModule() {
Devang Patel7e5a86e2010-05-10 22:49:55 +00002004 if (!FirstCU) return;
Bill Wendlingf5839192009-05-20 23:19:06 +00002005
Devang Patel90a0fe32009-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 Patelc50078e2009-11-21 02:48:08 +00002010 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00002011 }
2012
Devang Patel9c371c62010-05-07 20:54:48 +00002013 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel188c85d2009-12-03 19:11:07 +00002014 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2015 DIE *SPDie = CI->first;
Devang Patel9c371c62010-05-07 20:54:48 +00002016 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Patel188c85d2009-12-03 19:11:07 +00002017 if (!N) continue;
Devang Patel7e5a86e2010-05-10 22:49:55 +00002018 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Patel188c85d2009-12-03 19:11:07 +00002019 if (!NDie) continue;
2020 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel188c85d2009-12-03 19:11:07 +00002021 }
2022
Bill Wendlingf5839192009-05-20 23:19:06 +00002023 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00002024 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerb93558d2010-04-04 19:25:43 +00002025 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner73266f92009-08-19 05:49:37 +00002026 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerb93558d2010-04-04 19:25:43 +00002027 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf5839192009-05-20 23:19:06 +00002028
2029 // End text sections.
2030 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00002031 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002032 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf5839192009-05-20 23:19:06 +00002033 }
2034
2035 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00002036 emitCommonDebugFrame();
Bill Wendlingf5839192009-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 Patelc50078e2009-11-21 02:48:08 +00002041 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00002042
2043 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00002044 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00002045
2046 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00002047 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00002048
2049 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00002050 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00002051
2052 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00002053 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00002054
2055 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00002056 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00002057
Devang Patelec13b4f2009-11-24 01:14:22 +00002058 // Emit info into a debug pubtypes section.
2059 emitDebugPubTypes();
2060
Bill Wendlingf5839192009-05-20 23:19:06 +00002061 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00002062 emitDebugLoc();
Bill Wendlingf5839192009-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 Patelc50078e2009-11-21 02:48:08 +00002068 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00002069
2070 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00002071 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00002072
2073 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00002074 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00002075
Chris Lattner0f093f82010-03-13 02:17:42 +00002076 // Emit info into a debug str section.
2077 emitDebugStr();
2078
Devang Patel7e5a86e2010-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 Wendlingf5839192009-05-20 23:19:06 +00002083}
2084
Devang Patel90a0fe32009-11-10 23:06:00 +00002085/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00002086DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2087 unsigned FrameIdx,
Chris Lattnerb9692a72010-04-02 19:42:39 +00002088 DebugLoc ScopeLoc) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002089
Devang Patel19302aa2010-05-07 18:11:54 +00002090 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel90a0fe32009-11-10 23:06:00 +00002091 if (AbsDbgVariable)
2092 return AbsDbgVariable;
2093
Devang Patel19302aa2010-05-07 18:11:54 +00002094 LLVMContext &Ctx = Var->getContext();
Chris Lattnerb9692a72010-04-02 19:42:39 +00002095 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel90a0fe32009-11-10 23:06:00 +00002096 if (!Scope)
2097 return NULL;
2098
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002099 AbsDbgVariable = new DbgVariable(Var, FrameIdx,
2100 NULL /* No more-abstract variable*/);
Devang Patelc50078e2009-11-21 02:48:08 +00002101 Scope->addVariable(AbsDbgVariable);
Devang Patel19302aa2010-05-07 18:11:54 +00002102 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel90a0fe32009-11-10 23:06:00 +00002103 return AbsDbgVariable;
2104}
2105
Devang Patel14da02f2010-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 Lattnerb9692a72010-04-02 19:42:39 +00002110 DebugLoc ScopeLoc) {
Devang Patel14da02f2010-03-15 18:33:46 +00002111
Devang Patel19302aa2010-05-07 18:11:54 +00002112 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel14da02f2010-03-15 18:33:46 +00002113 if (AbsDbgVariable)
2114 return AbsDbgVariable;
2115
Devang Patel19302aa2010-05-07 18:11:54 +00002116 LLVMContext &Ctx = Var->getContext();
Chris Lattnerb9692a72010-04-02 19:42:39 +00002117 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel14da02f2010-03-15 18:33:46 +00002118 if (!Scope)
2119 return NULL;
2120
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002121 AbsDbgVariable = new DbgVariable(Var, MI,
Devang Patel14da02f2010-03-15 18:33:46 +00002122 NULL /* No more-abstract variable*/);
2123 Scope->addVariable(AbsDbgVariable);
Devang Patel19302aa2010-05-07 18:11:54 +00002124 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002125 DbgValueStartMap[MI] = AbsDbgVariable;
Devang Patel14da02f2010-03-15 18:33:46 +00002126 return AbsDbgVariable;
2127}
2128
Devang Patelc50078e2009-11-21 02:48:08 +00002129/// collectVariableInfo - Populate DbgScope entries with variables' info.
Devang Patelfad01682010-05-14 21:01:35 +00002130void DwarfDebug::collectVariableInfo(const MachineFunction *MF) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002131 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patelfad01682010-05-14 21:01:35 +00002132 SmallPtrSet<const MDNode *, 16> Processed;
Devang Patel84139992009-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 Patel9c371c62010-05-07 20:54:48 +00002136 const MDNode *Var = VI->first;
Devang Patel90a0fe32009-11-10 23:06:00 +00002137 if (!Var) continue;
Devang Patelfad01682010-05-14 21:01:35 +00002138 Processed.insert(Var);
Chris Lattnerb9692a72010-04-02 19:42:39 +00002139 DIVariable DV(Var);
2140 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00002141
Chris Lattnerb9692a72010-04-02 19:42:39 +00002142 DbgScope *Scope = 0;
Devang Patel9c371c62010-05-07 20:54:48 +00002143 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattnerb9692a72010-04-02 19:42:39 +00002144 Scope = ConcreteScopes.lookup(IA);
2145 if (Scope == 0)
2146 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
2147
Devang Patelbad42262009-11-10 23:20:04 +00002148 // If variable scope is not found then skip this variable.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002149 if (Scope == 0)
Devang Patelbad42262009-11-10 23:20:04 +00002150 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00002151
Chris Lattnerb9692a72010-04-02 19:42:39 +00002152 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, VP.second);
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002153 DbgVariable *RegVar = new DbgVariable(DV, VP.first, AbsDbgVariable);
Devang Patelc50078e2009-11-21 02:48:08 +00002154 Scope->addVariable(RegVar);
Devang Patel84139992009-10-06 01:26:37 +00002155 }
Devang Patel14da02f2010-03-15 18:33:46 +00002156
2157 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002158 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel14da02f2010-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 Lattner202f10b2010-03-31 05:39:57 +00002163 if (!MInsn->isDebugValue())
Devang Patel14da02f2010-03-15 18:33:46 +00002164 continue;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002165
Devang Patel7f78e212010-04-27 20:54:45 +00002166 // Ignore Undef values.
Devang Patelbecba812010-04-27 22:04:41 +00002167 if (MInsn->getOperand(0).isReg() && !MInsn->getOperand(0).getReg())
Devang Patel7f78e212010-04-27 20:54:45 +00002168 continue;
2169
Dan Gohman8db62492010-04-17 16:43:55 +00002170 DIVariable DV(
Devang Patel9c371c62010-05-07 20:54:48 +00002171 const_cast<const MDNode *>(MInsn->getOperand(MInsn->getNumOperands() - 1)
Dan Gohman8db62492010-04-17 16:43:55 +00002172 .getMetadata()));
Devang Patel14da02f2010-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 Patel9a9a4ac2010-03-29 22:59:58 +00002177 DbgValueStartMap[MInsn] = ArgVar;
Devang Patel14da02f2010-03-15 18:33:46 +00002178 continue;
2179 }
2180
2181 DebugLoc DL = MInsn->getDebugLoc();
2182 if (DL.isUnknown()) continue;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002183 DbgScope *Scope = 0;
Devang Patel9c371c62010-05-07 20:54:48 +00002184 if (const MDNode *IA = DL.getInlinedAt(Ctx))
Chris Lattnerb9692a72010-04-02 19:42:39 +00002185 Scope = ConcreteScopes.lookup(IA);
2186 if (Scope == 0)
2187 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2188
Devang Patel14da02f2010-03-15 18:33:46 +00002189 // If variable scope is not found then skip this variable.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002190 if (Scope == 0)
Devang Patel14da02f2010-03-15 18:33:46 +00002191 continue;
2192
Devang Patelfad01682010-05-14 21:01:35 +00002193 Processed.insert(DV);
Chris Lattnerb9692a72010-04-02 19:42:39 +00002194 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, MInsn, DL);
Devang Patel14da02f2010-03-15 18:33:46 +00002195 DbgVariable *RegVar = new DbgVariable(DV, MInsn, AbsDbgVariable);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002196 DbgValueStartMap[MInsn] = RegVar;
Devang Patel14da02f2010-03-15 18:33:46 +00002197 Scope->addVariable(RegVar);
2198 }
2199 }
Devang Patelfad01682010-05-14 21:01:35 +00002200
2201 // Collect info for variables that were optimized out.
2202 if (NamedMDNode *NMD =
2203 MF->getFunction()->getParent()->getNamedMetadata("llvm.dbg.lv")) {
2204 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2205 DIVariable DV(cast_or_null<MDNode>(NMD->getOperand(i)));
2206 if (!Processed.insert(DV))
2207 continue;
2208 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2209 if (Scope)
2210 Scope->addVariable(new DbgVariable(DV, ~0U, NULL));
2211 }
2212 }
2213
Devang Patel84139992009-10-06 01:26:37 +00002214}
2215
Devang Patelabc2b352010-03-29 17:20:31 +00002216/// beginScope - Process beginning of a scope.
2217void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patelabc2b352010-03-29 17:20:31 +00002218 // Check location.
2219 DebugLoc DL = MI->getDebugLoc();
Dan Gohman5d0dda52010-05-05 23:41:32 +00002220 if (DL.isUnknown()) {
Dan Gohman77c565e2010-05-07 01:08:53 +00002221 if (UnknownLocations) {
2222 // This instruction has no debug location. If the preceding instruction
2223 // did, emit debug location information to indicate that the debug
2224 // location is now unknown.
2225 MCSymbol *Label = NULL;
2226 if (DL == PrevInstLoc)
2227 Label = PrevLabel;
2228 else {
2229 Label = recordSourceLine(DL.getLine(), DL.getCol(), 0);
2230 PrevInstLoc = DL;
2231 PrevLabel = Label;
2232 }
Dan Gohmanf79f0f72010-05-06 00:29:41 +00002233
Dan Gohman77c565e2010-05-07 01:08:53 +00002234 // If this instruction begins a scope then note down corresponding label.
2235 if (InsnsBeginScopeSet.count(MI) != 0)
2236 LabelsBeforeInsn[MI] = Label;
2237 }
Dan Gohmanf79f0f72010-05-06 00:29:41 +00002238
Devang Patelabc2b352010-03-29 17:20:31 +00002239 return;
Dan Gohman5d0dda52010-05-05 23:41:32 +00002240 }
Devang Patelabc2b352010-03-29 17:20:31 +00002241
Devang Patel9c371c62010-05-07 20:54:48 +00002242 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Chris Lattnerb9692a72010-04-02 19:42:39 +00002243
2244 // FIXME: Should only verify each scope once!
2245 if (!DIScope(Scope).Verify())
Devang Patelabc2b352010-03-29 17:20:31 +00002246 return;
Devang Patelabc2b352010-03-29 17:20:31 +00002247
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002248 // DBG_VALUE instruction establishes new value.
Chris Lattner202f10b2010-03-31 05:39:57 +00002249 if (MI->isDebugValue()) {
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002250 DenseMap<const MachineInstr *, DbgVariable *>::iterator DI
2251 = DbgValueStartMap.find(MI);
2252 if (DI != DbgValueStartMap.end()) {
Devang Patelce7bf012010-04-27 19:46:33 +00002253 MCSymbol *Label = NULL;
2254 if (DL == PrevInstLoc)
2255 Label = PrevLabel;
2256 else {
2257 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2258 PrevInstLoc = DL;
2259 PrevLabel = Label;
2260 }
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002261 DI->second->setDbgValueLabel(Label);
2262 }
Devang Patel98ca2372010-03-30 18:07:00 +00002263 return;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002264 }
2265
Devang Patelabc2b352010-03-29 17:20:31 +00002266 // Emit a label to indicate location change. This is used for line
Devang Patelce7bf012010-04-27 19:46:33 +00002267 // table even if this instruction does not start a new scope.
2268 MCSymbol *Label = NULL;
2269 if (DL == PrevInstLoc)
2270 Label = PrevLabel;
2271 else {
2272 Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2273 PrevInstLoc = DL;
2274 PrevLabel = Label;
2275 }
Devang Patelabc2b352010-03-29 17:20:31 +00002276
Devang Patel6f878382010-04-08 16:50:29 +00002277 // If this instruction begins a scope then note down corresponding label.
2278 if (InsnsBeginScopeSet.count(MI) != 0)
Devang Patelce7bf012010-04-27 19:46:33 +00002279 LabelsBeforeInsn[MI] = Label;
Devang Patel393a46d2009-10-06 01:50:42 +00002280}
2281
Devang Patelc50078e2009-11-21 02:48:08 +00002282/// endScope - Process end of a scope.
2283void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel6f878382010-04-08 16:50:29 +00002284 if (InsnsEndScopeSet.count(MI) != 0) {
2285 // Emit a label if this instruction ends a scope.
2286 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2287 Asm->OutStreamer.EmitLabel(Label);
Devang Patelce7bf012010-04-27 19:46:33 +00002288 LabelsAfterInsn[MI] = Label;
Devang Patel6f878382010-04-08 16:50:29 +00002289 }
Devang Patel90a0fe32009-11-10 23:06:00 +00002290}
2291
Devang Patelce7bf012010-04-27 19:46:33 +00002292/// getOrCreateDbgScope - Create DbgScope for the scope.
Devang Patel9c371c62010-05-07 20:54:48 +00002293DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002294 if (!InlinedAt) {
2295 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2296 if (WScope)
Devang Patelce7bf012010-04-27 19:46:33 +00002297 return WScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00002298 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2299 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patelce7bf012010-04-27 19:46:33 +00002300 if (DIDescriptor(Scope).isLexicalBlock()) {
2301 DbgScope *Parent =
Devang Patel19302aa2010-05-07 18:11:54 +00002302 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Patelce7bf012010-04-27 19:46:33 +00002303 WScope->setParent(Parent);
2304 Parent->addScope(WScope);
2305 }
2306
2307 if (!WScope->getParent()) {
2308 StringRef SPName = DISubprogram(Scope).getLinkageName();
2309 if (SPName == Asm->MF->getFunction()->getName())
2310 CurrentFnDbgScope = WScope;
2311 }
2312
2313 return WScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00002314 }
2315
2316 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2317 if (WScope)
Devang Patelce7bf012010-04-27 19:46:33 +00002318 return WScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00002319
2320 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2321 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2322 DILocation DL(InlinedAt);
Devang Patelce7bf012010-04-27 19:46:33 +00002323 DbgScope *Parent =
Devang Patel19302aa2010-05-07 18:11:54 +00002324 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Patelce7bf012010-04-27 19:46:33 +00002325 WScope->setParent(Parent);
2326 Parent->addScope(WScope);
2327
2328 ConcreteScopes[InlinedAt] = WScope;
2329 getOrCreateAbstractScope(Scope);
2330
2331 return WScope;
Devang Patel393a46d2009-10-06 01:50:42 +00002332}
2333
Devang Patelce7bf012010-04-27 19:46:33 +00002334/// hasValidLocation - Return true if debug location entry attached with
2335/// machine instruction encodes valid location info.
2336static bool hasValidLocation(LLVMContext &Ctx,
2337 const MachineInstr *MInsn,
Devang Patel9c371c62010-05-07 20:54:48 +00002338 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Patelce7bf012010-04-27 19:46:33 +00002339 if (MInsn->isDebugValue())
2340 return false;
2341 DebugLoc DL = MInsn->getDebugLoc();
2342 if (DL.isUnknown()) return false;
2343
Devang Patel9c371c62010-05-07 20:54:48 +00002344 const MDNode *S = DL.getScope(Ctx);
Devang Patelce7bf012010-04-27 19:46:33 +00002345
2346 // There is no need to create another DIE for compile unit. For all
2347 // other scopes, create one DbgScope now. This will be translated
2348 // into a scope DIE at the end.
2349 if (DIScope(S).isCompileUnit()) return false;
2350
2351 Scope = S;
2352 InlinedAt = DL.getInlinedAt(Ctx);
2353 return true;
2354}
2355
2356/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2357/// hierarchy.
2358static void calculateDominanceGraph(DbgScope *Scope) {
2359 assert (Scope && "Unable to calculate scop edominance graph!");
2360 SmallVector<DbgScope *, 4> WorkStack;
2361 WorkStack.push_back(Scope);
2362 unsigned Counter = 0;
2363 while (!WorkStack.empty()) {
2364 DbgScope *WS = WorkStack.back();
2365 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2366 bool visitedChildren = false;
2367 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2368 SE = Children.end(); SI != SE; ++SI) {
2369 DbgScope *ChildScope = *SI;
2370 if (!ChildScope->getDFSOut()) {
2371 WorkStack.push_back(ChildScope);
2372 visitedChildren = true;
2373 ChildScope->setDFSIn(++Counter);
2374 break;
2375 }
2376 }
2377 if (!visitedChildren) {
2378 WorkStack.pop_back();
2379 WS->setDFSOut(++Counter);
2380 }
2381 }
2382}
2383
2384/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
2385static
2386void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2387 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2388{
2389#ifndef NDEBUG
2390 unsigned PrevDFSIn = 0;
2391 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2392 I != E; ++I) {
2393 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2394 II != IE; ++II) {
2395 const MachineInstr *MInsn = II;
Devang Patel9c371c62010-05-07 20:54:48 +00002396 const MDNode *Scope = NULL;
2397 const MDNode *InlinedAt = NULL;
Devang Patelce7bf012010-04-27 19:46:33 +00002398
2399 // Check if instruction has valid location information.
2400 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2401 dbgs() << " [ ";
2402 if (InlinedAt)
2403 dbgs() << "*";
2404 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
2405 MI2ScopeMap.find(MInsn);
2406 if (DI != MI2ScopeMap.end()) {
2407 DbgScope *S = DI->second;
2408 dbgs() << S->getDFSIn();
2409 PrevDFSIn = S->getDFSIn();
2410 } else
2411 dbgs() << PrevDFSIn;
2412 } else
2413 dbgs() << " [ x" << PrevDFSIn;
2414 dbgs() << " ]";
2415 MInsn->dump();
2416 }
2417 dbgs() << "\n";
2418 }
2419#endif
2420}
Devang Patelc50078e2009-11-21 02:48:08 +00002421/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner202f10b2010-03-31 05:39:57 +00002422/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattner69a76972010-01-26 23:18:02 +00002423bool DwarfDebug::extractScopeInformation() {
Devang Patel6a260102009-10-01 20:31:14 +00002424 // If scope information was extracted using .dbg intrinsics then there is not
2425 // any need to extract these information by scanning each instruction.
2426 if (!DbgScopeMap.empty())
2427 return false;
2428
Dan Gohman30573dd2010-04-23 01:18:53 +00002429 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelce7bf012010-04-27 19:46:33 +00002430 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2431 SmallVector<DbgRange, 4> MIRanges;
2432 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patel9c371c62010-05-07 20:54:48 +00002433 const MDNode *PrevScope = NULL;
2434 const MDNode *PrevInlinedAt = NULL;
Devang Patelce7bf012010-04-27 19:46:33 +00002435 const MachineInstr *RangeBeginMI = NULL;
2436 const MachineInstr *PrevMI = NULL;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002437 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel6a260102009-10-01 20:31:14 +00002438 I != E; ++I) {
2439 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2440 II != IE; ++II) {
2441 const MachineInstr *MInsn = II;
Devang Patel9c371c62010-05-07 20:54:48 +00002442 const MDNode *Scope = NULL;
2443 const MDNode *InlinedAt = NULL;
Devang Patelce7bf012010-04-27 19:46:33 +00002444
2445 // Check if instruction has valid location information.
2446 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2447 PrevMI = MInsn;
2448 continue;
2449 }
Chris Lattnerb9692a72010-04-02 19:42:39 +00002450
Devang Patelce7bf012010-04-27 19:46:33 +00002451 // If scope has not changed then skip this instruction.
2452 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2453 PrevMI = MInsn;
2454 continue;
2455 }
2456
2457 if (RangeBeginMI) {
2458 // If we have alread seen a beginning of a instruction range and
2459 // current instruction scope does not match scope of first instruction
2460 // in this range then create a new instruction range.
2461 DbgRange R(RangeBeginMI, PrevMI);
2462 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
2463 MIRanges.push_back(R);
2464 }
2465
2466 // This is a beginning of a new instruction range.
2467 RangeBeginMI = MInsn;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002468
Devang Patelce7bf012010-04-27 19:46:33 +00002469 // Reset previous markers.
2470 PrevMI = MInsn;
2471 PrevScope = Scope;
2472 PrevInlinedAt = InlinedAt;
Devang Patel90a0fe32009-11-10 23:06:00 +00002473 }
2474 }
2475
Devang Patelce7bf012010-04-27 19:46:33 +00002476 // Create last instruction range.
2477 if (RangeBeginMI && PrevMI && PrevScope) {
2478 DbgRange R(RangeBeginMI, PrevMI);
2479 MIRanges.push_back(R);
2480 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patel6a260102009-10-01 20:31:14 +00002481 }
Devang Patelce7bf012010-04-27 19:46:33 +00002482
Devang Patel98e77302010-01-04 20:44:00 +00002483 if (!CurrentFnDbgScope)
2484 return false;
2485
Devang Patelce7bf012010-04-27 19:46:33 +00002486 calculateDominanceGraph(CurrentFnDbgScope);
2487 if (PrintDbgScope)
2488 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2489
2490 // Find ranges of instructions covered by each DbgScope;
2491 DbgScope *PrevDbgScope = NULL;
2492 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2493 RE = MIRanges.end(); RI != RE; ++RI) {
2494 const DbgRange &R = *RI;
2495 DbgScope *S = MI2ScopeMap.lookup(R.first);
2496 assert (S && "Lost DbgScope for a machine instruction!");
2497 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2498 PrevDbgScope->closeInsnRange(S);
2499 S->openInsnRange(R.first);
2500 S->extendInsnRange(R.second);
2501 PrevDbgScope = S;
2502 }
2503
2504 if (PrevDbgScope)
2505 PrevDbgScope->closeInsnRange();
Devang Patel6a260102009-10-01 20:31:14 +00002506
Devang Patelf63b2262010-04-08 18:43:56 +00002507 identifyScopeMarkers();
Devang Patel0d8f3b72010-04-08 15:37:09 +00002508
2509 return !DbgScopeMap.empty();
2510}
2511
Devang Patelce7bf012010-04-27 19:46:33 +00002512/// identifyScopeMarkers() -
2513/// Each DbgScope has first instruction and last instruction to mark beginning
2514/// and end of a scope respectively. Create an inverse map that list scopes
2515/// starts (and ends) with an instruction. One instruction may start (or end)
2516/// multiple scopes. Ignore scopes that are not reachable.
Devang Patelf63b2262010-04-08 18:43:56 +00002517void DwarfDebug::identifyScopeMarkers() {
Devang Patelfd311df2010-01-20 02:05:23 +00002518 SmallVector<DbgScope *, 4> WorkList;
2519 WorkList.push_back(CurrentFnDbgScope);
2520 while (!WorkList.empty()) {
Chris Lattner202f10b2010-03-31 05:39:57 +00002521 DbgScope *S = WorkList.pop_back_val();
Devang Patelce7bf012010-04-27 19:46:33 +00002522
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002523 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patelfd311df2010-01-20 02:05:23 +00002524 if (!Children.empty())
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002525 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patelfd311df2010-01-20 02:05:23 +00002526 SE = Children.end(); SI != SE; ++SI)
2527 WorkList.push_back(*SI);
2528
Devang Patel90a0fe32009-11-10 23:06:00 +00002529 if (S->isAbstractScope())
2530 continue;
Devang Patelce7bf012010-04-27 19:46:33 +00002531
2532 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2533 if (Ranges.empty())
2534 continue;
2535 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2536 RE = Ranges.end(); RI != RE; ++RI) {
2537 assert(RI->first && "DbgRange does not have first instruction!");
2538 assert(RI->second && "DbgRange does not have second instruction!");
2539 InsnsBeginScopeSet.insert(RI->first);
2540 InsnsEndScopeSet.insert(RI->second);
2541 }
Devang Patel6a260102009-10-01 20:31:14 +00002542 }
Devang Patel6a260102009-10-01 20:31:14 +00002543}
2544
Dan Gohmand0e9b672010-04-20 00:37:27 +00002545/// FindFirstDebugLoc - Find the first debug location in the function. This
2546/// is intended to be an approximation for the source position of the
2547/// beginning of the function.
2548static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2549 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2550 I != E; ++I)
2551 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2552 MBBI != MBBE; ++MBBI) {
2553 DebugLoc DL = MBBI->getDebugLoc();
2554 if (!DL.isUnknown())
2555 return DL;
2556 }
2557 return DebugLoc();
2558}
2559
Devang Patelc50078e2009-11-21 02:48:08 +00002560/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002561/// emitted immediately after the function entry point.
Chris Lattner69a76972010-01-26 23:18:02 +00002562void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner2cb53302010-04-05 03:52:55 +00002563 if (!MMI->hasDebugInfo()) return;
Bill Wendlingce3c6252010-04-07 09:28:04 +00002564 if (!extractScopeInformation()) return;
Chris Lattnerb49c9f82010-03-29 20:38:20 +00002565
Devang Patelfad01682010-05-14 21:01:35 +00002566 collectVariableInfo(MF);
Devang Patel0feae422009-10-06 18:37:31 +00002567
Devang Patelce7bf012010-04-27 19:46:33 +00002568 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2569 Asm->getFunctionNumber());
Bill Wendlingf5839192009-05-20 23:19:06 +00002570 // Assumes in correct section after the entry point.
Devang Patelce7bf012010-04-27 19:46:33 +00002571 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf5839192009-05-20 23:19:06 +00002572
2573 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2574 // function.
Dan Gohmand0e9b672010-04-20 00:37:27 +00002575 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattnerb9692a72010-04-02 19:42:39 +00002576 if (FDL.isUnknown()) return;
2577
Devang Patel9c371c62010-05-07 20:54:48 +00002578 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Chris Lattnerb9692a72010-04-02 19:42:39 +00002579
2580 DISubprogram SP = getDISubprogram(Scope);
2581 unsigned Line, Col;
2582 if (SP.Verify()) {
2583 Line = SP.getLineNumber();
2584 Col = 0;
2585 } else {
2586 Line = FDL.getLine();
2587 Col = FDL.getCol();
Bill Wendlingf5839192009-05-20 23:19:06 +00002588 }
Chris Lattnerb9692a72010-04-02 19:42:39 +00002589
2590 recordSourceLine(Line, Col, Scope);
Bill Wendlingf5839192009-05-20 23:19:06 +00002591}
2592
Devang Patelc50078e2009-11-21 02:48:08 +00002593/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002594///
Chris Lattner69a76972010-01-26 23:18:02 +00002595void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingce3c6252010-04-07 09:28:04 +00002596 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002597
Devang Patel98e77302010-01-04 20:44:00 +00002598 if (CurrentFnDbgScope) {
2599 // Define end label for subprogram.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002600 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_end",
2601 Asm->getFunctionNumber()));
Devang Patel98e77302010-01-04 20:44:00 +00002602
2603 // Get function line info.
2604 if (!Lines.empty()) {
2605 // Get section line info.
2606 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2607 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2608 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2609 // Append the function info to section info.
2610 SectionLineInfos.insert(SectionLineInfos.end(),
2611 Lines.begin(), Lines.end());
2612 }
2613
2614 // Construct abstract scopes.
2615 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2616 AE = AbstractScopesList.end(); AI != AE; ++AI)
2617 constructScopeDIE(*AI);
2618
Devang Patelab4b3ce2010-05-04 06:15:30 +00002619 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Devang Patel98e77302010-01-04 20:44:00 +00002620
Devang Patelab4b3ce2010-05-04 06:15:30 +00002621 if (!DisableFramePointerElim(*MF))
2622 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
2623 dwarf::DW_FORM_flag, 1);
2624
2625
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002626 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel98e77302010-01-04 20:44:00 +00002627 MMI->getFrameMoves()));
Bill Wendlingf5839192009-05-20 23:19:06 +00002628 }
2629
Bill Wendlingf5839192009-05-20 23:19:06 +00002630 // Clear debug info
Devang Patel387e9c12010-01-19 01:26:02 +00002631 CurrentFnDbgScope = NULL;
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002632 DeleteContainerSeconds(DbgScopeMap);
Devang Patelfd0ebd52010-04-09 16:04:20 +00002633 InsnsBeginScopeSet.clear();
2634 InsnsEndScopeSet.clear();
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002635 DbgValueStartMap.clear();
Devang Patel387e9c12010-01-19 01:26:02 +00002636 ConcreteScopes.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002637 DeleteContainerSeconds(AbstractScopes);
Devang Patel387e9c12010-01-19 01:26:02 +00002638 AbstractScopesList.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002639 AbstractVariables.clear();
Devang Patelce7bf012010-04-27 19:46:33 +00002640 LabelsBeforeInsn.clear();
2641 LabelsAfterInsn.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002642 Lines.clear();
Devang Patel146c6f72010-04-16 23:33:45 +00002643 PrevLabel = NULL;
Bill Wendlingf5839192009-05-20 23:19:06 +00002644}
2645
Chris Lattner597b0fc2010-03-09 04:54:43 +00002646/// recordSourceLine - Register a source line with debug info. Returns the
2647/// unique label that was emitted and which provides correspondence to
2648/// the source line list.
Devang Patel9c371c62010-05-07 20:54:48 +00002649MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S) {
Devang Patel7f75bbe2009-11-25 17:36:49 +00002650 StringRef Dir;
2651 StringRef Fn;
Devang Patel946d0ae2009-10-05 18:03:19 +00002652
Dan Gohman5d0dda52010-05-05 23:41:32 +00002653 unsigned Src = 1;
2654 if (S) {
2655 DIDescriptor Scope(S);
Devang Patel946d0ae2009-10-05 18:03:19 +00002656
Dan Gohman5d0dda52010-05-05 23:41:32 +00002657 if (Scope.isCompileUnit()) {
2658 DICompileUnit CU(S);
2659 Dir = CU.getDirectory();
2660 Fn = CU.getFilename();
2661 } else if (Scope.isSubprogram()) {
2662 DISubprogram SP(S);
2663 Dir = SP.getDirectory();
2664 Fn = SP.getFilename();
2665 } else if (Scope.isLexicalBlock()) {
2666 DILexicalBlock DB(S);
2667 Dir = DB.getDirectory();
2668 Fn = DB.getFilename();
2669 } else
2670 assert(0 && "Unexpected scope info");
2671
2672 Src = GetOrCreateSourceID(Dir, Fn);
2673 }
2674
Chris Lattner54e56f22010-03-14 08:36:50 +00002675 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner8d9d06a2010-03-14 08:15:55 +00002676 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf5839192009-05-20 23:19:06 +00002677
Chris Lattner597b0fc2010-03-09 04:54:43 +00002678 Asm->OutStreamer.EmitLabel(Label);
2679 return Label;
Bill Wendlingf5839192009-05-20 23:19:06 +00002680}
2681
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002682//===----------------------------------------------------------------------===//
2683// Emit Methods
2684//===----------------------------------------------------------------------===//
2685
Devang Patelc50078e2009-11-21 02:48:08 +00002686/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002687///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002688unsigned
2689DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002690 // Get the children.
2691 const std::vector<DIE *> &Children = Die->getChildren();
2692
2693 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin2944ced2010-03-22 18:47:14 +00002694 if (!Last && !Children.empty())
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00002695 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling55fccda2009-05-20 23:21:38 +00002696
2697 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002698 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002699
2700 // Get the abbreviation for this DIE.
2701 unsigned AbbrevNumber = Die->getAbbrevNumber();
2702 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2703
2704 // Set DIE offset
2705 Die->setOffset(Offset);
2706
2707 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002708 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002709
2710 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2711 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2712
2713 // Size the DIE attribute values.
2714 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2715 // Size attribute value.
Chris Lattner352c8e22010-04-05 00:18:22 +00002716 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling55fccda2009-05-20 23:21:38 +00002717
2718 // Size the DIE children if any.
2719 if (!Children.empty()) {
2720 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2721 "Children flag not set");
2722
2723 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002724 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002725
2726 // End of children marker.
2727 Offset += sizeof(int8_t);
2728 }
2729
2730 Die->setSize(Offset - Die->getOffset());
2731 return Offset;
2732}
2733
Devang Patelc50078e2009-11-21 02:48:08 +00002734/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002735///
Devang Patelc50078e2009-11-21 02:48:08 +00002736void DwarfDebug::computeSizeAndOffsets() {
Devang Patel7e5a86e2010-05-10 22:49:55 +00002737 unsigned PrevOffset = 0;
2738 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2739 E = CUMap.end(); I != E; ++I) {
2740 // Compute size of compile unit header.
2741 static unsigned Offset = PrevOffset +
2742 sizeof(int32_t) + // Length of Compilation Unit Info
2743 sizeof(int16_t) + // DWARF version number
2744 sizeof(int32_t) + // Offset Into Abbrev. Section
2745 sizeof(int8_t); // Pointer Size (in bytes)
2746 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
2747 PrevOffset = Offset;
2748 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002749}
2750
Chris Lattner733c69d2010-04-04 23:02:02 +00002751/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2752/// temporary label to it if SymbolStem is specified.
Chris Lattner66143d22010-04-04 22:59:04 +00002753static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner733c69d2010-04-04 23:02:02 +00002754 const char *SymbolStem = 0) {
Chris Lattner66143d22010-04-04 22:59:04 +00002755 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner733c69d2010-04-04 23:02:02 +00002756 if (!SymbolStem) return 0;
2757
Chris Lattner66143d22010-04-04 22:59:04 +00002758 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2759 Asm->OutStreamer.EmitLabel(TmpSym);
2760 return TmpSym;
2761}
2762
2763/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2764/// the start of each one.
Chris Lattnerd91fd8c2010-04-04 22:33:59 +00002765void DwarfDebug::EmitSectionLabels() {
Chris Lattner73266f92009-08-19 05:49:37 +00002766 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002767
Bill Wendling55fccda2009-05-20 23:21:38 +00002768 // Dwarf sections base addresses.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002769 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner66143d22010-04-04 22:59:04 +00002770 DwarfFrameSectionSym =
2771 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2772 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002773
Chris Lattner66143d22010-04-04 22:59:04 +00002774 DwarfInfoSectionSym =
2775 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2776 DwarfAbbrevSectionSym =
2777 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner733c69d2010-04-04 23:02:02 +00002778 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Chris Lattner66143d22010-04-04 22:59:04 +00002779
2780 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner733c69d2010-04-04 23:02:02 +00002781 EmitSectionSym(Asm, MacroInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002782
Chris Lattner733c69d2010-04-04 23:02:02 +00002783 EmitSectionSym(Asm, TLOF.getDwarfLineSection());
2784 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2785 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2786 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Chris Lattner66143d22010-04-04 22:59:04 +00002787 DwarfStrSectionSym =
2788 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel146c6f72010-04-16 23:33:45 +00002789 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2790 "debug_range");
Bill Wendling55fccda2009-05-20 23:21:38 +00002791
Chris Lattner66143d22010-04-04 22:59:04 +00002792 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner094932e2010-04-04 23:10:38 +00002793 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002794}
2795
Devang Patelc50078e2009-11-21 02:48:08 +00002796/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002797///
Devang Patelc50078e2009-11-21 02:48:08 +00002798void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002799 // Get the abbreviation for this DIE.
2800 unsigned AbbrevNumber = Die->getAbbrevNumber();
2801 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2802
Bill Wendling55fccda2009-05-20 23:21:38 +00002803 // Emit the code (index) for the abbreviation.
Chris Lattner97c69b72010-04-04 18:52:31 +00002804 if (Asm->isVerbose())
Chris Lattnerbcc79432010-01-22 23:18:42 +00002805 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2806 Twine::utohexstr(Die->getOffset()) + ":0x" +
2807 Twine::utohexstr(Die->getSize()) + " " +
2808 dwarf::TagString(Abbrev->getTag()));
Chris Lattner26be1c12010-04-04 19:09:29 +00002809 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002810
Jeffrey Yasskin2944ced2010-03-22 18:47:14 +00002811 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling55fccda2009-05-20 23:21:38 +00002812 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2813
2814 // Emit the DIE attribute values.
2815 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2816 unsigned Attr = AbbrevData[i].getAttribute();
2817 unsigned Form = AbbrevData[i].getForm();
2818 assert(Form && "Too many attributes for DIE (check abbreviation)");
2819
Chris Lattner97c69b72010-04-04 18:52:31 +00002820 if (Asm->isVerbose())
Chris Lattner91e06b42010-01-24 18:54:17 +00002821 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2822
Bill Wendling55fccda2009-05-20 23:21:38 +00002823 switch (Attr) {
2824 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002825 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002826 break;
2827 case dwarf::DW_AT_abstract_origin: {
2828 DIEEntry *E = cast<DIEEntry>(Values[i]);
2829 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002830 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002831 Asm->EmitInt32(Addr);
2832 break;
2833 }
Devang Patel146c6f72010-04-16 23:33:45 +00002834 case dwarf::DW_AT_ranges: {
2835 // DW_AT_range Value encodes offset in debug_range section.
2836 DIEInteger *V = cast<DIEInteger>(Values[i]);
2837 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2838 V->getValue(),
2839 DwarfDebugRangeSectionSym,
2840 4);
2841 break;
2842 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002843 default:
2844 // Emit an attribute using the defined form.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002845 Values[i]->EmitValue(Asm, Form);
Bill Wendling55fccda2009-05-20 23:21:38 +00002846 break;
2847 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002848 }
2849
2850 // Emit the DIE children if any.
2851 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2852 const std::vector<DIE *> &Children = Die->getChildren();
2853
2854 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002855 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002856
Chris Lattner97c69b72010-04-04 18:52:31 +00002857 if (Asm->isVerbose())
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002858 Asm->OutStreamer.AddComment("End Of Children Mark");
2859 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002860 }
2861}
2862
Devang Patelfe0be132009-12-09 18:24:21 +00002863/// emitDebugInfo - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002864///
Devang Patelfe0be132009-12-09 18:24:21 +00002865void DwarfDebug::emitDebugInfo() {
2866 // Start debug info section.
2867 Asm->OutStreamer.SwitchSection(
2868 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel7e5a86e2010-05-10 22:49:55 +00002869 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2870 E = CUMap.end(); I != E; ++I) {
2871 CompileUnit *TheCU = I->second;
2872 DIE *Die = TheCU->getCUDie();
2873
2874 // Emit the compile units header.
2875 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2876 TheCU->getID()));
2877
2878 // Emit size of content not including length itself
2879 unsigned ContentSize = Die->getSize() +
2880 sizeof(int16_t) + // DWARF version number
2881 sizeof(int32_t) + // Offset Into Abbrev. Section
2882 sizeof(int8_t) + // Pointer Size (in bytes)
2883 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2884
2885 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2886 Asm->EmitInt32(ContentSize);
2887 Asm->OutStreamer.AddComment("DWARF version number");
2888 Asm->EmitInt16(dwarf::DWARF_VERSION);
2889 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2890 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2891 DwarfAbbrevSectionSym);
2892 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2893 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
2894
2895 emitDIE(Die);
2896 // FIXME - extra padding for gdb bug.
2897 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2898 Asm->EmitInt8(0);
2899 Asm->EmitInt8(0);
2900 Asm->EmitInt8(0);
2901 Asm->EmitInt8(0);
2902 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
2903 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002904}
2905
Devang Patelc50078e2009-11-21 02:48:08 +00002906/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002907///
Devang Patelc50078e2009-11-21 02:48:08 +00002908void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002909 // Check to see if it is worth the effort.
2910 if (!Abbreviations.empty()) {
2911 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002912 Asm->OutStreamer.SwitchSection(
2913 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002914
Chris Lattnerb93558d2010-04-04 19:25:43 +00002915 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002916
2917 // For each abbrevation.
2918 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2919 // Get abbreviation data
2920 const DIEAbbrev *Abbrev = Abbreviations[i];
2921
2922 // Emit the abbrevations code (base 1 index.)
Chris Lattner26be1c12010-04-04 19:09:29 +00002923 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling55fccda2009-05-20 23:21:38 +00002924
2925 // Emit the abbreviations data.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002926 Abbrev->Emit(Asm);
Bill Wendling55fccda2009-05-20 23:21:38 +00002927 }
2928
2929 // Mark end of abbreviations.
Chris Lattner26be1c12010-04-04 19:09:29 +00002930 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002931
Chris Lattnerb93558d2010-04-04 19:25:43 +00002932 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002933 }
2934}
2935
Devang Patelc50078e2009-11-21 02:48:08 +00002936/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002937/// the line matrix.
2938///
Devang Patelc50078e2009-11-21 02:48:08 +00002939void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002940 // Define last address of section.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002941 Asm->OutStreamer.AddComment("Extended Op");
2942 Asm->EmitInt8(0);
2943
2944 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002945 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002946 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2947 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2948
2949 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnereb159d62010-03-10 01:17:49 +00002950
Chris Lattnerb93558d2010-04-04 19:25:43 +00002951 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002952 Asm->getTargetData().getPointerSize(),
2953 0/*AddrSpace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00002954
2955 // Mark end of matrix.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002956 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2957 Asm->EmitInt8(0);
Chris Lattnerad653482010-01-22 22:09:00 +00002958 Asm->EmitInt8(1);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002959 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002960}
2961
Devang Patelc50078e2009-11-21 02:48:08 +00002962/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002963///
Devang Patelc50078e2009-11-21 02:48:08 +00002964void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002965 // If the target is using .loc/.file, the assembler will be emitting the
2966 // .debug_line table automatically.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002967 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002968 return;
2969
2970 // Minimum line delta, thus ranging from -10..(255-10).
2971 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2972 // Maximum line delta, thus ranging from -10..(255-10).
2973 const int MaxLineDelta = 255 + MinLineDelta;
2974
2975 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002976 Asm->OutStreamer.SwitchSection(
2977 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002978
2979 // Construct the section header.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002980 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002981 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
2982 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002983 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002984
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002985 Asm->OutStreamer.AddComment("DWARF version number");
2986 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling55fccda2009-05-20 23:21:38 +00002987
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002988 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002989 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
2990 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002991 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002992
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002993 Asm->OutStreamer.AddComment("Minimum Instruction Length");
2994 Asm->EmitInt8(1);
2995 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
2996 Asm->EmitInt8(1);
2997 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
2998 Asm->EmitInt8(MinLineDelta);
2999 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
3000 Asm->EmitInt8(MaxLineDelta);
3001 Asm->OutStreamer.AddComment("Special Opcode Base");
3002 Asm->EmitInt8(-MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00003003
3004 // Line number standard opcode encodings argument count
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003005 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3006 Asm->EmitInt8(0);
3007 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3008 Asm->EmitInt8(1);
3009 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3010 Asm->EmitInt8(1);
3011 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3012 Asm->EmitInt8(1);
3013 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3014 Asm->EmitInt8(1);
3015 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3016 Asm->EmitInt8(0);
3017 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3018 Asm->EmitInt8(0);
3019 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3020 Asm->EmitInt8(0);
3021 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3022 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00003023
3024 // Emit directories.
3025 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnercc564642010-01-23 03:11:46 +00003026 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner97c69b72010-04-04 18:52:31 +00003027 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattnercc564642010-01-23 03:11:46 +00003028 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003029 }
3030
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003031 Asm->OutStreamer.AddComment("End of directories");
3032 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003033
3034 // Emit files.
3035 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3036 // Remember source id starts at 1.
3037 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnercc564642010-01-23 03:11:46 +00003038 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner97c69b72010-04-04 18:52:31 +00003039 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattnercc564642010-01-23 03:11:46 +00003040 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
3041
Chris Lattner26be1c12010-04-04 19:09:29 +00003042 Asm->EmitULEB128(Id.first, "Directory #");
3043 Asm->EmitULEB128(0, "Mod date");
3044 Asm->EmitULEB128(0, "File size");
Bill Wendling55fccda2009-05-20 23:21:38 +00003045 }
3046
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003047 Asm->OutStreamer.AddComment("End of files");
3048 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003049
Chris Lattnerb93558d2010-04-04 19:25:43 +00003050 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00003051
3052 // A sequence for each text section.
3053 unsigned SecSrcLinesSize = SectionSourceLines.size();
3054
3055 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3056 // Isolate current sections line info.
3057 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3058
Bill Wendling55fccda2009-05-20 23:21:38 +00003059 // Dwarf assumes we start with first line of first source file.
3060 unsigned Source = 1;
3061 unsigned Line = 1;
3062
3063 // Construct rows of the address, source, line, column matrix.
3064 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3065 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner8d9d06a2010-03-14 08:15:55 +00003066 MCSymbol *Label = LineInfo.getLabel();
Chris Lattner31ae74d2010-03-14 02:20:58 +00003067 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling55fccda2009-05-20 23:21:38 +00003068
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003069 if (Asm->isVerbose()) {
Chris Lattneree3b40f2010-03-10 01:04:13 +00003070 std::pair<unsigned, unsigned> SrcID =
Bill Wendling55fccda2009-05-20 23:21:38 +00003071 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattneree3b40f2010-03-10 01:04:13 +00003072 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner48fb0b12010-03-10 02:29:31 +00003073 "/" +
3074 Twine(getSourceFileName(SrcID.second)) +
Chris Lattneree3b40f2010-03-10 01:04:13 +00003075 ":" + Twine(LineInfo.getLine()));
Bill Wendling55fccda2009-05-20 23:21:38 +00003076 }
3077
3078 // Define the line address.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003079 Asm->OutStreamer.AddComment("Extended Op");
3080 Asm->EmitInt8(0);
3081 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003082 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003083
3084 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3085 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3086
3087 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003088 Asm->OutStreamer.EmitSymbolValue(Label,
3089 Asm->getTargetData().getPointerSize(),
Chris Lattner31ae74d2010-03-14 02:20:58 +00003090 0/*AddrSpace*/);
Chris Lattnereb159d62010-03-10 01:17:49 +00003091
Bill Wendling55fccda2009-05-20 23:21:38 +00003092 // If change of source, then switch to the new source.
3093 if (Source != LineInfo.getSourceID()) {
3094 Source = LineInfo.getSourceID();
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003095 Asm->OutStreamer.AddComment("DW_LNS_set_file");
3096 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner26be1c12010-04-04 19:09:29 +00003097 Asm->EmitULEB128(Source, "New Source");
Bill Wendling55fccda2009-05-20 23:21:38 +00003098 }
3099
3100 // If change of line.
3101 if (Line != LineInfo.getLine()) {
3102 // Determine offset.
3103 int Offset = LineInfo.getLine() - Line;
3104 int Delta = Offset - MinLineDelta;
3105
3106 // Update line.
3107 Line = LineInfo.getLine();
3108
3109 // If delta is small enough and in range...
3110 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3111 // ... then use fast opcode.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003112 Asm->OutStreamer.AddComment("Line Delta");
3113 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00003114 } else {
3115 // ... otherwise use long hand.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003116 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling55fccda2009-05-20 23:21:38 +00003117 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner26be1c12010-04-04 19:09:29 +00003118 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003119 Asm->OutStreamer.AddComment("DW_LNS_copy");
3120 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00003121 }
3122 } else {
3123 // Copy the previous row (different address or source)
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003124 Asm->OutStreamer.AddComment("DW_LNS_copy");
3125 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00003126 }
3127 }
3128
Devang Patelc50078e2009-11-21 02:48:08 +00003129 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00003130 }
3131
3132 if (SecSrcLinesSize == 0)
3133 // Because we're emitting a debug_line section, we still need a line
3134 // table. The linker and friends expect it to exist. If there's nothing to
3135 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00003136 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00003137
Chris Lattnerb93558d2010-04-04 19:25:43 +00003138 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00003139}
3140
Devang Patelc50078e2009-11-21 02:48:08 +00003141/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003142///
Devang Patelc50078e2009-11-21 02:48:08 +00003143void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003144 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003145 return;
3146
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003147 int stackGrowth = Asm->getTargetData().getPointerSize();
3148 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3149 TargetFrameInfo::StackGrowsDown)
3150 stackGrowth *= -1;
Bill Wendling55fccda2009-05-20 23:21:38 +00003151
3152 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00003153 Asm->OutStreamer.SwitchSection(
3154 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003155
Chris Lattnerb93558d2010-04-04 19:25:43 +00003156 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003157 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003158 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3159 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003160
Chris Lattnerb93558d2010-04-04 19:25:43 +00003161 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003162 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling55fccda2009-05-20 23:21:38 +00003163 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003164 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling55fccda2009-05-20 23:21:38 +00003165 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003166 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnercc564642010-01-23 03:11:46 +00003167 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner26be1c12010-04-04 19:09:29 +00003168 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3169 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003170 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003171 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling55fccda2009-05-20 23:21:38 +00003172 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling55fccda2009-05-20 23:21:38 +00003173
3174 std::vector<MachineMove> Moves;
3175 RI->getInitialFrameState(Moves);
3176
Chris Lattner193fec02010-04-04 23:41:46 +00003177 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00003178
Chris Lattner7101e232010-04-28 01:05:45 +00003179 Asm->EmitAlignment(2);
Chris Lattnerb93558d2010-04-04 19:25:43 +00003180 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00003181}
3182
Devang Patelc50078e2009-11-21 02:48:08 +00003183/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00003184/// section.
Chris Lattnerdd21da82010-03-13 07:26:18 +00003185void DwarfDebug::
3186emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003187 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003188 return;
3189
3190 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00003191 Asm->OutStreamer.SwitchSection(
3192 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003193
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003194 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattnerdd21da82010-03-13 07:26:18 +00003195 MCSymbol *DebugFrameBegin =
Chris Lattnerb93558d2010-04-04 19:25:43 +00003196 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattnerdd21da82010-03-13 07:26:18 +00003197 MCSymbol *DebugFrameEnd =
Chris Lattnerb93558d2010-04-04 19:25:43 +00003198 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003199 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003200
Chris Lattnerdd21da82010-03-13 07:26:18 +00003201 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling55fccda2009-05-20 23:21:38 +00003202
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003203 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003204 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
3205 DwarfFrameSectionSym);
Bill Wendling55fccda2009-05-20 23:21:38 +00003206
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003207 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerb93558d2010-04-04 19:25:43 +00003208 MCSymbol *FuncBeginSym =
3209 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattner314e3362010-03-13 07:40:56 +00003210 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003211 Asm->getTargetData().getPointerSize(),
3212 0/*AddrSpace*/);
Chris Lattnereb159d62010-03-10 01:17:49 +00003213
3214
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003215 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003216 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003217 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00003218
Chris Lattner193fec02010-04-04 23:41:46 +00003219 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00003220
Chris Lattner7101e232010-04-28 01:05:45 +00003221 Asm->EmitAlignment(2);
Chris Lattnerdd21da82010-03-13 07:26:18 +00003222 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling55fccda2009-05-20 23:21:38 +00003223}
3224
Devang Patelfe0be132009-12-09 18:24:21 +00003225/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3226///
3227void DwarfDebug::emitDebugPubNames() {
Devang Patel7e5a86e2010-05-10 22:49:55 +00003228 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3229 E = CUMap.end(); I != E; ++I) {
3230 CompileUnit *TheCU = I->second;
3231 // Start the dwarf pubnames section.
3232 Asm->OutStreamer.SwitchSection(
3233 Asm->getObjFileLowering().getDwarfPubNamesSection());
Chris Lattnercc564642010-01-23 03:11:46 +00003234
Devang Patel7e5a86e2010-05-10 22:49:55 +00003235 Asm->OutStreamer.AddComment("Length of Public Names Info");
3236 Asm->EmitLabelDifference(
3237 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3238 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
3239
3240 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3241 TheCU->getID()));
3242
3243 Asm->OutStreamer.AddComment("DWARF Version");
3244 Asm->EmitInt16(dwarf::DWARF_VERSION);
3245
3246 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3247 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3248 DwarfInfoSectionSym);
3249
3250 Asm->OutStreamer.AddComment("Compilation Unit Length");
3251 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3252 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3253 4);
3254
3255 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3256 for (StringMap<DIE*>::const_iterator
3257 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3258 const char *Name = GI->getKeyData();
3259 DIE *Entity = GI->second;
3260
3261 Asm->OutStreamer.AddComment("DIE offset");
3262 Asm->EmitInt32(Entity->getOffset());
3263
3264 if (Asm->isVerbose())
3265 Asm->OutStreamer.AddComment("External Name");
3266 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3267 }
3268
3269 Asm->OutStreamer.AddComment("End Mark");
3270 Asm->EmitInt32(0);
3271 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3272 TheCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00003273 }
Bill Wendling55fccda2009-05-20 23:21:38 +00003274}
3275
Devang Patelec13b4f2009-11-24 01:14:22 +00003276void DwarfDebug::emitDebugPubTypes() {
Devang Patel7e5a86e2010-05-10 22:49:55 +00003277 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3278 E = CUMap.end(); I != E; ++I) {
3279 CompileUnit *TheCU = I->second;
3280 // Start the dwarf pubnames section.
3281 Asm->OutStreamer.SwitchSection(
3282 Asm->getObjFileLowering().getDwarfPubTypesSection());
3283 Asm->OutStreamer.AddComment("Length of Public Types Info");
3284 Asm->EmitLabelDifference(
3285 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3286 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Chris Lattnercc564642010-01-23 03:11:46 +00003287
Devang Patel7e5a86e2010-05-10 22:49:55 +00003288 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3289 TheCU->getID()));
3290
3291 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3292 Asm->EmitInt16(dwarf::DWARF_VERSION);
3293
3294 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3295 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3296 DwarfInfoSectionSym);
3297
3298 Asm->OutStreamer.AddComment("Compilation Unit Length");
3299 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3300 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3301 4);
3302
3303 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3304 for (StringMap<DIE*>::const_iterator
3305 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3306 const char *Name = GI->getKeyData();
3307 DIE * Entity = GI->second;
3308
3309 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3310 Asm->EmitInt32(Entity->getOffset());
3311
3312 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3313 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3314 }
3315
3316 Asm->OutStreamer.AddComment("End Mark");
3317 Asm->EmitInt32(0);
3318 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3319 TheCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00003320 }
Devang Patelec13b4f2009-11-24 01:14:22 +00003321}
3322
Devang Patelc50078e2009-11-21 02:48:08 +00003323/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003324///
Devang Patelc50078e2009-11-21 02:48:08 +00003325void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003326 // Check to see if it is worth the effort.
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003327 if (StringPool.empty()) return;
3328
3329 // Start the dwarf str section.
3330 Asm->OutStreamer.SwitchSection(
Chris Lattner73266f92009-08-19 05:49:37 +00003331 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003332
Chris Lattner0f093f82010-03-13 02:17:42 +00003333 // Get all of the string pool entries and put them in an array by their ID so
3334 // we can sort them.
3335 SmallVector<std::pair<unsigned,
3336 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3337
3338 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3339 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3340 Entries.push_back(std::make_pair(I->second.second, &*I));
3341
3342 array_pod_sort(Entries.begin(), Entries.end());
3343
3344 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003345 // Emit a label for reference from debug information entries.
Chris Lattner0f093f82010-03-13 02:17:42 +00003346 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003347
3348 // Emit the string itself.
Chris Lattner0f093f82010-03-13 02:17:42 +00003349 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00003350 }
3351}
3352
Devang Patelc50078e2009-11-21 02:48:08 +00003353/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003354///
Devang Patelc50078e2009-11-21 02:48:08 +00003355void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003356 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00003357 Asm->OutStreamer.SwitchSection(
3358 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003359}
3360
3361/// EmitDebugARanges - Emit visible names into a debug aranges section.
3362///
3363void DwarfDebug::EmitDebugARanges() {
3364 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00003365 Asm->OutStreamer.SwitchSection(
3366 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003367}
3368
Devang Patelc50078e2009-11-21 02:48:08 +00003369/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003370///
Devang Patelc50078e2009-11-21 02:48:08 +00003371void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003372 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00003373 Asm->OutStreamer.SwitchSection(
Devang Patel146c6f72010-04-16 23:33:45 +00003374 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patelce7bf012010-04-27 19:46:33 +00003375 unsigned char Size = Asm->getTargetData().getPointerSize();
3376 for (SmallVector<const MCSymbol *, 8>::iterator
3377 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
3378 I != E; ++I) {
3379 if (*I)
3380 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel146c6f72010-04-16 23:33:45 +00003381 else
Devang Patelce7bf012010-04-27 19:46:33 +00003382 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel146c6f72010-04-16 23:33:45 +00003383 }
Bill Wendling55fccda2009-05-20 23:21:38 +00003384}
3385
Devang Patelc50078e2009-11-21 02:48:08 +00003386/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003387///
Devang Patelc50078e2009-11-21 02:48:08 +00003388void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00003389 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00003390 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00003391 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00003392 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00003393 }
3394}
3395
Devang Patelc50078e2009-11-21 02:48:08 +00003396/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00003397/// Section Header:
3398/// 1. length of section
3399/// 2. Dwarf version number
3400/// 3. address size.
3401///
3402/// Entries (one "entry" for each function that was inlined):
3403///
3404/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3405/// otherwise offset into __debug_str for regular function name.
3406/// 2. offset into __debug_str section for regular function name.
3407/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3408/// instances for the function.
3409///
3410/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3411/// inlined instance; the die_offset points to the inlined_subroutine die in the
3412/// __debug_info section, and the low_pc is the starting address for the
3413/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00003414void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003415 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003416 return;
3417
Devang Patel7e5a86e2010-05-10 22:49:55 +00003418 if (!FirstCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00003419 return;
3420
Chris Lattner73266f92009-08-19 05:49:37 +00003421 Asm->OutStreamer.SwitchSection(
3422 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerad653482010-01-22 22:09:00 +00003423
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003424 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003425 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3426 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003427
Chris Lattnerb93558d2010-04-04 19:25:43 +00003428 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00003429
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003430 Asm->OutStreamer.AddComment("Dwarf Version");
3431 Asm->EmitInt16(dwarf::DWARF_VERSION);
3432 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003433 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00003434
Devang Patel9c371c62010-05-07 20:54:48 +00003435 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patel90a0fe32009-11-10 23:06:00 +00003436 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00003437
Devang Patel9c371c62010-05-07 20:54:48 +00003438 const MDNode *Node = *I;
3439 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbachb23f2422009-11-22 19:20:36 +00003440 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00003441 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00003442 DISubprogram SP(Node);
Devang Patel7f75bbe2009-11-25 17:36:49 +00003443 StringRef LName = SP.getLinkageName();
3444 StringRef Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00003445
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003446 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnercc564642010-01-23 03:11:46 +00003447 if (LName.empty()) {
3448 Asm->OutStreamer.EmitBytes(Name, 0);
3449 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3450 } else
Chris Lattnerc06b4742010-04-04 23:25:33 +00003451 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3452 DwarfStrSectionSym);
Devang Patel90a0fe32009-11-10 23:06:00 +00003453
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003454 Asm->OutStreamer.AddComment("Function name");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003455 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner26be1c12010-04-04 19:09:29 +00003456 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling55fccda2009-05-20 23:21:38 +00003457
Devang Patel90a0fe32009-11-10 23:06:00 +00003458 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00003459 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner97c69b72010-04-04 18:52:31 +00003460 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattneread58652010-03-09 00:31:02 +00003461 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00003462
Chris Lattner97c69b72010-04-04 18:52:31 +00003463 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003464 Asm->OutStreamer.EmitSymbolValue(LI->first,
3465 Asm->getTargetData().getPointerSize(),0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003466 }
3467 }
3468
Chris Lattnerb93558d2010-04-04 19:25:43 +00003469 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00003470}