blob: 83275fb92eb01c91ea4a487d7e5c5a8a3b71851a [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"
16#include "DwarfPrinter.h"
Jim Laskey52060a02006-01-24 00:49:18 +000017#include "llvm/Module.h"
Devang Pateld1ca9252009-01-05 23:03:32 +000018#include "llvm/DerivedTypes.h"
Devang Patelcf3a4482009-01-15 23:41:32 +000019#include "llvm/Constants.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000020#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000023#include "llvm/CodeGen/MachineLocation.h"
Devang Patel08f053f2009-01-05 17:57:47 +000024#include "llvm/Analysis/DebugInfo.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000025#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000026#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000027#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000028#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000029#include "llvm/Support/Mangler.h"
Bill Wendling91b8b802009-03-10 20:41:52 +000030#include "llvm/Support/Timer.h"
Owen Andersoncb371882008-08-21 00:14:44 +000031#include "llvm/Support/raw_ostream.h"
Dan Gohman85496362007-09-24 21:32:18 +000032#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000033#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000035#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000036#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000037#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000038#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000039#include "llvm/Target/TargetOptions.h"
Evan Chenge3d42322009-02-25 07:04:34 +000040#include "llvm/ADT/DenseMap.h"
41#include "llvm/ADT/FoldingSet.h"
42#include "llvm/ADT/StringExtras.h"
43#include "llvm/ADT/StringMap.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000044#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000045#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000046using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000047using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000048
Devang Pateleb3fc282009-01-08 23:40:34 +000049static RegisterPass<DwarfWriter>
50X("dwarfwriter", "DWARF Information Writer");
51char DwarfWriter::ID = 0;
52
Bill Wendling68edf5f2009-03-10 22:58:53 +000053static TimerGroup &getDwarfTimerGroup() {
54 static TimerGroup DwarfTimerGroup("Dwarf Exception and Debugging");
55 return DwarfTimerGroup;
Bill Wendling91b8b802009-03-10 20:41:52 +000056}
57
Jim Laskey0d086af2006-02-27 12:43:29 +000058namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000059
Jim Laskey65195462006-10-30 13:35:07 +000060//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000061
62/// Configuration values for initial hash set sizes (log2).
63///
Bill Wendlingb9dcef22009-02-03 21:17:20 +000064static const unsigned InitDiesSetSize = 9; // log2(512)
65static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
66static const unsigned InitValuesSetSize = 9; // log2(512)
Jim Laskeyef42a012006-11-02 20:12:39 +000067
68//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000069/// CompileUnit - This dwarf writer support class manages information associate
70/// with a source file.
71class CompileUnit {
Jim Laskeyef42a012006-11-02 20:12:39 +000072 /// ID - File identifier for source.
73 ///
74 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000075
Jim Laskeyef42a012006-11-02 20:12:39 +000076 /// Die - Compile unit debug information entry.
77 ///
78 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000079
Devang Patelbbdc8202009-01-13 23:54:55 +000080 /// GVToDieMap - Tracks the mapping of unit level debug informaton
81 /// variables to debug information entries.
Devang Patelc2997f42009-01-20 00:58:55 +000082 std::map<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +000083
Bill Wendling88423ee2009-05-15 00:11:17 +000084 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
85 /// descriptors to debug information entries using a DIEEntry proxy.
86 std::map<GlobalVariable *, DIEEntry *> GVToDIEEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +000087
88 /// Globals - A map of globally visible named entities for this unit.
89 ///
Bill Wendling972bbac2009-04-09 21:49:15 +000090 StringMap<DIE*> Globals;
Jim Laskeyef42a012006-11-02 20:12:39 +000091
92 /// DiesSet - Used to uniquely define dies within the compile unit.
93 ///
94 FoldingSet<DIE> DiesSet;
Jim Laskey0d086af2006-02-27 12:43:29 +000095public:
Devang Pateld1ca9252009-01-05 23:03:32 +000096 CompileUnit(unsigned I, DIE *D)
Devang Patelbbdc8202009-01-13 23:54:55 +000097 : ID(I), Die(D), GVToDieMap(),
Bill Wendling88423ee2009-05-15 00:11:17 +000098 GVToDIEEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Pateld1ca9252009-01-05 23:03:32 +000099 {}
100
Jim Laskeyef42a012006-11-02 20:12:39 +0000101 ~CompileUnit() {
102 delete Die;
Jim Laskeyef42a012006-11-02 20:12:39 +0000103 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000104
Jim Laskeybd761842006-02-27 17:27:12 +0000105 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000106 unsigned getID() const { return ID; }
107 DIE* getDie() const { return Die; }
Bill Wendling972bbac2009-04-09 21:49:15 +0000108 StringMap<DIE*> &getGlobals() { return Globals; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000109
110 /// hasContent - Return true if this compile unit has something to write out.
111 ///
112 bool hasContent() const {
113 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000114 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000115
116 /// AddGlobal - Add a new global entity to the compile unit.
117 ///
118 void AddGlobal(const std::string &Name, DIE *Die) {
119 Globals[Name] = Die;
120 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000121
Jim Laskeyef42a012006-11-02 20:12:39 +0000122 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000123 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000124 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
125 return GVToDieMap[GV];
126 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000127
Bill Wendling88423ee2009-05-15 00:11:17 +0000128 /// getDIEEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000129 /// specified debug variable.
Bill Wendling88423ee2009-05-15 00:11:17 +0000130 DIEEntry *&getDIEEntrySlotFor(GlobalVariable *GV) {
131 return GVToDIEEntryMap[GV];
Devang Patel48d190f2009-01-05 21:47:57 +0000132 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000133
Jim Laskeyef42a012006-11-02 20:12:39 +0000134 /// AddDie - Adds or interns the DIE to the compile unit.
135 ///
136 DIE *AddDie(DIE &Buffer) {
137 FoldingSetNodeID ID;
138 Buffer.Profile(ID);
139 void *Where;
140 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000141
Jim Laskeyef42a012006-11-02 20:12:39 +0000142 if (!Die) {
143 Die = new DIE(Buffer);
144 DiesSet.InsertNode(Die, Where);
145 this->Die->AddChild(Die);
146 Buffer.Detach();
147 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000148
Jim Laskeyef42a012006-11-02 20:12:39 +0000149 return Die;
150 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000151};
152
Jim Laskey65195462006-10-30 13:35:07 +0000153//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +0000154/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +0000155///
156class SrcLineInfo {
Bill Wendling88423ee2009-05-15 00:11:17 +0000157 unsigned Line; // Source line number.
158 unsigned Column; // Source column.
159 unsigned SourceID; // Source ID number.
160 unsigned LabelID; // Label in code ID number.
Devang Patel9f8fcfc2009-01-08 17:19:22 +0000161public:
162 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendlingb9dcef22009-02-03 21:17:20 +0000163 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000164
Devang Patel9f8fcfc2009-01-08 17:19:22 +0000165 // Accessors
166 unsigned getLine() const { return Line; }
167 unsigned getColumn() const { return Column; }
168 unsigned getSourceID() const { return SourceID; }
169 unsigned getLabelID() const { return LabelID; }
170};
171
Devang Patel9f8fcfc2009-01-08 17:19:22 +0000172//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +0000173/// DbgVariable - This class is used to track local variable information.
174///
175class DbgVariable {
Bill Wendling88423ee2009-05-15 00:11:17 +0000176 DIVariable Var; // Variable Descriptor.
Dan Gohman9a38e3e2009-05-07 19:46:24 +0000177 unsigned FrameIndex; // Variable frame index.
Devang Patel7a6e5a32009-01-08 02:33:41 +0000178public:
Devang Patel99ec3532009-01-16 19:28:14 +0000179 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel7a6e5a32009-01-08 02:33:41 +0000180
181 // Accessors.
Devang Patel99ec3532009-01-16 19:28:14 +0000182 DIVariable getVariable() const { return Var; }
Devang Patel7a6e5a32009-01-08 02:33:41 +0000183 unsigned getFrameIndex() const { return FrameIndex; }
184};
185
186//===----------------------------------------------------------------------===//
187/// DbgScope - This class is used to track scope information.
188///
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000189class DbgConcreteScope;
Devang Patel7a6e5a32009-01-08 02:33:41 +0000190class DbgScope {
Devang Patel7a6e5a32009-01-08 02:33:41 +0000191 DbgScope *Parent; // Parent to this scope.
Devang Patel7103c6a2009-01-16 18:01:58 +0000192 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +0000193 // Either subprogram or block.
194 unsigned StartLabelID; // Label ID of the beginning of scope.
195 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel7103c6a2009-01-16 18:01:58 +0000196 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel9795da52009-01-10 02:42:49 +0000197 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000198 SmallVector<DbgConcreteScope *, 8> ConcreteInsts;// Concrete insts of funcs.
Devang Patel7a6e5a32009-01-08 02:33:41 +0000199public:
Devang Patel0e5200f2009-01-15 18:25:17 +0000200 DbgScope(DbgScope *P, DIDescriptor D)
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000201 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0) {}
202 virtual ~DbgScope();
Devang Patel7a6e5a32009-01-08 02:33:41 +0000203
204 // Accessors.
Devang Patel7103c6a2009-01-16 18:01:58 +0000205 DbgScope *getParent() const { return Parent; }
206 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +0000207 unsigned getStartLabelID() const { return StartLabelID; }
208 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +0000209 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
210 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000211 SmallVector<DbgConcreteScope*,8> &getConcreteInsts() { return ConcreteInsts; }
Devang Patel7a6e5a32009-01-08 02:33:41 +0000212 void setStartLabelID(unsigned S) { StartLabelID = S; }
213 void setEndLabelID(unsigned E) { EndLabelID = E; }
214
215 /// AddScope - Add a scope to the scope.
216 ///
217 void AddScope(DbgScope *S) { Scopes.push_back(S); }
218
219 /// AddVariable - Add a variable to the scope.
220 ///
221 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
Devang Patel1be3ecc2009-04-15 00:10:26 +0000222
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000223 /// AddConcreteInst - Add a concrete instance to the scope.
224 ///
225 void AddConcreteInst(DbgConcreteScope *C) { ConcreteInsts.push_back(C); }
Bill Wendling3f500d92009-05-06 21:21:34 +0000226
227#ifndef NDEBUG
228 void dump() const;
229#endif
Devang Patel1be3ecc2009-04-15 00:10:26 +0000230};
231
Bill Wendling3f500d92009-05-06 21:21:34 +0000232#ifndef NDEBUG
233void DbgScope::dump() const {
234 static unsigned IndentLevel = 0;
235 std::string Indent(IndentLevel, ' ');
236
237 cerr << Indent; Desc.dump();
238 cerr << " [" << StartLabelID << ", " << EndLabelID << "]\n";
239
240 IndentLevel += 2;
241
242 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
243 if (Scopes[i] != this)
244 Scopes[i]->dump();
245
246 IndentLevel -= 2;
247}
248#endif
Devang Patel1be3ecc2009-04-15 00:10:26 +0000249
250//===----------------------------------------------------------------------===//
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000251/// DbgConcreteScope - This class is used to track a scope that holds concrete
252/// instance information.
253///
254class DbgConcreteScope : public DbgScope {
255 CompileUnit *Unit;
256 DIE *Die; // Debug info for this concrete scope.
Devang Patel1be3ecc2009-04-15 00:10:26 +0000257public:
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000258 DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {}
Devang Patel1be3ecc2009-04-15 00:10:26 +0000259
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000260 // Accessors.
261 DIE *getDie() const { return Die; }
262 void setDie(DIE *D) { Die = D; }
Devang Patel7a6e5a32009-01-08 02:33:41 +0000263};
264
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000265DbgScope::~DbgScope() {
266 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
267 delete Scopes[i];
268 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
269 delete Variables[j];
270 for (unsigned k = 0, O = ConcreteInsts.size(); k < O; ++k)
271 delete ConcreteInsts[k];
272}
273
Devang Patel7a6e5a32009-01-08 02:33:41 +0000274//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000275/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +0000276///
277class DwarfDebug : public Dwarf {
Jim Laskey65195462006-10-30 13:35:07 +0000278 //===--------------------------------------------------------------------===//
279 // Attributes used to construct specific Dwarf sections.
280 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000281
Evan Chenge3d42322009-02-25 07:04:34 +0000282 /// CompileUnitMap - A map of global variables representing compile units to
283 /// compile units.
284 DenseMap<Value *, CompileUnit *> CompileUnitMap;
285
286 /// CompileUnits - All the compile units in this module.
287 ///
288 SmallVector<CompileUnit *, 8> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000289
Devang Pateldd9db662009-01-30 18:20:31 +0000290 /// MainCU - Some platform prefers one compile unit per .o file. In such
291 /// cases, all dies are inserted in MainCU.
292 CompileUnit *MainCU;
Bill Wendling9a65cfe2009-02-20 20:40:28 +0000293
Jim Laskeyef42a012006-11-02 20:12:39 +0000294 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +0000295 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000296 FoldingSet<DIEAbbrev> AbbreviationsSet;
297
298 /// Abbreviations - A list of all the unique abbreviations in use.
299 ///
300 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000301
Evan Chenge3d42322009-02-25 07:04:34 +0000302 /// DirectoryIdMap - Directory name to directory id map.
303 ///
304 StringMap<unsigned> DirectoryIdMap;
Devang Patel8526cc02009-01-05 22:35:52 +0000305
Evan Chenge3d42322009-02-25 07:04:34 +0000306 /// DirectoryNames - A list of directory names.
307 SmallVector<std::string, 8> DirectoryNames;
308
309 /// SourceFileIdMap - Source file name to source file id map.
310 ///
311 StringMap<unsigned> SourceFileIdMap;
312
313 /// SourceFileNames - A list of source file names.
314 SmallVector<std::string, 8> SourceFileNames;
315
316 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
317 /// id mapped to a unique id.
318 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
319
320 /// SourceIds - Reverse map from source id to directory id + file id pair.
321 ///
322 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel8526cc02009-01-05 22:35:52 +0000323
Devang Patel07354e52009-01-16 21:07:53 +0000324 /// Lines - List of of source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +0000325 std::vector<SrcLineInfo> Lines;
326
Devang Patel07354e52009-01-16 21:07:53 +0000327 /// ValuesSet - Used to uniquely define values.
328 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000329 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000330
Jim Laskeyef42a012006-11-02 20:12:39 +0000331 /// Values - A list of all the unique values in use.
332 ///
333 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000334
Jim Laskey65195462006-10-30 13:35:07 +0000335 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +0000336 ///
Jim Laskey65195462006-10-30 13:35:07 +0000337 UniqueVector<std::string> StringPool;
338
Jim Laskey65195462006-10-30 13:35:07 +0000339 /// SectionMap - Provides a unique id per text section.
340 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +0000341 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000342
Jim Laskey65195462006-10-30 13:35:07 +0000343 /// SectionSourceLines - Tracks line numbers per text section.
344 ///
Devang Patelf3ee5142009-01-12 22:54:42 +0000345 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +0000346
Jim Laskeybacd3042007-02-21 22:48:45 +0000347 /// didInitial - Flag to indicate if initial emission has been done.
348 ///
349 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000350
Jim Laskeybacd3042007-02-21 22:48:45 +0000351 /// shouldEmit - Flag to indicate if debug information should be emitted.
352 ///
353 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +0000354
Devang Patel7d2f9722009-04-15 20:41:31 +0000355 // FunctionDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +0000356 //
Devang Patel7d2f9722009-04-15 20:41:31 +0000357 DbgScope *FunctionDbgScope;
Devang Patel7a6e5a32009-01-08 02:33:41 +0000358
Bill Wendling163ba3f2009-03-10 21:23:25 +0000359 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +0000360 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +0000361
Bill Wendlingf59d10f2009-05-13 23:55:49 +0000362 /// DbgAbstractScopeMap - Tracks abstract instance scopes in the current
363 /// function.
364 DenseMap<GlobalVariable *, DbgScope *> DbgAbstractScopeMap;
365
366 /// DbgConcreteScopeMap - Tracks concrete instance scopes in the current
367 /// function.
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000368 DenseMap<GlobalVariable *,
Bill Wendlingf59d10f2009-05-13 23:55:49 +0000369 SmallVector<DbgScope *, 8> > DbgConcreteScopeMap;
Devang Patel1be3ecc2009-04-15 00:10:26 +0000370
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000371 /// InlineInfo - Keep track of inlined functions and their location. This
372 /// information is used to populate debug_inlined section.
Devang Patel0f7fef32009-04-13 17:02:03 +0000373 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
374
Devang Patel1be3ecc2009-04-15 00:10:26 +0000375 /// InlinedVariableScopes - Scopes information for the inlined subroutine
376 /// variables.
377 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
378
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000379 /// AbstractInstanceRootMap - Map of abstract instance roots of inlined
380 /// functions. These are subroutine entries that contain a DW_AT_inline
381 /// attribute.
382 DenseMap<const GlobalVariable *, DbgScope *> AbstractInstanceRootMap;
383
384 /// AbstractInstanceRootList - List of abstract instance roots of inlined
385 /// functions. These are subroutine entries that contain a DW_AT_inline
386 /// attribute.
387 SmallVector<DbgScope *, 32> AbstractInstanceRootList;
388
389 /// LexicalScopeStack - A stack of lexical scopes. The top one is the current
390 /// scope.
391 SmallVector<DbgScope *, 16> LexicalScopeStack;
392
Bill Wendlinge688faf2009-05-08 21:03:15 +0000393 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
394 /// used when calculating the "origin" of a concrete instance of an inlined
395 /// function.
396 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
397
Bill Wendling163ba3f2009-03-10 21:23:25 +0000398 /// DebugTimer - Timer for the Dwarf debug writer.
399 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +0000400
Anton Korobeynikov185bc892007-05-13 17:30:11 +0000401 struct FunctionDebugFrameInfo {
402 unsigned Number;
403 std::vector<MachineMove> Moves;
404
405 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +0000406 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +0000407 };
408
409 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000410
Bill Wendling163ba3f2009-03-10 21:23:25 +0000411private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +0000412 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +0000413 /// maps to the source id. Source id starts at 1.
414 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +0000415 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +0000416 return SourceIds[SId-1];
417 }
418
419 /// getNumSourceDirectories - Return the number of source directories in the
420 /// debug info.
421 unsigned getNumSourceDirectories() const {
422 return DirectoryNames.size();
423 }
424
425 /// getSourceDirectoryName - Return the name of the directory corresponding
426 /// to the id.
427 const std::string &getSourceDirectoryName(unsigned Id) const {
428 return DirectoryNames[Id - 1];
429 }
430
431 /// getSourceFileName - Return the name of the source file corresponding
432 /// to the id.
433 const std::string &getSourceFileName(unsigned Id) const {
434 return SourceFileNames[Id - 1];
435 }
436
437 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +0000438 unsigned getNumSourceIds() const {
439 return SourceIds.size();
440 }
441
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000442 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000443 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000444 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
445 // Profile the node so that we can make it unique.
446 FoldingSetNodeID ID;
447 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000448
Jim Laskeyef42a012006-11-02 20:12:39 +0000449 // Check the set for priors.
450 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000451
Jim Laskeyef42a012006-11-02 20:12:39 +0000452 // If it's newly added.
453 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000454 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +0000455 Abbreviations.push_back(&Abbrev);
456 // Assign the vector position + 1 as its number.
457 Abbrev.setNumber(Abbreviations.size());
458 } else {
459 // Assign existing abbreviation number.
460 Abbrev.setNumber(InSet->getNumber());
461 }
462 }
463
Jim Laskey65195462006-10-30 13:35:07 +0000464 /// NewString - Add a string to the constant pool and returns a label.
465 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000466 DWLabel NewString(const std::string &String) {
467 unsigned StringID = StringPool.insert(String);
468 return DWLabel("string", StringID);
469 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000470
Bill Wendling88423ee2009-05-15 00:11:17 +0000471 /// NewDIEEntry - Creates a new DIEEntry to be a proxy for a debug information
Jim Laskeyef42a012006-11-02 20:12:39 +0000472 /// entry.
Bill Wendling88423ee2009-05-15 00:11:17 +0000473 DIEEntry *NewDIEEntry(DIE *Entry = NULL) {
474 DIEEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000475
Jim Laskeyef42a012006-11-02 20:12:39 +0000476 if (Entry) {
477 FoldingSetNodeID ID;
Bill Wendling88423ee2009-05-15 00:11:17 +0000478 DIEEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +0000479 void *Where;
Bill Wendling88423ee2009-05-15 00:11:17 +0000480 Value = static_cast<DIEEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000481
Jim Laskeyf6733882006-11-02 21:48:18 +0000482 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000483
Bill Wendling88423ee2009-05-15 00:11:17 +0000484 Value = new DIEEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +0000485 ValuesSet.InsertNode(Value, Where);
486 } else {
Bill Wendling88423ee2009-05-15 00:11:17 +0000487 Value = new DIEEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +0000488 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000489
Jim Laskeyef42a012006-11-02 20:12:39 +0000490 Values.push_back(Value);
491 return Value;
492 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000493
Bill Wendling88423ee2009-05-15 00:11:17 +0000494 /// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
Jim Laskeyef42a012006-11-02 20:12:39 +0000495 ///
Bill Wendling88423ee2009-05-15 00:11:17 +0000496 void SetDIEEntry(DIEEntry *Value, DIE *Entry) {
Bill Wendlingdd446322009-03-10 23:57:09 +0000497 Value->setEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +0000498 // Add to values set if not already there. If it is, we merely have a
499 // duplicate in the values list (no harm.)
500 ValuesSet.GetOrInsertNode(Value);
501 }
502
503 /// AddUInt - Add an unsigned integer attribute data and value.
504 ///
505 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
506 if (!Form) Form = DIEInteger::BestForm(false, Integer);
507
508 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000509 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +0000510 void *Where;
511 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
512 if (!Value) {
513 Value = new DIEInteger(Integer);
514 ValuesSet.InsertNode(Value, Where);
515 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000516 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000517
Jim Laskeyef42a012006-11-02 20:12:39 +0000518 Die->AddValue(Attribute, Form, Value);
519 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000520
Jim Laskeyef42a012006-11-02 20:12:39 +0000521 /// AddSInt - Add an signed integer attribute data and value.
522 ///
523 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
524 if (!Form) Form = DIEInteger::BestForm(true, Integer);
525
526 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000527 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +0000528 void *Where;
529 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
530 if (!Value) {
531 Value = new DIEInteger(Integer);
532 ValuesSet.InsertNode(Value, Where);
533 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000534 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000535
Jim Laskeyef42a012006-11-02 20:12:39 +0000536 Die->AddValue(Attribute, Form, Value);
537 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000538
Evan Chenge3d42322009-02-25 07:04:34 +0000539 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000540 ///
541 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
542 const std::string &String) {
543 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000544 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +0000545 void *Where;
546 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
547 if (!Value) {
548 Value = new DIEString(String);
549 ValuesSet.InsertNode(Value, Where);
550 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000551 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000552
Jim Laskeyef42a012006-11-02 20:12:39 +0000553 Die->AddValue(Attribute, Form, Value);
554 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000555
Jim Laskeyef42a012006-11-02 20:12:39 +0000556 /// AddLabel - Add a Dwarf label attribute data and value.
557 ///
558 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling88423ee2009-05-15 00:11:17 +0000559 const DWLabel &Label) {
Jim Laskeyef42a012006-11-02 20:12:39 +0000560 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000561 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +0000562 void *Where;
563 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
564 if (!Value) {
565 Value = new DIEDwarfLabel(Label);
566 ValuesSet.InsertNode(Value, Where);
567 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000568 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000569
Jim Laskeyef42a012006-11-02 20:12:39 +0000570 Die->AddValue(Attribute, Form, Value);
571 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000572
Jim Laskeyef42a012006-11-02 20:12:39 +0000573 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
574 ///
575 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
576 const std::string &Label) {
577 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000578 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +0000579 void *Where;
580 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
581 if (!Value) {
582 Value = new DIEObjectLabel(Label);
583 ValuesSet.InsertNode(Value, Where);
584 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000585 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000586
Jim Laskeyef42a012006-11-02 20:12:39 +0000587 Die->AddValue(Attribute, Form, Value);
588 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000589
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000590 /// AddSectionOffset - Add a section offset label attribute data and value.
591 ///
592 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
593 const DWLabel &Label, const DWLabel &Section,
594 bool isEH = false, bool useSet = true) {
595 FoldingSetNodeID ID;
596 DIESectionOffset::Profile(ID, Label, Section);
597 void *Where;
598 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
599 if (!Value) {
600 Value = new DIESectionOffset(Label, Section, isEH, useSet);
601 ValuesSet.InsertNode(Value, Where);
602 Values.push_back(Value);
603 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000604
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000605 Die->AddValue(Attribute, Form, Value);
606 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000607
Jim Laskeyef42a012006-11-02 20:12:39 +0000608 /// AddDelta - Add a label delta attribute data and value.
609 ///
610 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel1be3ecc2009-04-15 00:10:26 +0000611 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyef42a012006-11-02 20:12:39 +0000612 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000613 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +0000614 void *Where;
615 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
616 if (!Value) {
617 Value = new DIEDelta(Hi, Lo);
618 ValuesSet.InsertNode(Value, Where);
619 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000620 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000621
Jim Laskeyef42a012006-11-02 20:12:39 +0000622 Die->AddValue(Attribute, Form, Value);
623 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000624
Bill Wendling88423ee2009-05-15 00:11:17 +0000625 /// AddDIEEntry - Add a DIE attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000626 ///
Bill Wendling88423ee2009-05-15 00:11:17 +0000627 void AddDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
628 Die->AddValue(Attribute, Form, NewDIEEntry(Entry));
Jim Laskeyef42a012006-11-02 20:12:39 +0000629 }
630
631 /// AddBlock - Add block data.
632 ///
633 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000634 Block->ComputeSize(TD);
Jim Laskeyef42a012006-11-02 20:12:39 +0000635 FoldingSetNodeID ID;
636 Block->Profile(ID);
637 void *Where;
638 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
639 if (!Value) {
640 Value = Block;
641 ValuesSet.InsertNode(Value, Where);
642 Values.push_back(Value);
643 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +0000644 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +0000645 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +0000646 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +0000647 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000648
Jim Laskeyef42a012006-11-02 20:12:39 +0000649 Die->AddValue(Attribute, Block->BestForm(), Value);
650 }
651
Jim Laskey65195462006-10-30 13:35:07 +0000652 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +0000653 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +0000654 void AddSourceLine(DIE *Die, const DIVariable *V) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000655 // If there is no compile unit specified, don't add a line #.
656 if (V->getCompileUnit().isNull())
657 return;
658
Devang Patel7a6e5a32009-01-08 02:33:41 +0000659 unsigned Line = V->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000660 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
661 assert(FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +0000662 AddUInt(Die, DW_AT_decl_file, 0, FileID);
663 AddUInt(Die, DW_AT_decl_line, 0, Line);
664 }
665
666 /// AddSourceLine - Add location information to specified debug information
667 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +0000668 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000669 // If there is no compile unit specified, don't add a line #.
670 if (G->getCompileUnit().isNull())
671 return;
Devang Patel8526cc02009-01-05 22:35:52 +0000672 unsigned Line = G->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000673 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
674 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +0000675 AddUInt(Die, DW_AT_decl_file, 0, FileID);
676 AddUInt(Die, DW_AT_decl_line, 0, Line);
677 }
678
Devang Patel99ec3532009-01-16 19:28:14 +0000679 void AddSourceLine(DIE *Die, const DIType *Ty) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000680 // If there is no compile unit specified, don't add a line #.
Devang Pateldd9db662009-01-30 18:20:31 +0000681 DICompileUnit CU = Ty->getCompileUnit();
682 if (CU.isNull())
683 return;
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000684
685 unsigned Line = Ty->getLineNumber();
686 unsigned FileID = FindCompileUnit(CU).getID();
687 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +0000688 AddUInt(Die, DW_AT_decl_file, 0, FileID);
689 AddUInt(Die, DW_AT_decl_line, 0, Line);
690 }
691
Jim Laskey65195462006-10-30 13:35:07 +0000692 /// AddAddress - Add an address attribute to a die based on the location
693 /// provided.
694 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel1be3ecc2009-04-15 00:10:26 +0000695 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +0000696 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +0000697 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000698
Dan Gohmand735b802008-10-03 15:45:36 +0000699 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +0000700 if (Reg < 32) {
701 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
702 } else {
703 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
704 AddUInt(Block, 0, DW_FORM_udata, Reg);
705 }
706 } else {
707 if (Reg < 32) {
708 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
709 } else {
710 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
711 AddUInt(Block, 0, DW_FORM_udata, Reg);
712 }
713 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
714 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000715
Jim Laskeyef42a012006-11-02 20:12:39 +0000716 AddBlock(Die, Attribute, 0, Block);
717 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000718
Jim Laskeyef42a012006-11-02 20:12:39 +0000719 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +0000720 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +0000721 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +0000722 return;
Devang Patel48d190f2009-01-05 21:47:57 +0000723
724 // Check for pre-existence.
Bill Wendling88423ee2009-05-15 00:11:17 +0000725 DIEEntry *&Slot = DW_Unit->getDIEEntrySlotFor(Ty.getGV());
Devang Patel48d190f2009-01-05 21:47:57 +0000726 // If it exists then use the existing value.
727 if (Slot) {
728 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
729 return;
730 }
731
732 // Set up proxy.
Bill Wendling88423ee2009-05-15 00:11:17 +0000733 Slot = NewDIEEntry();
Devang Patel48d190f2009-01-05 21:47:57 +0000734
735 // Construct type.
736 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +0000737 if (Ty.isBasicType(Ty.getTag()))
738 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
739 else if (Ty.isDerivedType(Ty.getTag()))
740 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
741 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +0000742 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +0000743 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
744 }
745
Devang Patel7009d242009-01-27 23:22:55 +0000746 // Add debug information entry to entity and appropriate context.
747 DIE *Die = NULL;
748 DIDescriptor Context = Ty.getContext();
749 if (!Context.isNull())
750 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
751
752 if (Die) {
753 DIE *Child = new DIE(Buffer);
754 Die->AddChild(Child);
755 Buffer.Detach();
Bill Wendling88423ee2009-05-15 00:11:17 +0000756 SetDIEEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +0000757 } else {
Devang Patel7009d242009-01-27 23:22:55 +0000758 Die = DW_Unit->AddDie(Buffer);
Bill Wendling88423ee2009-05-15 00:11:17 +0000759 SetDIEEntry(Slot, Die);
Devang Patel7009d242009-01-27 23:22:55 +0000760 }
761
Devang Patel48d190f2009-01-05 21:47:57 +0000762 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
763 }
764
Devang Patele5202732009-01-05 19:07:53 +0000765 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
766 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +0000767 DIBasicType BTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +0000768
Devang Patel08f053f2009-01-05 17:57:47 +0000769 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +0000770 std::string Name;
771 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +0000772 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +0000773 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +0000774 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +0000775 if (!Name.empty())
Devang Patel08f053f2009-01-05 17:57:47 +0000776 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +0000777 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +0000778 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
779 }
780
Devang Patele5202732009-01-05 19:07:53 +0000781 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
782 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +0000783 DIDerivedType DTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +0000784
Devang Patel08f053f2009-01-05 17:57:47 +0000785 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +0000786 std::string Name;
787 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +0000788 uint64_t Size = DTy.getSizeInBits() >> 3;
789 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000790
Devang Patel08f053f2009-01-05 17:57:47 +0000791 // FIXME - Workaround for templates.
792 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
793
794 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000795
Devang Patel08f053f2009-01-05 17:57:47 +0000796 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +0000797 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +0000798 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +0000799
800 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +0000801 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +0000802 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +0000803
804 // Add size if non-zero (derived types might be zero-sized.)
805 if (Size)
806 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
807
808 // Add source line info if available and TyDesc is not a forward
809 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +0000810 if (!DTy.isForwardDecl())
811 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +0000812 }
813
Devang Patelf4215332009-01-05 19:55:51 +0000814 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
815 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +0000816 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +0000817 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +0000818 std::string Name;
819 CTy.getName(Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000820
Devang Patelf193ff02009-01-15 19:26:23 +0000821 uint64_t Size = CTy.getSizeInBits() >> 3;
822 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +0000823 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000824
Devang Patelf4215332009-01-05 19:55:51 +0000825 switch (Tag) {
826 case DW_TAG_vector_type:
827 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +0000828 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +0000829 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +0000830 case DW_TAG_enumeration_type:
831 {
832 DIArray Elements = CTy.getTypeArray();
833 // Add enumerators to enumeration type.
834 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
835 DIE *ElemDie = NULL;
836 DIEnumerator Enum(Elements.getElement(i).getGV());
837 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
838 Buffer.AddChild(ElemDie);
839 }
840 }
841 break;
Devang Patelf4215332009-01-05 19:55:51 +0000842 case DW_TAG_subroutine_type:
843 {
Devang Patelf4215332009-01-05 19:55:51 +0000844 // Add return type.
Bill Wendling3f500d92009-05-06 21:21:34 +0000845 DIArray Elements = CTy.getTypeArray();
Devang Patel48d190f2009-01-05 21:47:57 +0000846 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +0000847 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +0000848
Bill Wendling3f500d92009-05-06 21:21:34 +0000849 // Add prototype flag.
850 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
851
Devang Patelf4215332009-01-05 19:55:51 +0000852 // Add arguments.
853 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
854 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +0000855 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +0000856 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +0000857 Buffer.AddChild(Arg);
858 }
859 }
860 break;
861 case DW_TAG_structure_type:
862 case DW_TAG_union_type:
Devang Patel86bda412009-03-25 00:28:40 +0000863 case DW_TAG_class_type:
Devang Patelf4215332009-01-05 19:55:51 +0000864 {
865 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +0000866 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +0000867
868 // A forward struct declared type may not have elements available.
869 if (Elements.isNull())
870 break;
871
Devang Patelf4215332009-01-05 19:55:51 +0000872 // Add elements to structure type.
873 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
874 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +0000875 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +0000876 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +0000877 ElemDie = CreateSubprogramDIE(DW_Unit,
878 DISubprogram(Element.getGV()));
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000879 else if (Element.getTag() == dwarf::DW_TAG_variable) // ??
Devang Patel5aac3d32009-01-17 08:01:33 +0000880 ElemDie = CreateGlobalVariableDIE(DW_Unit,
881 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +0000882 else
883 ElemDie = CreateMemberDIE(DW_Unit,
884 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +0000885 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +0000886 }
Mike Stumpa6815152009-05-14 18:45:49 +0000887
888 // FIXME: We'd like an API to register additional attributes for the
889 // frontend to use while synthesizing, and then we'd use that api in
890 // clang instead of this.
891 if (Name == "__block_literal_generic")
892 AddUInt(&Buffer, DW_AT_APPLE_block, DW_FORM_flag, 1);
893
Devang Patel13319ce2009-02-17 22:43:44 +0000894 unsigned RLang = CTy.getRunTimeLang();
895 if (RLang)
896 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +0000897 }
898 break;
899 default:
900 break;
901 }
902
903 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +0000904 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +0000905 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +0000906
Devang Patelad165be2009-01-27 00:45:04 +0000907 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
908 || Tag == DW_TAG_union_type) {
909 // Add size if non-zero (derived types might be zero-sized.)
910 if (Size)
911 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
912 else {
913 // Add zero size if it is not a forward declaration.
914 if (CTy.isForwardDecl())
915 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
916 else
917 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
918 }
919
920 // Add source line info if available.
921 if (!CTy.isForwardDecl())
922 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +0000923 }
Devang Patelf4215332009-01-05 19:55:51 +0000924 }
925
Bill Wendlingb9dcef22009-02-03 21:17:20 +0000926 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
927 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +0000928 int64_t L = SR.getLo();
929 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +0000930 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
931 if (L != H) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000932 AddDIEEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +0000933 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +0000934 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
935 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +0000936 }
937 Buffer.AddChild(DW_Subrange);
938 }
939
940 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
941 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
942 DICompositeType *CTy) {
943 Buffer.setTag(DW_TAG_array_type);
944 if (CTy->getTag() == DW_TAG_vector_type)
945 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
946
Devang Patelf9235742009-01-28 21:08:20 +0000947 // Emit derived type.
948 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +0000949 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +0000950
951 // Construct an anonymous type for index type.
952 DIE IdxBuffer(DW_TAG_base_type);
953 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
954 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
955 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
956
957 // Add subranges to array type.
958 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +0000959 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +0000960 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
961 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +0000962 }
963 }
964
Bill Wendlingb9dcef22009-02-03 21:17:20 +0000965 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +0000966 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +0000967
968 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling0582ae92009-03-13 04:39:26 +0000969 std::string Name;
970 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +0000971 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +0000972 int64_t Value = ETy->getEnumValue();
973 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +0000974 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +0000975 }
Devang Patel68afdc32009-01-05 18:33:01 +0000976
Devang Patel5aac3d32009-01-17 08:01:33 +0000977 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +0000978 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +0000979 {
980 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling0582ae92009-03-13 04:39:26 +0000981 std::string Name;
982 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +0000983 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +0000984 std::string LinkageName;
985 GV.getLinkageName(LinkageName);
986 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +0000987 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
988 AddType(DW_Unit, GVDie, GV.getType());
989 if (!GV.isLocalToUnit())
990 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
991 AddSourceLine(GVDie, &GV);
992 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +0000993 }
994
Devang Patel2be58932009-01-20 21:02:02 +0000995 /// CreateMemberDIE - Create new member DIE.
996 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
997 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling0582ae92009-03-13 04:39:26 +0000998 std::string Name;
999 DT.getName(Name);
1000 if (!Name.empty())
Devang Patel2be58932009-01-20 21:02:02 +00001001 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1002
1003 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1004
1005 AddSourceLine(MemberDie, &DT);
1006
Devang Patel36375ee2009-02-17 21:23:59 +00001007 uint64_t Size = DT.getSizeInBits();
1008 uint64_t FieldSize = DT.getOriginalTypeSize();
1009
1010 if (Size != FieldSize) {
1011 // Handle bitfield.
1012 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1013 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1014
1015 uint64_t Offset = DT.getOffsetInBits();
1016 uint64_t FieldOffset = Offset;
1017 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1018 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1019 FieldOffset = (HiMark - FieldSize);
1020 Offset -= FieldOffset;
1021 // Maybe we need to work from the other end.
1022 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1023 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1024 }
Devang Patel2be58932009-01-20 21:02:02 +00001025 DIEBlock *Block = new DIEBlock();
1026 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1027 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1028 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1029
Devang Patel47661592009-01-21 00:08:04 +00001030 if (DT.isProtected())
1031 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1032 else if (DT.isPrivate())
1033 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1034
Devang Patel2be58932009-01-20 21:02:02 +00001035 return MemberDie;
1036 }
1037
Devang Patel5aac3d32009-01-17 08:01:33 +00001038 /// CreateSubprogramDIE - Create new DIE using SP.
1039 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendling3f500d92009-05-06 21:21:34 +00001040 const DISubprogram &SP,
Devang Patel2d1768c2009-01-17 08:05:14 +00001041 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001042 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling3f500d92009-05-06 21:21:34 +00001043
Bill Wendling0582ae92009-03-13 04:39:26 +00001044 std::string Name;
1045 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001046 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling3f500d92009-05-06 21:21:34 +00001047
Bill Wendling0582ae92009-03-13 04:39:26 +00001048 std::string LinkageName;
1049 SP.getLinkageName(LinkageName);
Bill Wendling3f500d92009-05-06 21:21:34 +00001050
Bill Wendling0582ae92009-03-13 04:39:26 +00001051 if (!LinkageName.empty())
Bill Wendling3f500d92009-05-06 21:21:34 +00001052 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1053
Devang Patel5aac3d32009-01-17 08:01:33 +00001054 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001055
Devang Patel5aac3d32009-01-17 08:01:33 +00001056 DICompositeType SPTy = SP.getType();
1057 DIArray Args = SPTy.getTypeArray();
Bill Wendling3f500d92009-05-06 21:21:34 +00001058
1059 // Add prototyped tag, if C or ObjC.
1060 unsigned Lang = SP.getCompileUnit().getLanguage();
1061 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89 || Lang == DW_LANG_ObjC)
1062 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patel5aac3d32009-01-17 08:01:33 +00001063
Devang Patel86ae1422009-01-05 18:59:44 +00001064 // Add Return Type.
Devang Patel75b27382009-04-08 22:18:45 +00001065 unsigned SPTag = SPTy.getTag();
Devang Patel9ac08d62009-02-27 18:05:21 +00001066 if (!IsConstructor) {
Devang Patel75b27382009-04-08 22:18:45 +00001067 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel9ac08d62009-02-27 18:05:21 +00001068 AddType(DW_Unit, SPDie, SPTy);
1069 else
1070 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1071 }
Devang Pateld234e592009-01-30 01:21:46 +00001072
Devang Pateld5863dd2009-02-02 17:51:41 +00001073 if (!SP.isDefinition()) {
1074 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00001075 // Add arguments. Do not add arguments for subprogram definition. They
1076 // will be handled through RecordVariable.
Devang Patel75b27382009-04-08 22:18:45 +00001077 if (SPTag == DW_TAG_subroutine_type)
Devang Pateld5863dd2009-02-02 17:51:41 +00001078 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1079 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1080 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
Bill Wendling3f500d92009-05-06 21:21:34 +00001081 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ??
Devang Pateld5863dd2009-02-02 17:51:41 +00001082 SPDie->AddChild(Arg);
1083 }
1084 }
Devang Pateld234e592009-01-30 01:21:46 +00001085
Devang Patelf193ff02009-01-15 19:26:23 +00001086 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00001087 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00001088
Devang Patel1be3ecc2009-04-15 00:10:26 +00001089 // DW_TAG_inlined_subroutine may refer to this DIE.
1090 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV());
1091 Slot = SPDie;
Devang Patel5aac3d32009-01-17 08:01:33 +00001092 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001093 }
1094
Devang Patel5aac3d32009-01-17 08:01:33 +00001095 /// FindCompileUnit - Get the compile unit for the given descriptor.
1096 ///
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001097 CompileUnit &FindCompileUnit(DICompileUnit Unit) const {
1098 DenseMap<Value *, CompileUnit *>::const_iterator I =
1099 CompileUnitMap.find(Unit.getGV());
1100 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1101 return *I->second;
Devang Patel8526cc02009-01-05 22:35:52 +00001102 }
1103
Devang Patelbbdc8202009-01-13 23:54:55 +00001104 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001105 ///
1106 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1107 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00001108 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001109
1110 // Translate tag to proper Dwarf tag. The result variable is dropped for
1111 // now.
1112 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00001113 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001114 case DW_TAG_return_variable: return NULL;
1115 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1116 case DW_TAG_auto_variable: // fall thru
1117 default: Tag = DW_TAG_variable; break;
1118 }
1119
1120 // Define variable debug information entry.
1121 DIE *VariableDie = new DIE(Tag);
Bill Wendling0582ae92009-03-13 04:39:26 +00001122 std::string Name;
1123 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001124 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001125
1126 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00001127 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001128
1129 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00001130 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001131
1132 // Add variable address.
1133 MachineLocation Location;
1134 Location.set(RI->getFrameRegister(*MF),
1135 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1136 AddAddress(VariableDie, DW_AT_location, Location);
1137
1138 return VariableDie;
1139 }
1140
Devang Patel7a6e5a32009-01-08 02:33:41 +00001141 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1142 ///
1143 DbgScope *getOrCreateScope(GlobalVariable *V) {
1144 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001145 if (Slot) return Slot;
1146
Devang Patel1be3ecc2009-04-15 00:10:26 +00001147 DbgScope *Parent = NULL;
1148 DIBlock Block(V);
Bill Wendling3f500d92009-05-06 21:21:34 +00001149
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001150 // Don't create a new scope if we already created one for an inlined
1151 // function.
1152 DenseMap<const GlobalVariable *, DbgScope *>::iterator
1153 II = AbstractInstanceRootMap.find(V);
1154 if (II != AbstractInstanceRootMap.end())
1155 return LexicalScopeStack.back();
1156
Devang Patel1be3ecc2009-04-15 00:10:26 +00001157 if (!Block.isNull()) {
1158 DIDescriptor ParentDesc = Block.getContext();
1159 Parent =
1160 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001161 }
Bill Wendling3f500d92009-05-06 21:21:34 +00001162
Devang Patel1be3ecc2009-04-15 00:10:26 +00001163 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001164
Devang Patel1be3ecc2009-04-15 00:10:26 +00001165 if (Parent)
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001166 Parent->AddScope(Slot);
Devang Patel1be3ecc2009-04-15 00:10:26 +00001167 else
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001168 // First function is top level function.
Devang Patel7d2f9722009-04-15 20:41:31 +00001169 FunctionDbgScope = Slot;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001170
Devang Patel7a6e5a32009-01-08 02:33:41 +00001171 return Slot;
1172 }
1173
1174 /// ConstructDbgScope - Construct the components of a scope.
1175 ///
1176 void ConstructDbgScope(DbgScope *ParentScope,
1177 unsigned ParentStartID, unsigned ParentEndID,
1178 DIE *ParentDie, CompileUnit *Unit) {
Dan Gohman9a38e3e2009-05-07 19:46:24 +00001179 // Add variables to scope.
1180 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
1181 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1182 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
1183 if (VariableDie) ParentDie->AddChild(VariableDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001184 }
1185
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001186 // Add concrete instances to scope.
1187 SmallVector<DbgConcreteScope *, 8> &ConcreteInsts = ParentScope->getConcreteInsts();
1188 for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) {
1189 DbgConcreteScope *ConcreteInst = ConcreteInsts[i];
1190 DIE *Die = ConcreteInst->getDie();
1191
1192 unsigned StartID = ConcreteInst->getStartLabelID();
1193 unsigned EndID = ConcreteInst->getEndLabelID();
1194
1195 // Add the scope bounds.
1196 if (StartID)
1197 AddLabel(Die, DW_AT_low_pc, DW_FORM_addr,
1198 DWLabel("label", StartID));
1199 else
1200 AddLabel(Die, DW_AT_low_pc, DW_FORM_addr,
1201 DWLabel("func_begin", SubprogramCount));
1202
1203 if (EndID)
1204 AddLabel(Die, DW_AT_high_pc, DW_FORM_addr,
1205 DWLabel("label", EndID));
1206 else
1207 AddLabel(Die, DW_AT_high_pc, DW_FORM_addr,
1208 DWLabel("func_end", SubprogramCount));
1209
1210 ParentDie->AddChild(Die);
1211 }
1212
Devang Patel7a6e5a32009-01-08 02:33:41 +00001213 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00001214 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001215 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1216 // Define the Scope debug information entry.
1217 DbgScope *Scope = Scopes[j];
Devang Patel7a6e5a32009-01-08 02:33:41 +00001218
Devang Patele9bfb0f2009-01-12 18:41:00 +00001219 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1220 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001221
Devang Patel1be3ecc2009-04-15 00:10:26 +00001222 // Ignore empty scopes.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001223 if (StartID == EndID && StartID != 0) continue;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001224
1225 // Do not ignore inlined scopes even if they don't have any variables or
1226 // scopes.
1227 if (Scope->getScopes().empty() && Scope->getVariables().empty() &&
1228 Scope->getConcreteInsts().empty())
Devang Patel0f7fef32009-04-13 17:02:03 +00001229 continue;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001230
1231 if (StartID == ParentStartID && EndID == ParentEndID) {
1232 // Just add stuff to the parent scope.
1233 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
1234 } else {
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001235 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Bill Wendling7fa81622009-05-01 08:25:13 +00001236
1237 // Add the scope bounds.
1238 if (StartID)
1239 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1240 DWLabel("label", StartID));
1241 else
1242 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1243 DWLabel("func_begin", SubprogramCount));
1244
1245 if (EndID)
1246 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1247 DWLabel("label", EndID));
1248 else
1249 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1250 DWLabel("func_end", SubprogramCount));
1251
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001252 // Add the scope's contents.
Bill Wendling7fa81622009-05-01 08:25:13 +00001253 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
1254 ParentDie->AddChild(ScopeDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00001255 }
1256 }
1257 }
1258
Devang Patel7d2f9722009-04-15 20:41:31 +00001259 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001260 ///
Devang Patel7d2f9722009-04-15 20:41:31 +00001261 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001262 // Exit if there is no root scope.
1263 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00001264 DIDescriptor Desc = RootScope->getDesc();
1265 if (Desc.isNull())
1266 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001267
1268 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00001269 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001270
1271 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00001272 CompileUnit *Unit = MainCU;
1273 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001274 Unit = &FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001275
1276 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00001277 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00001278 assert(SPDie && "Missing subprogram descriptor");
1279
1280 // Add the function bounds.
1281 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1282 DWLabel("func_begin", SubprogramCount));
1283 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1284 DWLabel("func_end", SubprogramCount));
1285 MachineLocation Location(RI->getFrameRegister(*MF));
1286 AddAddress(SPDie, DW_AT_frame_base, Location);
1287
1288 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
1289 }
1290
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001291 /// ConstructFunctionDbgScope - Construct the scope for the abstract debug
1292 /// scope.
1293 ///
1294 void ConstructAbstractDbgScope(DbgScope *AbsScope) {
1295 // Exit if there is no root scope.
1296 if (!AbsScope) return;
1297
1298 DIDescriptor Desc = AbsScope->getDesc();
1299 if (Desc.isNull())
1300 return;
1301
1302 // Get the subprogram debug information entry.
1303 DISubprogram SPD(Desc.getGV());
1304
1305 // Get the compile unit context.
1306 CompileUnit *Unit = MainCU;
1307 if (!Unit)
1308 Unit = &FindCompileUnit(SPD.getCompileUnit());
1309
1310 // Get the subprogram die.
1311 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
1312 assert(SPDie && "Missing subprogram descriptor");
1313
1314 ConstructDbgScope(AbsScope, 0, 0, SPDie, Unit);
1315 }
1316
Devang Patel7a6e5a32009-01-08 02:33:41 +00001317 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
1318 ///
1319 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00001320 const char *FnName = MF->getFunction()->getNameStart();
1321 if (MainCU) {
Bill Wendling972bbac2009-04-09 21:49:15 +00001322 StringMap<DIE*> &Globals = MainCU->getGlobals();
1323 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00001324 if (GI != Globals.end()) {
1325 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001326
1327 // Add the function bounds.
1328 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1329 DWLabel("func_begin", SubprogramCount));
1330 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1331 DWLabel("func_end", SubprogramCount));
1332
1333 MachineLocation Location(RI->getFrameRegister(*MF));
1334 AddAddress(SPDie, DW_AT_frame_base, Location);
1335 return;
1336 }
Evan Chenge3d42322009-02-25 07:04:34 +00001337 } else {
1338 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
1339 CompileUnit *Unit = CompileUnits[i];
Bill Wendling972bbac2009-04-09 21:49:15 +00001340 StringMap<DIE*> &Globals = Unit->getGlobals();
1341 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00001342 if (GI != Globals.end()) {
1343 DIE *SPDie = GI->second;
1344
1345 // Add the function bounds.
1346 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1347 DWLabel("func_begin", SubprogramCount));
1348 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1349 DWLabel("func_end", SubprogramCount));
1350
1351 MachineLocation Location(RI->getFrameRegister(*MF));
1352 AddAddress(SPDie, DW_AT_frame_base, Location);
1353 return;
1354 }
1355 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001356 }
Evan Chenge3d42322009-02-25 07:04:34 +00001357
Devang Patel7a6e5a32009-01-08 02:33:41 +00001358#if 0
1359 // FIXME: This is causing an abort because C++ mangled names are compared
1360 // with their unmangled counterparts. See PR2885. Don't do this assert.
1361 assert(0 && "Couldn't find DIE for machine function!");
1362#endif
1363 }
1364
Jim Laskeyef42a012006-11-02 20:12:39 +00001365 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1366 /// tools to recognize the object file contains Dwarf information.
1367 void EmitInitial() {
1368 // Check to see if we already emitted intial headers.
1369 if (didInitial) return;
1370 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001371
Jim Laskeyef42a012006-11-02 20:12:39 +00001372 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00001373 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001374 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001375 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00001376 }
1377 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1378 EmitLabel("section_info", 0);
1379 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1380 EmitLabel("section_abbrev", 0);
1381 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1382 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00001383 if (TAI->doesSupportMacInfoSection()) {
1384 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1385 EmitLabel("section_macinfo", 0);
1386 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001387 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1388 EmitLabel("section_line", 0);
1389 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1390 EmitLabel("section_loc", 0);
1391 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1392 EmitLabel("section_pubnames", 0);
1393 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1394 EmitLabel("section_str", 0);
1395 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1396 EmitLabel("section_ranges", 0);
1397
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001398 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00001399 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00001400 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00001401 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00001402 }
1403
Jim Laskey65195462006-10-30 13:35:07 +00001404 /// EmitDIE - Recusively Emits a debug information entry.
1405 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001406 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001407 // Get the abbreviation for this DIE.
1408 unsigned AbbrevNumber = Die->getAbbrevNumber();
1409 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001410
Jim Laskeybacd3042007-02-21 22:48:45 +00001411 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001412
1413 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001414 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00001415
Evan Cheng42bf74b2009-03-25 01:47:28 +00001416 if (Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00001417 Asm->EOL(std::string("Abbrev [" +
1418 utostr(AbbrevNumber) +
1419 "] 0x" + utohexstr(Die->getOffset()) +
1420 ":0x" + utohexstr(Die->getSize()) + " " +
1421 TagString(Abbrev->getTag())));
1422 else
1423 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001424
Owen Anderson873e1b52008-06-24 21:44:59 +00001425 SmallVector<DIEValue*, 32> &Values = Die->getValues();
1426 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001427
Jim Laskeyef42a012006-11-02 20:12:39 +00001428 // Emit the DIE attribute values.
1429 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1430 unsigned Attr = AbbrevData[i].getAttribute();
1431 unsigned Form = AbbrevData[i].getForm();
1432 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001433
Jim Laskeyef42a012006-11-02 20:12:39 +00001434 switch (Attr) {
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001435 case DW_AT_sibling:
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001436 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00001437 break;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001438 case DW_AT_abstract_origin: {
Bill Wendling88423ee2009-05-15 00:11:17 +00001439 DIEEntry *E = cast<DIEEntry>(Values[i]);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001440 DIE *Origin = E->getEntry();
1441 unsigned Addr =
1442 CompileUnitOffsets[Die->getAbstractCompileUnit()] +
1443 Origin->getOffset();
1444
1445 Asm->EmitInt32(Addr);
1446 break;
Jim Laskeyef42a012006-11-02 20:12:39 +00001447 }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001448 default:
Jim Laskeyef42a012006-11-02 20:12:39 +00001449 // Emit an attribute using the defined form.
Bill Wendling88423ee2009-05-15 00:11:17 +00001450 Values[i]->EmitValue(this, Form);
Jim Laskeyef42a012006-11-02 20:12:39 +00001451 break;
1452 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001453
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001454 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00001455 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001456
Jim Laskeyef42a012006-11-02 20:12:39 +00001457 // Emit the DIE children if any.
1458 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
1459 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001460
Bill Wendlinga9519572009-05-08 20:38:02 +00001461 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Jim Laskeyef42a012006-11-02 20:12:39 +00001462 EmitDIE(Children[j]);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001463
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001464 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00001465 }
1466 }
1467
Jim Laskey65195462006-10-30 13:35:07 +00001468 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1469 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001470 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1471 // Get the children.
1472 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001473
Jim Laskeyef42a012006-11-02 20:12:39 +00001474 // If not last sibling and has children then add sibling offset attribute.
1475 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1476
1477 // Record the abbreviation.
1478 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001479
Jim Laskeyef42a012006-11-02 20:12:39 +00001480 // Get the abbreviation for this DIE.
1481 unsigned AbbrevNumber = Die->getAbbrevNumber();
1482 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1483
1484 // Set DIE offset
1485 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001486
Jim Laskeyef42a012006-11-02 20:12:39 +00001487 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001488 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
1489
Owen Anderson873e1b52008-06-24 21:44:59 +00001490 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1491 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00001492
1493 // Size the DIE attribute values.
1494 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1495 // Size attribute value.
Bill Wendling88423ee2009-05-15 00:11:17 +00001496 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00001497 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001498
Jim Laskeyef42a012006-11-02 20:12:39 +00001499 // Size the DIE children if any.
1500 if (!Children.empty()) {
1501 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
1502 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001503
Jim Laskeyef42a012006-11-02 20:12:39 +00001504 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1505 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
1506 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001507
Jim Laskeyef42a012006-11-02 20:12:39 +00001508 // End of children marker.
1509 Offset += sizeof(int8_t);
1510 }
1511
1512 Die->setSize(Offset - Die->getOffset());
1513 return Offset;
1514 }
Jim Laskey65195462006-10-30 13:35:07 +00001515
1516 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1517 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001518 void SizeAndOffsets() {
Bill Wendlinge688faf2009-05-08 21:03:15 +00001519 // Compute size of compile unit header.
1520 static unsigned Offset =
1521 sizeof(int32_t) + // Length of Compilation Unit Info
1522 sizeof(int16_t) + // DWARF version number
1523 sizeof(int32_t) + // Offset Into Abbrev. Section
1524 sizeof(int8_t); // Pointer Size (in bytes)
1525
Jim Laskey5496f012006-11-09 14:52:14 +00001526 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00001527 if (MainCU) {
Devang Pateldd9db662009-01-30 18:20:31 +00001528 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
Bill Wendlinge688faf2009-05-08 21:03:15 +00001529 CompileUnitOffsets[MainCU] = 0;
Devang Pateldd9db662009-01-30 18:20:31 +00001530 return;
1531 }
Bill Wendlinge688faf2009-05-08 21:03:15 +00001532
1533 // Process all compile units.
1534 unsigned PrevOffset = 0;
1535
Evan Chenge3d42322009-02-25 07:04:34 +00001536 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
1537 CompileUnit *Unit = CompileUnits[i];
Bill Wendlinge688faf2009-05-08 21:03:15 +00001538 CompileUnitOffsets[Unit] = PrevOffset;
1539 PrevOffset += SizeAndOffsetDie(Unit->getDie(), Offset, true)
1540 + sizeof(int32_t); // FIXME - extra pad for gdb bug.
Devang Patel72b66352009-01-12 23:05:55 +00001541 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001542 }
1543
Evan Chenge3d42322009-02-25 07:04:34 +00001544 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00001545 ///
Evan Chenge3d42322009-02-25 07:04:34 +00001546 void EmitDebugInfoPerCU(CompileUnit *Unit) {
1547 DIE *Die = Unit->getDie();
1548 // Emit the compile units header.
1549 EmitLabel("info_begin", Unit->getID());
1550 // Emit size of content not including length itself
1551 unsigned ContentSize = Die->getSize() +
1552 sizeof(int16_t) + // DWARF version number
1553 sizeof(int32_t) + // Offset Into Abbrev. Section
1554 sizeof(int8_t) + // Pointer Size (in bytes)
1555 sizeof(int32_t); // FIXME - extra pad for gdb bug.
1556
1557 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
1558 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
1559 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
1560 Asm->EOL("Offset Into Abbrev. Section");
1561 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
1562
1563 EmitDIE(Die);
1564 // FIXME - extra padding for gdb bug.
1565 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
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 EmitLabel("info_end", Unit->getID());
1570
1571 Asm->EOL();
1572 }
1573
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001574 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00001575 // Start debug info section.
1576 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001577
Evan Chenge3d42322009-02-25 07:04:34 +00001578 if (MainCU) {
1579 EmitDebugInfoPerCU(MainCU);
1580 return;
Devang Patel72b66352009-01-12 23:05:55 +00001581 }
Evan Chenge3d42322009-02-25 07:04:34 +00001582
1583 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
1584 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00001585 }
1586
Jim Laskey65195462006-10-30 13:35:07 +00001587 /// EmitAbbreviations - Emit the abbreviation section.
1588 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001589 void EmitAbbreviations() const {
1590 // Check to see if it is worth the effort.
1591 if (!Abbreviations.empty()) {
1592 // Start the debug abbrev section.
1593 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001594
Jim Laskeyef42a012006-11-02 20:12:39 +00001595 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001596
Jim Laskeyef42a012006-11-02 20:12:39 +00001597 // For each abbrevation.
1598 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
1599 // Get abbreviation data
1600 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001601
Jim Laskeyef42a012006-11-02 20:12:39 +00001602 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001603 Asm->EmitULEB128Bytes(Abbrev->getNumber());
1604 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001605
Jim Laskeyef42a012006-11-02 20:12:39 +00001606 // Emit the abbreviations data.
Bill Wendling88423ee2009-05-15 00:11:17 +00001607 Abbrev->Emit(Asm);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001608
Jim Laskeybacd3042007-02-21 22:48:45 +00001609 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001610 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001611
Jim Laskey7b1b39d2007-02-22 18:22:42 +00001612 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00001613 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00001614
Jim Laskeyef42a012006-11-02 20:12:39 +00001615 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001616
Jim Laskeybacd3042007-02-21 22:48:45 +00001617 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001618 }
1619 }
1620
Bill Wendlingfbbd7012008-07-20 00:11:19 +00001621 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
1622 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001623 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00001624 void EmitEndOfLineMatrix(unsigned SectionEnd) {
1625 // Define last address of section.
1626 Asm->EmitInt8(0); Asm->EOL("Extended Op");
1627 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
1628 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
1629 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
1630
1631 // Mark end of matrix.
1632 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
1633 Asm->EmitULEB128Bytes(1); Asm->EOL();
1634 Asm->EmitInt8(1); Asm->EOL();
1635 }
1636
Jim Laskey65195462006-10-30 13:35:07 +00001637 /// EmitDebugLines - Emit source line information.
1638 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001639 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00001640 // If the target is using .loc/.file, the assembler will be emitting the
1641 // .debug_line table automatically.
1642 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00001643 return;
1644
Jim Laskeyef42a012006-11-02 20:12:39 +00001645 // Minimum line delta, thus ranging from -10..(255-10).
1646 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1647 // Maximum line delta, thus ranging from -10..(255-10).
1648 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00001649
Jim Laskeyef42a012006-11-02 20:12:39 +00001650 // Start the dwarf line section.
1651 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001652
Jim Laskeyef42a012006-11-02 20:12:39 +00001653 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001654
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001655 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001656 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00001657 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001658
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001659 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001660
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001661 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001662 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00001663 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001664
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001665 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00001666
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001667 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00001668
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001669 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001670
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001671 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00001672
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001673 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001674
Jim Laskeyef42a012006-11-02 20:12:39 +00001675 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001676 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
1677 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
1678 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
1679 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
1680 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
1681 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
1682 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
1683 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
1684 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00001685
Jim Laskeyef42a012006-11-02 20:12:39 +00001686 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00001687 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
1688 Asm->EmitString(getSourceDirectoryName(DI));
1689 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00001690 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001691 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001692
Jim Laskeyef42a012006-11-02 20:12:39 +00001693 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00001694 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
1695 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001696 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00001697 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001698 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00001699 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001700 Asm->EOL("Directory #");
1701 Asm->EmitULEB128Bytes(0);
1702 Asm->EOL("Mod date");
1703 Asm->EmitULEB128Bytes(0);
1704 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00001705 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001706 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001707
Jim Laskeyef42a012006-11-02 20:12:39 +00001708 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001709
Jim Laskeyef42a012006-11-02 20:12:39 +00001710 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00001711 unsigned SecSrcLinesSize = SectionSourceLines.size();
1712
1713 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001714 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00001715 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00001716
Evan Cheng42bf74b2009-03-25 01:47:28 +00001717 if (Asm->isVerbose()) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001718 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00001719 O << '\t' << TAI->getCommentString() << " Section"
1720 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001721 } else
Evan Cheng6547e402008-07-01 23:18:29 +00001722 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001723
1724 // Dwarf assumes we start with first line of first source file.
1725 unsigned Source = 1;
1726 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001727
Jim Laskeyef42a012006-11-02 20:12:39 +00001728 // Construct rows of the address, source, line, column matrix.
1729 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00001730 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001731 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00001732 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001733
Evan Cheng42bf74b2009-03-25 01:47:28 +00001734 if (!Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00001735 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00001736 else {
1737 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001738 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00001739 O << '\t' << TAI->getCommentString() << ' '
1740 << getSourceDirectoryName(SourceID.first) << ' '
1741 << getSourceFileName(SourceID.second)
1742 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
1743 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001744
1745 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001746 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00001747 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001748 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00001749 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001750
Jim Laskeyef42a012006-11-02 20:12:39 +00001751 // If change of source, then switch to the new source.
1752 if (Source != LineInfo.getSourceID()) {
1753 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001754 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
1755 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00001756 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001757
Jim Laskeyef42a012006-11-02 20:12:39 +00001758 // If change of line.
1759 if (Line != LineInfo.getLine()) {
1760 // Determine offset.
1761 int Offset = LineInfo.getLine() - Line;
1762 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001763
Jim Laskeyef42a012006-11-02 20:12:39 +00001764 // Update line.
1765 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001766
Jim Laskeyef42a012006-11-02 20:12:39 +00001767 // If delta is small enough and in range...
1768 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1769 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001770 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00001771 } else {
1772 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001773 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
1774 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
1775 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00001776 }
1777 } else {
1778 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001779 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00001780 }
1781 }
1782
Bill Wendlingfbbd7012008-07-20 00:11:19 +00001783 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001784 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00001785
1786 if (SecSrcLinesSize == 0)
1787 // Because we're emitting a debug_line section, we still need a line
1788 // table. The linker and friends expect it to exist. If there's nothing to
1789 // put into it, emit an empty table.
1790 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001791
Jim Laskeyef42a012006-11-02 20:12:39 +00001792 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001793
Jim Laskeybacd3042007-02-21 22:48:45 +00001794 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001795 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001796
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001797 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00001798 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001799 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00001800 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00001801 return;
1802
1803 int stackGrowth =
1804 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1805 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001806 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00001807
1808 // Start the dwarf frame section.
1809 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1810
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001811 EmitLabel("debug_frame_common", 0);
1812 EmitDifference("debug_frame_common_end", 0,
1813 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001814 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00001815
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001816 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001817 Asm->EmitInt32((int)DW_CIE_ID);
1818 Asm->EOL("CIE Identifier Tag");
1819 Asm->EmitInt8(DW_CIE_VERSION);
1820 Asm->EOL("CIE Version");
1821 Asm->EmitString("");
1822 Asm->EOL("CIE Augmentation");
1823 Asm->EmitULEB128Bytes(1);
1824 Asm->EOL("CIE Code Alignment Factor");
1825 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001826 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00001827 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001828 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001829
Jim Laskey5e73d5b2007-01-24 18:45:13 +00001830 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00001831 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001832
Dale Johannesenb97aec62007-11-13 19:13:01 +00001833 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001834
Evan Cheng05548eb2008-02-29 19:36:59 +00001835 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001836 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001837
Jim Laskeybacd3042007-02-21 22:48:45 +00001838 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001839 }
1840
Jim Laskey65195462006-10-30 13:35:07 +00001841 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
1842 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001843 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00001844 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00001845 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001846
Jim Laskeyef42a012006-11-02 20:12:39 +00001847 // Start the dwarf frame section.
1848 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001849
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001850 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
1851 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001852 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001853
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001854 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001855
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001856 EmitSectionOffset("debug_frame_common", "section_debug_frame",
1857 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001858 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00001859
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001860 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001861 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001862 EmitDifference("func_end", DebugFrameInfo.Number,
1863 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001864 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001865
Devang Patel5aac3d32009-01-17 08:01:33 +00001866 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00001867 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001868
Evan Cheng05548eb2008-02-29 19:36:59 +00001869 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001870 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00001871
Jim Laskeybacd3042007-02-21 22:48:45 +00001872 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001873 }
1874
Evan Chenge3d42322009-02-25 07:04:34 +00001875 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
1876 EmitDifference("pubnames_end", Unit->getID(),
1877 "pubnames_begin", Unit->getID(), true);
1878 Asm->EOL("Length of Public Names Info");
1879
1880 EmitLabel("pubnames_begin", Unit->getID());
1881
1882 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
1883
1884 EmitSectionOffset("info_begin", "section_info",
1885 Unit->getID(), 0, true, false);
1886 Asm->EOL("Offset of Compilation Unit Info");
1887
1888 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
1889 true);
1890 Asm->EOL("Compilation Unit Length");
1891
Bill Wendling972bbac2009-04-09 21:49:15 +00001892 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendlingf34be822009-04-09 23:51:31 +00001893 for (StringMap<DIE*>::const_iterator
Bill Wendling972bbac2009-04-09 21:49:15 +00001894 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendlingf34be822009-04-09 23:51:31 +00001895 const char *Name = GI->getKeyData();
Evan Chenge3d42322009-02-25 07:04:34 +00001896 DIE * Entity = GI->second;
1897
1898 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendlingf34be822009-04-09 23:51:31 +00001899 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Chenge3d42322009-02-25 07:04:34 +00001900 }
1901
1902 Asm->EmitInt32(0); Asm->EOL("End Mark");
1903 EmitLabel("pubnames_end", Unit->getID());
1904
1905 Asm->EOL();
1906 }
1907
Jim Laskeyef42a012006-11-02 20:12:39 +00001908 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00001909 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001910 void EmitDebugPubNames() {
1911 // Start the dwarf pubnames section.
1912 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001913
Evan Chenge3d42322009-02-25 07:04:34 +00001914 if (MainCU) {
1915 EmitDebugPubNamesPerCU(MainCU);
1916 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00001917 }
Evan Chenge3d42322009-02-25 07:04:34 +00001918
1919 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
1920 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00001921 }
1922
1923 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00001924 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001925 void EmitDebugStr() {
1926 // Check to see if it is worth the effort.
1927 if (!StringPool.empty()) {
1928 // Start the dwarf str section.
1929 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001930
Jim Laskeyef42a012006-11-02 20:12:39 +00001931 // For each of strings in the string pool.
1932 for (unsigned StringID = 1, N = StringPool.size();
1933 StringID <= N; ++StringID) {
1934 // Emit a label for reference from debug information entries.
1935 EmitLabel("string", StringID);
1936 // Emit the string itself.
1937 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00001938 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001939 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001940
Jim Laskeybacd3042007-02-21 22:48:45 +00001941 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001942 }
1943 }
1944
1945 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00001946 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001947 void EmitDebugLoc() {
1948 // Start the dwarf loc section.
1949 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001950
Jim Laskeybacd3042007-02-21 22:48:45 +00001951 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001952 }
1953
1954 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00001955 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001956 void EmitDebugARanges() {
1957 // Start the dwarf aranges section.
1958 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001959
Jim Laskeyef42a012006-11-02 20:12:39 +00001960 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00001961#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001962 CompileUnit *Unit = GetBaseCompileUnit();
1963
Jim Laskey5496f012006-11-09 14:52:14 +00001964 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001965 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001966
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001967 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001968
Jim Laskey5496f012006-11-09 14:52:14 +00001969 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001970 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00001971
Dan Gohman82482942007-09-27 23:12:31 +00001972 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00001973
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001974 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00001975
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001976 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
1977 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00001978
Jim Laskey5496f012006-11-09 14:52:14 +00001979 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001980 EmitReference("text_begin", 0); Asm->EOL("Address");
1981 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00001982
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001983 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
1984 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00001985#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001986
Jim Laskeybacd3042007-02-21 22:48:45 +00001987 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001988 }
1989
1990 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00001991 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001992 void EmitDebugRanges() {
1993 // Start the dwarf ranges section.
1994 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001995
Jim Laskeybacd3042007-02-21 22:48:45 +00001996 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001997 }
1998
1999 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002000 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002001 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002002 if (TAI->doesSupportMacInfoSection()) {
2003 // Start the dwarf macinfo section.
2004 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002005
Scott Michel210de722009-01-26 22:32:51 +00002006 Asm->EOL();
2007 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002008 }
2009
Devang Patel0f7fef32009-04-13 17:02:03 +00002010 /// EmitDebugInlineInfo - Emit inline info using following format.
2011 /// Section Header:
2012 /// 1. length of section
2013 /// 2. Dwarf version number
2014 /// 3. address size.
2015 ///
2016 /// Entries (one "entry" for each function that was inlined):
2017 ///
2018 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2019 /// otherwise offset into __debug_str for regular function name.
2020 /// 2. offset into __debug_str section for regular function name.
2021 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2022 /// instances for the function.
2023 ///
2024 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2025 /// inlined instance; the die_offset points to the inlined_subroutine die in
2026 /// the __debug_info section, and the low_pc is the starting address for the
2027 /// inlining instance.
2028 void EmitDebugInlineInfo() {
2029 if (!TAI->doesDwarfUsesInlineInfoSection())
2030 return;
2031
2032 if (!MainCU)
2033 return;
2034
2035 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2036 Asm->EOL();
2037 EmitDifference("debug_inlined_end", 1,
2038 "debug_inlined_begin", 1, true);
2039 Asm->EOL("Length of Debug Inlined Information Entry");
2040
2041 EmitLabel("debug_inlined_begin", 1);
2042
2043 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2044 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2045
2046 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2047 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2048 GlobalVariable *GV = I->first;
2049 SmallVector<unsigned, 4> &Labels = I->second;
2050 DISubprogram SP(GV);
2051 std::string Name;
2052 std::string LName;
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002053
Devang Patel0f7fef32009-04-13 17:02:03 +00002054 SP.getLinkageName(LName);
2055 SP.getName(Name);
2056
2057 Asm->EmitString(LName.empty() ? Name : LName);
2058 Asm->EOL("MIPS linkage name");
2059
2060 Asm->EmitString(Name); Asm->EOL("Function name");
2061
2062 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2063
2064 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2065 LE = Labels.end(); LI != LE; ++LI) {
2066 DIE *SP = MainCU->getDieMapSlotFor(GV);
2067 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2068
2069 if (TD->getPointerSize() == sizeof(int32_t))
2070 O << TAI->getData32bitsDirective();
2071 else
2072 O << TAI->getData64bitsDirective();
2073 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2074 }
2075 }
2076
2077 EmitLabel("debug_inlined_end", 1);
2078 Asm->EOL();
2079 }
2080
Bill Wendlingc8615e42009-03-10 21:47:45 +00002081 /// GetOrCreateSourceID - Look up the source id with the given directory and
2082 /// source file names. If none currently exists, create a new id and insert it
2083 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2084 /// as well.
2085 unsigned GetOrCreateSourceID(const std::string &DirName,
2086 const std::string &FileName) {
2087 unsigned DId;
2088 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
2089 if (DI != DirectoryIdMap.end()) {
2090 DId = DI->getValue();
2091 } else {
2092 DId = DirectoryNames.size() + 1;
2093 DirectoryIdMap[DirName] = DId;
2094 DirectoryNames.push_back(DirName);
2095 }
2096
2097 unsigned FId;
2098 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
2099 if (FI != SourceFileIdMap.end()) {
2100 FId = FI->getValue();
2101 } else {
2102 FId = SourceFileNames.size() + 1;
2103 SourceFileIdMap[FileName] = FId;
2104 SourceFileNames.push_back(FileName);
2105 }
2106
2107 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
2108 SourceIdMap.find(std::make_pair(DId, FId));
2109 if (SI != SourceIdMap.end())
2110 return SI->second;
2111
2112 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
2113 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
2114 SourceIds.push_back(std::make_pair(DId, FId));
2115
2116 return SrcId;
2117 }
2118
Evan Chenge3d42322009-02-25 07:04:34 +00002119 void ConstructCompileUnit(GlobalVariable *GV) {
2120 DICompileUnit DIUnit(GV);
Bill Wendling0582ae92009-03-13 04:39:26 +00002121 std::string Dir, FN, Prod;
2122 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2123 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00002124
2125 DIE *Die = new DIE(DW_TAG_compile_unit);
2126 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2127 DWLabel("section_line", 0), DWLabel("section_line", 0),
2128 false);
Bill Wendling0582ae92009-03-13 04:39:26 +00002129 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00002130 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002131 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendling0582ae92009-03-13 04:39:26 +00002132 if (!Dir.empty())
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00002133 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00002134 if (DIUnit.isOptimized())
2135 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling0582ae92009-03-13 04:39:26 +00002136 std::string Flags;
2137 DIUnit.getFlags(Flags);
2138 if (!Flags.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00002139 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
2140 unsigned RVer = DIUnit.getRunTimeVersion();
2141 if (RVer)
2142 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
2143
2144 CompileUnit *Unit = new CompileUnit(ID, Die);
2145 if (DIUnit.isMain()) {
2146 assert(!MainCU && "Multiple main compile units are found!");
2147 MainCU = Unit;
2148 }
2149 CompileUnitMap[DIUnit.getGV()] = Unit;
2150 CompileUnits.push_back(Unit);
2151 }
2152
Devang Patelc4523242009-01-05 23:11:11 +00002153 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002154 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00002155 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
2156 if (!Root)
2157 return;
2158 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2159 "Malformed compile unit descriptor anchor type");
2160 Constant *RootC = cast<Constant>(*Root->use_begin());
2161 assert(RootC->hasNUsesOrMore(1) &&
2162 "Malformed compile unit descriptor anchor type");
2163 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2164 UI != UE; ++UI)
2165 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
2166 UUI != UUE; ++UUI) {
2167 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
2168 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00002169 }
Evan Chenge3d42322009-02-25 07:04:34 +00002170 }
2171
2172 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
2173 DIGlobalVariable DI_GV(GV);
2174 CompileUnit *DW_Unit = MainCU;
2175 if (!DW_Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00002176 DW_Unit = &FindCompileUnit(DI_GV.getCompileUnit());
Evan Chenge3d42322009-02-25 07:04:34 +00002177
2178 // Check for pre-existence.
2179 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2180 if (Slot)
2181 return false;
2182
2183 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2184
2185 // Add address.
2186 DIEBlock *Block = new DIEBlock();
2187 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling7d16e852009-04-10 00:12:49 +00002188 std::string GLN;
Evan Chenge3d42322009-02-25 07:04:34 +00002189 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling7d16e852009-04-10 00:12:49 +00002190 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Chenge3d42322009-02-25 07:04:34 +00002191 AddBlock(VariableDie, DW_AT_location, 0, Block);
2192
2193 // Add to map.
2194 Slot = VariableDie;
Bill Wendling3f500d92009-05-06 21:21:34 +00002195
Evan Chenge3d42322009-02-25 07:04:34 +00002196 // Add to context owner.
2197 DW_Unit->getDie()->AddChild(VariableDie);
Bill Wendling3f500d92009-05-06 21:21:34 +00002198
Evan Chenge3d42322009-02-25 07:04:34 +00002199 // Expose as global. FIXME - need to check external flag.
Bill Wendling0582ae92009-03-13 04:39:26 +00002200 std::string Name;
2201 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002202 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00002203 }
2204
Devang Patelc4523242009-01-05 23:11:11 +00002205 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00002206 /// visible global variables. Return true if at least one global DIE is
2207 /// created.
2208 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00002209 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
2210 if (!Root)
2211 return false;
Devang Patelc4523242009-01-05 23:11:11 +00002212
Evan Chenge3d42322009-02-25 07:04:34 +00002213 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2214 "Malformed global variable descriptor anchor type");
2215 Constant *RootC = cast<Constant>(*Root->use_begin());
2216 assert(RootC->hasNUsesOrMore(1) &&
2217 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00002218
Evan Chenge3d42322009-02-25 07:04:34 +00002219 bool Result = false;
2220 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2221 UI != UE; ++UI)
2222 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00002223 UUI != UUE; ++UUI)
2224 Result |= ConstructGlobalVariableDIE(cast<GlobalVariable>(*UUI));
2225
Evan Chenge3d42322009-02-25 07:04:34 +00002226 return Result;
2227 }
Devang Patelc4523242009-01-05 23:11:11 +00002228
Evan Chenge3d42322009-02-25 07:04:34 +00002229 bool ConstructSubprogram(GlobalVariable *GV) {
2230 DISubprogram SP(GV);
2231 CompileUnit *Unit = MainCU;
2232 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00002233 Unit = &FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00002234
Evan Chenge3d42322009-02-25 07:04:34 +00002235 // Check for pre-existence.
2236 DIE *&Slot = Unit->getDieMapSlotFor(GV);
2237 if (Slot)
2238 return false;
2239
2240 if (!SP.isDefinition())
2241 // This is a method declaration which will be handled while
2242 // constructing class type.
2243 return false;
2244
2245 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
2246
2247 // Add to map.
2248 Slot = SubprogramDie;
2249 // Add to context owner.
2250 Unit->getDie()->AddChild(SubprogramDie);
2251 // Expose as global.
Bill Wendling0582ae92009-03-13 04:39:26 +00002252 std::string Name;
2253 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00002254 return true;
Devang Patelc4523242009-01-05 23:11:11 +00002255 }
2256
Devang Patel78eb6ad2009-01-05 23:21:35 +00002257 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00002258 /// subprograms. Return true if at least one subprogram DIE is created.
2259 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00002260 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
2261 if (!Root)
2262 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00002263
Evan Chenge3d42322009-02-25 07:04:34 +00002264 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
2265 "Malformed subprogram descriptor anchor type");
2266 Constant *RootC = cast<Constant>(*Root->use_begin());
2267 assert(RootC->hasNUsesOrMore(1) &&
2268 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00002269
Evan Chenge3d42322009-02-25 07:04:34 +00002270 bool Result = false;
2271 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
2272 UI != UE; ++UI)
2273 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00002274 UUI != UUE; ++UUI)
2275 Result |= ConstructSubprogram(cast<GlobalVariable>(*UUI));
2276
Evan Chenge3d42322009-02-25 07:04:34 +00002277 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00002278 }
2279
Jim Laskey65195462006-10-30 13:35:07 +00002280public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002281 //===--------------------------------------------------------------------===//
2282 // Main entry points.
2283 //
Owen Andersoncb371882008-08-21 00:14:44 +00002284 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00002285 : Dwarf(OS, A, T, "dbg"), MainCU(0),
2286 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
2287 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
2288 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel7d2f9722009-04-15 20:41:31 +00002289 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002290 if (TimePassesIsEnabled)
2291 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00002292 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00002293 }
Jim Laskey072200c2007-01-29 18:51:14 +00002294 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002295 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2296 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00002297
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002298 for (DenseMap<const GlobalVariable *, DbgScope *>::iterator
2299 I = AbstractInstanceRootMap.begin(),
2300 E = AbstractInstanceRootMap.end(); I != E;++I)
2301 delete I->second;
2302
Bill Wendling163ba3f2009-03-10 21:23:25 +00002303 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00002304 }
2305
Bill Wendlingc8615e42009-03-10 21:47:45 +00002306 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
2307 /// be emitted.
2308 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
2309
Devang Patel5d315982009-01-06 21:07:30 +00002310 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
2311 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00002312 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002313 if (TimePassesIsEnabled)
2314 DebugTimer->startTimer();
2315
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002316 // Create all the compile unit DIEs.
2317 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00002318
Bill Wendling163ba3f2009-03-10 21:23:25 +00002319 if (CompileUnits.empty()) {
2320 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00002321 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00002322
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002323 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00002324 }
Devang Patel3f7833a2009-01-12 23:09:42 +00002325
Devang Patel53cac182009-02-24 00:02:15 +00002326 // Create DIEs for each of the externally visible global variables.
2327 bool globalDIEs = ConstructGlobalVariableDIEs();
2328
2329 // Create DIEs for each of the externally visible subprograms.
2330 bool subprogramDIEs = ConstructSubprograms();
2331
2332 // If there is not any debug info available for any global variables
2333 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00002334 if (!globalDIEs && !subprogramDIEs) {
2335 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00002336 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00002337
Devang Patel53cac182009-02-24 00:02:15 +00002338 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00002339 }
Devang Patel53cac182009-02-24 00:02:15 +00002340
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002341 MMI = mmi;
2342 shouldEmit = true;
2343 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00002344
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002345 // Prime section data.
2346 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00002347
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002348 // Print out .file directives to specify files for .loc directives. These
2349 // are printed out early so that they precede any .loc directives.
2350 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00002351 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
2352 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002353 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00002354 sys::Path FullPath(getSourceDirectoryName(Id.first));
2355 bool AppendOk =
2356 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002357 assert(AppendOk && "Could not append filename to directory!");
2358 AppendOk = false;
2359 Asm->EmitFile(i, FullPath.toString());
2360 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00002361 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002362 }
Devang Patel5d315982009-01-06 21:07:30 +00002363
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002364 // Emit initial sections
2365 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00002366
2367 if (TimePassesIsEnabled)
2368 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00002369 }
2370
Jim Laskey65195462006-10-30 13:35:07 +00002371 /// BeginModule - Emit all Dwarf sections that should come prior to the
2372 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002373 void BeginModule(Module *M) {
2374 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00002375 }
2376
Jim Laskey65195462006-10-30 13:35:07 +00002377 /// EndModule - Emit all Dwarf sections that should come after the content.
2378 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002379 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002380 if (!ShouldEmitDwarfDebug())
2381 return;
2382
2383 if (TimePassesIsEnabled)
2384 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002385
Jim Laskeyef42a012006-11-02 20:12:39 +00002386 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002387 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002388 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002389 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002390 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002391
Jim Laskeyef42a012006-11-02 20:12:39 +00002392 // End text sections.
2393 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002394 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002395 EmitLabel("section_end", i);
2396 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002397
2398 // Emit common frame information.
2399 EmitCommonDebugFrame();
2400
2401 // Emit function debug frame information
2402 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2403 E = DebugFrames.end(); I != E; ++I)
2404 EmitFunctionDebugFrame(*I);
2405
Jim Laskeyef42a012006-11-02 20:12:39 +00002406 // Compute DIE offsets and sizes.
2407 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002408
Jim Laskeyef42a012006-11-02 20:12:39 +00002409 // Emit all the DIEs into a debug info section
2410 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002411
Jim Laskeyef42a012006-11-02 20:12:39 +00002412 // Corresponding abbreviations into a abbrev section.
2413 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002414
Jim Laskeyef42a012006-11-02 20:12:39 +00002415 // Emit source line correspondence into a debug line section.
2416 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002417
Jim Laskeyef42a012006-11-02 20:12:39 +00002418 // Emit info into a debug pubnames section.
2419 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002420
Jim Laskeyef42a012006-11-02 20:12:39 +00002421 // Emit info into a debug str section.
2422 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002423
Jim Laskeyef42a012006-11-02 20:12:39 +00002424 // Emit info into a debug loc section.
2425 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002426
Jim Laskeyef42a012006-11-02 20:12:39 +00002427 // Emit info into a debug aranges section.
2428 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002429
Jim Laskeyef42a012006-11-02 20:12:39 +00002430 // Emit info into a debug ranges section.
2431 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002432
Jim Laskeyef42a012006-11-02 20:12:39 +00002433 // Emit info into a debug macinfo section.
2434 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00002435
Devang Patel0f7fef32009-04-13 17:02:03 +00002436 // Emit inline info.
2437 EmitDebugInlineInfo();
2438
Bill Wendling163ba3f2009-03-10 21:23:25 +00002439 if (TimePassesIsEnabled)
2440 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00002441 }
2442
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002443 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00002444 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002445 void BeginFunction(MachineFunction *MF) {
Bill Wendling1362f972009-03-11 00:03:50 +00002446 this->MF = MF;
2447
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00002448 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002449
Bill Wendling163ba3f2009-03-10 21:23:25 +00002450 if (TimePassesIsEnabled)
2451 DebugTimer->startTimer();
2452
Jim Laskeyef42a012006-11-02 20:12:39 +00002453 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002454 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002455
Jim Laskeyef42a012006-11-02 20:12:39 +00002456 // Assumes in correct section after the entry point.
2457 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00002458
Argyrios Kyrtzidis28f14122009-05-04 19:23:45 +00002459 // Emit label for the implicitly defined dbg.stoppoint at the start of
2460 // the function.
Mike Stumpfe095f32009-05-04 18:40:41 +00002461 DebugLoc FDL = MF->getDefaultDebugLoc();
2462 if (!FDL.isUnknown()) {
2463 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2464 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
2465 DICompileUnit(DLT.CompileUnit));
2466 Asm->printLabel(LabelID);
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00002467 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00002468
2469 if (TimePassesIsEnabled)
2470 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00002471 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002472
Jim Laskey65195462006-10-30 13:35:07 +00002473 /// EndFunction - Gather and emit post-function debug information.
2474 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00002475 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00002476 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002477
Bill Wendling163ba3f2009-03-10 21:23:25 +00002478 if (TimePassesIsEnabled)
2479 DebugTimer->startTimer();
2480
Jim Laskeyb82313f2007-02-01 16:31:34 +00002481 // Define end label for subprogram.
2482 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002483
Jim Laskeyef42a012006-11-02 20:12:39 +00002484 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002485 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002486 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002487 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00002488 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00002489 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00002490 // Append the function info to section info.
2491 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00002492 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00002493 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002494
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002495 // Construct the DbgScope for abstract instances.
2496 for (SmallVector<DbgScope *, 32>::iterator
2497 I = AbstractInstanceRootList.begin(),
2498 E = AbstractInstanceRootList.end(); I != E; ++I)
2499 ConstructAbstractDbgScope(*I);
2500
Jim Laskeyef42a012006-11-02 20:12:39 +00002501 // Construct scopes for subprogram.
Devang Patel7d2f9722009-04-15 20:41:31 +00002502 if (FunctionDbgScope)
2503 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00002504 else
2505 // FIXME: This is wrong. We are essentially getting past a problem with
2506 // debug information not being able to handle unreachable blocks that have
2507 // debug information in them. In particular, those unreachable blocks that
2508 // have "region end" info in them. That situation results in the "root
2509 // scope" not being created. If that's the case, then emit a "default"
2510 // scope, i.e., one that encompasses the whole function. This isn't
2511 // desirable. And a better way of handling this (and all of the debugging
2512 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00002513 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002514
2515 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2516 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00002517
2518 // Clear debug info
Devang Patel7d2f9722009-04-15 20:41:31 +00002519 if (FunctionDbgScope) {
2520 delete FunctionDbgScope;
Devang Patel481ff5b2009-01-12 18:48:36 +00002521 DbgScopeMap.clear();
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002522 DbgAbstractScopeMap.clear();
2523 DbgConcreteScopeMap.clear();
Devang Patel1be3ecc2009-04-15 00:10:26 +00002524 InlinedVariableScopes.clear();
Devang Patel7d2f9722009-04-15 20:41:31 +00002525 FunctionDbgScope = NULL;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002526 LexicalScopeStack.clear();
2527 AbstractInstanceRootList.clear();
Devang Patel481ff5b2009-01-12 18:48:36 +00002528 }
Devang Patelccca7fe2009-01-12 19:17:34 +00002529
Bill Wendling163ba3f2009-03-10 21:23:25 +00002530 Lines.clear();
2531
2532 if (TimePassesIsEnabled)
2533 DebugTimer->stopTimer();
2534 }
Devang Patelccca7fe2009-01-12 19:17:34 +00002535
2536 /// RecordSourceLine - Records location information and associates it with a
2537 /// label. Returns a unique label ID used to generate a label and provide
2538 /// correspondence to the source line list.
2539 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002540 if (TimePassesIsEnabled)
2541 DebugTimer->startTimer();
2542
Evan Chenge3d42322009-02-25 07:04:34 +00002543 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00002544 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00002545 unsigned ID = MMI->NextLabelID();
2546 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00002547
2548 if (TimePassesIsEnabled)
2549 DebugTimer->stopTimer();
2550
Devang Patelccca7fe2009-01-12 19:17:34 +00002551 return ID;
2552 }
2553
2554 /// RecordSourceLine - Records location information and associates it with a
2555 /// label. Returns a unique label ID used to generate a label and provide
2556 /// correspondence to the source line list.
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00002557 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002558 if (TimePassesIsEnabled)
2559 DebugTimer->startTimer();
2560
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00002561 std::string Dir, Fn;
2562 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
2563 CU.getFilename(Fn));
Devang Patelccca7fe2009-01-12 19:17:34 +00002564 unsigned ID = MMI->NextLabelID();
2565 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00002566
2567 if (TimePassesIsEnabled)
2568 DebugTimer->stopTimer();
2569
Devang Patelccca7fe2009-01-12 19:17:34 +00002570 return ID;
2571 }
2572
Bill Wendlingc8615e42009-03-10 21:47:45 +00002573 /// getRecordSourceLineCount - Return the number of source lines in the debug
2574 /// info.
2575 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00002576 return Lines.size();
2577 }
2578
Bill Wendlingc8615e42009-03-10 21:47:45 +00002579 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2580 /// timed. Look up the source id with the given directory and source file
2581 /// names. If none currently exists, create a new id and insert it in the
2582 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2583 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00002584 unsigned getOrCreateSourceID(const std::string &DirName,
2585 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002586 if (TimePassesIsEnabled)
2587 DebugTimer->startTimer();
2588
Bill Wendlingc8615e42009-03-10 21:47:45 +00002589 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00002590
2591 if (TimePassesIsEnabled)
2592 DebugTimer->stopTimer();
2593
Evan Chenge3d42322009-02-25 07:04:34 +00002594 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00002595 }
2596
2597 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00002598 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002599 if (TimePassesIsEnabled)
2600 DebugTimer->startTimer();
2601
Devang Patelccca7fe2009-01-12 19:17:34 +00002602 DbgScope *Scope = getOrCreateScope(V);
2603 unsigned ID = MMI->NextLabelID();
2604 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002605 LexicalScopeStack.push_back(Scope);
Bill Wendling163ba3f2009-03-10 21:23:25 +00002606
2607 if (TimePassesIsEnabled)
2608 DebugTimer->stopTimer();
2609
Devang Patelccca7fe2009-01-12 19:17:34 +00002610 return ID;
2611 }
2612
2613 /// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002614 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002615 if (TimePassesIsEnabled)
2616 DebugTimer->startTimer();
2617
Bill Wendling5b8479c2009-05-07 17:26:14 +00002618 DbgScope *Scope = getOrCreateScope(V);
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002619 unsigned ID = MMI->NextLabelID();
Devang Patelccca7fe2009-01-12 19:17:34 +00002620 Scope->setEndLabelID(ID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002621 if (LexicalScopeStack.size() != 0)
2622 LexicalScopeStack.pop_back();
Bill Wendling163ba3f2009-03-10 21:23:25 +00002623
2624 if (TimePassesIsEnabled)
2625 DebugTimer->stopTimer();
2626
Devang Patelccca7fe2009-01-12 19:17:34 +00002627 return ID;
2628 }
2629
2630 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00002631 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
2632 const MachineInstr *MI) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00002633 if (TimePassesIsEnabled)
2634 DebugTimer->startTimer();
2635
Devang Patel0e5200f2009-01-15 18:25:17 +00002636 DIDescriptor Desc(GV);
2637 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00002638
Devang Patel0e5200f2009-01-15 18:25:17 +00002639 if (Desc.getTag() == DW_TAG_variable) {
2640 // GV is a global variable.
2641 DIGlobalVariable DG(GV);
2642 Scope = getOrCreateScope(DG.getContext().getGV());
2643 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002644 DenseMap<const MachineInstr *, DbgScope *>::iterator
2645 SI = InlinedVariableScopes.find(MI);
Bill Wendling3f500d92009-05-06 21:21:34 +00002646
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002647 if (SI != InlinedVariableScopes.end()) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002648 // or GV is an inlined local variable.
2649 Scope = SI->second;
2650 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00002651 DIVariable DV(GV);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002652 GlobalVariable *V = DV.getContext().getGV();
2653
2654 // FIXME: The code that checks for the inlined local variable is a hack!
2655 DenseMap<const GlobalVariable *, DbgScope *>::iterator
2656 AI = AbstractInstanceRootMap.find(V);
2657
2658 if (AI != AbstractInstanceRootMap.end())
2659 // or GV is an inlined local variable.
2660 Scope = AI->second;
2661 else
2662 // or GV is a local variable.
2663 Scope = getOrCreateScope(V);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002664 }
Devang Patel0e5200f2009-01-15 18:25:17 +00002665 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00002666
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002667 assert(Scope && "Unable to find the variable's scope");
Devang Patel99ec3532009-01-16 19:28:14 +00002668 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00002669 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00002670
2671 if (TimePassesIsEnabled)
2672 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00002673 }
Devang Patel0f7fef32009-04-13 17:02:03 +00002674
Devang Patel1be3ecc2009-04-15 00:10:26 +00002675 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00002676 unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
2677 unsigned Line, unsigned Col) {
2678 unsigned LabelID = MMI->NextLabelID();
2679
Devang Patel1be3ecc2009-04-15 00:10:26 +00002680 if (!TAI->doesDwarfUsesInlineInfoSection())
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00002681 return LabelID;
Devang Patel1be3ecc2009-04-15 00:10:26 +00002682
Bill Wendlingd5a63812009-05-01 08:40:06 +00002683 if (TimePassesIsEnabled)
2684 DebugTimer->startTimer();
2685
Devang Patel1be3ecc2009-04-15 00:10:26 +00002686 GlobalVariable *GV = SP.getGV();
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002687 DenseMap<const GlobalVariable *, DbgScope *>::iterator
2688 II = AbstractInstanceRootMap.find(GV);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002689
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002690 if (II == AbstractInstanceRootMap.end()) {
2691 // Create an abstract instance entry for this inlined function if it
2692 // doesn't already exist.
2693 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(GV));
Bill Wendlingef956fc2009-05-01 08:32:14 +00002694
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002695 // Get the compile unit context.
2696 CompileUnit *Unit = &FindCompileUnit(SP.getCompileUnit());
2697 DIE *SPDie = Unit->getDieMapSlotFor(GV);
2698 if (!SPDie)
2699 SPDie = CreateSubprogramDIE(Unit, SP);
2700
2701 // Mark as being inlined. This makes this subprogram entry an abstract
2702 // instance root.
2703 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
2704 // that it's defined. It probably won't change in the future, but this
2705 // could be more elegant.
2706 AddUInt(SPDie, DW_AT_inline, 0, DW_INL_declared_not_inlined);
2707
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002708 // Keep track of the abstract scope for this function.
2709 DbgAbstractScopeMap[GV] = Scope;
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00002710
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002711 AbstractInstanceRootMap[GV] = Scope;
2712 AbstractInstanceRootList.push_back(Scope);
2713 }
2714
2715 // Create a concrete inlined instance for this inlined function.
2716 DbgConcreteScope *ConcreteScope = new DbgConcreteScope(DIDescriptor(GV));
2717 DIE *ScopeDie = new DIE(DW_TAG_inlined_subroutine);
2718 CompileUnit *Unit = &FindCompileUnit(SP.getCompileUnit());
2719 ScopeDie->setAbstractCompileUnit(Unit);
2720
2721 DIE *Origin = Unit->getDieMapSlotFor(GV);
Bill Wendling88423ee2009-05-15 00:11:17 +00002722 AddDIEEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002723 AddUInt(ScopeDie, DW_AT_call_file, 0, Unit->getID());
2724 AddUInt(ScopeDie, DW_AT_call_line, 0, Line);
2725 AddUInt(ScopeDie, DW_AT_call_column, 0, Col);
2726
2727 ConcreteScope->setDie(ScopeDie);
2728 ConcreteScope->setStartLabelID(LabelID);
Bill Wendlingbc12c2b2009-05-12 00:06:59 +00002729 MMI->RecordUsedDbgLabel(LabelID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002730
2731 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope);
2732
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002733 // Keep track of the concrete scope that's inlined into this function.
2734 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator
2735 SI = DbgConcreteScopeMap.find(GV);
2736
2737 if (SI == DbgConcreteScopeMap.end())
2738 DbgConcreteScopeMap[GV].push_back(ConcreteScope);
2739 else
2740 SI->second.push_back(ConcreteScope);
2741
Bill Wendlingbc12c2b2009-05-12 00:06:59 +00002742 // Track the start label for this inlined function.
2743 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2744 I = InlineInfo.find(GV);
2745
2746 if (I == InlineInfo.end())
2747 InlineInfo[GV].push_back(LabelID);
2748 else
2749 I->second.push_back(LabelID);
2750
Bill Wendlingd5a63812009-05-01 08:40:06 +00002751 if (TimePassesIsEnabled)
2752 DebugTimer->stopTimer();
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00002753
2754 return LabelID;
Devang Patel0f7fef32009-04-13 17:02:03 +00002755 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00002756
2757 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
2758 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
2759 if (!TAI->doesDwarfUsesInlineInfoSection())
2760 return 0;
2761
Bill Wendlingd5a63812009-05-01 08:40:06 +00002762 if (TimePassesIsEnabled)
2763 DebugTimer->startTimer();
2764
Devang Patel1be3ecc2009-04-15 00:10:26 +00002765 GlobalVariable *GV = SP.getGV();
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00002766 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002767 I = DbgConcreteScopeMap.find(GV);
Bill Wendling3f500d92009-05-06 21:21:34 +00002768
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002769 if (I == DbgConcreteScopeMap.end()) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00002770 if (TimePassesIsEnabled)
2771 DebugTimer->stopTimer();
2772
Devang Patel1be3ecc2009-04-15 00:10:26 +00002773 return 0;
Bill Wendlingd5a63812009-05-01 08:40:06 +00002774 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00002775
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00002776 SmallVector<DbgScope *, 8> &Scopes = I->second;
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002777 assert(!Scopes.empty() && "We should have at least one debug scope!");
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00002778 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
Devang Patel1be3ecc2009-04-15 00:10:26 +00002779 unsigned ID = MMI->NextLabelID();
2780 MMI->RecordUsedDbgLabel(ID);
2781 Scope->setEndLabelID(ID);
Bill Wendlingd5a63812009-05-01 08:40:06 +00002782
2783 if (TimePassesIsEnabled)
2784 DebugTimer->stopTimer();
2785
Devang Patel1be3ecc2009-04-15 00:10:26 +00002786 return ID;
2787 }
2788
2789 /// RecordVariableScope - Record scope for the variable declared by
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002790 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE. Record scopes
2791 /// for only inlined subroutine variables. Other variables's scopes are
2792 /// determined during RecordVariable().
Devang Patel1be3ecc2009-04-15 00:10:26 +00002793 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00002794 if (TimePassesIsEnabled)
2795 DebugTimer->startTimer();
2796
Devang Patel1be3ecc2009-04-15 00:10:26 +00002797 DISubprogram SP(DV.getContext().getGV());
Bill Wendlingd5a63812009-05-01 08:40:06 +00002798
2799 if (SP.isNull()) {
2800 if (TimePassesIsEnabled)
2801 DebugTimer->stopTimer();
2802
Devang Patel1be3ecc2009-04-15 00:10:26 +00002803 return;
Bill Wendlingd5a63812009-05-01 08:40:06 +00002804 }
2805
Bill Wendlingf59d10f2009-05-13 23:55:49 +00002806 DenseMap<GlobalVariable *, DbgScope *>::iterator
2807 I = DbgAbstractScopeMap.find(SP.getGV());
2808 if (I != DbgAbstractScopeMap.end())
2809 InlinedVariableScopes[DeclareMI] = I->second;
Devang Patel1be3ecc2009-04-15 00:10:26 +00002810
Bill Wendlingd5a63812009-05-01 08:40:06 +00002811 if (TimePassesIsEnabled)
2812 DebugTimer->stopTimer();
Devang Patel1be3ecc2009-04-15 00:10:26 +00002813 }
Jim Laskey65195462006-10-30 13:35:07 +00002814};
2815
Jim Laskey072200c2007-01-29 18:51:14 +00002816//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002817/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00002818///
2819class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002820 struct FunctionEHFrameInfo {
2821 std::string FnName;
2822 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002823 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002824 bool hasCalls;
2825 bool hasLandingPads;
2826 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002827 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00002828
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002829 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
2830 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002831 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002832 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002833 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002834 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002835 };
2836
2837 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002838
2839 /// shouldEmitTable - Per-function flag to indicate if EH tables should
2840 /// be emitted.
2841 bool shouldEmitTable;
2842
2843 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
2844 /// should be emitted.
2845 bool shouldEmitMoves;
2846
2847 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
2848 /// should be emitted.
2849 bool shouldEmitTableModule;
2850
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002851 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002852 /// should be emitted.
2853 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00002854
Bill Wendling163ba3f2009-03-10 21:23:25 +00002855 /// ExceptionTimer - Timer for the Dwarf exception writer.
2856 Timer *ExceptionTimer;
2857
Jim Laskey3f09fc22007-02-28 18:38:31 +00002858 /// EmitCommonEHFrame - Emit the common eh unwind frame.
2859 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002860 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00002861 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002862 int stackGrowth =
2863 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2864 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002865 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002866
Jim Laskeybacd3042007-02-21 22:48:45 +00002867 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002868 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00002869
2870 if (!TAI->doesRequireNonLocalEHFrameLabel())
2871 O << TAI->getEHGlobalPrefix();
2872 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002873 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002874
Jim Laskeybacd3042007-02-21 22:48:45 +00002875 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002876 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00002877
Jim Laskeybacd3042007-02-21 22:48:45 +00002878 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002879 EmitDifference("eh_frame_common_end", Index,
2880 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002881 Asm->EOL("Length of Common Information Entry");
2882
Jim Laskeybacd3042007-02-21 22:48:45 +00002883 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002884 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002885 Asm->EmitInt32((int)0);
2886 Asm->EOL("CIE Identifier Tag");
2887 Asm->EmitInt8(DW_CIE_VERSION);
2888 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00002889
Jim Laskeybacd3042007-02-21 22:48:45 +00002890 // The personality presence indicates that language specific information
2891 // will show up in the eh frame.
2892 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00002893 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00002894
Jim Laskeybacd3042007-02-21 22:48:45 +00002895 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002896 Asm->EmitULEB128Bytes(1);
2897 Asm->EOL("CIE Code Alignment Factor");
2898 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00002899 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002900 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00002901 Asm->EOL("CIE Return Address Column");
2902
Jim Laskeybacd3042007-02-21 22:48:45 +00002903 // If there is a personality, we need to indicate the functions location.
2904 if (Personality) {
2905 Asm->EmitULEB128Bytes(7);
2906 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00002907
Duncan Sands671fa972008-05-07 19:11:09 +00002908 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00002909 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00002910 Asm->EOL("Personality (pcrel sdata4 indirect)");
2911 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00002912 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00002913 Asm->EOL("Personality (pcrel sdata4)");
2914 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00002915
Duncan Sands671fa972008-05-07 19:11:09 +00002916 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00002917 O << TAI->getPersonalityPrefix();
2918 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
2919 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00002920 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
2921 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00002922 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00002923
Duncan Sands671fa972008-05-07 19:11:09 +00002924 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2925 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00002926
Bill Wendlingd60de512009-01-05 22:53:45 +00002927 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2928 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00002929 } else {
2930 Asm->EmitULEB128Bytes(1);
2931 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00002932
Bill Wendlingd60de512009-01-05 22:53:45 +00002933 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2934 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00002935 }
2936
2937 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002938 std::vector<MachineMove> Moves;
2939 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00002940 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002941
Dale Johannesen21d972a2008-04-30 00:43:29 +00002942 // On Darwin the linker honors the alignment of eh_frame, which means it
2943 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
2944 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002945 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00002946 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002947 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00002948
Jim Laskeybacd3042007-02-21 22:48:45 +00002949 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002950 }
Duncan Sands671fa972008-05-07 19:11:09 +00002951
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002952 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002953 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002954 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002955 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner266c7bb2009-04-13 05:44:34 +00002956
2957 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
2958 "Should not emit 'available externally' functions at all");
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002959
Jim Laskeybacd3042007-02-21 22:48:45 +00002960 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
2961
2962 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002963 // If the corresponding function is static, this should not be
2964 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00002965 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00002966 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002967 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
2968 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
2969 }
2970
Dale Johannesen038129d2008-01-10 02:03:30 +00002971 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00002972 if ((linkage == Function::WeakAnyLinkage ||
2973 linkage == Function::WeakODRLinkage ||
2974 linkage == Function::LinkOnceAnyLinkage ||
2975 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00002976 TAI->getWeakDefDirective())
2977 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
2978
2979 // If there are no calls then you can't unwind. This may mean we can
2980 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002981 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00002982 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00002983 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00002984 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00002985 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00002986 ((linkage != Function::WeakAnyLinkage &&
2987 linkage != Function::WeakODRLinkage &&
2988 linkage != Function::LinkOnceAnyLinkage &&
2989 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00002990 !TAI->getWeakDefDirective() ||
2991 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002992 {
Bill Wendling6e198962007-09-18 01:47:22 +00002993 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002994 // This name has no connection to the function, so it might get
2995 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002996 // dead-stripping unconditionally.
2997 if (const char *UsedDirective = TAI->getUsedDirective())
2998 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00002999 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003000 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003001
Jim Laskeybacd3042007-02-21 22:48:45 +00003002 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003003 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3004 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003005 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003006
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003007 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003008
Bill Wendling722f5f12008-12-24 08:05:17 +00003009 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3010 PrintRelDirective(true, true);
3011 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3012
3013 if (!TAI->isAbsoluteEHSectionOffsets())
3014 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3015 } else {
3016 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3017 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3018 true, true, false);
3019 }
3020
Jim Laskeybacd3042007-02-21 22:48:45 +00003021 Asm->EOL("FDE CIE offset");
3022
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003023 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003024 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003025 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003026 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003027 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003028
Jim Laskey3f09fc22007-02-28 18:38:31 +00003029 // If there is a personality and landing pads then point to the language
3030 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003031 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003032 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003033 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003034
3035 if (EHFrameInfo.hasLandingPads)
3036 EmitReference("exception", EHFrameInfo.Number, true, true);
3037 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003038 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003039 Asm->EOL("Language Specific Data Area");
3040 } else {
3041 Asm->EmitULEB128Bytes(0);
3042 Asm->EOL("Augmentation size");
3043 }
Duncan Sands671fa972008-05-07 19:11:09 +00003044
Jim Laskeybacd3042007-02-21 22:48:45 +00003045 // Indicate locations of function specific callee saved registers in
3046 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003047 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003048 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003049
Dale Johannesen21d972a2008-04-30 00:43:29 +00003050 // On Darwin the linker honors the alignment of eh_frame, which means it
3051 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3052 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003053 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003054 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003055 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003056
3057 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003058 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003059 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003060 // that depends on unused functions (calling undefined externals) being
3061 // dead-stripped to link correctly. Yes, there really is.
3062 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3063 if (const char *UsedDirective = TAI->getUsedDirective())
3064 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3065 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003066 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003067
Duncan Sands57810cd2007-09-05 11:27:52 +00003068 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003069 ///
3070 /// The general organization of the table is complex, but the basic concepts
3071 /// are easy. First there is a header which describes the location and
3072 /// organization of the three components that follow.
3073 /// 1. The landing pad site information describes the range of code covered
3074 /// by the try. In our case it's an accumulation of the ranges covered
3075 /// by the invokes in the try. There is also a reference to the landing
3076 /// pad that handles the exception once processed. Finally an index into
3077 /// the actions table.
3078 /// 2. The action table, in our case, is composed of pairs of type ids
3079 /// and next action offset. Starting with the action index from the
3080 /// landing pad site, each type Id is checked for a match to the current
3081 /// exception. If it matches then the exception and type id are passed
3082 /// on to the landing pad. Otherwise the next action is looked up. This
3083 /// chain is terminated with a next action of zero. If no type id is
3084 /// found the the frame is unwound and handling continues.
3085 /// 3. Type id table contains references to all the C++ typeinfo for all
3086 /// catches in the function. This tables is reversed indexed base 1.
3087
Duncan Sandsb32edb42007-06-06 15:37:31 +00003088 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3089 static unsigned SharedTypeIds(const LandingPadInfo *L,
3090 const LandingPadInfo *R) {
3091 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003092 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003093 unsigned MinSize = LSize < RSize ? LSize : RSize;
3094 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003095
Duncan Sandsb32edb42007-06-06 15:37:31 +00003096 for (; Count != MinSize; ++Count)
3097 if (LIds[Count] != RIds[Count])
3098 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003099
Duncan Sandsb32edb42007-06-06 15:37:31 +00003100 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003101 }
3102
Duncan Sandsb32edb42007-06-06 15:37:31 +00003103 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003104 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003105 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003106 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003107 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003108
Duncan Sandsb32edb42007-06-06 15:37:31 +00003109 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003110 if (LIds[i] != RIds[i])
3111 return LIds[i] < RIds[i];
3112
Duncan Sandsb32edb42007-06-06 15:37:31 +00003113 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003114 }
3115
Duncan Sands53c3a332007-05-16 12:12:23 +00003116 struct KeyInfo {
3117 static inline unsigned getEmptyKey() { return -1U; }
3118 static inline unsigned getTombstoneKey() { return -2U; }
3119 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003120 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003121 static bool isPod() { return true; }
3122 };
3123
Duncan Sands57810cd2007-09-05 11:27:52 +00003124 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003125 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003126 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003127 int NextAction;
3128 struct ActionEntry *Previous;
3129 };
3130
Duncan Sands57810cd2007-09-05 11:27:52 +00003131 /// PadRange - Structure holding a try-range and the associated landing pad.
3132 struct PadRange {
3133 // The index of the landing pad.
3134 unsigned PadIndex;
3135 // The index of the begin and end labels in the landing pad's label lists.
3136 unsigned RangeIndex;
3137 };
3138
3139 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3140
3141 /// CallSiteEntry - Structure describing an entry in the call-site table.
3142 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003143 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003144 unsigned BeginLabel; // zero indicates the start of the function.
3145 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003146 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003147 unsigned PadLabel; // zero indicates that there is no landing pad.
3148 unsigned Action;
3149 };
3150
Jim Laskeybacd3042007-02-21 22:48:45 +00003151 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003152 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003153 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003154 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3155 if (PadInfos.empty()) return;
3156
3157 // Sort the landing pads in order of their type ids. This is used to fold
3158 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003159 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003160 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003161 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003162 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003163 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3164
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003165 // Negative type ids index into FilterIds, positive type ids index into
3166 // TypeInfos. The value written for a positive type id is just the type
3167 // id itself. For a negative type id, however, the value written is the
3168 // (negative) byte offset of the corresponding FilterIds entry. The byte
3169 // offset is usually equal to the type id, because the FilterIds entries
3170 // are written using a variable width encoding which outputs one byte per
3171 // entry as long as the value written is not too large, but can differ.
3172 // This kind of complication does not occur for positive type ids because
3173 // type infos are output using a fixed width encoding.
3174 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3175 SmallVector<int, 16> FilterOffsets;
3176 FilterOffsets.reserve(FilterIds.size());
3177 int Offset = -1;
3178 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3179 E = FilterIds.end(); I != E; ++I) {
3180 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003181 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003182 }
3183
Duncan Sands57810cd2007-09-05 11:27:52 +00003184 // Compute the actions table and gather the first action index for each
3185 // landing pad site.
3186 SmallVector<ActionEntry, 32> Actions;
3187 SmallVector<unsigned, 64> FirstActions;
3188 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003189
Duncan Sandsb32edb42007-06-06 15:37:31 +00003190 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003191 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003192 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003193 const LandingPadInfo *LP = LandingPads[i];
3194 const std::vector<int> &TypeIds = LP->TypeIds;
3195 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003196 unsigned SizeSiteActions = 0;
3197
Duncan Sandsb32edb42007-06-06 15:37:31 +00003198 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003199 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003200 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003201
Duncan Sandsb32edb42007-06-06 15:37:31 +00003202 if (NumShared) {
3203 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3204 assert(Actions.size());
3205 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003206 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3207 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003208 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003209 SizeAction -=
3210 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003211 SizeAction += -PrevAction->NextAction;
3212 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003213 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003214 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003215
Duncan Sandsb32edb42007-06-06 15:37:31 +00003216 // Compute the actions.
3217 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3218 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003219 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3220 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003221 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003222
3223 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003224 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003225 SizeSiteActions += SizeAction;
3226
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003227 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003228 Actions.push_back(Action);
3229
3230 PrevAction = &Actions.back();
3231 }
3232
3233 // Record the first action of the landing pad site.
3234 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3235 } // else identical - re-use previous FirstAction
3236
3237 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003238
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003239 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003240 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003241 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003242
Duncan Sands481dc722007-12-19 07:36:31 +00003243 // Compute the call-site table. The entry for an invoke has a try-range
3244 // containing the call, a non-zero landing pad and an appropriate action.
3245 // The entry for an ordinary call has a try-range containing the call and
3246 // zero for the landing pad and the action. Calls marked 'nounwind' have
3247 // no entry and must not be contained in the try-range of any entry - they
3248 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003249 SmallVector<CallSiteEntry, 64> CallSites;
3250
3251 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003252 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3253 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3254 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003255 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3256 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003257 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003258 unsigned BeginLabel = LandingPad->BeginLabels[j];
3259 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3260 PadRange P = { i, j };
3261 PadMap[BeginLabel] = P;
3262 }
3263 }
3264
Duncan Sands481dc722007-12-19 07:36:31 +00003265 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003266 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003267
3268 // Whether there is a potentially throwing instruction (currently this means
3269 // an ordinary call) between the end of the previous try-range and now.
3270 bool SawPotentiallyThrowing = false;
3271
3272 // Whether the last callsite entry was for an invoke.
3273 bool PreviousIsInvoke = false;
3274
Duncan Sands481dc722007-12-19 07:36:31 +00003275 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003276 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3277 I != E; ++I) {
3278 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3279 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00003280 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003281 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003282 continue;
3283 }
3284
Chris Lattner9e330492007-12-30 20:50:28 +00003285 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003286 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003287
Duncan Sands481dc722007-12-19 07:36:31 +00003288 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003289 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003290 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003291
Duncan Sands481dc722007-12-19 07:36:31 +00003292 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003293 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003294 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003295 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003296 continue;
3297
3298 PadRange P = L->second;
3299 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3300
3301 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3302 "Inconsistent landing pad map!");
3303
3304 // If some instruction between the previous try-range and this one may
3305 // throw, create a call-site entry with no landing pad for the region
3306 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003307 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003308 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3309 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003310 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003311 }
3312
3313 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003314 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003315
Duncan Sands481dc722007-12-19 07:36:31 +00003316 if (LandingPad->LandingPadLabel) {
3317 // This try-range is for an invoke.
3318 CallSiteEntry Site = {BeginLabel, LastLabel,
3319 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00003320
Duncan Sands481dc722007-12-19 07:36:31 +00003321 // Try to merge with the previous call-site.
3322 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00003323 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00003324 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3325 // Extend the range of the previous entry.
3326 Prev.EndLabel = Site.EndLabel;
3327 continue;
3328 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003329 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003330
Duncan Sands481dc722007-12-19 07:36:31 +00003331 // Otherwise, create a new call-site.
3332 CallSites.push_back(Site);
3333 PreviousIsInvoke = true;
3334 } else {
3335 // Create a gap.
3336 PreviousIsInvoke = false;
3337 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003338 }
3339 }
3340 // If some instruction between the previous try-range and the end of the
3341 // function may throw, create a call-site entry with no landing pad for the
3342 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00003343 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003344 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3345 CallSites.push_back(Site);
3346 }
3347
Jim Laskeybacd3042007-02-21 22:48:45 +00003348 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00003349
3350 // Call sites.
3351 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3352 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3353 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3354 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3355 SiteLengthSize +
3356 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00003357 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003358 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00003359
Duncan Sands671fa972008-05-07 19:11:09 +00003360 // Type infos.
3361 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3362 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003363
3364 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003365 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003366 SizeSites + SizeActions + SizeTypes;
3367
3368 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3369 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003370 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003371 TypeOffset;
3372
3373 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003374
Jim Laskeybacd3042007-02-21 22:48:45 +00003375 // Begin the exception table.
3376 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00003377 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00003378 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003379 for (unsigned i = 0; i != SizeAlign; ++i) {
3380 Asm->EmitInt8(0);
3381 Asm->EOL("Padding");
3382 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003383 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003384
Jim Laskeybacd3042007-02-21 22:48:45 +00003385 // Emit the header.
3386 Asm->EmitInt8(DW_EH_PE_omit);
3387 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3388 Asm->EmitInt8(DW_EH_PE_absptr);
3389 Asm->EOL("TType format (DW_EH_PE_absptr)");
3390 Asm->EmitULEB128Bytes(TypeOffset);
3391 Asm->EOL("TType base offset");
3392 Asm->EmitInt8(DW_EH_PE_udata4);
3393 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3394 Asm->EmitULEB128Bytes(SizeSites);
3395 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003396
Duncan Sands57810cd2007-09-05 11:27:52 +00003397 // Emit the landing pad site information.
3398 for (unsigned i = 0; i < CallSites.size(); ++i) {
3399 CallSiteEntry &S = CallSites[i];
3400 const char *BeginTag;
3401 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00003402
Duncan Sands57810cd2007-09-05 11:27:52 +00003403 if (!S.BeginLabel) {
3404 BeginTag = "eh_func_begin";
3405 BeginNumber = SubprogramCount;
3406 } else {
3407 BeginTag = "label";
3408 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00003409 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003410
Duncan Sands57810cd2007-09-05 11:27:52 +00003411 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003412 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003413 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00003414
Duncan Sands57810cd2007-09-05 11:27:52 +00003415 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00003416 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00003417 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003418 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00003419 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003420 }
3421 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003422
Duncan Sands671fa972008-05-07 19:11:09 +00003423 if (!S.PadLabel)
3424 Asm->EmitInt32(0);
3425 else
Duncan Sands57810cd2007-09-05 11:27:52 +00003426 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003427 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003428 Asm->EOL("Landing pad");
3429
3430 Asm->EmitULEB128Bytes(S.Action);
3431 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003432 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003433
Jim Laskeybacd3042007-02-21 22:48:45 +00003434 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003435 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3436 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00003437
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003438 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003439 Asm->EOL("TypeInfo index");
3440 Asm->EmitSLEB128Bytes(Action.NextAction);
3441 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003442 }
3443
3444 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00003445 for (unsigned M = TypeInfos.size(); M; --M) {
3446 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00003447
3448 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00003449
Bill Wendling7d16e852009-04-10 00:12:49 +00003450 if (GV) {
3451 std::string GLN;
3452 O << Asm->getGlobalLinkName(GV, GLN);
3453 } else {
Jim Laskeybacd3042007-02-21 22:48:45 +00003454 O << "0";
Bill Wendling7d16e852009-04-10 00:12:49 +00003455 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003456
Jim Laskeybacd3042007-02-21 22:48:45 +00003457 Asm->EOL("TypeInfo");
3458 }
3459
Duncan Sands73ef58a2007-06-02 16:53:42 +00003460 // Emit the filter typeids.
3461 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3462 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00003463 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00003464 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00003465 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003466
Evan Cheng05548eb2008-02-29 19:36:59 +00003467 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003468 }
3469
Jim Laskey072200c2007-01-29 18:51:14 +00003470public:
3471 //===--------------------------------------------------------------------===//
3472 // Main entry points.
3473 //
Owen Andersoncb371882008-08-21 00:14:44 +00003474 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003475 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
3476 shouldEmitTableModule(false), shouldEmitMovesModule(false),
3477 ExceptionTimer(0) {
3478 if (TimePassesIsEnabled)
3479 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00003480 getDwarfTimerGroup());
Bill Wendling163ba3f2009-03-10 21:23:25 +00003481 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003482
Bill Wendling163ba3f2009-03-10 21:23:25 +00003483 virtual ~DwarfException() {
3484 delete ExceptionTimer;
3485 }
Jim Laskey072200c2007-01-29 18:51:14 +00003486
3487 /// SetModuleInfo - Set machine module information when it's known that pass
3488 /// manager has created it. Set by the target AsmPrinter.
3489 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003490 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00003491 }
3492
3493 /// BeginModule - Emit all exception information that should come prior to the
3494 /// content.
3495 void BeginModule(Module *M) {
3496 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00003497 }
3498
3499 /// EndModule - Emit all exception information that should come after the
3500 /// content.
3501 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003502 if (TimePassesIsEnabled)
3503 ExceptionTimer->startTimer();
3504
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003505 if (shouldEmitMovesModule || shouldEmitTableModule) {
3506 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00003507 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003508 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003509
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003510 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3511 E = EHFrames.end(); I != E; ++I)
3512 EmitEHFrame(*I);
3513 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003514
3515 if (TimePassesIsEnabled)
3516 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00003517 }
3518
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003519 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00003520 /// emitted immediately after the function entry point.
3521 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003522 if (TimePassesIsEnabled)
3523 ExceptionTimer->startTimer();
3524
Jim Laskey072200c2007-01-29 18:51:14 +00003525 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003526 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003527
Bill Wendling163ba3f2009-03-10 21:23:25 +00003528 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003529 // Map all labels and get rid of any dead landing pads.
3530 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003531
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003532 // If any landing pads survive, we need an EH table.
3533 if (MMI->getLandingPads().size())
3534 shouldEmitTable = true;
3535
3536 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00003537 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003538 shouldEmitMoves = true;
3539
3540 if (shouldEmitMoves || shouldEmitTable)
3541 // Assumes in correct section after the entry point.
3542 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00003543 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003544
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003545 shouldEmitTableModule |= shouldEmitTable;
3546 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003547
3548 if (TimePassesIsEnabled)
3549 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00003550 }
3551
3552 /// EndFunction - Gather and emit post-function exception information.
3553 ///
3554 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003555 if (TimePassesIsEnabled)
3556 ExceptionTimer->startTimer();
3557
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003558 if (shouldEmitMoves || shouldEmitTable) {
3559 EmitLabel("eh_func_end", SubprogramCount);
3560 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003561
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003562 // Save EH frame information
Bill Wendling7d16e852009-04-10 00:12:49 +00003563 std::string Name;
3564 EHFrames.push_back(
3565 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
3566 SubprogramCount,
3567 MMI->getPersonalityIndex(),
3568 MF->getFrameInfo()->hasCalls(),
3569 !MMI->getLandingPads().empty(),
3570 MMI->getFrameMoves(),
3571 MF->getFunction()));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003572 }
3573
3574 if (TimePassesIsEnabled)
3575 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00003576 }
3577};
3578
Jim Laskey0d086af2006-02-27 12:43:29 +00003579} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00003580
3581//===----------------------------------------------------------------------===//
Jim Laskey65195462006-10-30 13:35:07 +00003582/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003583///
Jim Laskey65195462006-10-30 13:35:07 +00003584
Bill Wendling91b8b802009-03-10 20:41:52 +00003585DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00003586 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00003587
3588DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00003589 delete DE;
3590 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00003591}
3592
Jim Laskey65195462006-10-30 13:35:07 +00003593/// BeginModule - Emit all Dwarf sections that should come prior to the
3594/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00003595void DwarfWriter::BeginModule(Module *M,
3596 MachineModuleInfo *MMI,
3597 raw_ostream &OS, AsmPrinter *A,
3598 const TargetAsmInfo *T) {
3599 DE = new DwarfException(OS, A, T);
3600 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00003601 DE->BeginModule(M);
3602 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00003603 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00003604 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00003605}
3606
3607/// EndModule - Emit all Dwarf sections that should come after the content.
3608///
3609void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00003610 DE->EndModule();
3611 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00003612}
3613
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003614/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003615/// emitted immediately after the function entry point.
3616void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00003617 DE->BeginFunction(MF);
3618 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00003619}
3620
3621/// EndFunction - Gather and emit post-function debug information.
3622///
Bill Wendlingd751c642008-09-26 00:28:12 +00003623void DwarfWriter::EndFunction(MachineFunction *MF) {
3624 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003625 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003626
Bill Wendlingc91d0b92008-07-22 00:53:37 +00003627 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00003628 // Clear function debug information.
3629 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00003630}
Devang Patelccca7fe2009-01-12 19:17:34 +00003631
3632/// RecordSourceLine - Records location information and associates it with a
3633/// label. Returns a unique label ID used to generate a label and provide
3634/// correspondence to the source line list.
3635unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003636 DICompileUnit CU) {
3637 return DD->RecordSourceLine(Line, Col, CU);
Devang Patelccca7fe2009-01-12 19:17:34 +00003638}
3639
3640/// RecordRegionStart - Indicate the start of a region.
3641unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003642 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00003643}
3644
3645/// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003646unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
3647 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00003648}
3649
3650/// getRecordSourceLineCount - Count source lines.
3651unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003652 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00003653}
Devang Patelbb8c5952009-01-13 21:25:00 +00003654
Devang Patelc48c5502009-01-13 21:44:10 +00003655/// RecordVariable - Indicate the declaration of a local variable.
3656///
Devang Patel1be3ecc2009-04-15 00:10:26 +00003657void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3658 const MachineInstr *MI) {
3659 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelc48c5502009-01-13 21:44:10 +00003660}
Devang Patelbbdc8202009-01-13 23:54:55 +00003661
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003662/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3663/// be emitted.
3664bool DwarfWriter::ShouldEmitDwarfDebug() const {
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00003665 return DD && DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003666}
Devang Patel0f7fef32009-04-13 17:02:03 +00003667
Devang Patel1be3ecc2009-04-15 00:10:26 +00003668//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel0f7fef32009-04-13 17:02:03 +00003669//// by LabelID label.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003670unsigned DwarfWriter::RecordInlinedFnStart(DISubprogram SP, DICompileUnit CU,
3671 unsigned Line, unsigned Col) {
3672 return DD->RecordInlinedFnStart(SP, CU, Line, Col);
Devang Patel0f7fef32009-04-13 17:02:03 +00003673}
3674
Devang Patel1be3ecc2009-04-15 00:10:26 +00003675/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003676unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram SP) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003677 return DD->RecordInlinedFnEnd(SP);
3678}
3679
3680/// RecordVariableScope - Record scope for the variable declared by
3681/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
3682void DwarfWriter::RecordVariableScope(DIVariable &DV,
3683 const MachineInstr *DeclareMI) {
3684 DD->RecordVariableScope(DV, DeclareMI);
3685}