blob: 71f73a59f57feca08801c467c96945aa93d8df70 [file] [log] [blame]
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework --------------------===//
Jim Laskeye5032892005-12-21 19:48:16 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskeye5032892005-12-21 19:48:16 +00007//
8//===----------------------------------------------------------------------===//
9//
Jim Laskey072200c2007-01-29 18:51:14 +000010// This file contains support for writing dwarf info into asm files.
Jim Laskeye5032892005-12-21 19:48:16 +000011//
12//===----------------------------------------------------------------------===//
Jim Laskey3ea0e0e2006-01-27 18:32:41 +000013
Jim Laskeyb2efb852006-01-04 22:28:25 +000014#include "llvm/CodeGen/DwarfWriter.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000015#include "DIE.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000016#include "DwarfException.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000017#include "DwarfPrinter.h"
Jim Laskey52060a02006-01-24 00:49:18 +000018#include "llvm/Module.h"
Devang Pateld1ca9252009-01-05 23:03:32 +000019#include "llvm/DerivedTypes.h"
Devang Patelcf3a4482009-01-15 23:41:32 +000020#include "llvm/Constants.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000021#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000024#include "llvm/CodeGen/MachineLocation.h"
Devang Patel08f053f2009-01-05 17:57:47 +000025#include "llvm/Analysis/DebugInfo.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000026#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000027#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000028#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000029#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000030#include "llvm/Support/Mangler.h"
Bill Wendling91b8b802009-03-10 20:41:52 +000031#include "llvm/Support/Timer.h"
Owen Andersoncb371882008-08-21 00:14:44 +000032#include "llvm/Support/raw_ostream.h"
Dan Gohman85496362007-09-24 21:32:18 +000033#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000034#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000035#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000036#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000037#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000038#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000039#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000040#include "llvm/Target/TargetOptions.h"
Evan Chenge3d42322009-02-25 07:04:34 +000041#include "llvm/ADT/DenseMap.h"
42#include "llvm/ADT/FoldingSet.h"
43#include "llvm/ADT/StringExtras.h"
44#include "llvm/ADT/StringMap.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000045#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000046#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000047using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000048using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000049
Devang Pateleb3fc282009-01-08 23:40:34 +000050static RegisterPass<DwarfWriter>
51X("dwarfwriter", "DWARF Information Writer");
52char DwarfWriter::ID = 0;
53
Bill Wendling68edf5f2009-03-10 22:58:53 +000054static TimerGroup &getDwarfTimerGroup() {
55 static TimerGroup DwarfTimerGroup("Dwarf Exception and Debugging");
56 return DwarfTimerGroup;
Bill Wendling91b8b802009-03-10 20:41:52 +000057}
58
Jim Laskey0d086af2006-02-27 12:43:29 +000059namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000060
Jim Laskey65195462006-10-30 13:35:07 +000061//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000062
63/// Configuration values for initial hash set sizes (log2).
64///
Bill Wendlingb9dcef22009-02-03 21:17:20 +000065static const unsigned InitDiesSetSize = 9; // log2(512)
66static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
67static const unsigned InitValuesSetSize = 9; // log2(512)
Jim Laskeyef42a012006-11-02 20:12:39 +000068
69//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000070/// CompileUnit - This dwarf writer support class manages information associate
71/// with a source file.
Bill Wendlingeb907212009-05-15 01:12:28 +000072class VISIBILITY_HIDDEN CompileUnit {
Jim Laskeyef42a012006-11-02 20:12:39 +000073 /// ID - File identifier for source.
74 ///
75 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000076
Jim Laskeyef42a012006-11-02 20:12:39 +000077 /// Die - Compile unit debug information entry.
78 ///
79 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000080
Devang Patelbbdc8202009-01-13 23:54:55 +000081 /// GVToDieMap - Tracks the mapping of unit level debug informaton
82 /// variables to debug information entries.
Devang Patelc2997f42009-01-20 00:58:55 +000083 std::map<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +000084
Bill Wendling88423ee2009-05-15 00:11:17 +000085 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
86 /// descriptors to debug information entries using a DIEEntry proxy.
87 std::map<GlobalVariable *, DIEEntry *> GVToDIEEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +000088
89 /// Globals - A map of globally visible named entities for this unit.
90 ///
Bill Wendling972bbac2009-04-09 21:49:15 +000091 StringMap<DIE*> Globals;
Jim Laskeyef42a012006-11-02 20:12:39 +000092
93 /// DiesSet - Used to uniquely define dies within the compile unit.
94 ///
95 FoldingSet<DIE> DiesSet;
Jim Laskey0d086af2006-02-27 12:43:29 +000096public:
Devang Pateld1ca9252009-01-05 23:03:32 +000097 CompileUnit(unsigned I, DIE *D)
Devang Patelbbdc8202009-01-13 23:54:55 +000098 : ID(I), Die(D), GVToDieMap(),
Bill Wendling88423ee2009-05-15 00:11:17 +000099 GVToDIEEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Pateld1ca9252009-01-05 23:03:32 +0000100 {}
101
Jim Laskeyef42a012006-11-02 20:12:39 +0000102 ~CompileUnit() {
103 delete Die;
Jim Laskeyef42a012006-11-02 20:12:39 +0000104 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000105
Jim Laskeybd761842006-02-27 17:27:12 +0000106 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000107 unsigned getID() const { return ID; }
108 DIE* getDie() const { return Die; }
Bill Wendling972bbac2009-04-09 21:49:15 +0000109 StringMap<DIE*> &getGlobals() { return Globals; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000110
111 /// hasContent - Return true if this compile unit has something to write out.
112 ///
113 bool hasContent() const {
114 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000115 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000116
117 /// AddGlobal - Add a new global entity to the compile unit.
118 ///
119 void AddGlobal(const std::string &Name, DIE *Die) {
120 Globals[Name] = Die;
121 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000122
Jim Laskeyef42a012006-11-02 20:12:39 +0000123 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000124 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000125 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
126 return GVToDieMap[GV];
127 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000128
Bill Wendling88423ee2009-05-15 00:11:17 +0000129 /// getDIEEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000130 /// specified debug variable.
Bill Wendling88423ee2009-05-15 00:11:17 +0000131 DIEEntry *&getDIEEntrySlotFor(GlobalVariable *GV) {
132 return GVToDIEEntryMap[GV];
Devang Patel48d190f2009-01-05 21:47:57 +0000133 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000134
Jim Laskeyef42a012006-11-02 20:12:39 +0000135 /// AddDie - Adds or interns the DIE to the compile unit.
136 ///
137 DIE *AddDie(DIE &Buffer) {
138 FoldingSetNodeID ID;
139 Buffer.Profile(ID);
140 void *Where;
141 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000142
Jim Laskeyef42a012006-11-02 20:12:39 +0000143 if (!Die) {
144 Die = new DIE(Buffer);
145 DiesSet.InsertNode(Die, Where);
146 this->Die->AddChild(Die);
147 Buffer.Detach();
148 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000149
Jim Laskeyef42a012006-11-02 20:12:39 +0000150 return Die;
151 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000152};
153
Jim Laskey65195462006-10-30 13:35:07 +0000154//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +0000155/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +0000156///
Bill Wendlingeb907212009-05-15 01:12:28 +0000157class VISIBILITY_HIDDEN SrcLineInfo {
Bill Wendling88423ee2009-05-15 00:11:17 +0000158 unsigned Line; // Source line number.
159 unsigned Column; // Source column.
160 unsigned SourceID; // Source ID number.
161 unsigned LabelID; // Label in code ID number.
Devang Patel9f8fcfc2009-01-08 17:19:22 +0000162public:
163 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendlingb9dcef22009-02-03 21:17:20 +0000164 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000165
Devang Patel9f8fcfc2009-01-08 17:19:22 +0000166 // Accessors
167 unsigned getLine() const { return Line; }
168 unsigned getColumn() const { return Column; }
169 unsigned getSourceID() const { return SourceID; }
170 unsigned getLabelID() const { return LabelID; }
171};
172
Devang Patel9f8fcfc2009-01-08 17:19:22 +0000173//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +0000174/// DbgVariable - This class is used to track local variable information.
175///
Bill Wendlingeb907212009-05-15 01:12:28 +0000176class VISIBILITY_HIDDEN DbgVariable {
Bill Wendling88423ee2009-05-15 00:11:17 +0000177 DIVariable Var; // Variable Descriptor.
Dan Gohman9a38e3e2009-05-07 19:46:24 +0000178 unsigned FrameIndex; // Variable frame index.
Devang Patel7a6e5a32009-01-08 02:33:41 +0000179public:
Devang Patel99ec3532009-01-16 19:28:14 +0000180 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel7a6e5a32009-01-08 02:33:41 +0000181
182 // Accessors.
Devang Patel99ec3532009-01-16 19:28:14 +0000183 DIVariable getVariable() const { return Var; }
Devang Patel7a6e5a32009-01-08 02:33:41 +0000184 unsigned getFrameIndex() const { return FrameIndex; }
185};
186
187//===----------------------------------------------------------------------===//
188/// DbgScope - This class is used to track scope information.
189///
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000190class DbgConcreteScope;
Bill Wendlingeb907212009-05-15 01:12:28 +0000191class VISIBILITY_HIDDEN DbgScope {
Devang Patel7a6e5a32009-01-08 02:33:41 +0000192 DbgScope *Parent; // Parent to this scope.
Devang Patel7103c6a2009-01-16 18:01:58 +0000193 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +0000194 // Either subprogram or block.
195 unsigned StartLabelID; // Label ID of the beginning of scope.
196 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel7103c6a2009-01-16 18:01:58 +0000197 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel9795da52009-01-10 02:42:49 +0000198 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000199 SmallVector<DbgConcreteScope *, 8> ConcreteInsts;// Concrete insts of funcs.
Devang Patel7a6e5a32009-01-08 02:33:41 +0000200public:
Devang Patel0e5200f2009-01-15 18:25:17 +0000201 DbgScope(DbgScope *P, DIDescriptor D)
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000202 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0) {}
203 virtual ~DbgScope();
Devang Patel7a6e5a32009-01-08 02:33:41 +0000204
205 // Accessors.
Devang Patel7103c6a2009-01-16 18:01:58 +0000206 DbgScope *getParent() const { return Parent; }
207 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +0000208 unsigned getStartLabelID() const { return StartLabelID; }
209 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +0000210 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
211 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000212 SmallVector<DbgConcreteScope*,8> &getConcreteInsts() { return ConcreteInsts; }
Devang Patel7a6e5a32009-01-08 02:33:41 +0000213 void setStartLabelID(unsigned S) { StartLabelID = S; }
214 void setEndLabelID(unsigned E) { EndLabelID = E; }
215
216 /// AddScope - Add a scope to the scope.
217 ///
218 void AddScope(DbgScope *S) { Scopes.push_back(S); }
219
220 /// AddVariable - Add a variable to the scope.
221 ///
222 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
Devang Patel1be3ecc2009-04-15 00:10:26 +0000223
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000224 /// AddConcreteInst - Add a concrete instance to the scope.
225 ///
226 void AddConcreteInst(DbgConcreteScope *C) { ConcreteInsts.push_back(C); }
Bill Wendling3f500d92009-05-06 21:21:34 +0000227
228#ifndef NDEBUG
229 void dump() const;
230#endif
Devang Patel1be3ecc2009-04-15 00:10:26 +0000231};
232
Bill Wendling3f500d92009-05-06 21:21:34 +0000233#ifndef NDEBUG
234void DbgScope::dump() const {
235 static unsigned IndentLevel = 0;
236 std::string Indent(IndentLevel, ' ');
237
238 cerr << Indent; Desc.dump();
239 cerr << " [" << StartLabelID << ", " << EndLabelID << "]\n";
240
241 IndentLevel += 2;
242
243 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
244 if (Scopes[i] != this)
245 Scopes[i]->dump();
246
247 IndentLevel -= 2;
248}
249#endif
Devang Patel1be3ecc2009-04-15 00:10:26 +0000250
251//===----------------------------------------------------------------------===//
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000252/// DbgConcreteScope - This class is used to track a scope that holds concrete
253/// instance information.
254///
Bill Wendlingeb907212009-05-15 01:12:28 +0000255class VISIBILITY_HIDDEN DbgConcreteScope : public DbgScope {
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000256 CompileUnit *Unit;
257 DIE *Die; // Debug info for this concrete scope.
Devang Patel1be3ecc2009-04-15 00:10:26 +0000258public:
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000259 DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {}
Devang Patel1be3ecc2009-04-15 00:10:26 +0000260
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000261 // Accessors.
262 DIE *getDie() const { return Die; }
263 void setDie(DIE *D) { Die = D; }
Devang Patel7a6e5a32009-01-08 02:33:41 +0000264};
265
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000266DbgScope::~DbgScope() {
267 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
268 delete Scopes[i];
269 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
270 delete Variables[j];
271 for (unsigned k = 0, O = ConcreteInsts.size(); k < O; ++k)
272 delete ConcreteInsts[k];
273}
274
Devang Patel7a6e5a32009-01-08 02:33:41 +0000275//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000276/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +0000277///
Bill Wendlingeb907212009-05-15 01:12:28 +0000278class VISIBILITY_HIDDEN DwarfDebug : public Dwarf {
Jim Laskey65195462006-10-30 13:35:07 +0000279 //===--------------------------------------------------------------------===//
280 // Attributes used to construct specific Dwarf sections.
281 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000282
Evan Chenge3d42322009-02-25 07:04:34 +0000283 /// CompileUnitMap - A map of global variables representing compile units to
284 /// compile units.
285 DenseMap<Value *, CompileUnit *> CompileUnitMap;
286
287 /// CompileUnits - All the compile units in this module.
288 ///
289 SmallVector<CompileUnit *, 8> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000290
Devang Pateldd9db662009-01-30 18:20:31 +0000291 /// MainCU - Some platform prefers one compile unit per .o file. In such
292 /// cases, all dies are inserted in MainCU.
293 CompileUnit *MainCU;
Bill Wendling9a65cfe2009-02-20 20:40:28 +0000294
Jim Laskeyef42a012006-11-02 20:12:39 +0000295 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +0000296 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000297 FoldingSet<DIEAbbrev> AbbreviationsSet;
298
299 /// Abbreviations - A list of all the unique abbreviations in use.
300 ///
301 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000302
Evan Chenge3d42322009-02-25 07:04:34 +0000303 /// DirectoryIdMap - Directory name to directory id map.
304 ///
305 StringMap<unsigned> DirectoryIdMap;
Devang Patel8526cc02009-01-05 22:35:52 +0000306
Evan Chenge3d42322009-02-25 07:04:34 +0000307 /// DirectoryNames - A list of directory names.
308 SmallVector<std::string, 8> DirectoryNames;
309
310 /// SourceFileIdMap - Source file name to source file id map.
311 ///
312 StringMap<unsigned> SourceFileIdMap;
313
314 /// SourceFileNames - A list of source file names.
315 SmallVector<std::string, 8> SourceFileNames;
316
317 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
318 /// id mapped to a unique id.
319 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
320
321 /// SourceIds - Reverse map from source id to directory id + file id pair.
322 ///
323 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel8526cc02009-01-05 22:35:52 +0000324
Devang Patel07354e52009-01-16 21:07:53 +0000325 /// Lines - List of of source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +0000326 std::vector<SrcLineInfo> Lines;
327
Devang Patel07354e52009-01-16 21:07:53 +0000328 /// ValuesSet - Used to uniquely define values.
329 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000330 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000331
Jim Laskeyef42a012006-11-02 20:12:39 +0000332 /// Values - A list of all the unique values in use.
333 ///
334 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000335
Jim Laskey65195462006-10-30 13:35:07 +0000336 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +0000337 ///
Jim Laskey65195462006-10-30 13:35:07 +0000338 UniqueVector<std::string> StringPool;
339
Jim Laskey65195462006-10-30 13:35:07 +0000340 /// SectionMap - Provides a unique id per text section.
341 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +0000342 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000343
Jim Laskey65195462006-10-30 13:35:07 +0000344 /// SectionSourceLines - Tracks line numbers per text section.
345 ///
Devang Patelf3ee5142009-01-12 22:54:42 +0000346 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +0000347
Jim Laskeybacd3042007-02-21 22:48:45 +0000348 /// didInitial - Flag to indicate if initial emission has been done.
349 ///
350 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000351
Jim Laskeybacd3042007-02-21 22:48:45 +0000352 /// shouldEmit - Flag to indicate if debug information should be emitted.
353 ///
354 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +0000355
Devang Patel7d2f9722009-04-15 20:41:31 +0000356 // FunctionDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +0000357 //
Devang Patel7d2f9722009-04-15 20:41:31 +0000358 DbgScope *FunctionDbgScope;
Devang Patel7a6e5a32009-01-08 02:33:41 +0000359
Bill Wendling163ba3f2009-03-10 21:23:25 +0000360 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +0000361 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +0000362
Bill Wendlingf59d10f2009-05-13 23:55:49 +0000363 /// DbgAbstractScopeMap - Tracks abstract instance scopes in the current
364 /// function.
365 DenseMap<GlobalVariable *, DbgScope *> DbgAbstractScopeMap;
366
367 /// DbgConcreteScopeMap - Tracks concrete instance scopes in the current
368 /// function.
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000369 DenseMap<GlobalVariable *,
Bill Wendlingf59d10f2009-05-13 23:55:49 +0000370 SmallVector<DbgScope *, 8> > DbgConcreteScopeMap;
Devang Patel1be3ecc2009-04-15 00:10:26 +0000371
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000372 /// InlineInfo - Keep track of inlined functions and their location. This
373 /// information is used to populate debug_inlined section.
Devang Patel0f7fef32009-04-13 17:02:03 +0000374 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
375
Devang Patel1be3ecc2009-04-15 00:10:26 +0000376 /// InlinedVariableScopes - Scopes information for the inlined subroutine
377 /// variables.
378 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
379
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000380 /// AbstractInstanceRootMap - Map of abstract instance roots of inlined
381 /// functions. These are subroutine entries that contain a DW_AT_inline
382 /// attribute.
383 DenseMap<const GlobalVariable *, DbgScope *> AbstractInstanceRootMap;
384
385 /// AbstractInstanceRootList - List of abstract instance roots of inlined
386 /// functions. These are subroutine entries that contain a DW_AT_inline
387 /// attribute.
388 SmallVector<DbgScope *, 32> AbstractInstanceRootList;
389
390 /// LexicalScopeStack - A stack of lexical scopes. The top one is the current
391 /// scope.
392 SmallVector<DbgScope *, 16> LexicalScopeStack;
393
Bill Wendlinge688faf2009-05-08 21:03:15 +0000394 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
395 /// used when calculating the "origin" of a concrete instance of an inlined
396 /// function.
397 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
398
Bill Wendling163ba3f2009-03-10 21:23:25 +0000399 /// DebugTimer - Timer for the Dwarf debug writer.
400 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +0000401
Anton Korobeynikov185bc892007-05-13 17:30:11 +0000402 struct FunctionDebugFrameInfo {
403 unsigned Number;
404 std::vector<MachineMove> Moves;
405
406 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +0000407 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +0000408 };
409
410 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000411
Bill Wendling163ba3f2009-03-10 21:23:25 +0000412private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +0000413 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +0000414 /// maps to the source id. Source id starts at 1.
415 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +0000416 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +0000417 return SourceIds[SId-1];
418 }
419
420 /// getNumSourceDirectories - Return the number of source directories in the
421 /// debug info.
422 unsigned getNumSourceDirectories() const {
423 return DirectoryNames.size();
424 }
425
426 /// getSourceDirectoryName - Return the name of the directory corresponding
427 /// to the id.
428 const std::string &getSourceDirectoryName(unsigned Id) const {
429 return DirectoryNames[Id - 1];
430 }
431
432 /// getSourceFileName - Return the name of the source file corresponding
433 /// to the id.
434 const std::string &getSourceFileName(unsigned Id) const {
435 return SourceFileNames[Id - 1];
436 }
437
438 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +0000439 unsigned getNumSourceIds() const {
440 return SourceIds.size();
441 }
442
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000443 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000444 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000445 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
446 // Profile the node so that we can make it unique.
447 FoldingSetNodeID ID;
448 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000449
Jim Laskeyef42a012006-11-02 20:12:39 +0000450 // Check the set for priors.
451 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000452
Jim Laskeyef42a012006-11-02 20:12:39 +0000453 // If it's newly added.
454 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000455 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +0000456 Abbreviations.push_back(&Abbrev);
457 // Assign the vector position + 1 as its number.
458 Abbrev.setNumber(Abbreviations.size());
459 } else {
460 // Assign existing abbreviation number.
461 Abbrev.setNumber(InSet->getNumber());
462 }
463 }
464
Jim Laskey65195462006-10-30 13:35:07 +0000465 /// NewString - Add a string to the constant pool and returns a label.
466 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000467 DWLabel NewString(const std::string &String) {
468 unsigned StringID = StringPool.insert(String);
469 return DWLabel("string", StringID);
470 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000471
Bill Wendling88423ee2009-05-15 00:11:17 +0000472 /// NewDIEEntry - Creates a new DIEEntry to be a proxy for a debug information
Jim Laskeyef42a012006-11-02 20:12:39 +0000473 /// entry.
Bill Wendling88423ee2009-05-15 00:11:17 +0000474 DIEEntry *NewDIEEntry(DIE *Entry = NULL) {
475 DIEEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000476
Jim Laskeyef42a012006-11-02 20:12:39 +0000477 if (Entry) {
478 FoldingSetNodeID ID;
Bill Wendling88423ee2009-05-15 00:11:17 +0000479 DIEEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +0000480 void *Where;
Bill Wendling88423ee2009-05-15 00:11:17 +0000481 Value = static_cast<DIEEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000482
Jim Laskeyf6733882006-11-02 21:48:18 +0000483 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000484
Bill Wendling88423ee2009-05-15 00:11:17 +0000485 Value = new DIEEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +0000486 ValuesSet.InsertNode(Value, Where);
487 } else {
Bill Wendling88423ee2009-05-15 00:11:17 +0000488 Value = new DIEEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +0000489 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000490
Jim Laskeyef42a012006-11-02 20:12:39 +0000491 Values.push_back(Value);
492 return Value;
493 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000494
Bill Wendling88423ee2009-05-15 00:11:17 +0000495 /// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
Jim Laskeyef42a012006-11-02 20:12:39 +0000496 ///
Bill Wendling88423ee2009-05-15 00:11:17 +0000497 void SetDIEEntry(DIEEntry *Value, DIE *Entry) {
Bill Wendlingdd446322009-03-10 23:57:09 +0000498 Value->setEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +0000499 // Add to values set if not already there. If it is, we merely have a
500 // duplicate in the values list (no harm.)
501 ValuesSet.GetOrInsertNode(Value);
502 }
503
504 /// AddUInt - Add an unsigned integer attribute data and value.
505 ///
506 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
507 if (!Form) Form = DIEInteger::BestForm(false, Integer);
508
509 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000510 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +0000511 void *Where;
512 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
513 if (!Value) {
514 Value = new DIEInteger(Integer);
515 ValuesSet.InsertNode(Value, Where);
516 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000517 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000518
Jim Laskeyef42a012006-11-02 20:12:39 +0000519 Die->AddValue(Attribute, Form, Value);
520 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000521
Jim Laskeyef42a012006-11-02 20:12:39 +0000522 /// AddSInt - Add an signed integer attribute data and value.
523 ///
524 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
525 if (!Form) Form = DIEInteger::BestForm(true, Integer);
526
527 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000528 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +0000529 void *Where;
530 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
531 if (!Value) {
532 Value = new DIEInteger(Integer);
533 ValuesSet.InsertNode(Value, Where);
534 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000535 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000536
Jim Laskeyef42a012006-11-02 20:12:39 +0000537 Die->AddValue(Attribute, Form, Value);
538 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000539
Evan Chenge3d42322009-02-25 07:04:34 +0000540 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000541 ///
542 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
543 const std::string &String) {
544 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000545 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +0000546 void *Where;
547 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
548 if (!Value) {
549 Value = new DIEString(String);
550 ValuesSet.InsertNode(Value, Where);
551 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000552 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000553
Jim Laskeyef42a012006-11-02 20:12:39 +0000554 Die->AddValue(Attribute, Form, Value);
555 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000556
Jim Laskeyef42a012006-11-02 20:12:39 +0000557 /// AddLabel - Add a Dwarf label attribute data and value.
558 ///
559 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling88423ee2009-05-15 00:11:17 +0000560 const DWLabel &Label) {
Jim Laskeyef42a012006-11-02 20:12:39 +0000561 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000562 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +0000563 void *Where;
564 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
565 if (!Value) {
566 Value = new DIEDwarfLabel(Label);
567 ValuesSet.InsertNode(Value, Where);
568 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000569 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000570
Jim Laskeyef42a012006-11-02 20:12:39 +0000571 Die->AddValue(Attribute, Form, Value);
572 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000573
Jim Laskeyef42a012006-11-02 20:12:39 +0000574 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
575 ///
576 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
577 const std::string &Label) {
578 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000579 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +0000580 void *Where;
581 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
582 if (!Value) {
583 Value = new DIEObjectLabel(Label);
584 ValuesSet.InsertNode(Value, Where);
585 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000586 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000587
Jim Laskeyef42a012006-11-02 20:12:39 +0000588 Die->AddValue(Attribute, Form, Value);
589 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000590
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000591 /// AddSectionOffset - Add a section offset label attribute data and value.
592 ///
593 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
594 const DWLabel &Label, const DWLabel &Section,
595 bool isEH = false, bool useSet = true) {
596 FoldingSetNodeID ID;
597 DIESectionOffset::Profile(ID, Label, Section);
598 void *Where;
599 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
600 if (!Value) {
601 Value = new DIESectionOffset(Label, Section, isEH, useSet);
602 ValuesSet.InsertNode(Value, Where);
603 Values.push_back(Value);
604 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000605
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000606 Die->AddValue(Attribute, Form, Value);
607 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000608
Jim Laskeyef42a012006-11-02 20:12:39 +0000609 /// AddDelta - Add a label delta attribute data and value.
610 ///
611 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel1be3ecc2009-04-15 00:10:26 +0000612 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyef42a012006-11-02 20:12:39 +0000613 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000614 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +0000615 void *Where;
616 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
617 if (!Value) {
618 Value = new DIEDelta(Hi, Lo);
619 ValuesSet.InsertNode(Value, Where);
620 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000621 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000622
Jim Laskeyef42a012006-11-02 20:12:39 +0000623 Die->AddValue(Attribute, Form, Value);
624 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000625
Bill Wendling88423ee2009-05-15 00:11:17 +0000626 /// AddDIEEntry - Add a DIE attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000627 ///
Bill Wendling88423ee2009-05-15 00:11:17 +0000628 void AddDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
629 Die->AddValue(Attribute, Form, NewDIEEntry(Entry));
Jim Laskeyef42a012006-11-02 20:12:39 +0000630 }
631
632 /// AddBlock - Add block data.
633 ///
634 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000635 Block->ComputeSize(TD);
Jim Laskeyef42a012006-11-02 20:12:39 +0000636 FoldingSetNodeID ID;
637 Block->Profile(ID);
638 void *Where;
639 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
640 if (!Value) {
641 Value = Block;
642 ValuesSet.InsertNode(Value, Where);
643 Values.push_back(Value);
644 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +0000645 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +0000646 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +0000647 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000648 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000649
Jim Laskeyef42a012006-11-02 20:12:39 +0000650 Die->AddValue(Attribute, Block->BestForm(), Value);
651 }
652
Jim Laskey65195462006-10-30 13:35:07 +0000653 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +0000654 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +0000655 void AddSourceLine(DIE *Die, const DIVariable *V) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000656 // If there is no compile unit specified, don't add a line #.
657 if (V->getCompileUnit().isNull())
658 return;
659
Devang Patel7a6e5a32009-01-08 02:33:41 +0000660 unsigned Line = V->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000661 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
662 assert(FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +0000663 AddUInt(Die, DW_AT_decl_file, 0, FileID);
664 AddUInt(Die, DW_AT_decl_line, 0, Line);
665 }
666
667 /// AddSourceLine - Add location information to specified debug information
668 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +0000669 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000670 // If there is no compile unit specified, don't add a line #.
671 if (G->getCompileUnit().isNull())
672 return;
Devang Patel8526cc02009-01-05 22:35:52 +0000673 unsigned Line = G->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000674 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
675 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +0000676 AddUInt(Die, DW_AT_decl_file, 0, FileID);
677 AddUInt(Die, DW_AT_decl_line, 0, Line);
678 }
679
Devang Patel99ec3532009-01-16 19:28:14 +0000680 void AddSourceLine(DIE *Die, const DIType *Ty) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000681 // If there is no compile unit specified, don't add a line #.
Devang Pateldd9db662009-01-30 18:20:31 +0000682 DICompileUnit CU = Ty->getCompileUnit();
683 if (CU.isNull())
684 return;
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000685
686 unsigned Line = Ty->getLineNumber();
687 unsigned FileID = FindCompileUnit(CU).getID();
688 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +0000689 AddUInt(Die, DW_AT_decl_file, 0, FileID);
690 AddUInt(Die, DW_AT_decl_line, 0, Line);
691 }
692
Jim Laskey65195462006-10-30 13:35:07 +0000693 /// AddAddress - Add an address attribute to a die based on the location
694 /// provided.
695 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel1be3ecc2009-04-15 00:10:26 +0000696 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +0000697 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +0000698 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000699
Dan Gohmand735b802008-10-03 15:45:36 +0000700 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +0000701 if (Reg < 32) {
702 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
703 } else {
704 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
705 AddUInt(Block, 0, DW_FORM_udata, Reg);
706 }
707 } else {
708 if (Reg < 32) {
709 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
710 } else {
711 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
712 AddUInt(Block, 0, DW_FORM_udata, Reg);
713 }
714 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
715 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000716
Jim Laskeyef42a012006-11-02 20:12:39 +0000717 AddBlock(Die, Attribute, 0, Block);
718 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000719
Jim Laskeyef42a012006-11-02 20:12:39 +0000720 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +0000721 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +0000722 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +0000723 return;
Devang Patel48d190f2009-01-05 21:47:57 +0000724
725 // Check for pre-existence.
Bill Wendling88423ee2009-05-15 00:11:17 +0000726 DIEEntry *&Slot = DW_Unit->getDIEEntrySlotFor(Ty.getGV());
Devang Patel48d190f2009-01-05 21:47:57 +0000727 // If it exists then use the existing value.
728 if (Slot) {
729 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
730 return;
731 }
732
733 // Set up proxy.
Bill Wendling88423ee2009-05-15 00:11:17 +0000734 Slot = NewDIEEntry();
Devang Patel48d190f2009-01-05 21:47:57 +0000735
736 // Construct type.
737 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +0000738 if (Ty.isBasicType(Ty.getTag()))
739 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
740 else if (Ty.isDerivedType(Ty.getTag()))
741 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
742 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +0000743 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +0000744 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
745 }
746
Devang Patel7009d242009-01-27 23:22:55 +0000747 // Add debug information entry to entity and appropriate context.
748 DIE *Die = NULL;
749 DIDescriptor Context = Ty.getContext();
750 if (!Context.isNull())
751 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
752
753 if (Die) {
754 DIE *Child = new DIE(Buffer);
755 Die->AddChild(Child);
756 Buffer.Detach();
Bill Wendling88423ee2009-05-15 00:11:17 +0000757 SetDIEEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +0000758 } else {
Devang Patel7009d242009-01-27 23:22:55 +0000759 Die = DW_Unit->AddDie(Buffer);
Bill Wendling88423ee2009-05-15 00:11:17 +0000760 SetDIEEntry(Slot, Die);
Devang Patel7009d242009-01-27 23:22:55 +0000761 }
762
Devang Patel48d190f2009-01-05 21:47:57 +0000763 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
764 }
765
Devang Patele5202732009-01-05 19:07:53 +0000766 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
767 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +0000768 DIBasicType BTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +0000769
Devang Patel08f053f2009-01-05 17:57:47 +0000770 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +0000771 std::string Name;
772 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +0000773 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +0000774 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +0000775 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +0000776 if (!Name.empty())
Devang Patel08f053f2009-01-05 17:57:47 +0000777 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +0000778 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +0000779 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
780 }
781
Devang Patele5202732009-01-05 19:07:53 +0000782 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
783 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +0000784 DIDerivedType DTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +0000785
Devang Patel08f053f2009-01-05 17:57:47 +0000786 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +0000787 std::string Name;
788 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +0000789 uint64_t Size = DTy.getSizeInBits() >> 3;
790 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000791
Devang Patel08f053f2009-01-05 17:57:47 +0000792 // FIXME - Workaround for templates.
793 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
794
795 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000796
Devang Patel08f053f2009-01-05 17:57:47 +0000797 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +0000798 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +0000799 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +0000800
801 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +0000802 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +0000803 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +0000804
805 // Add size if non-zero (derived types might be zero-sized.)
806 if (Size)
807 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
808
809 // Add source line info if available and TyDesc is not a forward
810 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +0000811 if (!DTy.isForwardDecl())
812 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +0000813 }
814
Devang Patelf4215332009-01-05 19:55:51 +0000815 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
816 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +0000817 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +0000818 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +0000819 std::string Name;
820 CTy.getName(Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000821
Devang Patelf193ff02009-01-15 19:26:23 +0000822 uint64_t Size = CTy.getSizeInBits() >> 3;
823 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +0000824 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000825
Devang Patelf4215332009-01-05 19:55:51 +0000826 switch (Tag) {
827 case DW_TAG_vector_type:
828 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +0000829 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +0000830 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +0000831 case DW_TAG_enumeration_type:
832 {
833 DIArray Elements = CTy.getTypeArray();
834 // Add enumerators to enumeration type.
835 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
836 DIE *ElemDie = NULL;
837 DIEnumerator Enum(Elements.getElement(i).getGV());
838 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
839 Buffer.AddChild(ElemDie);
840 }
841 }
842 break;
Devang Patelf4215332009-01-05 19:55:51 +0000843 case DW_TAG_subroutine_type:
844 {
Devang Patelf4215332009-01-05 19:55:51 +0000845 // Add return type.
Bill Wendling3f500d92009-05-06 21:21:34 +0000846 DIArray Elements = CTy.getTypeArray();
Devang Patel48d190f2009-01-05 21:47:57 +0000847 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +0000848 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +0000849
Bill Wendling3f500d92009-05-06 21:21:34 +0000850 // Add prototype flag.
851 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
852
Devang Patelf4215332009-01-05 19:55:51 +0000853 // Add arguments.
854 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
855 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +0000856 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +0000857 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +0000858 Buffer.AddChild(Arg);
859 }
860 }
861 break;
862 case DW_TAG_structure_type:
863 case DW_TAG_union_type:
Devang Patel86bda412009-03-25 00:28:40 +0000864 case DW_TAG_class_type:
Devang Patelf4215332009-01-05 19:55:51 +0000865 {
866 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +0000867 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +0000868
869 // A forward struct declared type may not have elements available.
870 if (Elements.isNull())
871 break;
872
Devang Patelf4215332009-01-05 19:55:51 +0000873 // Add elements to structure type.
874 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
875 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +0000876 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +0000877 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +0000878 ElemDie = CreateSubprogramDIE(DW_Unit,
879 DISubprogram(Element.getGV()));
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000880 else if (Element.getTag() == dwarf::DW_TAG_variable) // ??
Devang Patel5aac3d32009-01-17 08:01:33 +0000881 ElemDie = CreateGlobalVariableDIE(DW_Unit,
882 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +0000883 else
884 ElemDie = CreateMemberDIE(DW_Unit,
885 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +0000886 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +0000887 }
Mike Stumpa6815152009-05-14 18:45:49 +0000888
889 // FIXME: We'd like an API to register additional attributes for the
890 // frontend to use while synthesizing, and then we'd use that api in
891 // clang instead of this.
892 if (Name == "__block_literal_generic")
893 AddUInt(&Buffer, DW_AT_APPLE_block, DW_FORM_flag, 1);
894
Devang Patel13319ce2009-02-17 22:43:44 +0000895 unsigned RLang = CTy.getRunTimeLang();
896 if (RLang)
897 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +0000898 }
899 break;
900 default:
901 break;
902 }
903
904 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +0000905 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +0000906 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +0000907
Devang Patelad165be2009-01-27 00:45:04 +0000908 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
909 || Tag == DW_TAG_union_type) {
910 // Add size if non-zero (derived types might be zero-sized.)
911 if (Size)
912 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
913 else {
914 // Add zero size if it is not a forward declaration.
915 if (CTy.isForwardDecl())
916 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
917 else
918 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
919 }
920
921 // Add source line info if available.
922 if (!CTy.isForwardDecl())
923 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +0000924 }
Devang Patelf4215332009-01-05 19:55:51 +0000925 }
926
Bill Wendlingb9dcef22009-02-03 21:17:20 +0000927 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
928 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +0000929 int64_t L = SR.getLo();
930 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +0000931 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
932 if (L != H) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000933 AddDIEEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +0000934 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +0000935 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
936 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +0000937 }
938 Buffer.AddChild(DW_Subrange);
939 }
940
941 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
942 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
943 DICompositeType *CTy) {
944 Buffer.setTag(DW_TAG_array_type);
945 if (CTy->getTag() == DW_TAG_vector_type)
946 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
947
Devang Patelf9235742009-01-28 21:08:20 +0000948 // Emit derived type.
949 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +0000950 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +0000951
952 // Construct an anonymous type for index type.
953 DIE IdxBuffer(DW_TAG_base_type);
954 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
955 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
956 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
957
958 // Add subranges to array type.
959 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +0000960 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +0000961 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
962 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +0000963 }
964 }
965
Bill Wendlingb9dcef22009-02-03 21:17:20 +0000966 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +0000967 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +0000968
969 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling0582ae92009-03-13 04:39:26 +0000970 std::string Name;
971 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +0000972 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +0000973 int64_t Value = ETy->getEnumValue();
974 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +0000975 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +0000976 }
Devang Patel68afdc32009-01-05 18:33:01 +0000977
Devang Patel5aac3d32009-01-17 08:01:33 +0000978 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +0000979 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +0000980 {
981 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling0582ae92009-03-13 04:39:26 +0000982 std::string Name;
983 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +0000984 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +0000985 std::string LinkageName;
986 GV.getLinkageName(LinkageName);
987 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +0000988 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
989 AddType(DW_Unit, GVDie, GV.getType());
990 if (!GV.isLocalToUnit())
991 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
992 AddSourceLine(GVDie, &GV);
993 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +0000994 }
995
Devang Patel2be58932009-01-20 21:02:02 +0000996 /// CreateMemberDIE - Create new member DIE.
997 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
998 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling0582ae92009-03-13 04:39:26 +0000999 std::string Name;
1000 DT.getName(Name);
1001 if (!Name.empty())
Devang Patel2be58932009-01-20 21:02:02 +00001002 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1003
1004 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1005
1006 AddSourceLine(MemberDie, &DT);
1007
Devang Patel36375ee2009-02-17 21:23:59 +00001008 uint64_t Size = DT.getSizeInBits();
1009 uint64_t FieldSize = DT.getOriginalTypeSize();
1010
1011 if (Size != FieldSize) {
1012 // Handle bitfield.
1013 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1014 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1015
1016 uint64_t Offset = DT.getOffsetInBits();
1017 uint64_t FieldOffset = Offset;
1018 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1019 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1020 FieldOffset = (HiMark - FieldSize);
1021 Offset -= FieldOffset;
1022 // Maybe we need to work from the other end.
1023 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1024 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1025 }
Devang Patel2be58932009-01-20 21:02:02 +00001026 DIEBlock *Block = new DIEBlock();
1027 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1028 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1029 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1030
Devang Patel47661592009-01-21 00:08:04 +00001031 if (DT.isProtected())
1032 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1033 else if (DT.isPrivate())
1034 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1035
Devang Patel2be58932009-01-20 21:02:02 +00001036 return MemberDie;
1037 }
1038
Devang Patel5aac3d32009-01-17 08:01:33 +00001039 /// CreateSubprogramDIE - Create new DIE using SP.
1040 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendling3f500d92009-05-06 21:21:34 +00001041 const DISubprogram &SP,
Devang Patel2d1768c2009-01-17 08:05:14 +00001042 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001043 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling3f500d92009-05-06 21:21:34 +00001044
Bill Wendling0582ae92009-03-13 04:39:26 +00001045 std::string Name;
1046 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001047 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling3f500d92009-05-06 21:21:34 +00001048
Bill Wendling0582ae92009-03-13 04:39:26 +00001049 std::string LinkageName;
1050 SP.getLinkageName(LinkageName);
Bill Wendling3f500d92009-05-06 21:21:34 +00001051
Bill Wendling0582ae92009-03-13 04:39:26 +00001052 if (!LinkageName.empty())
Bill Wendling3f500d92009-05-06 21:21:34 +00001053 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1054
Devang Patel5aac3d32009-01-17 08:01:33 +00001055 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001056
Devang Patel5aac3d32009-01-17 08:01:33 +00001057 DICompositeType SPTy = SP.getType();
1058 DIArray Args = SPTy.getTypeArray();
Bill Wendling3f500d92009-05-06 21:21:34 +00001059
1060 // Add prototyped tag, if C or ObjC.
1061 unsigned Lang = SP.getCompileUnit().getLanguage();
1062 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89 || Lang == DW_LANG_ObjC)
1063 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patel5aac3d32009-01-17 08:01:33 +00001064
Devang Patel86ae1422009-01-05 18:59:44 +00001065 // Add Return Type.
Devang Patel75b27382009-04-08 22:18:45 +00001066 unsigned SPTag = SPTy.getTag();
Devang Patel9ac08d62009-02-27 18:05:21 +00001067 if (!IsConstructor) {
Devang Patel75b27382009-04-08 22:18:45 +00001068 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel9ac08d62009-02-27 18:05:21 +00001069 AddType(DW_Unit, SPDie, SPTy);
1070 else
1071 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1072 }
Devang Pateld234e592009-01-30 01:21:46 +00001073
Devang Pateld5863dd2009-02-02 17:51:41 +00001074 if (!SP.isDefinition()) {
1075 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00001076 // Add arguments. Do not add arguments for subprogram definition. They
1077 // will be handled through RecordVariable.
Devang Patel75b27382009-04-08 22:18:45 +00001078 if (SPTag == DW_TAG_subroutine_type)
Devang Pateld5863dd2009-02-02 17:51:41 +00001079 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1080 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1081 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
Bill Wendling3f500d92009-05-06 21:21:34 +00001082 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ??
Devang Pateld5863dd2009-02-02 17:51:41 +00001083 SPDie->AddChild(Arg);
1084 }
1085 }
Devang Pateld234e592009-01-30 01:21:46 +00001086
Devang Patelf193ff02009-01-15 19:26:23 +00001087 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00001088 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00001089
Devang Patel1be3ecc2009-04-15 00:10:26 +00001090 // DW_TAG_inlined_subroutine may refer to this DIE.
1091 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV());
1092 Slot = SPDie;
Devang Patel5aac3d32009-01-17 08:01:33 +00001093 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001094 }
1095
Devang Patel5aac3d32009-01-17 08:01:33 +00001096 /// FindCompileUnit - Get the compile unit for the given descriptor.
1097 ///
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001098 CompileUnit &FindCompileUnit(DICompileUnit Unit) const {
1099 DenseMap<Value *, CompileUnit *>::const_iterator I =
1100 CompileUnitMap.find(Unit.getGV());
1101 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1102 return *I->second;
Devang Patel8526cc02009-01-05 22:35:52 +00001103 }
1104
Devang Patelbbdc8202009-01-13 23:54:55 +00001105 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001106 ///
1107 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1108 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00001109 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001110
1111 // Translate tag to proper Dwarf tag. The result variable is dropped for
1112 // now.
1113 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00001114 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001115 case DW_TAG_return_variable: return NULL;
1116 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1117 case DW_TAG_auto_variable: // fall thru
1118 default: Tag = DW_TAG_variable; break;
1119 }
1120
1121 // Define variable debug information entry.
1122 DIE *VariableDie = new DIE(Tag);
Bill Wendling0582ae92009-03-13 04:39:26 +00001123 std::string Name;
1124 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001125 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001126
1127 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00001128 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001129
1130 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00001131 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001132
1133 // Add variable address.
1134 MachineLocation Location;
1135 Location.set(RI->getFrameRegister(*MF),
1136 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1137 AddAddress(VariableDie, DW_AT_location, Location);
1138
1139 return VariableDie;
1140 }
1141
Devang Patel7a6e5a32009-01-08 02:33:41 +00001142 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1143 ///
1144 DbgScope *getOrCreateScope(GlobalVariable *V) {
1145 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001146 if (Slot) return Slot;
1147
Devang Patel1be3ecc2009-04-15 00:10:26 +00001148 DbgScope *Parent = NULL;
1149 DIBlock Block(V);
Bill Wendling3f500d92009-05-06 21:21:34 +00001150
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001151 // Don't create a new scope if we already created one for an inlined
1152 // function.
1153 DenseMap<const GlobalVariable *, DbgScope *>::iterator
1154 II = AbstractInstanceRootMap.find(V);
1155 if (II != AbstractInstanceRootMap.end())
1156 return LexicalScopeStack.back();
1157
Devang Patel1be3ecc2009-04-15 00:10:26 +00001158 if (!Block.isNull()) {
1159 DIDescriptor ParentDesc = Block.getContext();
1160 Parent =
1161 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001162 }
Bill Wendling3f500d92009-05-06 21:21:34 +00001163
Devang Patel1be3ecc2009-04-15 00:10:26 +00001164 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001165
Devang Patel1be3ecc2009-04-15 00:10:26 +00001166 if (Parent)
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001167 Parent->AddScope(Slot);
Devang Patel1be3ecc2009-04-15 00:10:26 +00001168 else
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001169 // First function is top level function.
Devang Patel7d2f9722009-04-15 20:41:31 +00001170 FunctionDbgScope = Slot;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001171
Devang Patel7a6e5a32009-01-08 02:33:41 +00001172 return Slot;
1173 }
1174
1175 /// ConstructDbgScope - Construct the components of a scope.
1176 ///
1177 void ConstructDbgScope(DbgScope *ParentScope,
1178 unsigned ParentStartID, unsigned ParentEndID,
1179 DIE *ParentDie, CompileUnit *Unit) {
Dan Gohman9a38e3e2009-05-07 19:46:24 +00001180 // Add variables to scope.
1181 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
1182 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1183 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
1184 if (VariableDie) ParentDie->AddChild(VariableDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001185 }
1186
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001187 // Add concrete instances to scope.
1188 SmallVector<DbgConcreteScope *, 8> &ConcreteInsts = ParentScope->getConcreteInsts();
1189 for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) {
1190 DbgConcreteScope *ConcreteInst = ConcreteInsts[i];
1191 DIE *Die = ConcreteInst->getDie();
1192
1193 unsigned StartID = ConcreteInst->getStartLabelID();
1194 unsigned EndID = ConcreteInst->getEndLabelID();
1195
1196 // Add the scope bounds.
1197 if (StartID)
1198 AddLabel(Die, DW_AT_low_pc, DW_FORM_addr,
1199 DWLabel("label", StartID));
1200 else
1201 AddLabel(Die, DW_AT_low_pc, DW_FORM_addr,
1202 DWLabel("func_begin", SubprogramCount));
1203
1204 if (EndID)
1205 AddLabel(Die, DW_AT_high_pc, DW_FORM_addr,
1206 DWLabel("label", EndID));
1207 else
1208 AddLabel(Die, DW_AT_high_pc, DW_FORM_addr,
1209 DWLabel("func_end", SubprogramCount));
1210
1211 ParentDie->AddChild(Die);
1212 }
1213
Devang Patel7a6e5a32009-01-08 02:33:41 +00001214 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00001215 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001216 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1217 // Define the Scope debug information entry.
1218 DbgScope *Scope = Scopes[j];
Devang Patel7a6e5a32009-01-08 02:33:41 +00001219
Devang Patele9bfb0f2009-01-12 18:41:00 +00001220 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1221 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001222
Devang Patel1be3ecc2009-04-15 00:10:26 +00001223 // Ignore empty scopes.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001224 if (StartID == EndID && StartID != 0) continue;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001225
1226 // Do not ignore inlined scopes even if they don't have any variables or
1227 // scopes.
1228 if (Scope->getScopes().empty() && Scope->getVariables().empty() &&
1229 Scope->getConcreteInsts().empty())
Devang Patel0f7fef32009-04-13 17:02:03 +00001230 continue;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001231
1232 if (StartID == ParentStartID && EndID == ParentEndID) {
1233 // Just add stuff to the parent scope.
1234 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
1235 } else {
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001236 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Bill Wendling7fa81622009-05-01 08:25:13 +00001237
1238 // Add the scope bounds.
1239 if (StartID)
1240 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1241 DWLabel("label", StartID));
1242 else
1243 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1244 DWLabel("func_begin", SubprogramCount));
1245
1246 if (EndID)
1247 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1248 DWLabel("label", EndID));
1249 else
1250 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1251 DWLabel("func_end", SubprogramCount));
1252
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001253 // Add the scope's contents.
Bill Wendling7fa81622009-05-01 08:25:13 +00001254 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
1255 ParentDie->AddChild(ScopeDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001256 }
1257 }
1258 }
1259
Devang Patel7d2f9722009-04-15 20:41:31 +00001260 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001261 ///
Devang Patel7d2f9722009-04-15 20:41:31 +00001262 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001263 // Exit if there is no root scope.
1264 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00001265 DIDescriptor Desc = RootScope->getDesc();
1266 if (Desc.isNull())
1267 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001268
1269 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00001270 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001271
1272 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00001273 CompileUnit *Unit = MainCU;
1274 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001275 Unit = &FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001276
1277 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00001278 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001279 assert(SPDie && "Missing subprogram descriptor");
1280
1281 // Add the function bounds.
1282 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1283 DWLabel("func_begin", SubprogramCount));
1284 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1285 DWLabel("func_end", SubprogramCount));
1286 MachineLocation Location(RI->getFrameRegister(*MF));
1287 AddAddress(SPDie, DW_AT_frame_base, Location);
1288
1289 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
1290 }
1291
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001292 /// ConstructFunctionDbgScope - Construct the scope for the abstract debug
1293 /// scope.
1294 ///
1295 void ConstructAbstractDbgScope(DbgScope *AbsScope) {
1296 // Exit if there is no root scope.
1297 if (!AbsScope) return;
1298
1299 DIDescriptor Desc = AbsScope->getDesc();
1300 if (Desc.isNull())
1301 return;
1302
1303 // Get the subprogram debug information entry.
1304 DISubprogram SPD(Desc.getGV());
1305
1306 // Get the compile unit context.
1307 CompileUnit *Unit = MainCU;
1308 if (!Unit)
1309 Unit = &FindCompileUnit(SPD.getCompileUnit());
1310
1311 // Get the subprogram die.
1312 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
1313 assert(SPDie && "Missing subprogram descriptor");
1314
1315 ConstructDbgScope(AbsScope, 0, 0, SPDie, Unit);
1316 }
1317
Devang Patel7a6e5a32009-01-08 02:33:41 +00001318 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
1319 ///
1320 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00001321 const char *FnName = MF->getFunction()->getNameStart();
1322 if (MainCU) {
Bill Wendling972bbac2009-04-09 21:49:15 +00001323 StringMap<DIE*> &Globals = MainCU->getGlobals();
1324 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00001325 if (GI != Globals.end()) {
1326 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001327
1328 // Add the function bounds.
1329 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1330 DWLabel("func_begin", SubprogramCount));
1331 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1332 DWLabel("func_end", SubprogramCount));
1333
1334 MachineLocation Location(RI->getFrameRegister(*MF));
1335 AddAddress(SPDie, DW_AT_frame_base, Location);
1336 return;
1337 }
Evan Chenge3d42322009-02-25 07:04:34 +00001338 } else {
1339 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
1340 CompileUnit *Unit = CompileUnits[i];
Bill Wendling972bbac2009-04-09 21:49:15 +00001341 StringMap<DIE*> &Globals = Unit->getGlobals();
1342 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00001343 if (GI != Globals.end()) {
1344 DIE *SPDie = GI->second;
1345
1346 // Add the function bounds.
1347 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1348 DWLabel("func_begin", SubprogramCount));
1349 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1350 DWLabel("func_end", SubprogramCount));
1351
1352 MachineLocation Location(RI->getFrameRegister(*MF));
1353 AddAddress(SPDie, DW_AT_frame_base, Location);
1354 return;
1355 }
1356 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001357 }
Evan Chenge3d42322009-02-25 07:04:34 +00001358
Devang Patel7a6e5a32009-01-08 02:33:41 +00001359#if 0
1360 // FIXME: This is causing an abort because C++ mangled names are compared
1361 // with their unmangled counterparts. See PR2885. Don't do this assert.
1362 assert(0 && "Couldn't find DIE for machine function!");
1363#endif
1364 }
1365
Jim Laskeyef42a012006-11-02 20:12:39 +00001366 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1367 /// tools to recognize the object file contains Dwarf information.
1368 void EmitInitial() {
1369 // Check to see if we already emitted intial headers.
1370 if (didInitial) return;
1371 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001372
Jim Laskeyef42a012006-11-02 20:12:39 +00001373 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00001374 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001375 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001376 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00001377 }
1378 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1379 EmitLabel("section_info", 0);
1380 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1381 EmitLabel("section_abbrev", 0);
1382 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1383 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00001384 if (TAI->doesSupportMacInfoSection()) {
1385 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1386 EmitLabel("section_macinfo", 0);
1387 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001388 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1389 EmitLabel("section_line", 0);
1390 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1391 EmitLabel("section_loc", 0);
1392 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1393 EmitLabel("section_pubnames", 0);
1394 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1395 EmitLabel("section_str", 0);
1396 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1397 EmitLabel("section_ranges", 0);
1398
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001399 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00001400 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00001401 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00001402 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00001403 }
1404
Jim Laskey65195462006-10-30 13:35:07 +00001405 /// EmitDIE - Recusively Emits a debug information entry.
1406 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001407 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001408 // Get the abbreviation for this DIE.
1409 unsigned AbbrevNumber = Die->getAbbrevNumber();
1410 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001411
Jim Laskeybacd3042007-02-21 22:48:45 +00001412 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001413
1414 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001415 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00001416
Evan Cheng42bf74b2009-03-25 01:47:28 +00001417 if (Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00001418 Asm->EOL(std::string("Abbrev [" +
1419 utostr(AbbrevNumber) +
1420 "] 0x" + utohexstr(Die->getOffset()) +
1421 ":0x" + utohexstr(Die->getSize()) + " " +
1422 TagString(Abbrev->getTag())));
1423 else
1424 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001425
Owen Anderson873e1b52008-06-24 21:44:59 +00001426 SmallVector<DIEValue*, 32> &Values = Die->getValues();
1427 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001428
Jim Laskeyef42a012006-11-02 20:12:39 +00001429 // Emit the DIE attribute values.
1430 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1431 unsigned Attr = AbbrevData[i].getAttribute();
1432 unsigned Form = AbbrevData[i].getForm();
1433 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001434
Jim Laskeyef42a012006-11-02 20:12:39 +00001435 switch (Attr) {
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001436 case DW_AT_sibling:
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001437 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00001438 break;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001439 case DW_AT_abstract_origin: {
Bill Wendling88423ee2009-05-15 00:11:17 +00001440 DIEEntry *E = cast<DIEEntry>(Values[i]);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001441 DIE *Origin = E->getEntry();
1442 unsigned Addr =
1443 CompileUnitOffsets[Die->getAbstractCompileUnit()] +
1444 Origin->getOffset();
1445
1446 Asm->EmitInt32(Addr);
1447 break;
Jim Laskeyef42a012006-11-02 20:12:39 +00001448 }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001449 default:
Jim Laskeyef42a012006-11-02 20:12:39 +00001450 // Emit an attribute using the defined form.
Bill Wendling88423ee2009-05-15 00:11:17 +00001451 Values[i]->EmitValue(this, Form);
Jim Laskeyef42a012006-11-02 20:12:39 +00001452 break;
1453 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001454
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001455 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00001456 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001457
Jim Laskeyef42a012006-11-02 20:12:39 +00001458 // Emit the DIE children if any.
1459 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
1460 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001461
Bill Wendlinga9519572009-05-08 20:38:02 +00001462 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Jim Laskeyef42a012006-11-02 20:12:39 +00001463 EmitDIE(Children[j]);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001464
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001465 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00001466 }
1467 }
1468
Jim Laskey65195462006-10-30 13:35:07 +00001469 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1470 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001471 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1472 // Get the children.
1473 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001474
Jim Laskeyef42a012006-11-02 20:12:39 +00001475 // If not last sibling and has children then add sibling offset attribute.
1476 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1477
1478 // Record the abbreviation.
1479 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001480
Jim Laskeyef42a012006-11-02 20:12:39 +00001481 // Get the abbreviation for this DIE.
1482 unsigned AbbrevNumber = Die->getAbbrevNumber();
1483 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1484
1485 // Set DIE offset
1486 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001487
Jim Laskeyef42a012006-11-02 20:12:39 +00001488 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001489 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
1490
Owen Anderson873e1b52008-06-24 21:44:59 +00001491 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1492 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00001493
1494 // Size the DIE attribute values.
1495 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1496 // Size attribute value.
Bill Wendling88423ee2009-05-15 00:11:17 +00001497 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00001498 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001499
Jim Laskeyef42a012006-11-02 20:12:39 +00001500 // Size the DIE children if any.
1501 if (!Children.empty()) {
1502 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
1503 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001504
Jim Laskeyef42a012006-11-02 20:12:39 +00001505 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1506 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
1507 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001508
Jim Laskeyef42a012006-11-02 20:12:39 +00001509 // End of children marker.
1510 Offset += sizeof(int8_t);
1511 }
1512
1513 Die->setSize(Offset - Die->getOffset());
1514 return Offset;
1515 }
Jim Laskey65195462006-10-30 13:35:07 +00001516
1517 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1518 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001519 void SizeAndOffsets() {
Bill Wendlinge688faf2009-05-08 21:03:15 +00001520 // Compute size of compile unit header.
1521 static unsigned Offset =
1522 sizeof(int32_t) + // Length of Compilation Unit Info
1523 sizeof(int16_t) + // DWARF version number
1524 sizeof(int32_t) + // Offset Into Abbrev. Section
1525 sizeof(int8_t); // Pointer Size (in bytes)
1526
Jim Laskey5496f012006-11-09 14:52:14 +00001527 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00001528 if (MainCU) {
Devang Pateldd9db662009-01-30 18:20:31 +00001529 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
Bill Wendlinge688faf2009-05-08 21:03:15 +00001530 CompileUnitOffsets[MainCU] = 0;
Devang Pateldd9db662009-01-30 18:20:31 +00001531 return;
1532 }
Bill Wendlinge688faf2009-05-08 21:03:15 +00001533
1534 // Process all compile units.
1535 unsigned PrevOffset = 0;
1536
Evan Chenge3d42322009-02-25 07:04:34 +00001537 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
1538 CompileUnit *Unit = CompileUnits[i];
Bill Wendlinge688faf2009-05-08 21:03:15 +00001539 CompileUnitOffsets[Unit] = PrevOffset;
1540 PrevOffset += SizeAndOffsetDie(Unit->getDie(), Offset, true)
1541 + sizeof(int32_t); // FIXME - extra pad for gdb bug.
Devang Patel72b66352009-01-12 23:05:55 +00001542 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001543 }
1544
Evan Chenge3d42322009-02-25 07:04:34 +00001545 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00001546 ///
Evan Chenge3d42322009-02-25 07:04:34 +00001547 void EmitDebugInfoPerCU(CompileUnit *Unit) {
1548 DIE *Die = Unit->getDie();
1549 // Emit the compile units header.
1550 EmitLabel("info_begin", Unit->getID());
1551 // Emit size of content not including length itself
1552 unsigned ContentSize = Die->getSize() +
1553 sizeof(int16_t) + // DWARF version number
1554 sizeof(int32_t) + // Offset Into Abbrev. Section
1555 sizeof(int8_t) + // Pointer Size (in bytes)
1556 sizeof(int32_t); // FIXME - extra pad for gdb bug.
1557
1558 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
1559 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
1560 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
1561 Asm->EOL("Offset Into Abbrev. Section");
1562 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
1563
1564 EmitDIE(Die);
1565 // FIXME - extra padding for gdb bug.
1566 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
1567 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
1568 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
1569 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
1570 EmitLabel("info_end", Unit->getID());
1571
1572 Asm->EOL();
1573 }
1574
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001575 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00001576 // Start debug info section.
1577 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001578
Evan Chenge3d42322009-02-25 07:04:34 +00001579 if (MainCU) {
1580 EmitDebugInfoPerCU(MainCU);
1581 return;
Devang Patel72b66352009-01-12 23:05:55 +00001582 }
Evan Chenge3d42322009-02-25 07:04:34 +00001583
1584 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
1585 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00001586 }
1587
Jim Laskey65195462006-10-30 13:35:07 +00001588 /// EmitAbbreviations - Emit the abbreviation section.
1589 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001590 void EmitAbbreviations() const {
1591 // Check to see if it is worth the effort.
1592 if (!Abbreviations.empty()) {
1593 // Start the debug abbrev section.
1594 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001595
Jim Laskeyef42a012006-11-02 20:12:39 +00001596 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001597
Jim Laskeyef42a012006-11-02 20:12:39 +00001598 // For each abbrevation.
1599 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1600 // Get abbreviation data
1601 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001602
Jim Laskeyef42a012006-11-02 20:12:39 +00001603 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001604 Asm->EmitULEB128Bytes(Abbrev->getNumber());
1605 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001606
Jim Laskeyef42a012006-11-02 20:12:39 +00001607 // Emit the abbreviations data.
Bill Wendling88423ee2009-05-15 00:11:17 +00001608 Abbrev->Emit(Asm);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001609
Jim Laskeybacd3042007-02-21 22:48:45 +00001610 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001611 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001612
Jim Laskey7b1b39d2007-02-22 18:22:42 +00001613 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00001614 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00001615
Jim Laskeyef42a012006-11-02 20:12:39 +00001616 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001617
Jim Laskeybacd3042007-02-21 22:48:45 +00001618 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001619 }
1620 }
1621
Bill Wendlingfbbd7012008-07-20 00:11:19 +00001622 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
1623 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001624 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00001625 void EmitEndOfLineMatrix(unsigned SectionEnd) {
1626 // Define last address of section.
1627 Asm->EmitInt8(0); Asm->EOL("Extended Op");
1628 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
1629 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
1630 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
1631
1632 // Mark end of matrix.
1633 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
1634 Asm->EmitULEB128Bytes(1); Asm->EOL();
1635 Asm->EmitInt8(1); Asm->EOL();
1636 }
1637
Jim Laskey65195462006-10-30 13:35:07 +00001638 /// EmitDebugLines - Emit source line information.
1639 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001640 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00001641 // If the target is using .loc/.file, the assembler will be emitting the
1642 // .debug_line table automatically.
1643 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00001644 return;
1645
Jim Laskeyef42a012006-11-02 20:12:39 +00001646 // Minimum line delta, thus ranging from -10..(255-10).
1647 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1648 // Maximum line delta, thus ranging from -10..(255-10).
1649 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00001650
Jim Laskeyef42a012006-11-02 20:12:39 +00001651 // Start the dwarf line section.
1652 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001653
Jim Laskeyef42a012006-11-02 20:12:39 +00001654 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001655
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001656 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001657 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00001658 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001659
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001660 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001661
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001662 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001663 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00001664 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001665
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001666 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00001667
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001668 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00001669
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001670 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001671
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001672 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00001673
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001674 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001675
Jim Laskeyef42a012006-11-02 20:12:39 +00001676 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001677 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
1678 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
1679 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
1680 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
1681 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
1682 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
1683 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
1684 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
1685 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00001686
Jim Laskeyef42a012006-11-02 20:12:39 +00001687 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00001688 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
1689 Asm->EmitString(getSourceDirectoryName(DI));
1690 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00001691 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001692 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001693
Jim Laskeyef42a012006-11-02 20:12:39 +00001694 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00001695 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
1696 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001697 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00001698 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001699 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00001700 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001701 Asm->EOL("Directory #");
1702 Asm->EmitULEB128Bytes(0);
1703 Asm->EOL("Mod date");
1704 Asm->EmitULEB128Bytes(0);
1705 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00001706 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001707 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001708
Jim Laskeyef42a012006-11-02 20:12:39 +00001709 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001710
Jim Laskeyef42a012006-11-02 20:12:39 +00001711 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00001712 unsigned SecSrcLinesSize = SectionSourceLines.size();
1713
1714 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001715 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00001716 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00001717
Evan Cheng42bf74b2009-03-25 01:47:28 +00001718 if (Asm->isVerbose()) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001719 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00001720 O << '\t' << TAI->getCommentString() << " Section"
1721 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001722 } else
Evan Cheng6547e402008-07-01 23:18:29 +00001723 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001724
1725 // Dwarf assumes we start with first line of first source file.
1726 unsigned Source = 1;
1727 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001728
Jim Laskeyef42a012006-11-02 20:12:39 +00001729 // Construct rows of the address, source, line, column matrix.
1730 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00001731 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001732 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00001733 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001734
Evan Cheng42bf74b2009-03-25 01:47:28 +00001735 if (!Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00001736 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00001737 else {
1738 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001739 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00001740 O << '\t' << TAI->getCommentString() << ' '
1741 << getSourceDirectoryName(SourceID.first) << ' '
1742 << getSourceFileName(SourceID.second)
1743 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
1744 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001745
1746 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001747 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00001748 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001749 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00001750 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001751
Jim Laskeyef42a012006-11-02 20:12:39 +00001752 // If change of source, then switch to the new source.
1753 if (Source != LineInfo.getSourceID()) {
1754 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001755 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
1756 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00001757 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001758
Jim Laskeyef42a012006-11-02 20:12:39 +00001759 // If change of line.
1760 if (Line != LineInfo.getLine()) {
1761 // Determine offset.
1762 int Offset = LineInfo.getLine() - Line;
1763 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001764
Jim Laskeyef42a012006-11-02 20:12:39 +00001765 // Update line.
1766 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001767
Jim Laskeyef42a012006-11-02 20:12:39 +00001768 // If delta is small enough and in range...
1769 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1770 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001771 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00001772 } else {
1773 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001774 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
1775 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
1776 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00001777 }
1778 } else {
1779 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001780 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00001781 }
1782 }
1783
Bill Wendlingfbbd7012008-07-20 00:11:19 +00001784 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001785 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00001786
1787 if (SecSrcLinesSize == 0)
1788 // Because we're emitting a debug_line section, we still need a line
1789 // table. The linker and friends expect it to exist. If there's nothing to
1790 // put into it, emit an empty table.
1791 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001792
Jim Laskeyef42a012006-11-02 20:12:39 +00001793 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001794
Jim Laskeybacd3042007-02-21 22:48:45 +00001795 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001796 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001797
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001798 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00001799 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001800 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00001801 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00001802 return;
1803
1804 int stackGrowth =
1805 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1806 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001807 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00001808
1809 // Start the dwarf frame section.
1810 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1811
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001812 EmitLabel("debug_frame_common", 0);
1813 EmitDifference("debug_frame_common_end", 0,
1814 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001815 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00001816
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001817 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001818 Asm->EmitInt32((int)DW_CIE_ID);
1819 Asm->EOL("CIE Identifier Tag");
1820 Asm->EmitInt8(DW_CIE_VERSION);
1821 Asm->EOL("CIE Version");
1822 Asm->EmitString("");
1823 Asm->EOL("CIE Augmentation");
1824 Asm->EmitULEB128Bytes(1);
1825 Asm->EOL("CIE Code Alignment Factor");
1826 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001827 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00001828 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001829 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001830
Jim Laskey5e73d5b2007-01-24 18:45:13 +00001831 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00001832 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001833
Dale Johannesenb97aec62007-11-13 19:13:01 +00001834 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001835
Evan Cheng05548eb2008-02-29 19:36:59 +00001836 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001837 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001838
Jim Laskeybacd3042007-02-21 22:48:45 +00001839 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001840 }
1841
Jim Laskey65195462006-10-30 13:35:07 +00001842 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
1843 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001844 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00001845 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00001846 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001847
Jim Laskeyef42a012006-11-02 20:12:39 +00001848 // Start the dwarf frame section.
1849 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001850
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001851 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
1852 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001853 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001854
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001855 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001856
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001857 EmitSectionOffset("debug_frame_common", "section_debug_frame",
1858 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001859 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00001860
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001861 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001862 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001863 EmitDifference("func_end", DebugFrameInfo.Number,
1864 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001865 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001866
Devang Patel5aac3d32009-01-17 08:01:33 +00001867 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00001868 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001869
Evan Cheng05548eb2008-02-29 19:36:59 +00001870 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001871 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00001872
Jim Laskeybacd3042007-02-21 22:48:45 +00001873 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001874 }
1875
Evan Chenge3d42322009-02-25 07:04:34 +00001876 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
1877 EmitDifference("pubnames_end", Unit->getID(),
1878 "pubnames_begin", Unit->getID(), true);
1879 Asm->EOL("Length of Public Names Info");
1880
1881 EmitLabel("pubnames_begin", Unit->getID());
1882
1883 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
1884
1885 EmitSectionOffset("info_begin", "section_info",
1886 Unit->getID(), 0, true, false);
1887 Asm->EOL("Offset of Compilation Unit Info");
1888
1889 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
1890 true);
1891 Asm->EOL("Compilation Unit Length");
1892
Bill Wendling972bbac2009-04-09 21:49:15 +00001893 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendlingf34be822009-04-09 23:51:31 +00001894 for (StringMap<DIE*>::const_iterator
Bill Wendling972bbac2009-04-09 21:49:15 +00001895 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendlingf34be822009-04-09 23:51:31 +00001896 const char *Name = GI->getKeyData();
Evan Chenge3d42322009-02-25 07:04:34 +00001897 DIE * Entity = GI->second;
1898
1899 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendlingf34be822009-04-09 23:51:31 +00001900 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Chenge3d42322009-02-25 07:04:34 +00001901 }
1902
1903 Asm->EmitInt32(0); Asm->EOL("End Mark");
1904 EmitLabel("pubnames_end", Unit->getID());
1905
1906 Asm->EOL();
1907 }
1908
Jim Laskeyef42a012006-11-02 20:12:39 +00001909 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00001910 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001911 void EmitDebugPubNames() {
1912 // Start the dwarf pubnames section.
1913 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001914
Evan Chenge3d42322009-02-25 07:04:34 +00001915 if (MainCU) {
1916 EmitDebugPubNamesPerCU(MainCU);
1917 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00001918 }
Evan Chenge3d42322009-02-25 07:04:34 +00001919
1920 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
1921 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00001922 }
1923
1924 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00001925 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001926 void EmitDebugStr() {
1927 // Check to see if it is worth the effort.
1928 if (!StringPool.empty()) {
1929 // Start the dwarf str section.
1930 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001931
Jim Laskeyef42a012006-11-02 20:12:39 +00001932 // For each of strings in the string pool.
1933 for (unsigned StringID = 1, N = StringPool.size();
1934 StringID <= N; ++StringID) {
1935 // Emit a label for reference from debug information entries.
1936 EmitLabel("string", StringID);
1937 // Emit the string itself.
1938 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00001939 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001940 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001941
Jim Laskeybacd3042007-02-21 22:48:45 +00001942 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001943 }
1944 }
1945
1946 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00001947 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001948 void EmitDebugLoc() {
1949 // Start the dwarf loc section.
1950 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001951
Jim Laskeybacd3042007-02-21 22:48:45 +00001952 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001953 }
1954
1955 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00001956 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001957 void EmitDebugARanges() {
1958 // Start the dwarf aranges section.
1959 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001960
Jim Laskeyef42a012006-11-02 20:12:39 +00001961 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00001962#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001963 CompileUnit *Unit = GetBaseCompileUnit();
1964
Jim Laskey5496f012006-11-09 14:52:14 +00001965 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001966 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001967
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001968 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001969
Jim Laskey5496f012006-11-09 14:52:14 +00001970 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001971 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00001972
Dan Gohman82482942007-09-27 23:12:31 +00001973 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00001974
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001975 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00001976
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001977 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
1978 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00001979
Jim Laskey5496f012006-11-09 14:52:14 +00001980 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001981 EmitReference("text_begin", 0); Asm->EOL("Address");
1982 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00001983
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001984 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
1985 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00001986#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001987
Jim Laskeybacd3042007-02-21 22:48:45 +00001988 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001989 }
1990
1991 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00001992 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001993 void EmitDebugRanges() {
1994 // Start the dwarf ranges section.
1995 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001996
Jim Laskeybacd3042007-02-21 22:48:45 +00001997 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001998 }
1999
2000 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002001 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002002 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002003 if (TAI->doesSupportMacInfoSection()) {
2004 // Start the dwarf macinfo section.
2005 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002006
Scott Michel210de722009-01-26 22:32:51 +00002007 Asm->EOL();
2008 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002009 }
2010
Devang Patel0f7fef32009-04-13 17:02:03 +00002011 /// EmitDebugInlineInfo - Emit inline info using following format.
2012 /// Section Header:
2013 /// 1. length of section
2014 /// 2. Dwarf version number
2015 /// 3. address size.
2016 ///
2017 /// Entries (one "entry" for each function that was inlined):
2018 ///
2019 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2020 /// otherwise offset into __debug_str for regular function name.
2021 /// 2. offset into __debug_str section for regular function name.
2022 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2023 /// instances for the function.
2024 ///
2025 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2026 /// inlined instance; the die_offset points to the inlined_subroutine die in
2027 /// the __debug_info section, and the low_pc is the starting address for the
2028 /// inlining instance.
2029 void EmitDebugInlineInfo() {
2030 if (!TAI->doesDwarfUsesInlineInfoSection())
2031 return;
2032
2033 if (!MainCU)
2034 return;
2035
2036 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2037 Asm->EOL();
2038 EmitDifference("debug_inlined_end", 1,
2039 "debug_inlined_begin", 1, true);
2040 Asm->EOL("Length of Debug Inlined Information Entry");
2041
2042 EmitLabel("debug_inlined_begin", 1);
2043
2044 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2045 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2046
2047 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2048 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2049 GlobalVariable *GV = I->first;
2050 SmallVector<unsigned, 4> &Labels = I->second;
2051 DISubprogram SP(GV);
2052 std::string Name;
2053 std::string LName;
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002054
Devang Patel0f7fef32009-04-13 17:02:03 +00002055 SP.getLinkageName(LName);
2056 SP.getName(Name);
2057
2058 Asm->EmitString(LName.empty() ? Name : LName);
2059 Asm->EOL("MIPS linkage name");
2060
2061 Asm->EmitString(Name); Asm->EOL("Function name");
2062
2063 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2064
2065 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2066 LE = Labels.end(); LI != LE; ++LI) {
2067 DIE *SP = MainCU->getDieMapSlotFor(GV);
2068 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2069
2070 if (TD->getPointerSize() == sizeof(int32_t))
2071 O << TAI->getData32bitsDirective();
2072 else
2073 O << TAI->getData64bitsDirective();
2074 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2075 }
2076 }
2077
2078 EmitLabel("debug_inlined_end", 1);
2079 Asm->EOL();
2080 }
2081
Bill Wendlingc8615e42009-03-10 21:47:45 +00002082 /// GetOrCreateSourceID - Look up the source id with the given directory and
2083 /// source file names. If none currently exists, create a new id and insert it
2084 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2085 /// as well.
2086 unsigned GetOrCreateSourceID(const std::string &DirName,
2087 const std::string &FileName) {
2088 unsigned DId;
2089 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2090 if (DI != DirectoryIdMap.end()) {
2091 DId = DI->getValue();
2092 } else {
2093 DId = DirectoryNames.size() + 1;
2094 DirectoryIdMap[DirName] = DId;
2095 DirectoryNames.push_back(DirName);
2096 }
2097
2098 unsigned FId;
2099 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2100 if (FI != SourceFileIdMap.end()) {
2101 FId = FI->getValue();
2102 } else {
2103 FId = SourceFileNames.size() + 1;
2104 SourceFileIdMap[FileName] = FId;
2105 SourceFileNames.push_back(FileName);
2106 }
2107
2108 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2109 SourceIdMap.find(std::make_pair(DId, FId));
2110 if (SI != SourceIdMap.end())
2111 return SI->second;
2112
2113 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2114 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2115 SourceIds.push_back(std::make_pair(DId, FId));
2116
2117 return SrcId;
2118 }
2119
Evan Chenge3d42322009-02-25 07:04:34 +00002120 void ConstructCompileUnit(GlobalVariable *GV) {
2121 DICompileUnit DIUnit(GV);
Bill Wendling0582ae92009-03-13 04:39:26 +00002122 std::string Dir, FN, Prod;
2123 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2124 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00002125
2126 DIE *Die = new DIE(DW_TAG_compile_unit);
2127 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2128 DWLabel("section_line", 0), DWLabel("section_line", 0),
2129 false);
Bill Wendling0582ae92009-03-13 04:39:26 +00002130 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00002131 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002132 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendling0582ae92009-03-13 04:39:26 +00002133 if (!Dir.empty())
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002134 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00002135 if (DIUnit.isOptimized())
2136 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling0582ae92009-03-13 04:39:26 +00002137 std::string Flags;
2138 DIUnit.getFlags(Flags);
2139 if (!Flags.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00002140 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2141 unsigned RVer = DIUnit.getRunTimeVersion();
2142 if (RVer)
2143 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2144
2145 CompileUnit *Unit = new CompileUnit(ID, Die);
2146 if (DIUnit.isMain()) {
2147 assert(!MainCU && "Multiple main compile units are found!");
2148 MainCU = Unit;
2149 }
2150 CompileUnitMap[DIUnit.getGV()] = Unit;
2151 CompileUnits.push_back(Unit);
2152 }
2153
Devang Patelc4523242009-01-05 23:11:11 +00002154 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002155 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00002156 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2157 if (!Root)
2158 return;
2159 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2160 "Malformed compile unit descriptor anchor type");
2161 Constant *RootC = cast<Constant>(*Root->use_begin());
2162 assert(RootC->hasNUsesOrMore(1) &&
2163 "Malformed compile unit descriptor anchor type");
2164 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2165 UI != UE; ++UI)
2166 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2167 UUI != UUE; ++UUI) {
2168 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2169 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00002170 }
Evan Chenge3d42322009-02-25 07:04:34 +00002171 }
2172
2173 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2174 DIGlobalVariable DI_GV(GV);
2175 CompileUnit *DW_Unit = MainCU;
2176 if (!DW_Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00002177 DW_Unit = &FindCompileUnit(DI_GV.getCompileUnit());
Evan Chenge3d42322009-02-25 07:04:34 +00002178
2179 // Check for pre-existence.
2180 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2181 if (Slot)
2182 return false;
2183
2184 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2185
2186 // Add address.
2187 DIEBlock *Block = new DIEBlock();
2188 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling7d16e852009-04-10 00:12:49 +00002189 std::string GLN;
Evan Chenge3d42322009-02-25 07:04:34 +00002190 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling7d16e852009-04-10 00:12:49 +00002191 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Chenge3d42322009-02-25 07:04:34 +00002192 AddBlock(VariableDie, DW_AT_location, 0, Block);
2193
2194 // Add to map.
2195 Slot = VariableDie;
Bill Wendling3f500d92009-05-06 21:21:34 +00002196
Evan Chenge3d42322009-02-25 07:04:34 +00002197 // Add to context owner.
2198 DW_Unit->getDie()->AddChild(VariableDie);
Bill Wendling3f500d92009-05-06 21:21:34 +00002199
Evan Chenge3d42322009-02-25 07:04:34 +00002200 // Expose as global. FIXME - need to check external flag.
Bill Wendling0582ae92009-03-13 04:39:26 +00002201 std::string Name;
2202 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002203 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00002204 }
2205
Devang Patelc4523242009-01-05 23:11:11 +00002206 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00002207 /// visible global variables. Return true if at least one global DIE is
2208 /// created.
2209 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00002210 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
2211 if (!Root)
2212 return false;
Devang Patelc4523242009-01-05 23:11:11 +00002213
Evan Chenge3d42322009-02-25 07:04:34 +00002214 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2215 "Malformed global variable descriptor anchor type");
2216 Constant *RootC = cast<Constant>(*Root->use_begin());
2217 assert(RootC->hasNUsesOrMore(1) &&
2218 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00002219
Evan Chenge3d42322009-02-25 07:04:34 +00002220 bool Result = false;
2221 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2222 UI != UE; ++UI)
2223 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00002224 UUI != UUE; ++UUI)
2225 Result |= ConstructGlobalVariableDIE(cast<GlobalVariable>(*UUI));
2226
Evan Chenge3d42322009-02-25 07:04:34 +00002227 return Result;
2228 }
Devang Patelc4523242009-01-05 23:11:11 +00002229
Evan Chenge3d42322009-02-25 07:04:34 +00002230 bool ConstructSubprogram(GlobalVariable *GV) {
2231 DISubprogram SP(GV);
2232 CompileUnit *Unit = MainCU;
2233 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00002234 Unit = &FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00002235
Evan Chenge3d42322009-02-25 07:04:34 +00002236 // Check for pre-existence.
2237 DIE *&Slot = Unit->getDieMapSlotFor(GV);
2238 if (Slot)
2239 return false;
2240
2241 if (!SP.isDefinition())
2242 // This is a method declaration which will be handled while
2243 // constructing class type.
2244 return false;
2245
2246 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
2247
2248 // Add to map.
2249 Slot = SubprogramDie;
2250 // Add to context owner.
2251 Unit->getDie()->AddChild(SubprogramDie);
2252 // Expose as global.
Bill Wendling0582ae92009-03-13 04:39:26 +00002253 std::string Name;
2254 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002255 return true;
Devang Patelc4523242009-01-05 23:11:11 +00002256 }
2257
Devang Patel78eb6ad2009-01-05 23:21:35 +00002258 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00002259 /// subprograms. Return true if at least one subprogram DIE is created.
2260 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00002261 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
2262 if (!Root)
2263 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00002264
Evan Chenge3d42322009-02-25 07:04:34 +00002265 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2266 "Malformed subprogram descriptor anchor type");
2267 Constant *RootC = cast<Constant>(*Root->use_begin());
2268 assert(RootC->hasNUsesOrMore(1) &&
2269 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00002270
Evan Chenge3d42322009-02-25 07:04:34 +00002271 bool Result = false;
2272 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2273 UI != UE; ++UI)
2274 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00002275 UUI != UUE; ++UUI)
2276 Result |= ConstructSubprogram(cast<GlobalVariable>(*UUI));
2277
Evan Chenge3d42322009-02-25 07:04:34 +00002278 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00002279 }
2280
Jim Laskey65195462006-10-30 13:35:07 +00002281public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002282 //===--------------------------------------------------------------------===//
2283 // Main entry points.
2284 //
Owen Andersoncb371882008-08-21 00:14:44 +00002285 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00002286 : Dwarf(OS, A, T, "dbg"), MainCU(0),
2287 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
2288 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
2289 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel7d2f9722009-04-15 20:41:31 +00002290 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002291 if (TimePassesIsEnabled)
2292 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00002293 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00002294 }
Jim Laskey072200c2007-01-29 18:51:14 +00002295 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002296 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2297 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00002298
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002299 for (DenseMap<const GlobalVariable *, DbgScope *>::iterator
2300 I = AbstractInstanceRootMap.begin(),
2301 E = AbstractInstanceRootMap.end(); I != E;++I)
2302 delete I->second;
2303
Bill Wendling163ba3f2009-03-10 21:23:25 +00002304 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00002305 }
2306
Bill Wendlingc8615e42009-03-10 21:47:45 +00002307 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
2308 /// be emitted.
2309 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
2310
Devang Patel5d315982009-01-06 21:07:30 +00002311 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
2312 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00002313 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002314 if (TimePassesIsEnabled)
2315 DebugTimer->startTimer();
2316
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002317 // Create all the compile unit DIEs.
2318 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00002319
Bill Wendling163ba3f2009-03-10 21:23:25 +00002320 if (CompileUnits.empty()) {
2321 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00002322 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00002323
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002324 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00002325 }
Devang Patel3f7833a2009-01-12 23:09:42 +00002326
Devang Patel53cac182009-02-24 00:02:15 +00002327 // Create DIEs for each of the externally visible global variables.
2328 bool globalDIEs = ConstructGlobalVariableDIEs();
2329
2330 // Create DIEs for each of the externally visible subprograms.
2331 bool subprogramDIEs = ConstructSubprograms();
2332
2333 // If there is not any debug info available for any global variables
2334 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00002335 if (!globalDIEs && !subprogramDIEs) {
2336 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00002337 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00002338
Devang Patel53cac182009-02-24 00:02:15 +00002339 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00002340 }
Devang Patel53cac182009-02-24 00:02:15 +00002341
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002342 MMI = mmi;
2343 shouldEmit = true;
2344 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00002345
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002346 // Prime section data.
2347 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00002348
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002349 // Print out .file directives to specify files for .loc directives. These
2350 // are printed out early so that they precede any .loc directives.
2351 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00002352 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
2353 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002354 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00002355 sys::Path FullPath(getSourceDirectoryName(Id.first));
2356 bool AppendOk =
2357 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002358 assert(AppendOk && "Could not append filename to directory!");
2359 AppendOk = false;
2360 Asm->EmitFile(i, FullPath.toString());
2361 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00002362 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002363 }
Devang Patel5d315982009-01-06 21:07:30 +00002364
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002365 // Emit initial sections
2366 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00002367
2368 if (TimePassesIsEnabled)
2369 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00002370 }
2371
Jim Laskey65195462006-10-30 13:35:07 +00002372 /// BeginModule - Emit all Dwarf sections that should come prior to the
2373 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002374 void BeginModule(Module *M) {
2375 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00002376 }
2377
Jim Laskey65195462006-10-30 13:35:07 +00002378 /// EndModule - Emit all Dwarf sections that should come after the content.
2379 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002380 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002381 if (!ShouldEmitDwarfDebug())
2382 return;
2383
2384 if (TimePassesIsEnabled)
2385 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002386
Jim Laskeyef42a012006-11-02 20:12:39 +00002387 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002388 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002389 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002390 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002391 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002392
Jim Laskeyef42a012006-11-02 20:12:39 +00002393 // End text sections.
2394 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002395 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002396 EmitLabel("section_end", i);
2397 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002398
2399 // Emit common frame information.
2400 EmitCommonDebugFrame();
2401
2402 // Emit function debug frame information
2403 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2404 E = DebugFrames.end(); I != E; ++I)
2405 EmitFunctionDebugFrame(*I);
2406
Jim Laskeyef42a012006-11-02 20:12:39 +00002407 // Compute DIE offsets and sizes.
2408 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002409
Jim Laskeyef42a012006-11-02 20:12:39 +00002410 // Emit all the DIEs into a debug info section
2411 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002412
Jim Laskeyef42a012006-11-02 20:12:39 +00002413 // Corresponding abbreviations into a abbrev section.
2414 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002415
Jim Laskeyef42a012006-11-02 20:12:39 +00002416 // Emit source line correspondence into a debug line section.
2417 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002418
Jim Laskeyef42a012006-11-02 20:12:39 +00002419 // Emit info into a debug pubnames section.
2420 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002421
Jim Laskeyef42a012006-11-02 20:12:39 +00002422 // Emit info into a debug str section.
2423 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002424
Jim Laskeyef42a012006-11-02 20:12:39 +00002425 // Emit info into a debug loc section.
2426 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002427
Jim Laskeyef42a012006-11-02 20:12:39 +00002428 // Emit info into a debug aranges section.
2429 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002430
Jim Laskeyef42a012006-11-02 20:12:39 +00002431 // Emit info into a debug ranges section.
2432 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002433
Jim Laskeyef42a012006-11-02 20:12:39 +00002434 // Emit info into a debug macinfo section.
2435 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00002436
Devang Patel0f7fef32009-04-13 17:02:03 +00002437 // Emit inline info.
2438 EmitDebugInlineInfo();
2439
Bill Wendling163ba3f2009-03-10 21:23:25 +00002440 if (TimePassesIsEnabled)
2441 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00002442 }
2443
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002444 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00002445 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002446 void BeginFunction(MachineFunction *MF) {
Bill Wendling1362f972009-03-11 00:03:50 +00002447 this->MF = MF;
2448
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00002449 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002450
Bill Wendling163ba3f2009-03-10 21:23:25 +00002451 if (TimePassesIsEnabled)
2452 DebugTimer->startTimer();
2453
Jim Laskeyef42a012006-11-02 20:12:39 +00002454 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002455 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002456
Jim Laskeyef42a012006-11-02 20:12:39 +00002457 // Assumes in correct section after the entry point.
2458 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00002459
Argyrios Kyrtzidis28f14122009-05-04 19:23:45 +00002460 // Emit label for the implicitly defined dbg.stoppoint at the start of
2461 // the function.
Mike Stumpfe095f32009-05-04 18:40:41 +00002462 DebugLoc FDL = MF->getDefaultDebugLoc();
2463 if (!FDL.isUnknown()) {
2464 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2465 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
2466 DICompileUnit(DLT.CompileUnit));
2467 Asm->printLabel(LabelID);
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00002468 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00002469
2470 if (TimePassesIsEnabled)
2471 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00002472 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002473
Jim Laskey65195462006-10-30 13:35:07 +00002474 /// EndFunction - Gather and emit post-function debug information.
2475 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00002476 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00002477 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002478
Bill Wendling163ba3f2009-03-10 21:23:25 +00002479 if (TimePassesIsEnabled)
2480 DebugTimer->startTimer();
2481
Jim Laskeyb82313f2007-02-01 16:31:34 +00002482 // Define end label for subprogram.
2483 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002484
Jim Laskeyef42a012006-11-02 20:12:39 +00002485 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002486 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002487 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002488 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00002489 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00002490 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00002491 // Append the function info to section info.
2492 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00002493 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00002494 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002495
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002496 // Construct the DbgScope for abstract instances.
2497 for (SmallVector<DbgScope *, 32>::iterator
2498 I = AbstractInstanceRootList.begin(),
2499 E = AbstractInstanceRootList.end(); I != E; ++I)
2500 ConstructAbstractDbgScope(*I);
2501
Jim Laskeyef42a012006-11-02 20:12:39 +00002502 // Construct scopes for subprogram.
Devang Patel7d2f9722009-04-15 20:41:31 +00002503 if (FunctionDbgScope)
2504 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00002505 else
2506 // FIXME: This is wrong. We are essentially getting past a problem with
2507 // debug information not being able to handle unreachable blocks that have
2508 // debug information in them. In particular, those unreachable blocks that
2509 // have "region end" info in them. That situation results in the "root
2510 // scope" not being created. If that's the case, then emit a "default"
2511 // scope, i.e., one that encompasses the whole function. This isn't
2512 // desirable. And a better way of handling this (and all of the debugging
2513 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00002514 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002515
2516 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2517 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00002518
2519 // Clear debug info
Devang Patel7d2f9722009-04-15 20:41:31 +00002520 if (FunctionDbgScope) {
2521 delete FunctionDbgScope;
Devang Patel481ff5b2009-01-12 18:48:36 +00002522 DbgScopeMap.clear();
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002523 DbgAbstractScopeMap.clear();
2524 DbgConcreteScopeMap.clear();
Devang Patel1be3ecc2009-04-15 00:10:26 +00002525 InlinedVariableScopes.clear();
Devang Patel7d2f9722009-04-15 20:41:31 +00002526 FunctionDbgScope = NULL;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002527 LexicalScopeStack.clear();
2528 AbstractInstanceRootList.clear();
Devang Patel481ff5b2009-01-12 18:48:36 +00002529 }
Devang Patelccca7fe2009-01-12 19:17:34 +00002530
Bill Wendling163ba3f2009-03-10 21:23:25 +00002531 Lines.clear();
2532
2533 if (TimePassesIsEnabled)
2534 DebugTimer->stopTimer();
2535 }
Devang Patelccca7fe2009-01-12 19:17:34 +00002536
2537 /// RecordSourceLine - Records location information and associates it with a
2538 /// label. Returns a unique label ID used to generate a label and provide
2539 /// correspondence to the source line list.
2540 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002541 if (TimePassesIsEnabled)
2542 DebugTimer->startTimer();
2543
Evan Chenge3d42322009-02-25 07:04:34 +00002544 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002545 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00002546 unsigned ID = MMI->NextLabelID();
2547 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00002548
2549 if (TimePassesIsEnabled)
2550 DebugTimer->stopTimer();
2551
Devang Patelccca7fe2009-01-12 19:17:34 +00002552 return ID;
2553 }
2554
2555 /// RecordSourceLine - Records location information and associates it with a
2556 /// label. Returns a unique label ID used to generate a label and provide
2557 /// correspondence to the source line list.
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00002558 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002559 if (TimePassesIsEnabled)
2560 DebugTimer->startTimer();
2561
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00002562 std::string Dir, Fn;
2563 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
2564 CU.getFilename(Fn));
Devang Patelccca7fe2009-01-12 19:17:34 +00002565 unsigned ID = MMI->NextLabelID();
2566 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00002567
2568 if (TimePassesIsEnabled)
2569 DebugTimer->stopTimer();
2570
Devang Patelccca7fe2009-01-12 19:17:34 +00002571 return ID;
2572 }
2573
Bill Wendlingc8615e42009-03-10 21:47:45 +00002574 /// getRecordSourceLineCount - Return the number of source lines in the debug
2575 /// info.
2576 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00002577 return Lines.size();
2578 }
2579
Bill Wendlingc8615e42009-03-10 21:47:45 +00002580 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2581 /// timed. Look up the source id with the given directory and source file
2582 /// names. If none currently exists, create a new id and insert it in the
2583 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2584 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00002585 unsigned getOrCreateSourceID(const std::string &DirName,
2586 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002587 if (TimePassesIsEnabled)
2588 DebugTimer->startTimer();
2589
Bill Wendlingc8615e42009-03-10 21:47:45 +00002590 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00002591
2592 if (TimePassesIsEnabled)
2593 DebugTimer->stopTimer();
2594
Evan Chenge3d42322009-02-25 07:04:34 +00002595 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00002596 }
2597
2598 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00002599 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002600 if (TimePassesIsEnabled)
2601 DebugTimer->startTimer();
2602
Devang Patelccca7fe2009-01-12 19:17:34 +00002603 DbgScope *Scope = getOrCreateScope(V);
2604 unsigned ID = MMI->NextLabelID();
2605 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002606 LexicalScopeStack.push_back(Scope);
Bill Wendling163ba3f2009-03-10 21:23:25 +00002607
2608 if (TimePassesIsEnabled)
2609 DebugTimer->stopTimer();
2610
Devang Patelccca7fe2009-01-12 19:17:34 +00002611 return ID;
2612 }
2613
2614 /// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002615 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002616 if (TimePassesIsEnabled)
2617 DebugTimer->startTimer();
2618
Bill Wendling5b8479c2009-05-07 17:26:14 +00002619 DbgScope *Scope = getOrCreateScope(V);
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002620 unsigned ID = MMI->NextLabelID();
Devang Patelccca7fe2009-01-12 19:17:34 +00002621 Scope->setEndLabelID(ID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002622 if (LexicalScopeStack.size() != 0)
2623 LexicalScopeStack.pop_back();
Bill Wendling163ba3f2009-03-10 21:23:25 +00002624
2625 if (TimePassesIsEnabled)
2626 DebugTimer->stopTimer();
2627
Devang Patelccca7fe2009-01-12 19:17:34 +00002628 return ID;
2629 }
2630
2631 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00002632 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
2633 const MachineInstr *MI) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002634 if (TimePassesIsEnabled)
2635 DebugTimer->startTimer();
2636
Devang Patel0e5200f2009-01-15 18:25:17 +00002637 DIDescriptor Desc(GV);
2638 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00002639
Devang Patel0e5200f2009-01-15 18:25:17 +00002640 if (Desc.getTag() == DW_TAG_variable) {
2641 // GV is a global variable.
2642 DIGlobalVariable DG(GV);
2643 Scope = getOrCreateScope(DG.getContext().getGV());
2644 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002645 DenseMap<const MachineInstr *, DbgScope *>::iterator
2646 SI = InlinedVariableScopes.find(MI);
Bill Wendling3f500d92009-05-06 21:21:34 +00002647
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002648 if (SI != InlinedVariableScopes.end()) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002649 // or GV is an inlined local variable.
2650 Scope = SI->second;
2651 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002652 DIVariable DV(GV);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002653 GlobalVariable *V = DV.getContext().getGV();
2654
2655 // FIXME: The code that checks for the inlined local variable is a hack!
2656 DenseMap<const GlobalVariable *, DbgScope *>::iterator
2657 AI = AbstractInstanceRootMap.find(V);
2658
2659 if (AI != AbstractInstanceRootMap.end())
2660 // or GV is an inlined local variable.
2661 Scope = AI->second;
2662 else
2663 // or GV is a local variable.
2664 Scope = getOrCreateScope(V);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002665 }
Devang Patel0e5200f2009-01-15 18:25:17 +00002666 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00002667
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002668 assert(Scope && "Unable to find the variable's scope");
Devang Patel99ec3532009-01-16 19:28:14 +00002669 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00002670 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00002671
2672 if (TimePassesIsEnabled)
2673 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00002674 }
Devang Patel0f7fef32009-04-13 17:02:03 +00002675
Devang Patel1be3ecc2009-04-15 00:10:26 +00002676 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00002677 unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
2678 unsigned Line, unsigned Col) {
2679 unsigned LabelID = MMI->NextLabelID();
2680
Devang Patel1be3ecc2009-04-15 00:10:26 +00002681 if (!TAI->doesDwarfUsesInlineInfoSection())
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00002682 return LabelID;
Devang Patel1be3ecc2009-04-15 00:10:26 +00002683
Bill Wendlingd5a63812009-05-01 08:40:06 +00002684 if (TimePassesIsEnabled)
2685 DebugTimer->startTimer();
2686
Devang Patel1be3ecc2009-04-15 00:10:26 +00002687 GlobalVariable *GV = SP.getGV();
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002688 DenseMap<const GlobalVariable *, DbgScope *>::iterator
2689 II = AbstractInstanceRootMap.find(GV);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002690
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002691 if (II == AbstractInstanceRootMap.end()) {
2692 // Create an abstract instance entry for this inlined function if it
2693 // doesn't already exist.
2694 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(GV));
Bill Wendlingef956fc2009-05-01 08:32:14 +00002695
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002696 // Get the compile unit context.
2697 CompileUnit *Unit = &FindCompileUnit(SP.getCompileUnit());
2698 DIE *SPDie = Unit->getDieMapSlotFor(GV);
2699 if (!SPDie)
2700 SPDie = CreateSubprogramDIE(Unit, SP);
2701
2702 // Mark as being inlined. This makes this subprogram entry an abstract
2703 // instance root.
2704 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
2705 // that it's defined. It probably won't change in the future, but this
2706 // could be more elegant.
2707 AddUInt(SPDie, DW_AT_inline, 0, DW_INL_declared_not_inlined);
2708
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002709 // Keep track of the abstract scope for this function.
2710 DbgAbstractScopeMap[GV] = Scope;
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00002711
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002712 AbstractInstanceRootMap[GV] = Scope;
2713 AbstractInstanceRootList.push_back(Scope);
2714 }
2715
2716 // Create a concrete inlined instance for this inlined function.
2717 DbgConcreteScope *ConcreteScope = new DbgConcreteScope(DIDescriptor(GV));
2718 DIE *ScopeDie = new DIE(DW_TAG_inlined_subroutine);
2719 CompileUnit *Unit = &FindCompileUnit(SP.getCompileUnit());
2720 ScopeDie->setAbstractCompileUnit(Unit);
2721
2722 DIE *Origin = Unit->getDieMapSlotFor(GV);
Bill Wendling88423ee2009-05-15 00:11:17 +00002723 AddDIEEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002724 AddUInt(ScopeDie, DW_AT_call_file, 0, Unit->getID());
2725 AddUInt(ScopeDie, DW_AT_call_line, 0, Line);
2726 AddUInt(ScopeDie, DW_AT_call_column, 0, Col);
2727
2728 ConcreteScope->setDie(ScopeDie);
2729 ConcreteScope->setStartLabelID(LabelID);
Bill Wendlingbc12c2b2009-05-12 00:06:59 +00002730 MMI->RecordUsedDbgLabel(LabelID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002731
2732 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope);
2733
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002734 // Keep track of the concrete scope that's inlined into this function.
2735 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator
2736 SI = DbgConcreteScopeMap.find(GV);
2737
2738 if (SI == DbgConcreteScopeMap.end())
2739 DbgConcreteScopeMap[GV].push_back(ConcreteScope);
2740 else
2741 SI->second.push_back(ConcreteScope);
2742
Bill Wendlingbc12c2b2009-05-12 00:06:59 +00002743 // Track the start label for this inlined function.
2744 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2745 I = InlineInfo.find(GV);
2746
2747 if (I == InlineInfo.end())
2748 InlineInfo[GV].push_back(LabelID);
2749 else
2750 I->second.push_back(LabelID);
2751
Bill Wendlingd5a63812009-05-01 08:40:06 +00002752 if (TimePassesIsEnabled)
2753 DebugTimer->stopTimer();
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00002754
2755 return LabelID;
Devang Patel0f7fef32009-04-13 17:02:03 +00002756 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00002757
2758 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
2759 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
2760 if (!TAI->doesDwarfUsesInlineInfoSection())
2761 return 0;
2762
Bill Wendlingd5a63812009-05-01 08:40:06 +00002763 if (TimePassesIsEnabled)
2764 DebugTimer->startTimer();
2765
Devang Patel1be3ecc2009-04-15 00:10:26 +00002766 GlobalVariable *GV = SP.getGV();
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00002767 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002768 I = DbgConcreteScopeMap.find(GV);
Bill Wendling3f500d92009-05-06 21:21:34 +00002769
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002770 if (I == DbgConcreteScopeMap.end()) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00002771 if (TimePassesIsEnabled)
2772 DebugTimer->stopTimer();
2773
Devang Patel1be3ecc2009-04-15 00:10:26 +00002774 return 0;
Bill Wendlingd5a63812009-05-01 08:40:06 +00002775 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00002776
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00002777 SmallVector<DbgScope *, 8> &Scopes = I->second;
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002778 assert(!Scopes.empty() && "We should have at least one debug scope!");
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00002779 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
Devang Patel1be3ecc2009-04-15 00:10:26 +00002780 unsigned ID = MMI->NextLabelID();
2781 MMI->RecordUsedDbgLabel(ID);
2782 Scope->setEndLabelID(ID);
Bill Wendlingd5a63812009-05-01 08:40:06 +00002783
2784 if (TimePassesIsEnabled)
2785 DebugTimer->stopTimer();
2786
Devang Patel1be3ecc2009-04-15 00:10:26 +00002787 return ID;
2788 }
2789
2790 /// RecordVariableScope - Record scope for the variable declared by
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002791 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE. Record scopes
2792 /// for only inlined subroutine variables. Other variables's scopes are
2793 /// determined during RecordVariable().
Devang Patel1be3ecc2009-04-15 00:10:26 +00002794 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00002795 if (TimePassesIsEnabled)
2796 DebugTimer->startTimer();
2797
Devang Patel1be3ecc2009-04-15 00:10:26 +00002798 DISubprogram SP(DV.getContext().getGV());
Bill Wendlingd5a63812009-05-01 08:40:06 +00002799
2800 if (SP.isNull()) {
2801 if (TimePassesIsEnabled)
2802 DebugTimer->stopTimer();
2803
Devang Patel1be3ecc2009-04-15 00:10:26 +00002804 return;
Bill Wendlingd5a63812009-05-01 08:40:06 +00002805 }
2806
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002807 DenseMap<GlobalVariable *, DbgScope *>::iterator
2808 I = DbgAbstractScopeMap.find(SP.getGV());
2809 if (I != DbgAbstractScopeMap.end())
2810 InlinedVariableScopes[DeclareMI] = I->second;
Devang Patel1be3ecc2009-04-15 00:10:26 +00002811
Bill Wendlingd5a63812009-05-01 08:40:06 +00002812 if (TimePassesIsEnabled)
2813 DebugTimer->stopTimer();
Devang Patel1be3ecc2009-04-15 00:10:26 +00002814 }
Jim Laskey65195462006-10-30 13:35:07 +00002815};
2816
Jim Laskey0d086af2006-02-27 12:43:29 +00002817} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00002818
2819//===----------------------------------------------------------------------===//
Jim Laskey65195462006-10-30 13:35:07 +00002820/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00002821///
Jim Laskey65195462006-10-30 13:35:07 +00002822
Bill Wendling91b8b802009-03-10 20:41:52 +00002823DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00002824 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00002825
2826DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00002827 delete DE;
2828 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00002829}
2830
Jim Laskey65195462006-10-30 13:35:07 +00002831/// BeginModule - Emit all Dwarf sections that should come prior to the
2832/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00002833void DwarfWriter::BeginModule(Module *M,
2834 MachineModuleInfo *MMI,
2835 raw_ostream &OS, AsmPrinter *A,
2836 const TargetAsmInfo *T) {
2837 DE = new DwarfException(OS, A, T);
2838 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00002839 DE->BeginModule(M);
2840 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00002841 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00002842 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00002843}
2844
2845/// EndModule - Emit all Dwarf sections that should come after the content.
2846///
2847void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00002848 DE->EndModule();
2849 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00002850}
2851
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002852/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00002853/// emitted immediately after the function entry point.
2854void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00002855 DE->BeginFunction(MF);
2856 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00002857}
2858
2859/// EndFunction - Gather and emit post-function debug information.
2860///
Bill Wendlingd751c642008-09-26 00:28:12 +00002861void DwarfWriter::EndFunction(MachineFunction *MF) {
2862 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002863 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002864
Bill Wendlingc91d0b92008-07-22 00:53:37 +00002865 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00002866 // Clear function debug information.
2867 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00002868}
Devang Patelccca7fe2009-01-12 19:17:34 +00002869
2870/// RecordSourceLine - Records location information and associates it with a
2871/// label. Returns a unique label ID used to generate a label and provide
2872/// correspondence to the source line list.
2873unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00002874 DICompileUnit CU) {
2875 return DD->RecordSourceLine(Line, Col, CU);
Devang Patelccca7fe2009-01-12 19:17:34 +00002876}
2877
2878/// RecordRegionStart - Indicate the start of a region.
2879unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002880 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00002881}
2882
2883/// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002884unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
2885 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00002886}
2887
2888/// getRecordSourceLineCount - Count source lines.
2889unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002890 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00002891}
Devang Patelbb8c5952009-01-13 21:25:00 +00002892
Devang Patelc48c5502009-01-13 21:44:10 +00002893/// RecordVariable - Indicate the declaration of a local variable.
2894///
Devang Patel1be3ecc2009-04-15 00:10:26 +00002895void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
2896 const MachineInstr *MI) {
2897 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelc48c5502009-01-13 21:44:10 +00002898}
Devang Patelbbdc8202009-01-13 23:54:55 +00002899
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00002900/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
2901/// be emitted.
2902bool DwarfWriter::ShouldEmitDwarfDebug() const {
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00002903 return DD && DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00002904}
Devang Patel0f7fef32009-04-13 17:02:03 +00002905
Devang Patel1be3ecc2009-04-15 00:10:26 +00002906//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel0f7fef32009-04-13 17:02:03 +00002907//// by LabelID label.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00002908unsigned DwarfWriter::RecordInlinedFnStart(DISubprogram SP, DICompileUnit CU,
2909 unsigned Line, unsigned Col) {
2910 return DD->RecordInlinedFnStart(SP, CU, Line, Col);
Devang Patel0f7fef32009-04-13 17:02:03 +00002911}
2912
Devang Patel1be3ecc2009-04-15 00:10:26 +00002913/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00002914unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram SP) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002915 return DD->RecordInlinedFnEnd(SP);
2916}
2917
2918/// RecordVariableScope - Record scope for the variable declared by
2919/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
2920void DwarfWriter::RecordVariableScope(DIVariable &DV,
2921 const MachineInstr *DeclareMI) {
2922 DD->RecordVariableScope(DV, DeclareMI);
2923}